Skip to content

Commit

Permalink
[CONJ-726] removing possible NPE after failover on aurora cluster
Browse files Browse the repository at this point in the history
  • Loading branch information
rusher committed Sep 13, 2019
1 parent a41bf18 commit fa120f2
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 81 deletions.
6 changes: 1 addition & 5 deletions src/main/java/org/mariadb/jdbc/UrlParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public class UrlParser implements Cloneable {
private UrlParser() {
}

protected UrlParser(String database, List<HostAddress> addresses, Options options,
public UrlParser(String database, List<HostAddress> addresses, Options options,
HaMode haMode) {
this.options = options;
this.database = database;
Expand Down Expand Up @@ -424,10 +424,6 @@ public List<HostAddress> getHostAddresses() {
return this.addresses;
}

public void setHostAddresses(List<HostAddress> addresses) {
this.addresses = addresses;
}

public Options getOptions() {
return options;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,19 +91,15 @@ public class AuroraListener extends MastersSlavesListener {
*/
public AuroraListener(UrlParser urlParser, final GlobalStateInfo globalInfo) throws SQLException {
super(urlParser, globalInfo);
masterProtocol = null;
secondaryProtocol = null;
clusterHostAddress = findClusterHostAddress(urlParser);
clusterHostAddress = findClusterHostAddress();
}

/**
* Retrieves the cluster host address from the UrlParser instance.
*
* @param urlParser object that holds the connection information
* @return cluster host address
*/
private HostAddress findClusterHostAddress(UrlParser urlParser) throws SQLException {
List<HostAddress> hostAddresses = urlParser.getHostAddresses();
private HostAddress findClusterHostAddress() throws SQLException {
Matcher matcher;
for (HostAddress hostAddress : hostAddresses) {
matcher = auroraDnsPattern.matcher(hostAddress.host);
Expand Down Expand Up @@ -179,11 +175,11 @@ public void reconnectFailedConnection(SearchFilter initialSearchFilter) throws S
// - random order not connected host and not blacklisted
// - random blacklisted host
// - connected host at end.
List<HostAddress> loopAddress = new LinkedList<>(urlParser.getHostAddresses());
List<HostAddress> loopAddress = new LinkedList<>(hostAddresses);
loopAddress.removeAll(getBlacklistKeys());
Collections.shuffle(loopAddress);
List<HostAddress> blacklistShuffle = new LinkedList<>(getBlacklistKeys());
blacklistShuffle.retainAll(urlParser.getHostAddresses());
blacklistShuffle.retainAll(hostAddresses);
Collections.shuffle(blacklistShuffle);
loopAddress.addAll(blacklistShuffle);

Expand All @@ -198,7 +194,7 @@ public void reconnectFailedConnection(SearchFilter initialSearchFilter) throws S
loopAddress.add(secondaryProtocol.getHostAddress());
}

if (urlParser.getHostAddresses().size() <= 1) {
if (hostAddresses.size() <= 1) {
searchFilter = new SearchFilter(true, false);
}
if ((isMasterHostFail() || isSecondaryHostFail())
Expand Down Expand Up @@ -303,13 +299,14 @@ private List<String> getCurrentEndpointIdentifiers(Protocol protocol) throws SQL
private void setUrlParserFromEndpoints(List<String> endpoints, int port) {
List<HostAddress> addresses = new ArrayList<>();
for (String endpoint : endpoints) {
HostAddress newHostAddress = new HostAddress(endpoint, port, null);
addresses.add(newHostAddress);
if (endpoint != null) {
addresses.add(new HostAddress(endpoint, port, null));
}
}

synchronized (urlParser) {
urlParser.setHostAddresses(addresses);
if (addresses.isEmpty()) {
addresses.addAll(urlParser.getHostAddresses());
}
hostAddresses = addresses;
}

/**
Expand Down Expand Up @@ -418,7 +415,7 @@ private HostAddress searchForMasterHostAddress(Protocol protocol, List<HostAddre
masterHostAddress = new HostAddress(masterHostName + "." + clusterDnsSuffix,
protocol.getPort(), null);
loopAddress.add(masterHostAddress);
urlParser.setHostAddresses(loopAddress);
if (!hostAddresses.contains(masterHostAddress)) hostAddresses.add(masterHostAddress);
return masterHostAddress;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ public void run() {

protected Protocol masterProtocol;
protected Protocol secondaryProtocol;
protected List<HostAddress> hostAddresses;

/**
* Initialisation.
Expand All @@ -148,6 +149,7 @@ public MastersSlavesListener(final UrlParser urlParser, final GlobalStateInfo gl
listenerCount.incrementAndGet();
masterProtocol = null;
secondaryProtocol = null;
hostAddresses = urlParser.getHostAddresses();
setMasterHostFail();
setSecondaryHostFail();
}
Expand Down Expand Up @@ -502,11 +504,11 @@ public void reconnectFailedConnection(SearchFilter searchFilter) throws SQLExcep
// - random order not blacklist and not connected host
// - random order blacklist host
// - connected host
List<HostAddress> loopAddress = new LinkedList<>(urlParser.getHostAddresses());
List<HostAddress> loopAddress = new LinkedList<>(hostAddresses);
loopAddress.removeAll(getBlacklistKeys());
Collections.shuffle(loopAddress);
List<HostAddress> blacklistShuffle = new LinkedList<>(getBlacklistKeys());
blacklistShuffle.retainAll(urlParser.getHostAddresses());
blacklistShuffle.retainAll(hostAddresses);
Collections.shuffle(blacklistShuffle);
loopAddress.addAll(blacklistShuffle);

Expand Down
Loading

0 comments on commit fa120f2

Please sign in to comment.