#582: Always find a connection to read

pull/583/head
Shailender Bathula 9 years ago
parent 5f0801d4ef
commit 3b5c1f848b

@ -117,7 +117,6 @@ public class WeightedRoundRobinBalancer implements LoadBalancer {
Map<InetSocketAddress, WeightEntry> weightsCopy = new HashMap<InetSocketAddress, WeightEntry>(weights); Map<InetSocketAddress, WeightEntry> weightsCopy = new HashMap<InetSocketAddress, WeightEntry>(weights);
List<ClientConnectionsEntry> clientsCopy = new ArrayList<ClientConnectionsEntry>();
synchronized (this) { synchronized (this) {
for (Iterator<WeightEntry> iterator = weightsCopy.values().iterator(); iterator.hasNext();) { for (Iterator<WeightEntry> iterator = weightsCopy.values().iterator(); iterator.hasNext();) {
@ -136,15 +135,18 @@ public class WeightedRoundRobinBalancer implements LoadBalancer {
weightsCopy = weights; weightsCopy = weights;
} }
List<ClientConnectionsEntry> clientsCopy = findClients(clients, weightsCopy);
for (InetSocketAddress addr : weightsCopy.keySet()) { // If there are no connections available to servers that have a weight counter
for (ClientConnectionsEntry clientConnectionsEntry : clients) { // remaining, then reset the weight counters and find a connection again. In the worst
if (clientConnectionsEntry.getClient().getAddr().equals(addr) // case, there should always be a connection to the master.
&& !clientConnectionsEntry.isFreezed()) { if (clientsCopy.isEmpty()) {
clientsCopy.add(clientConnectionsEntry); for (WeightEntry entry : weights.values()) {
break; entry.resetWeightCounter();
}
} }
weightsCopy = weights;
clientsCopy = findClients(clients, weightsCopy);
} }
int ind = Math.abs(index.incrementAndGet() % clientsCopy.size()); int ind = Math.abs(index.incrementAndGet() % clientsCopy.size());
@ -155,4 +157,18 @@ public class WeightedRoundRobinBalancer implements LoadBalancer {
} }
} }
private List<ClientConnectionsEntry> findClients(List<ClientConnectionsEntry> clients, Map<InetSocketAddress, WeightEntry> weightsCopy) {
List<ClientConnectionsEntry> clientsCopy = new ArrayList<ClientConnectionsEntry>();
for (InetSocketAddress addr : weightsCopy.keySet()) {
for (ClientConnectionsEntry clientConnectionsEntry : clients) {
if (clientConnectionsEntry.getClient().getAddr().equals(addr)
&& !clientConnectionsEntry.isFreezed()) {
clientsCopy.add(clientConnectionsEntry);
break;
}
}
}
return clientsCopy;
}
} }

Loading…
Cancel
Save