-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: query timeout kill query directed to incorrect instance
- Loading branch information
1 parent
ec12d1b
commit bc6f795
Showing
4 changed files
with
240 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
142 changes: 142 additions & 0 deletions
142
src/main/user-impl/java/com/mysql/cj/jdbc/ha/RdsUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* This program is free software; you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License, version 2.0 | ||
* (GPLv2), as published by the Free Software Foundation, with the | ||
* following additional permissions: | ||
* | ||
* This program is distributed with certain software that is licensed | ||
* under separate terms, as designated in a particular file or component | ||
* or in the license documentation. Without limiting your rights under | ||
* the GPLv2, the authors of this program hereby grant you an additional | ||
* permission to link the program and your derivative works with the | ||
* separately licensed software that they have included with the program. | ||
* | ||
* Without limiting the foregoing grant of rights under the GPLv2 and | ||
* additional permission as to separately licensed software, this | ||
* program is also subject to the Universal FOSS Exception, version 1.0, | ||
* a copy of which can be found along with its FAQ at | ||
* http://oss.oracle.com/licenses/universal-foss-exception. | ||
* | ||
* This program is distributed in the hope that it will be useful, but | ||
* WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||
* See the GNU General Public License, version 2.0, for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see | ||
* http://www.gnu.org/licenses/gpl-2.0.html. | ||
*/ | ||
|
||
package com.mysql.cj.jdbc.ha; | ||
|
||
import com.mysql.cj.util.StringUtils; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
public class RdsUtils { | ||
|
||
// Aurora DB clusters support different endpoints. More details about Aurora RDS endpoints | ||
// can be found at | ||
// https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/Aurora.Overview.Endpoints.html | ||
// | ||
// Details how to use RDS Proxy endpoints can be found at | ||
// https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/rds-proxy-endpoints.html | ||
// | ||
// Values like "<...>" depend on particular Aurora cluster. | ||
// For example: "<database-cluster-name>" | ||
// | ||
// | ||
// | ||
// Cluster (Writer) Endpoint: <database-cluster-name>.cluster-<xyz>.<aws-region>.rds.amazonaws.com | ||
// Example: test-postgres.cluster-123456789012.us-east-2.rds.amazonaws.com | ||
// | ||
// Cluster Reader Endpoint: <database-cluster-name>.cluster-ro-<xyz>.<aws-region>.rds.amazonaws.com | ||
// Example: test-postgres.cluster-ro-123456789012.us-east-2.rds.amazonaws.com | ||
// | ||
// Cluster Custom Endpoint: <cluster-name-alias>.cluster-custom-<xyz>.<aws-region>.rds.amazonaws.com | ||
// Example: test-postgres-alias.cluster-custom-123456789012.us-east-2.rds.amazonaws.com | ||
// | ||
// Instance Endpoint: <instance-name>.<xyz>.<aws-region>.rds.amazonaws.com | ||
// Example: test-postgres-instance-1.123456789012.us-east-2.rds.amazonaws.com | ||
// | ||
// | ||
// | ||
// Similar endpoints for China regions have different structure and are presented below. | ||
// | ||
// Cluster (Writer) Endpoint: <database-cluster-name>.cluster-<xyz>.rds.<aws-region>.amazonaws.com.cn | ||
// Example: test-postgres.cluster-123456789012.rds.cn-northwest-1.amazonaws.com.cn | ||
// | ||
// Cluster Reader Endpoint: <database-cluster-name>.cluster-ro-<xyz>.rds.<aws-region>.amazonaws.com.cn | ||
// Example: test-postgres.cluster-ro-123456789012.rds.cn-northwest-1.amazonaws.com.cn | ||
// | ||
// Cluster Custom Endpoint: <cluster-name-alias>.cluster-custom-<xyz>.rds.<aws-region>.amazonaws.com.cn | ||
// Example: test-postgres-alias.cluster-custom-123456789012.rds.cn-northwest-1.amazonaws.com.cn | ||
// | ||
// Instance Endpoint: <instance-name>.<xyz>.rds.<aws-region>.amazonaws.com.cn | ||
// Example: test-postgres-instance-1.123456789012.rds.cn-northwest-1.amazonaws.com.cn | ||
|
||
private static final Pattern AURORA_DNS_PATTERN = | ||
Pattern.compile( | ||
"(?<instance>.+)\\." | ||
+ "(?<dns>proxy-|cluster-|cluster-ro-|cluster-custom-)?" | ||
+ "(?<domain>[a-zA-Z0-9]+\\.(?<region>[a-zA-Z0-9\\-]+)\\.rds\\.amazonaws\\.com)", | ||
Pattern.CASE_INSENSITIVE); | ||
|
||
private static final Pattern AURORA_CLUSTER_PATTERN = | ||
Pattern.compile( | ||
"(?<instance>.+)\\." | ||
+ "(?<dns>cluster-|cluster-ro-)+" | ||
+ "(?<domain>[a-zA-Z0-9]+\\.(?<region>[a-zA-Z0-9\\-]+)\\.rds\\.amazonaws\\.com)", | ||
Pattern.CASE_INSENSITIVE); | ||
|
||
private static final Pattern AURORA_CHINA_DNS_PATTERN = | ||
Pattern.compile( | ||
"(?<instance>.+)\\." | ||
+ "(?<dns>proxy-|cluster-|cluster-ro-|cluster-custom-)?" | ||
+ "(?<domain>[a-zA-Z0-9]+\\.rds\\.(?<region>[a-zA-Z0-9\\-]+)\\.amazonaws\\.com\\.cn)", | ||
Pattern.CASE_INSENSITIVE); | ||
|
||
private static final Pattern AURORA_CHINA_CLUSTER_PATTERN = | ||
Pattern.compile( | ||
"(?<instance>.+)\\." | ||
+ "(?<dns>cluster-|cluster-ro-)+" | ||
+ "(?<domain>[a-zA-Z0-9]+\\.rds\\.(?<region>[a-zA-Z0-9\\-]+)\\.amazonaws\\.com\\.cn)", | ||
Pattern.CASE_INSENSITIVE); | ||
|
||
private static final String DNS_GROUP = "dns"; | ||
private static final String DOMAIN_GROUP = "domain"; | ||
|
||
public String getRdsInstanceHostPattern(final String host) { | ||
if (StringUtils.isNullOrEmpty(host)) { | ||
return "?"; | ||
} | ||
|
||
final Matcher matcher = AURORA_DNS_PATTERN.matcher(host); | ||
if (matcher.find()) { | ||
return "?." + matcher.group(DOMAIN_GROUP); | ||
} | ||
final Matcher chinaMatcher = AURORA_CHINA_DNS_PATTERN.matcher(host); | ||
if (chinaMatcher.find()) { | ||
return "?." + chinaMatcher.group(DOMAIN_GROUP); | ||
} | ||
return "?"; | ||
} | ||
|
||
public boolean isReaderClusterDns(final String host) { | ||
if (StringUtils.isNullOrEmpty(host)) { | ||
return false; | ||
} | ||
|
||
final Matcher matcher = AURORA_CLUSTER_PATTERN.matcher(host); | ||
if (matcher.find()) { | ||
return "cluster-ro-".equalsIgnoreCase(matcher.group(DNS_GROUP)); | ||
} | ||
final Matcher chinaMatcher = AURORA_CHINA_CLUSTER_PATTERN.matcher(host); | ||
if (chinaMatcher.find()) { | ||
return "cluster-ro-".equalsIgnoreCase(chinaMatcher.group(DNS_GROUP)); | ||
} | ||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters