Skip to content

Commit

Permalink
Synchronized the array list in setUrlParserFromEndpoints
Browse files Browse the repository at this point in the history
- For thread safety
- Change based on feedback
  • Loading branch information
Phan committed Jul 8, 2016
1 parent 0af765c commit 82dd75c
Showing 1 changed file with 19 additions and 12 deletions.
Expand Up @@ -300,25 +300,32 @@ private List<String> getCurrentEndpointIdentifiers(Protocol protocol, String url
* @param port port that is common to all endpoints
*/
private void setUrlParserFromEndpoints(List<String> endpoints, String clusterHost, int port) {
List<HostAddress> addresses = urlParser.getHostAddresses();
List<HostAddress> addresses = Collections.synchronizedList(urlParser.getHostAddresses());

List<String> currentHosts = new ArrayList<>();
for (HostAddress address: addresses) {
currentHosts.add(address.host);
synchronized (addresses) {
for (HostAddress address : addresses) {
currentHosts.add(address.host);
}
}

for (int i = 0; i < addresses.size() && endpoints.size() > 0; i++) {
if (!addresses.get(i).host.equals(clusterHost) && !endpoints.contains(addresses.get(i).host)) {
removeFromBlacklist(addresses.get(i));
addresses.remove(i);
i--;
synchronized (addresses) {
Iterator<HostAddress> iterator = addresses.iterator();
while (iterator.hasNext() && endpoints.size() > 0) {
HostAddress address = iterator.next();
if (!address.host.equals(clusterHost) && !endpoints.contains(address.host)) {
removeFromBlacklist(address);
iterator.remove();
}
}
}

for (String endpoint: endpoints) {
if (!currentHosts.contains(endpoint)) {
HostAddress newHostAddress = new HostAddress(endpoint, port, null);
addresses.add(newHostAddress);
synchronized (addresses) {
for (String endpoint : endpoints) {
if (!currentHosts.contains(endpoint)) {
HostAddress newHostAddress = new HostAddress(endpoint, port, null);
addresses.add(newHostAddress);
}
}
}
}
Expand Down

0 comments on commit 82dd75c

Please sign in to comment.