kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | cf128f62111221d38153869cba9998023cc25ba32cce85093072cccfd7cc9bad |
|---|---|
| Author | Chris Lohfink <Chris.Lohfink@datastax.com> · 2015-08-04 21:21 -05:00 |
| Committer | Chris Lohfink <Chris.Lohfink@datastax.com> · 2015-08-11 10:30 -05:00 |
| Parents | 9e1d5fce1b40 |
3 files changed
modified changelog/README.md +1 −0
| @@ -34,6 +34,7 @@ | ||
| 34 | 34 | - [improvement] Provide an option to prepare statements only on one node (JAVA-797) |
| 35 | 35 | - [improvement] Provide an option to not re-prepare all statements in onUp (JAVA-658) |
| 36 | 36 | - [improvement] Customizable creation of netty timer (JAVA-853) |
| 37 | +- [bug] Avoid quadratic ring processing with invalid replication factors (JAVA-859) | |
| 37 | 38 | |
| 38 | 39 | Merged from 2.0.10_fixes branch: |
| 39 | 40 |
modified driver-core/src/main/java/com/datastax/driver/core/ReplicationStategy.java +18 −8
| @@ -18,6 +18,7 @@ | ||
| 18 | 18 | import java.util.*; |
| 19 | 19 | |
| 20 | 20 | import com.google.common.collect.ImmutableSet; |
| 21 | +import com.google.common.collect.Maps; | |
| 21 | 22 | import com.google.common.collect.Sets; |
| 22 | 23 | import org.slf4j.Logger; |
| 23 | 24 | import org.slf4j.LoggerFactory; |
| @@ -121,9 +122,16 @@ | ||
| 121 | 122 | // This is essentially a copy of org.apache.cassandra.locator.NetworkTopologyStrategy |
| 122 | 123 | Map<String, Set<String>> racks = getRacksInDcs(tokenToPrimary.values()); |
| 123 | 124 | Map<Token, Set<Host>> replicaMap = new HashMap<Token, Set<Host>>(tokenToPrimary.size()); |
| 124 | - | |
| 125 | + Map<String, Integer> dcHostCount = Maps.newHashMapWithExpectedSize(replicationFactors.size()); | |
| 125 | 126 | Set<String> warnedDcs = Sets.newHashSetWithExpectedSize(replicationFactors.size()); |
| 126 | - | |
| 127 | + // find maximum number of nodes in each DC | |
| 128 | + for (Host host : Sets.newHashSet(tokenToPrimary.values())) { | |
| 129 | + String dc = host.getDatacenter(); | |
| 130 | + if(dcHostCount.get(dc) == null) { | |
| 131 | + dcHostCount.put(dc, 0); | |
| 132 | + } | |
| 133 | + dcHostCount.put(dc, dcHostCount.get(dc) + 1); | |
| 134 | + } | |
| 127 | 135 | for (int i = 0; i < ring.size(); i++) { |
| 128 | 136 | Map<String, Set<Host>> allDcReplicas = new HashMap<String, Set<Host>>(); |
| 129 | 137 | Map<String, Set<String>> seenRacks = new HashMap<String, Set<String>>(); |
| @@ -136,7 +144,7 @@ | ||
| 136 | 144 | |
| 137 | 145 | // Preserve order - primary replica will be first |
| 138 | 146 | Set<Host> replicas = new LinkedHashSet<Host>(); |
| 139 | - for (int j = 0; j < ring.size() && !allDone(allDcReplicas); j++) { | |
| 147 | + for (int j = 0; j < ring.size() && !allDone(allDcReplicas, dcHostCount); j++) { | |
| 140 | 148 | Host h = tokenToPrimary.get(getTokenWrapping(i + j, ring)); |
| 141 | 149 | String dc = h.getDatacenter(); |
| 142 | 150 | if (dc == null || !allDcReplicas.containsKey(dc)) |
| @@ -183,8 +191,7 @@ | ||
| 183 | 191 | if (achievedFactor < expectedFactor && !warnedDcs.contains(dcName)) { |
| 184 | 192 | logger.warn("Error while computing token map for datacenter {}: " |
| 185 | 193 | + "could not achieve replication factor {} (found {} replicas only), " |
| 186 | - + "check your keyspace replication settings. " | |
| 187 | - + "Note that this can affect the performance of the driver.", | |
| 194 | + + "check your keyspace replication settings.", | |
| 188 | 195 | dcName, expectedFactor, achievedFactor); |
| 189 | 196 | // only warn once per DC |
| 190 | 197 | warnedDcs.add(dcName); |
| @@ -196,10 +203,13 @@ | ||
| 196 | 203 | return replicaMap; |
| 197 | 204 | } |
| 198 | 205 | |
| 199 | - private boolean allDone(Map<String, Set<Host>> map) { | |
| 200 | - for (Map.Entry<String, Set<Host>> entry : map.entrySet()) | |
| 201 | - if (entry.getValue().size() < replicationFactors.get(entry.getKey())) | |
| 206 | + private boolean allDone(Map<String, Set<Host>> map, Map<String, Integer> dcHostCount) { | |
| 207 | + for (Map.Entry<String, Set<Host>> entry : map.entrySet()) { | |
| 208 | + String dc = entry.getKey(); | |
| 209 | + int dcCount = dcHostCount.get(dc) == null? 0 : dcHostCount.get(dc); | |
| 210 | + if (entry.getValue().size() < Math.min(replicationFactors.get(dc), dcCount)) | |
| 202 | 211 | return false; |
| 212 | + } | |
| 203 | 213 | return true; |
| 204 | 214 | } |
| 205 | 215 |
modified driver-core/src/test/java/com/datastax/driver/core/NetworkTopologyStrategyTest.java +31 −0
| @@ -22,6 +22,8 @@ | ||
| 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 com.google.common.collect.Lists; | |
| 26 | +import com.google.common.collect.Maps; | |
| 25 | 27 | import org.apache.log4j.Level; |
| 26 | 28 | import org.apache.log4j.Logger; |
| 27 | 29 | import org.testng.annotations.Test; |
| @@ -104,6 +106,18 @@ | ||
| 104 | 106 | |
| 105 | 107 | private static final ReplicationStrategy exampleStrategyTooManyReplicas = networkTopologyStrategy(rf(DC1, 4), rf(DC2, 4)); |
| 106 | 108 | |
| 109 | + private static final List<Token> largeRing = Lists.newArrayList(); | |
| 110 | + private static final Map<Token, Host> largeRingTokenToPrimary = Maps.newHashMap(); | |
| 111 | + static { | |
| 112 | + for (int i = 0; i < 100; i++) { | |
| 113 | + for (int vnodes = 0; vnodes < 256; vnodes++) { | |
| 114 | + Token token = token("" + ((i * 256) + vnodes)); | |
| 115 | + largeRing.add(token); | |
| 116 | + largeRingTokenToPrimary.put(token, host("127.0.0." + i, DC1, RACK11)); | |
| 117 | + } | |
| 118 | + } | |
| 119 | + } | |
| 120 | + | |
| 107 | 121 | private static final List<Token> exampleRing = ImmutableList.<Token>builder() |
| 108 | 122 | .add(TOKEN01) |
| 109 | 123 | .add(TOKEN02) |
| @@ -570,6 +584,23 @@ | ||
| 570 | 584 | assertReplicaPlacement(replicaMap, TOKEN18, IP6, IP1, IP5, IP2); |
| 571 | 585 | } |
| 572 | 586 | |
| 587 | + @Test(groups = "unit") | |
| 588 | + public void networkTopologyStrategyNoNodesInDCTest() { | |
| 589 | + long t1 = System.currentTimeMillis(); | |
| 590 | + Map<Token, Set<Host>> replicaMap = networkTopologyStrategy(rf(DC1, 2), rf(DC2, 2)) | |
| 591 | + .computeTokenToReplicaMap(largeRingTokenToPrimary, largeRing); | |
| 592 | + assertThat(System.currentTimeMillis() - t1).isLessThan(10000); | |
| 593 | + | |
| 594 | + for (int node = 0; node < 99; node++) { // 100th wraps so doesn't match this, check after | |
| 595 | + for (int vnodes = 0; vnodes < 256; vnodes++) { | |
| 596 | + Token token = token("" + ((node * 256) + vnodes)); | |
| 597 | + assertReplicaPlacement(replicaMap, token, "127.0.0." + node, "127.0.0." + (node + 1)); | |
| 598 | + } | |
| 599 | + } | |
| 600 | + assertReplicaPlacement(replicaMap, token(""+ 99 * 256), "127.0.0.99", "127.0.0.0"); | |
| 601 | + } | |
| 602 | + | |
| 603 | + | |
| 573 | 604 | @Test(groups = "unit") |
| 574 | 605 | public void networkTopologyStrategyExampleTopologyTooManyReplicasTest() { |
| 575 | 606 | Map<Token, Set<Host>> replicaMap = exampleStrategyTooManyReplicas.computeTokenToReplicaMap(exampleTokenToPrimary, exampleRing); |