Elffuss BraidHub

kikocis/java-driver public

Extra fix for JAVA-684.

Also refactored TokenRange assertions and removed obsolete test for 415
(none of the TokenRange integration tests would work if 415 wasn't
fixed).
Changeseted65c15f34d284a1d470b4003d586fd96d2825689bc7c251678d20f865203e86
Authorolim7t <omichallat+github@gmail.com> · 2015-03-10 19:05 +01:00
Committerolim7t <omichallat+github@gmail.com> · 2015-03-10 19:47 +01:00
Parentsf19dee63aee4

Browse the files at this changeset

8 files changed

modified driver-core/src/main/java/com/datastax/driver/core/Metadata.java +9 −1
@@ -22,6 +22,7 @@
2222 import java.util.concurrent.ConcurrentMap;
2323 import java.util.regex.Pattern;
2424
25+import com.google.common.collect.ImmutableMap;
2526 import com.google.common.collect.ImmutableSet;
2627 import com.google.common.collect.Maps;
2728 import org.slf4j.Logger;
@@ -510,7 +511,14 @@
510511
511512 tokenToHosts.put(keyspace.getName(), ksTokens);
512513
513- Map<Host, Set<TokenRange>> ksRanges = computeHostsToRangesMap(tokenRanges, ksTokens, hosts.size());
514+ Map<Host, Set<TokenRange>> ksRanges;
515+ if (ring.size() == 1) {
516+ // The single range is ]minToken,minToken] which does not necessarily match the host's token
517+ assert hosts.size() == 1;
518+ ksRanges = ImmutableMap.of(hosts.iterator().next(), tokenRanges);
519+ } else {
520+ ksRanges = computeHostsToRangesMap(tokenRanges, ksTokens, hosts.size());
521+ }
514522 hostsToRanges.put(keyspace.getName(), ksRanges);
515523 }
516524 return new TokenMap(factory, primaryToTokens, tokenToHosts, hostsToRanges, ring, tokenRanges, hosts);
modified driver-core/src/main/java/com/datastax/driver/core/TokenRange.java +3 −1
@@ -19,6 +19,7 @@
1919 import java.util.Iterator;
2020 import java.util.List;
2121
22+import com.google.common.annotations.VisibleForTesting;
2223 import com.google.common.base.Objects;
2324 import com.google.common.collect.ImmutableList;
2425 import com.google.common.collect.Lists;
@@ -34,7 +35,8 @@
3435 public final class TokenRange implements Comparable<TokenRange> {
3536 private final Token start;
3637 private final Token end;
37- private final Token.Factory factory;
38+ @VisibleForTesting
39+ final Token.Factory factory;
3840
3941 TokenRange(Token start, Token end, Token.Factory factory) {
4042 this.start = start;
modified driver-core/src/test/java/com/datastax/driver/core/CCMBridge.java +2 −2
@@ -178,9 +178,9 @@
178178
179179 public void bootstrapNode(int n, String dc) {
180180 if (dc == null)
181- execute("ccm add node%d -i %s%d -j %d -b", n, IP_PREFIX, n, 7000 + 100*n);
181+ execute("ccm add node%d -i %s%d -j %d -b -s", n, IP_PREFIX, n, 7000 + 100*n);
182182 else
183- execute("ccm add node%d -i %s%d -j %d -b -d %s", n, IP_PREFIX, n, 7000 + 100*n, dc);
183+ execute("ccm add node%d -i %s%d -j %d -b -d %s -s", n, IP_PREFIX, n, 7000 + 100*n, dc);
184184 execute("ccm node%d start --wait-other-notice --wait-for-binary-proto", n);
185185 }
186186
added driver-core/src/test/java/com/datastax/driver/core/SingleTokenIntegrationTest.java +65 −0
@@ -0,0 +1,65 @@
1+/*
2+ * Copyright (C) 2012-2014 DataStax Inc.
3+ *
4+ * Licensed under the Apache License, Version 2.0 (the "License");
5+ * you may not use this file except in compliance with the License.
6+ * You may obtain a copy of the License at
7+ *
8+ * http://www.apache.org/licenses/LICENSE-2.0
9+ *
10+ * Unless required by applicable law or agreed to in writing, software
11+ * distributed under the License is distributed on an "AS IS" BASIS,
12+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+ * See the License for the specific language governing permissions and
14+ * limitations under the License.
15+ */
16+package com.datastax.driver.core;
17+
18+import java.util.Set;
19+
20+import org.testng.annotations.Test;
21+
22+import static com.datastax.driver.core.Assertions.assertThat;
23+
24+public class SingleTokenIntegrationTest {
25+ /**
26+ * JAVA-684: Empty TokenRange returned in a one token cluster
27+ */
28+ @Test(groups = "unit")
29+ public void should_return_single_non_empty_range_when_cluster_has_one_single_token() {
30+ CCMBridge ccm = null;
31+ Cluster cluster = null;
32+
33+ try {
34+ ccm = CCMBridge.create("test");
35+ // Set an initial token that is not the minimum
36+ ccm.updateConfig("initial_token", "1");
37+ ccm.bootstrapNode(1);
38+ ccm.start();
39+
40+ cluster = Cluster.builder().addContactPoint(CCMBridge.ipOfNode(1)).build();
41+
42+ Session session = cluster.connect();
43+ session.execute("create keyspace test with replication = {'class': 'SimpleStrategy', 'replication_factor': 1}");
44+
45+ Set<TokenRange> tokenRanges = cluster.getMetadata().getTokenRanges();
46+ assertThat(tokenRanges).hasSize(1);
47+ TokenRange tokenRange = tokenRanges.iterator().next();
48+ assertThat(tokenRange)
49+ .startsWith(Token.M3PToken.FACTORY.minToken())
50+ .endsWith(Token.M3PToken.FACTORY.minToken())
51+ .isNotEmpty()
52+ .isNotWrappedAround();
53+
54+ Set<Host> rangeHosts = cluster.getMetadata().getReplicas("test", tokenRange);
55+ assertThat(rangeHosts)
56+ .containsOnly(TestUtils.findHost(cluster, 1));
57+
58+ } finally {
59+ if (cluster != null)
60+ cluster.close();
61+ if (ccm != null)
62+ ccm.remove();
63+ }
64+ }
65+}
modified driver-core/src/test/java/com/datastax/driver/core/TokenIntegrationTest.java +2 −2
@@ -352,9 +352,9 @@
352352 .isNull();
353353 wrappedRange = range;
354354
355- assertThat(range).unwrapsOverMinToken(tokenFactory());
355+ assertThat(range).isWrappedAround(); // this also checks the unwrapped ranges
356356 } else {
357- assertThat(range).unwrapsToItself();
357+ assertThat(range).isNotWrappedAround();
358358 }
359359 }
360360 }
deleted driver-core/src/test/java/com/datastax/driver/core/TokenMapTest.java +0 −87
@@ -1,87 +0,0 @@
1-/*
2- * Copyright (C) 2012-2014 DataStax Inc.
3- *
4- * Licensed under the Apache License, Version 2.0 (the "License");
5- * you may not use this file except in compliance with the License.
6- * You may obtain a copy of the License at
7- *
8- * http://www.apache.org/licenses/LICENSE-2.0
9- *
10- * Unless required by applicable law or agreed to in writing, software
11- * distributed under the License is distributed on an "AS IS" BASIS,
12- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13- * See the License for the specific language governing permissions and
14- * limitations under the License.
15- */
16-package com.datastax.driver.core;
17-
18-import java.nio.ByteBuffer;
19-import java.util.Collection;
20-import java.util.Map;
21-import java.util.Set;
22-
23-import com.google.common.collect.ImmutableMap;
24-import com.google.common.collect.Lists;
25-import com.google.common.collect.Sets;
26-import org.testng.annotations.*;
27-
28-import static org.mockito.Mockito.mock;
29-import static org.testng.Assert.assertEquals;
30-import static org.testng.Assert.assertFalse;
31-import static org.assertj.core.api.Assertions.*;
32-
33-import com.datastax.driver.core.CCMBridge.CCMCluster;
34-
35-import static com.datastax.driver.core.Token.M3PToken.FACTORY;
36-
37-/**
38- * Tests for {@link com.datastax.driver.core.Metadata.TokenMap}.
39- */
40-public class TokenMapTest {
41- CCMCluster ccmCluster = null;
42- Cluster cluster = null;
43-
44- @BeforeMethod(groups = "short")
45- public void setup() {
46- ccmCluster = CCMBridge.buildCluster(1, Cluster.builder());
47- cluster = ccmCluster.cluster;
48- }
49-
50- /**
51- * Tests that the token map is correctly initialized at startup (JAVA-415).
52- */
53- @Test(groups = "short")
54- public void initTest() {
55- // If the token map is initialized correctly, we should get replicas for any partition key
56- ByteBuffer anyKey = ByteBuffer.wrap(new byte[]{});
57- Set<Host> replicas = cluster.getMetadata().getReplicas("system", anyKey);
58- assertFalse(replicas.isEmpty());
59- }
60-
61- /**
62- * DEVC-684: Empty TokenRange returned in a one token cluster
63- */
64- @Test(groups = "unit")
65- public void should_return_single_non_empty_range_when_cluster_has_one_single_token() {
66- // given a single node cluster with one single token "1"
67- // ("1" is purposely chosen because it is not the minimum token for the partitioner in use)
68- Collection<String> tokens = Sets.newHashSet("1");
69- Map<Host, Collection<String>> allTokens = ImmutableMap.of(mock(Host.class), tokens);
70- // when the token map is built
71- Metadata metadata = new Metadata(mock(Cluster.Manager.class));
72- metadata.rebuildTokenMap("Murmur3Partitioner", allTokens);
73- // then
74- Set<TokenRange> tokenRanges = metadata.getTokenRanges();
75- assertThat(tokenRanges).hasSize(1);
76- TokenRange tokenRange = tokenRanges.iterator().next();
77- assertThat(tokenRange.getStart()).isEqualTo(FACTORY.minToken());
78- assertThat(tokenRange.getEnd()).isEqualTo(FACTORY.minToken());
79- assertThat(tokenRange.isEmpty()).isFalse();
80- assertThat(tokenRange.isWrappedAround()).isFalse();
81- }
82-
83- @AfterMethod(groups = "short")
84- public void teardown() {
85- ccmCluster.discard();
86- }
87-}
modified driver-core/src/test/java/com/datastax/driver/core/TokenRangeAssert.java +47 −22
@@ -5,41 +5,38 @@
55
66 import org.assertj.core.api.AbstractAssert;
77
8-import static org.assertj.core.api.Assertions.assertThat;
8+import static com.datastax.driver.core.Assertions.assertThat;
99
1010 public class TokenRangeAssert extends AbstractAssert<TokenRangeAssert, TokenRange> {
1111 protected TokenRangeAssert(TokenRange actual) {
1212 super(actual, TokenRangeAssert.class);
1313 }
1414
15- public TokenRangeAssert intersects(TokenRange that) {
16- assertThat(actual.intersects(that))
17- .as("%s should intersect %s", actual, that)
18- .isTrue();
19- assertThat(that.intersects(actual))
20- .as("%s should intersect %s", that, actual)
21- .isTrue();
15+ public TokenRangeAssert startsWith(Token token) {
16+ assertThat(actual.getStart()).isEqualTo(token);
2217 return this;
2318 }
2419
25- public TokenRangeAssert doesNotIntersect(TokenRange ... that) {
26- for(TokenRange thatRange : that) {
27- assertThat(actual.intersects(thatRange))
28- .as("%s should not intersect %s", actual, thatRange)
29- .isFalse();
30- assertThat(thatRange.intersects(actual))
31- .as("%s should not intersect %s", thatRange, actual)
32- .isFalse();
33- }
20+ public TokenRangeAssert endsWith(Token token) {
21+ assertThat(actual.getEnd()).isEqualTo(token);
3422 return this;
3523 }
3624
37- public TokenRangeAssert unwrapsToItself() {
38- assertThat(actual.unwrap()).containsExactly(actual);
25+ public TokenRangeAssert isEmpty() {
26+ assertThat(actual.isEmpty()).isTrue();
27+ return this;
28+ }
29+
30+ public TokenRangeAssert isNotEmpty() {
31+ assertThat(actual.isEmpty()).isFalse();
3932 return this;
4033 }
4134
42- public TokenRangeAssert unwrapsOverMinToken(Token.Factory factory) {
35+ public TokenRangeAssert isWrappedAround() {
36+ assertThat(actual.isWrappedAround()).isTrue();
37+
38+ Token.Factory factory = actual.factory;
39+
4340 List<TokenRange> unwrapped = actual.unwrap();
4441 assertThat(unwrapped.size())
4542 .as("%s should unwrap to two ranges, but unwrapped to %s", actual, unwrapped)
@@ -47,11 +44,17 @@
4744
4845 Iterator<TokenRange> unwrappedIt = unwrapped.iterator();
4946 TokenRange firstRange = unwrappedIt.next();
50- assertThat(firstRange.getEnd()).isEqualTo(factory.minToken());
47+ assertThat(firstRange).endsWith(factory.minToken());
5148
5249 TokenRange secondRange = unwrappedIt.next();
53- assertThat(secondRange.getStart()).isEqualTo(factory.minToken());
50+ assertThat(secondRange).startsWith(factory.minToken());
51+
52+ return this;
53+ }
5454
55+ public TokenRangeAssert isNotWrappedAround() {
56+ assertThat(actual.isWrappedAround()).isFalse();
57+ assertThat(actual.unwrap()).containsExactly(actual);
5558 return this;
5659 }
5760
@@ -59,4 +62,26 @@
5962 assertThat(actual.unwrap()).containsExactly(subRanges);
6063 return this;
6164 }
65+
66+ public TokenRangeAssert intersects(TokenRange that) {
67+ assertThat(actual.intersects(that))
68+ .as("%s should intersect %s", actual, that)
69+ .isTrue();
70+ assertThat(that.intersects(actual))
71+ .as("%s should intersect %s", that, actual)
72+ .isTrue();
73+ return this;
74+ }
75+
76+ public TokenRangeAssert doesNotIntersect(TokenRange ... that) {
77+ for(TokenRange thatRange : that) {
78+ assertThat(actual.intersects(thatRange))
79+ .as("%s should not intersect %s", actual, thatRange)
80+ .isFalse();
81+ assertThat(thatRange.intersects(actual))
82+ .as("%s should not intersect %s", thatRange, actual)
83+ .isFalse();
84+ }
85+ return this;
86+ }
6287 }
modified driver-core/src/test/java/com/datastax/driver/core/TokenRangeTest.java +5 −5
@@ -171,11 +171,11 @@
171171 @Test(groups = "unit")
172172 public void should_unwrap_to_non_wrapping_ranges() {
173173 assertThat(tokenRange(9, 3)).unwrapsTo(tokenRange(9, minToken), tokenRange(minToken, 3));
174- assertThat(tokenRange(3, 9)).unwrapsToItself();
175- assertThat(tokenRange(3, minToken)).unwrapsToItself();
176- assertThat(tokenRange(minToken, 3)).unwrapsToItself();
177- assertThat(tokenRange(3, 3)).unwrapsToItself();
178- assertThat(tokenRange(minToken, minToken)).unwrapsToItself();
174+ assertThat(tokenRange(3, 9)).isNotWrappedAround();
175+ assertThat(tokenRange(3, minToken)).isNotWrappedAround();
176+ assertThat(tokenRange(minToken, 3)).isNotWrappedAround();
177+ assertThat(tokenRange(3, 3)).isNotWrappedAround();
178+ assertThat(tokenRange(minToken, minToken)).isNotWrappedAround();
179179 }
180180
181181 @Test(groups = "unit")