kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | c5629210f8948e4839684c6ac150573175aae7b530f8ce358d0c6212fa47ccc0 |
|---|---|
| Author | Olivier Michallat <olim7t@users.noreply.github.com> · 2015-06-22 17:16 +02:00 |
| Committer | Olivier Michallat <olim7t@users.noreply.github.com> · 2015-06-22 17:16 +02:00 |
| Parents | 20cf9ec03bbf, 1937b0a7aae0 |
3 files changed
modified changelog/README.md +2 −0
| @@ -13,6 +13,8 @@ | ||
| 13 | 13 | - [bug] Support bind marker in QueryBuilder DELETE's list index (JAVA-679) |
| 14 | 14 | - [improvement] Improve QueryBuilder API for SELECT DISTINCT (JAVA-475) |
| 15 | 15 | - [improvement] Create values() function for Insert builder using List (JAVA-225) |
| 16 | +- [improvement] Warn when ReplicationStrategy encounters invalid | |
| 17 | + replication factors (JAVA-702) | |
| 16 | 18 | |
| 17 | 19 | Merged from 2.0.10_fixes branch: |
| 18 | 20 |
modified driver-core/src/main/java/com/datastax/driver/core/ReplicationStategy.java +26 −0
| @@ -18,6 +18,9 @@ | ||
| 18 | 18 | import java.util.*; |
| 19 | 19 | |
| 20 | 20 | import com.google.common.collect.ImmutableSet; |
| 21 | +import com.google.common.collect.Sets; | |
| 22 | +import org.slf4j.Logger; | |
| 23 | +import org.slf4j.LoggerFactory; | |
| 21 | 24 | |
| 22 | 25 | /* |
| 23 | 26 | * Computes the token->list<replica> association, given the token ring and token->primary token map. |
| @@ -87,6 +90,7 @@ | ||
| 87 | 90 | } |
| 88 | 91 | |
| 89 | 92 | static class NetworkTopologyStrategy extends ReplicationStrategy { |
| 93 | + private static final Logger logger = LoggerFactory.getLogger(NetworkTopologyStrategy.class); | |
| 90 | 94 | |
| 91 | 95 | private final Map<String, Integer> replicationFactors; |
| 92 | 96 | |
| @@ -99,6 +103,9 @@ | ||
| 99 | 103 | // This is essentially a copy of org.apache.cassandra.locator.NetworkTopologyStrategy |
| 100 | 104 | Map<String, Set<String>> racks = getRacksInDcs(tokenToPrimary.values()); |
| 101 | 105 | Map<Token, Set<Host>> replicaMap = new HashMap<Token, Set<Host>>(tokenToPrimary.size()); |
| 106 | + | |
| 107 | + Set<String> warnedDcs = Sets.newHashSetWithExpectedSize(replicationFactors.size()); | |
| 108 | + | |
| 102 | 109 | for (int i = 0; i < ring.size(); i++) { |
| 103 | 110 | Map<String, Set<Host>> allDcReplicas = new HashMap<String, Set<Host>>(); |
| 104 | 111 | Map<String, Set<String>> seenRacks = new HashMap<String, Set<String>>(); |
| @@ -147,6 +154,25 @@ | ||
| 147 | 154 | } |
| 148 | 155 | } |
| 149 | 156 | } |
| 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 | + | |
| 150 | 176 | replicaMap.put(ring.get(i), ImmutableSet.copyOf(replicas)); |
| 151 | 177 | } |
| 152 | 178 | return replicaMap; |
modified driver-core/src/test/java/com/datastax/driver/core/NetworkTopologyStrategyTest.java +41 −0
| @@ -22,8 +22,12 @@ | ||
| 22 | 22 | import com.google.common.collect.ImmutableList; |
| 23 | 23 | import com.google.common.collect.ImmutableMap; |
| 24 | 24 | import com.google.common.collect.ImmutableMap.Builder; |
| 25 | +import org.apache.log4j.Level; | |
| 26 | +import org.apache.log4j.Logger; | |
| 25 | 27 | import org.testng.annotations.Test; |
| 26 | 28 | |
| 29 | +import static org.assertj.core.api.Assertions.assertThat; | |
| 30 | + | |
| 27 | 31 | public class NetworkTopologyStrategyTest extends AbstractReplicationStrategyTest { |
| 28 | 32 | |
| 29 | 33 | private static class ReplicationFactorDefinition { |
| @@ -589,4 +593,41 @@ | ||
| 589 | 593 | assertReplicaPlacement(replicaMap, TOKEN17, IP2, IP6, IP1, IP5, IP3, IP4); |
| 590 | 594 | assertReplicaPlacement(replicaMap, TOKEN18, IP6, IP1, IP5, IP3, IP2, IP4); |
| 591 | 595 | } |
| 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 | + } | |
| 592 | 633 | } |