Skip to content

Commit

Permalink
JCES-1896 added compatibleWithPreviousVersion for safety rollout
Browse files Browse the repository at this point in the history
  • Loading branch information
afaruga-atlassian committed Mar 10, 2021
1 parent e69f2a9 commit 09757be
Show file tree
Hide file tree
Showing 7 changed files with 237 additions and 334 deletions.
31 changes: 19 additions & 12 deletions src/main/java/com/atlassian/db/replica/api/DualConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ private DualConnection(
@Override
public Statement createStatement() throws SQLException {
checkClosed();
return ReplicaStatement.builder(connectionProvider, consistency, databaseCall, readOnlyFunctions).build();
return ReplicaStatement.builder(connectionProvider, consistency, databaseCall, readOnlyFunctions, compatibleWithPreviousVersion).build();
}

@Override
Expand All @@ -85,7 +85,8 @@ public PreparedStatement prepareStatement(String sql) throws SQLException {
consistency,
databaseCall,
sql,
readOnlyFunctions
readOnlyFunctions,
compatibleWithPreviousVersion
).build();
}

Expand All @@ -97,7 +98,8 @@ public CallableStatement prepareCall(String sql) throws SQLException {
consistency,
databaseCall,
sql,
readOnlyFunctions
readOnlyFunctions,
compatibleWithPreviousVersion
).build();
}

Expand Down Expand Up @@ -201,7 +203,7 @@ public void clearWarnings() throws SQLException {
public Statement createStatement(int resultSetType, int resultSetConcurrency) throws SQLException {
checkClosed();
return ReplicaStatement
.builder(connectionProvider, consistency, databaseCall, readOnlyFunctions)
.builder(connectionProvider, consistency, databaseCall, readOnlyFunctions, compatibleWithPreviousVersion)
.resultSetType(resultSetType)
.resultSetConcurrency(resultSetConcurrency)
.build();
Expand All @@ -219,7 +221,8 @@ public PreparedStatement prepareStatement(
consistency,
databaseCall,
sql,
readOnlyFunctions
readOnlyFunctions,
compatibleWithPreviousVersion
).resultSetType(resultSetType)
.resultSetConcurrency(resultSetConcurrency)
.build();
Expand All @@ -229,7 +232,7 @@ public PreparedStatement prepareStatement(
public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency) throws SQLException {
checkClosed();
return new ReplicaCallableStatement
.Builder(connectionProvider, consistency, databaseCall, sql, readOnlyFunctions)
.Builder(connectionProvider, consistency, databaseCall, sql, readOnlyFunctions, compatibleWithPreviousVersion)
.resultSetType(resultSetType)
.resultSetConcurrency(resultSetConcurrency)
.build();
Expand Down Expand Up @@ -290,7 +293,7 @@ public Statement createStatement(
int resultSetHoldability
) throws SQLException {
checkClosed();
return ReplicaStatement.builder(connectionProvider, consistency, databaseCall, readOnlyFunctions)
return ReplicaStatement.builder(connectionProvider, consistency, databaseCall, readOnlyFunctions, compatibleWithPreviousVersion)
.resultSetType(resultSetType)
.resultSetConcurrency(resultSetConcurrency)
.resultSetHoldability(resultSetHoldability)
Expand All @@ -310,7 +313,8 @@ public PreparedStatement prepareStatement(
consistency,
databaseCall,
sql,
readOnlyFunctions
readOnlyFunctions,
compatibleWithPreviousVersion
).resultSetType(resultSetType)
.resultSetConcurrency(resultSetConcurrency)
.resultSetHoldability(resultSetHoldability)
Expand All @@ -326,7 +330,7 @@ public CallableStatement prepareCall(
) throws SQLException {
checkClosed();
return new ReplicaCallableStatement
.Builder(connectionProvider, consistency, databaseCall, sql, readOnlyFunctions)
.Builder(connectionProvider, consistency, databaseCall, sql, readOnlyFunctions, compatibleWithPreviousVersion)
.resultSetType(resultSetType)
.resultSetConcurrency(resultSetConcurrency)
.resultSetHoldability(resultSetHoldability)
Expand All @@ -341,7 +345,8 @@ public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys) thr
consistency,
databaseCall,
sql,
readOnlyFunctions
readOnlyFunctions,
compatibleWithPreviousVersion
).autoGeneratedKeys(autoGeneratedKeys)
.build();
}
Expand All @@ -354,7 +359,8 @@ public PreparedStatement prepareStatement(String sql, int[] columnIndexes) throw
consistency,
databaseCall,
sql,
readOnlyFunctions
readOnlyFunctions,
compatibleWithPreviousVersion
).columnIndexes(columnIndexes)
.build();
}
Expand All @@ -367,7 +373,8 @@ public PreparedStatement prepareStatement(String sql, String[] columnNames) thro
consistency,
databaseCall,
sql,
readOnlyFunctions
readOnlyFunctions,
compatibleWithPreviousVersion
).columnNames(columnNames)
.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public Optional<RouteDecision> getCause() {
}

/**
* @return true if the accompanying {@link SqlCall#call()} would fail when run on replica.
* @return true if the accompanying {@link com.atlassian.db.replica.api.SqlCall#call()} would fail when run on replica.
*/
public boolean mustRunOnMain() {
return reason.isWrite();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ public ReplicaCallableStatement(
Integer resultSetType,
Integer resultSetConcurrency,
Integer resultSetHoldability,
Set<String> readOnlyFunctions
Set<String> readOnlyFunctions,
boolean compatibleWithPreviousVersion
) {
super(
connectionProvider,
Expand All @@ -48,8 +49,8 @@ public ReplicaCallableStatement(
resultSetType,
resultSetConcurrency,
resultSetHoldability,
readOnlyFunctions
);
readOnlyFunctions,
compatibleWithPreviousVersion);
this.sql = sql;
this.resultSetType = resultSetType;
this.resultSetConcurrency = resultSetConcurrency;
Expand Down Expand Up @@ -637,6 +638,7 @@ public static class Builder {
private final DatabaseCall databaseCall;
private final String sql;
private final Set<String> readOnlyFunctions;
private final boolean compatibleWithPreviousVersion;
private Integer resultSetType;
private Integer resultSetConcurrency;
private Integer resultSetHoldability;
Expand All @@ -646,13 +648,15 @@ public Builder(
ReplicaConsistency consistency,
DatabaseCall databaseCall,
String sql,
Set<String> readOnlyFunctions
Set<String> readOnlyFunctions,
boolean compatibleWithPreviousVersion
) {
this.connectionProvider = connectionProvider;
this.consistency = consistency;
this.databaseCall = databaseCall;
this.sql = sql;
this.readOnlyFunctions = readOnlyFunctions;
this.compatibleWithPreviousVersion = compatibleWithPreviousVersion;
}

public ReplicaCallableStatement.Builder resultSetType(int resultSetType) {
Expand All @@ -679,8 +683,8 @@ public ReplicaCallableStatement build() {
resultSetType,
resultSetConcurrency,
resultSetHoldability,
readOnlyFunctions
);
readOnlyFunctions,
compatibleWithPreviousVersion);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ protected ReplicaPreparedStatement(
Integer autoGeneratedKeys,
String[] columnNames,
int[] columnIndexes,
Set<String> readOnlyFunctions
Set<String> readOnlyFunctions,
boolean compatibleWithPreviousVersion
) {
super(
consistency,
Expand All @@ -43,8 +44,8 @@ protected ReplicaPreparedStatement(
resultSetType,
resultSetConcurrency,
resultSetHoldability,
readOnlyFunctions
);
readOnlyFunctions,
compatibleWithPreviousVersion);
this.sql = sql;
this.resultSetType = resultSetType;
this.resultSetConcurrency = resultSetConcurrency;
Expand All @@ -62,7 +63,8 @@ protected ReplicaPreparedStatement(
Integer resultSetType,
Integer resultSetConcurrency,
Integer resultSetHoldability,
Set<String> readOnlyFunctions
Set<String> readOnlyFunctions,
boolean compatibleWithPreviousVersion
) {
super(
consistency,
Expand All @@ -71,8 +73,8 @@ protected ReplicaPreparedStatement(
resultSetType,
resultSetConcurrency,
resultSetHoldability,
readOnlyFunctions
);
readOnlyFunctions,
compatibleWithPreviousVersion);
this.sql = sql;
this.resultSetType = resultSetType;
this.resultSetConcurrency = resultSetConcurrency;
Expand Down Expand Up @@ -586,6 +588,7 @@ public static class Builder {
private final DatabaseCall databaseCall;
private final String sql;
private final Set<String> readOnlyFunctions;
private final boolean compatibleWithPreviousVersion;
private Integer resultSetType;
private Integer resultSetConcurrency;
private Integer resultSetHoldability;
Expand All @@ -598,13 +601,15 @@ public Builder(
ReplicaConsistency consistency,
DatabaseCall databaseCall,
String sql,
Set<String> readOnlyFunctions
Set<String> readOnlyFunctions,
boolean compatibleWithPreviousVersion
) {
this.connectionProvider = connectionProvider;
this.consistency = consistency;
this.databaseCall = databaseCall;
this.sql = sql;
this.readOnlyFunctions = readOnlyFunctions;
this.compatibleWithPreviousVersion = compatibleWithPreviousVersion;
}

public ReplicaPreparedStatement.Builder resultSetType(int resultSetType) {
Expand Down Expand Up @@ -649,8 +654,8 @@ public ReplicaPreparedStatement build() {
autoGeneratedKeys,
columnNames,
columnIndexes,
readOnlyFunctions
);
readOnlyFunctions,
compatibleWithPreviousVersion);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,17 @@ public Statement create() throws Exception {
}
};

private boolean compatibleWithPreviousVersion;

public ReplicaStatement(
ReplicaConsistency consistency,
ReplicaConnectionProvider connectionProvider,
DatabaseCall databaseCall,
Integer resultSetType,
Integer resultSetConcurrency,
Integer resultSetHoldability,
Set<String> readOnlyFunctions
Set<String> readOnlyFunctions,
boolean compatibleWithPreviousVersion
) {
this.consistency = consistency;
this.connectionProvider = connectionProvider;
Expand All @@ -55,6 +58,7 @@ public ReplicaStatement(
this.resultSetConcurrency = resultSetConcurrency;
this.resultSetHoldability = resultSetHoldability;
this.sqlFunction = new SqlFunction(readOnlyFunctions);
this.compatibleWithPreviousVersion = compatibleWithPreviousVersion;
}

@Override
Expand Down Expand Up @@ -502,8 +506,14 @@ public long executeLargeUpdate(String sql, String[] columnNames) throws SQLExcep

<T> T execute(final SqlCall<T> call, final RouteDecision routeDecision) throws SQLException {
final T result = databaseCall.call(call, routeDecision);
if (routeDecision.isWrite() && isWriteOperation) {
recordWriteAfterQueryExecution();
if (compatibleWithPreviousVersion) {
if (routeDecision.willRunOnMain() && isWriteOperation) {
recordWriteAfterQueryExecution();
}
} else {
if (routeDecision.mustRunOnMain()) {
recordWriteAfterQueryExecution();
}
}
return result;
}
Expand Down Expand Up @@ -541,9 +551,10 @@ public static Builder builder(
ReplicaConnectionProvider connectionProvider,
ReplicaConsistency consistency,
DatabaseCall databaseCall,
Set<String> readOnlyFunctions
Set<String> readOnlyFunctions,
boolean compatibleWithPreviousVersion
) {
return new Builder(connectionProvider, consistency, databaseCall, readOnlyFunctions);
return new Builder(connectionProvider, consistency, databaseCall, readOnlyFunctions, compatibleWithPreviousVersion);
}

void recordWriteAfterQueryExecution() throws SQLException {
Expand All @@ -560,9 +571,17 @@ public Statement getReadStatement(RouteDecisionBuilder decisionBuilder) {
return prepareWriteStatement(decisionBuilder);
}
SqlQuery sqlQuery = new SqlQuery(decisionBuilder.getSql());
if (sqlQuery.isWriteOperation(sqlFunction)) {
decisionBuilder.reason(WRITE_OPERATION);
return prepareWriteStatement(decisionBuilder);
if (compatibleWithPreviousVersion) {
isWriteOperation = sqlQuery.isWriteOperation(sqlFunction);
if (isWriteOperation) {
decisionBuilder.reason(WRITE_OPERATION);
return prepareWriteStatement(decisionBuilder);
}
} else {
if (sqlQuery.isWriteOperation(sqlFunction)) {
decisionBuilder.reason(WRITE_OPERATION);
return prepareWriteStatement(decisionBuilder);
}
}

if (sqlQuery.isSelectForUpdate()) {
Expand All @@ -575,7 +594,9 @@ public Statement getReadStatement(RouteDecisionBuilder decisionBuilder) {
}

protected Statement getWriteStatement(RouteDecisionBuilder decisionBuilder) {
isWriteOperation = true;
if (compatibleWithPreviousVersion) {
isWriteOperation = true;
}
return prepareWriteStatement(decisionBuilder);
}

Expand Down Expand Up @@ -611,6 +632,7 @@ public static class Builder {
private final ReplicaConsistency consistency;
private final DatabaseCall databaseCall;
private final Set<String> readOnlyFunctions;
private final boolean compatibleWithPreviousVersion;
private Integer resultSetType;
private Integer resultSetConcurrency;
private Integer resultSetHoldability;
Expand All @@ -619,12 +641,14 @@ private Builder(
ReplicaConnectionProvider connectionProvider,
ReplicaConsistency consistency,
DatabaseCall databaseCall,
Set<String> readOnlyFunctions
Set<String> readOnlyFunctions,
boolean compatibleWithPreviousVersion
) {
this.connectionProvider = connectionProvider;
this.consistency = consistency;
this.databaseCall = databaseCall;
this.readOnlyFunctions = readOnlyFunctions;
this.compatibleWithPreviousVersion = compatibleWithPreviousVersion;
}

public Builder resultSetType(int resultSetType) {
Expand All @@ -650,8 +674,8 @@ public ReplicaStatement build() {
resultSetType,
resultSetConcurrency,
resultSetHoldability,
readOnlyFunctions
);
readOnlyFunctions,
compatibleWithPreviousVersion);
}
}

Expand Down
Loading

0 comments on commit 09757be

Please sign in to comment.