Skip to content

Commit

Permalink
[CONJ-510] allow execution of read-only statements on slaves when mas…
Browse files Browse the repository at this point in the history
…ter is down
  • Loading branch information
rusher committed Nov 7, 2017
1 parent 83f8ec4 commit cc2cfbd
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 13 deletions.
Expand Up @@ -212,14 +212,19 @@ public void reconnectFailedConnection(SearchFilter initialSearchFilter) throws S
//don't throw an exception for this specific exception
}
}
} while (searchFilter.isInitialConnection() && masterProtocol == null);
} while (searchFilter.isInitialConnection()
&& !( masterProtocol != null || (urlParser.getOptions().allowMasterDownConnection && secondaryProtocol != null)));
}

//When reconnecting, search if replicas list has change since first initialisation
if (getCurrentProtocol() != null && !getCurrentProtocol().isClosed()) {
retrieveAllEndpointsAndSet(getCurrentProtocol());
}

if (searchFilter.isInitialConnection() && masterProtocol == null && !currentReadOnlyAsked) {
currentProtocol = this.secondaryProtocol;
currentReadOnlyAsked = true;
}
}

/**
Expand Down
Expand Up @@ -314,7 +314,14 @@ public void reconnectFailedConnection(SearchFilter searchFilter) throws SQLExcep
//don't throw an exception for this specific exception
}
}
} while (searchFilter.isInitialConnection() && masterProtocol == null);
} while (searchFilter.isInitialConnection()
&& !( masterProtocol != null || (urlParser.getOptions().allowMasterDownConnection && secondaryProtocol != null)));

if (searchFilter.isInitialConnection() && masterProtocol == null && !currentReadOnlyAsked) {
currentProtocol = this.secondaryProtocol;
currentReadOnlyAsked = true;
}

}

}
Expand Down
Expand Up @@ -199,17 +199,21 @@ public static void loop(AuroraListener listener, final GlobalStateInfo globalInf
}
}

}

if (listener.isSecondaryHostFailReconnect()
|| (listener.isMasterHostFailReconnect() && probableMasterHost == null)) {
probableMasterHost = listener.searchByStartName(protocol, listener.getUrlParser().getHostAddresses());
if (probableMasterHost != null) {
loopAddresses.remove(probableMasterHost);
AuroraProtocol.searchProbableMaster(listener, globalInfo, probableMasterHost);
if (listener.isMasterHostFailReconnect() && searchFilter.isFineIfFoundOnlySlave()) {
return;
} else {
try {
if (listener.isSecondaryHostFailReconnect()
|| (listener.isMasterHostFailReconnect() && probableMasterHost == null)) {
probableMasterHost = listener.searchByStartName(protocol, listener.getUrlParser().getHostAddresses());
if (probableMasterHost != null) {
loopAddresses.remove(probableMasterHost);
AuroraProtocol.searchProbableMaster(listener, globalInfo, probableMasterHost);
if (listener.isMasterHostFailReconnect() && searchFilter.isFineIfFoundOnlySlave()) {
return;
}
}
}
} finally {
protocol.close();
}
}
} else {
Expand All @@ -225,6 +229,14 @@ public static void loop(AuroraListener listener, final GlobalStateInfo globalInf
return;
}

//in case master not found but slave is , and allowing master down
if (loopAddresses.isEmpty()
&& (listener.isMasterHostFailReconnect()
&& listener.urlParser.getOptions().allowMasterDownConnection
&& !listener.isSecondaryHostFailReconnect())) {
return;
}

// if server has try to connect to all host, and there is remaining master or slave that fail
// add all servers back to continue looping until maxConnectionTry is reached
if (loopAddresses.isEmpty() && !searchFilter.isFailoverLoop() && maxConnectionTry > 0) {
Expand Down
Expand Up @@ -136,6 +136,14 @@ public static void loop(MastersSlavesListener listener, final GlobalStateInfo gl
return;
}

//in case master not found but slave is , and allowing master down
if (loopAddresses.isEmpty()
&& (listener.isMasterHostFailReconnect()
&& listener.urlParser.getOptions().allowMasterDownConnection
&& !listener.isSecondaryHostFailReconnect())) {
return;
}

// if server has try to connect to all host, and there is remaining master or slave that fail
// add all servers back to continue looping until maxConnectionTry is reached
if (loopAddresses.isEmpty() && !searchFilter.isFailoverLoop() && maxConnectionTry > 0) {
Expand Down
Expand Up @@ -527,7 +527,15 @@ public enum DefaultOptions {
*
* Default: false
*/
USE_RESET_CONNECTION("useResetConnection", Boolean.FALSE, "2.2.0");
USE_RESET_CONNECTION("useResetConnection", Boolean.FALSE, "2.2.0"),

/**
* On master/slave configuration, permit to connect Connection defaulting to a slave
* when master is down.
*/
ALLOW_MASTER_DOWN("allowMasterDownConnection", Boolean.FALSE, "2.2.0");



private final String optionName;
private final Object objType;
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/org/mariadb/jdbc/internal/util/Options.java
Expand Up @@ -136,6 +136,7 @@ public class Options implements Cloneable {
public int validConnectionTimeout;
public int loadBalanceBlacklistTimeout = 50;
public int failoverLoopRetries = 120;
public boolean allowMasterDownConnection;

//Pool options
public boolean pool;
Expand Down Expand Up @@ -216,6 +217,7 @@ public boolean equals(Object obj) {
if (assureReadOnly != opt.assureReadOnly) return false;
if (autoReconnect != opt.autoReconnect) return false;
if (failOnReadOnly != opt.failOnReadOnly) return false;
if (allowMasterDownConnection != opt.allowMasterDownConnection) return false;
if (retriesAllDown != opt.retriesAllDown) return false;
if (validConnectionTimeout != opt.validConnectionTimeout) return false;
if (loadBalanceBlacklistTimeout != opt.loadBalanceBlacklistTimeout) return false;
Expand Down Expand Up @@ -344,6 +346,7 @@ public int hashCode() {
result = 31 * result + (assureReadOnly ? 1 : 0);
result = 31 * result + (autoReconnect ? 1 : 0);
result = 31 * result + (failOnReadOnly ? 1 : 0);
result = 31 * result + (allowMasterDownConnection ? 1 : 0);
result = 31 * result + retriesAllDown;
result = 31 * result + validConnectionTimeout;
result = 31 * result + loadBalanceBlacklistTimeout;
Expand Down

0 comments on commit cc2cfbd

Please sign in to comment.