Skip to content

Commit

Permalink
[CONJ-895] Wrong pattern for detection of aurora
Browse files Browse the repository at this point in the history
  • Loading branch information
diego Dupin committed Aug 31, 2021
1 parent a3c83e4 commit a3cf531
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 72 deletions.
36 changes: 4 additions & 32 deletions src/main/java/org/mariadb/jdbc/UrlParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,6 @@ public class UrlParser implements Cloneable {
private static final String DISABLE_MYSQL_URL = "disableMariaDbDriver";
private static final Pattern URL_PARAMETER =
Pattern.compile("(\\/([^\\?]*))?(\\?(.+))*", Pattern.DOTALL);
private static final Pattern AWS_PATTERN =
Pattern.compile("(.+)\\.([a-z0-9\\-]+\\.rds\\.amazonaws\\.com)", Pattern.CASE_INSENSITIVE);

private String database;
private Options options = null;
Expand Down Expand Up @@ -213,8 +211,7 @@ private static void parseInternal(UrlParser urlParser, String url, Properties pr
if ((dbIndex < paramIndex && dbIndex < 0) || (dbIndex > paramIndex && paramIndex > -1)) {
hostAddressesString = urlSecondPart.substring(0, paramIndex);
additionalParameters = urlSecondPart.substring(paramIndex);
} else if ((dbIndex < paramIndex && dbIndex > -1)
|| (dbIndex > paramIndex && paramIndex < 0)) {
} else if (dbIndex < paramIndex || dbIndex > paramIndex) {
hostAddressesString = urlSecondPart.substring(0, dbIndex);
additionalParameters = urlSecondPart.substring(dbIndex);
} else {
Expand Down Expand Up @@ -255,9 +252,8 @@ private static void defineUrlParserParameters(
throws SQLException {

if (additionalParameters != null) {
//noinspection Annotator
Matcher matcher = URL_PARAMETER.matcher(additionalParameters);
matcher.find();
boolean unused = matcher.find();
urlParser.database = matcher.group(2);
urlParser.options =
DefaultOptions.parse(urlParser.haMode, matcher.group(4), properties, urlParser.options);
Expand Down Expand Up @@ -351,7 +347,7 @@ private void setInitialUrl() {
}

/**
* Permit to set parameters not forced. if options useBatchMultiSend and usePipelineAuth are not
* Permit setting parameters not forced. if options useBatchMultiSend and usePipelineAuth are not
* explicitly set in connection string, value will default to true or false according if aurora
* detection.
*
Expand All @@ -361,7 +357,7 @@ public UrlParser auroraPipelineQuirks() {

// Aurora has issue with pipelining, depending on network speed.
// Driver must rely on information provided by user : hostname if dns, and HA mode.</p>
boolean disablePipeline = isAurora();
boolean disablePipeline = haMode == HaMode.AURORA;

if (options.useBatchMultiSend == null) {
options.useBatchMultiSend = disablePipeline ? Boolean.FALSE : Boolean.TRUE;
Expand All @@ -373,30 +369,6 @@ public UrlParser auroraPipelineQuirks() {
return this;
}

/**
* Detection of Aurora.
*
* <p>Aurora rely on MySQL, then cannot be identified by protocol. But Aurora doesn't permit some
* behaviour normally working with MySQL : pipelining. So Driver must identified if server is
* Aurora to disable pipeline options that are enable by default.
*
* @return true if aurora.
*/
public boolean isAurora() {
if (haMode == HaMode.AURORA) {
return true;
}
if (addresses != null) {
for (HostAddress hostAddress : addresses) {
Matcher matcher = AWS_PATTERN.matcher(hostAddress.host);
if (matcher.find()) {
return true;
}
}
}
return false;
}

/**
* Parse url connection string.
*
Expand Down
3 changes: 2 additions & 1 deletion src/test/java/org/mariadb/jdbc/BaseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
import org.mariadb.jdbc.failover.TcpProxy;
import org.mariadb.jdbc.internal.failover.AbstractMastersListener;
import org.mariadb.jdbc.internal.protocol.Protocol;
import org.mariadb.jdbc.internal.util.constant.HaMode;
import org.mariadb.jdbc.util.Options;

/**
Expand Down Expand Up @@ -965,7 +966,7 @@ public boolean sharedUseCompression() {
}

public boolean sharedIsAurora() {
return urlParser.isAurora();
return urlParser.getHaMode() == HaMode.AURORA;
}

/**
Expand Down
39 changes: 0 additions & 39 deletions src/test/java/org/mariadb/jdbc/JdbcParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -131,32 +131,9 @@ public void testAuroraUseBatchMultiSend() throws Throwable {
.getOptions()
.useBatchMultiSend);

String hostAurora =
"jdbc:mariadb://localhost,instance-1-cluster.cluster-cvz6gk5op1wk.us-east-1.rds.amazonaws.com:3306/test";
assertFalse(UrlParser.parse(hostAurora).auroraPipelineQuirks().getOptions().useBatchMultiSend);
assertTrue(
UrlParser.parse(hostAurora + "?useBatchMultiSend=true")
.auroraPipelineQuirks()
.getOptions()
.useBatchMultiSend);

String hostAuroraUpper =
"jdbc:mariadb://localhost,instance-1-cluster.cluster-cvz6gk5op1wk.us-east-1.rds.AMAZONAWS.com:3306/test";
assertFalse(
UrlParser.parse(hostAuroraUpper).auroraPipelineQuirks().getOptions().useBatchMultiSend);
assertTrue(
UrlParser.parse(hostAuroraUpper + "?useBatchMultiSend=true")
.auroraPipelineQuirks()
.getOptions()
.useBatchMultiSend);

MariaDbDataSource datasource = new MariaDbDataSource();
datasource.initialize();
assertNull(datasource.getUrlParser().getOptions().useBatchMultiSend);
datasource.setUrl(hostAurora);
assertFalse(datasource.getUrlParser().auroraPipelineQuirks().getOptions().useBatchMultiSend);
datasource.setUrl(hostAurora + "?useBatchMultiSend=true");
assertTrue(datasource.getUrlParser().auroraPipelineQuirks().getOptions().useBatchMultiSend);
}

@Test
Expand Down Expand Up @@ -247,25 +224,9 @@ public void testAuroraUsePipelineAuth() throws Throwable {
.getOptions()
.usePipelineAuth);

String hostAurora =
"jdbc:mariadb://localhost,instance-1-cluster.cluster-cvz6gk5op1wk.us-east-1.rds.amazonaws.com:3306/test";
assertFalse(UrlParser.parse(hostAurora).auroraPipelineQuirks().getOptions().usePipelineAuth);
assertTrue(UrlParser.parse(hostAurora + "?usePipelineAuth=true").getOptions().usePipelineAuth);

String hostAuroraUpper =
"jdbc:mariadb://localhost,instance-1-cluster.cluster-cvz6gk5op1wk.us-east-1.RDS.amazonaws.com:3306/test";
assertFalse(
UrlParser.parse(hostAuroraUpper).auroraPipelineQuirks().getOptions().usePipelineAuth);
assertTrue(
UrlParser.parse(hostAuroraUpper + "?usePipelineAuth=true").getOptions().usePipelineAuth);

MariaDbDataSource datasource = new MariaDbDataSource();
datasource.initialize();
assertNull(datasource.getUrlParser().getOptions().usePipelineAuth);
datasource.setUrl(hostAurora);
assertFalse(datasource.getUrlParser().auroraPipelineQuirks().getOptions().usePipelineAuth);
datasource.setUrl(hostAurora + "?usePipelineAuth=true");
assertTrue(datasource.getUrlParser().auroraPipelineQuirks().getOptions().usePipelineAuth);
}

@Test
Expand Down

0 comments on commit a3cf531

Please sign in to comment.