Elffuss BraidHub

kikocis/java-driver public

Warn when ReplicationStrategy encounters invalid replication factors (JAVA-702).
Changeset1937b0a7aae0089170b9ecf8308f2c817f18d655742010e9a9c644f5e3c78dd7
Authorolim7t <omichallat+github@gmail.com> · 2015-06-17 14:55 +02:00
Committerolim7t <omichallat+github@gmail.com> · 2015-06-22 17:15 +02:00
Parents20cf9ec03bbf

Browse the files at this changeset

3 files changed

modified changelog/README.md +2 −0
@@ -13,6 +13,8 @@
1313 - [bug] Support bind marker in QueryBuilder DELETE's list index (JAVA-679)
1414 - [improvement] Improve QueryBuilder API for SELECT DISTINCT (JAVA-475)
1515 - [improvement] Create values() function for Insert builder using List (JAVA-225)
16+- [improvement] Warn when ReplicationStrategy encounters invalid
17+ replication factors (JAVA-702)
1618
1719 Merged from 2.0.10_fixes branch:
1820
modified driver-core/src/main/java/com/datastax/driver/core/ReplicationStategy.java +26 −0
@@ -18,6 +18,9 @@
1818 import java.util.*;
1919
2020 import com.google.common.collect.ImmutableSet;
21+import com.google.common.collect.Sets;
22+import org.slf4j.Logger;
23+import org.slf4j.LoggerFactory;
2124
2225 /*
2326 * Computes the token->list<replica> association, given the token ring and token->primary token map.
@@ -87,6 +90,7 @@
8790 }
8891
8992 static class NetworkTopologyStrategy extends ReplicationStrategy {
93+ private static final Logger logger = LoggerFactory.getLogger(NetworkTopologyStrategy.class);
9094
9195 private final Map<String, Integer> replicationFactors;
9296
@@ -99,6 +103,9 @@
99103 // This is essentially a copy of org.apache.cassandra.locator.NetworkTopologyStrategy
100104 Map<String, Set<String>> racks = getRacksInDcs(tokenToPrimary.values());
101105 Map<Token, Set<Host>> replicaMap = new HashMap<Token, Set<Host>>(tokenToPrimary.size());
106+
107+ Set<String> warnedDcs = Sets.newHashSetWithExpectedSize(replicationFactors.size());
108+
102109 for (int i = 0; i < ring.size(); i++) {
103110 Map<String, Set<Host>> allDcReplicas = new HashMap<String, Set<Host>>();
104111 Map<String, Set<String>> seenRacks = new HashMap<String, Set<String>>();
@@ -147,6 +154,25 @@
147154 }
148155 }
149156 }
157+
158+ // If we haven't found enough replicas after a whole trip around the ring, this probably
159+ // means that the replication factors are broken.
160+ // Warn the user because that leads to quadratic performance of this method (JAVA-702).
161+ for (Map.Entry<String, Set<Host>> entry : allDcReplicas.entrySet()) {
162+ String dcName = entry.getKey();
163+ int expectedFactor = replicationFactors.get(dcName);
164+ int achievedFactor = entry.getValue().size();
165+ if (achievedFactor < expectedFactor && !warnedDcs.contains(dcName)) {
166+ logger.warn("Error while computing token map for datacenter {}: "
167+ + "could not achieve replication factor {} (found {} replicas only), "
168+ + "check your keyspace replication settings. "
169+ + "Note that this can affect the performance of the driver.",
170+ dcName, expectedFactor, achievedFactor);
171+ // only warn once per DC
172+ warnedDcs.add(dcName);
173+ }
174+ }
175+
150176 replicaMap.put(ring.get(i), ImmutableSet.copyOf(replicas));
151177 }
152178 return replicaMap;
modified driver-core/src/test/java/com/datastax/driver/core/NetworkTopologyStrategyTest.java +41 −0
@@ -22,8 +22,12 @@
2222 import com.google.common.collect.ImmutableList;
2323 import com.google.common.collect.ImmutableMap;
2424 import com.google.common.collect.ImmutableMap.Builder;
25+import org.apache.log4j.Level;
26+import org.apache.log4j.Logger;
2527 import org.testng.annotations.Test;
2628
29+import static org.assertj.core.api.Assertions.assertThat;
30+
2731 public class NetworkTopologyStrategyTest extends AbstractReplicationStrategyTest {
2832
2933 private static class ReplicationFactorDefinition {
@@ -589,4 +593,41 @@
589593 assertReplicaPlacement(replicaMap, TOKEN17, IP2, IP6, IP1, IP5, IP3, IP4);
590594 assertReplicaPlacement(replicaMap, TOKEN18, IP6, IP1, IP5, IP3, IP2, IP4);
591595 }
596+
597+ @Test(groups = "unit")
598+ public void should_warn_if_replication_factor_cannot_be_met() {
599+ Logger logger = Logger.getLogger(ReplicationStrategy.NetworkTopologyStrategy.class);
600+ MemoryAppender logs = new MemoryAppender();
601+ logger.setLevel(Level.WARN);
602+ logger.addAppender(logs);
603+
604+ List<Token> ring = ImmutableList.<Token>builder()
605+ .add(TOKEN01)
606+ .add(TOKEN02)
607+ .add(TOKEN03)
608+ .add(TOKEN04)
609+ .build();
610+
611+ Map<Token, Host> tokenToPrimary = ImmutableMap.<Token, Host>builder()
612+ .put(TOKEN01, host(IP1, DC1, RACK11))
613+ .put(TOKEN02, host(IP2, DC1, RACK12))
614+ .put(TOKEN03, host(IP3, DC2, RACK21))
615+ .put(TOKEN04, host(IP4, DC2, RACK22))
616+ .build();
617+
618+ // Wrong configuration: impossible replication factor for DC2
619+ networkTopologyStrategy(rf(DC1, 2), rf(DC2, 3))
620+ .computeTokenToReplicaMap(tokenToPrimary, ring);
621+ assertThat(logs.getNext())
622+ .contains("Error while computing token map for datacenter DC2");
623+
624+ // Wrong configuration: non-existing datacenter
625+ networkTopologyStrategy(rf(DC1, 2), rf("does_not_exist", 2))
626+ .computeTokenToReplicaMap(tokenToPrimary, ring);
627+ assertThat(logs.getNext())
628+ .contains("Error while computing token map for datacenter does_not_exist");
629+
630+ logger.setLevel(null);
631+ logger.removeAppender(logs);
632+ }
592633 }