Elffuss BraidHub

kikocis/java-driver public

Merge branch '2.0' into 2.1
Changeset3c659615631699fb3c95dd73c9eaffc532335978fe0c3eb64b71f98da9803cbc
Authorolim7t <omichallat+github@gmail.com> · 2015-03-11 09:06 +01:00
Committerolim7t <omichallat+github@gmail.com> · 2015-03-11 09:06 +01:00
Parentscd9155c21b83, a30aaa55e581

Browse the files at this changeset

9 files changed

modified driver-core/src/main/java/com/datastax/driver/core/Metadata.java +11 −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;
@@ -540,7 +541,16 @@
540541
541542 tokenToHosts.put(keyspace.getName(), ksTokens);
542543
543- Map<Host, Set<TokenRange>> ksRanges = computeHostsToRangesMap(tokenRanges, ksTokens, hosts.size());
544+ Map<Host, Set<TokenRange>> ksRanges;
545+ if (ring.size() == 1) {
546+ // We forced the single range to ]minToken,minToken], make sure to use that instead of relying on the host's token
547+ ImmutableMap.Builder<Host, Set<TokenRange>> builder = ImmutableMap.builder();
548+ for (Host host : allTokens.keySet())
549+ builder.put(host, tokenRanges);
550+ ksRanges = builder.build();
551+ } else {
552+ ksRanges = computeHostsToRangesMap(tokenRanges, ksTokens, hosts.size());
553+ }
544554 hostsToRanges.put(keyspace.getName(), ksRanges);
545555 }
546556 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
@@ -177,9 +177,9 @@
177177
178178 public void bootstrapNode(int n, String dc) {
179179 if (dc == null)
180- execute("ccm add node%d -i %s%d -j %d -b", n, IP_PREFIX, n, 7000 + 100*n);
180+ execute("ccm add node%d -i %s%d -j %d -r %d -b -s", n, IP_PREFIX, n, 7000 + 100*n, 8000 + 100*n);
181181 else
182- execute("ccm add node%d -i %s%d -j %d -b -d %s", n, IP_PREFIX, n, 7000 + 100*n, dc);
182+ execute("ccm add node%d -i %s%d -j %d -b -d %s -s", n, IP_PREFIX, n, 7000 + 100*n, dc);
183183 execute("ccm node%d start --wait-other-notice --wait-for-binary-proto", n);
184184 }
185185
modified driver-core/src/test/java/com/datastax/driver/core/RequestHandlerTest.java +2 −1
@@ -31,7 +31,8 @@
3131 cluster = Cluster.builder().addContactPoint("127.0.0.1").withPort(scassandra.getBinaryPort())
3232 .withPoolingOptions(new PoolingOptions()
3333 .setCoreConnectionsPerHost(HostDistance.LOCAL, 1)
34- .setMaxConnectionsPerHost(HostDistance.LOCAL, 1))
34+ .setMaxConnectionsPerHost(HostDistance.LOCAL, 1)
35+ .setHeartbeatIntervalSeconds(0))
3536 .build();
3637
3738 Session session = cluster.connect();
added driver-core/src/test/java/com/datastax/driver/core/SingleTokenIntegrationTest.java +76 −0
@@ -0,0 +1,76 @@
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.Set;
20+
21+import org.testng.annotations.Test;
22+
23+import com.datastax.driver.core.utils.Bytes;
24+
25+import static com.datastax.driver.core.Assertions.assertThat;
26+
27+public class SingleTokenIntegrationTest {
28+ /**
29+ * JAVA-684: Empty TokenRange returned in a one token cluster
30+ */
31+ @Test(groups = "unit")
32+ public void should_return_single_non_empty_range_when_cluster_has_one_single_token() {
33+ CCMBridge ccm = null;
34+ Cluster cluster = null;
35+
36+ try {
37+ ccm = CCMBridge.create("test");
38+ // force the initial token to a non-min value to validate that the single range will always be ]minToken, minToken]
39+ ccm.updateConfig("initial_token", "1");
40+ ccm.bootstrapNode(1);
41+ ccm.start();
42+
43+ cluster = Cluster.builder().addContactPoint(CCMBridge.ipOfNode(1)).build();
44+
45+ Session session = cluster.connect();
46+ session.execute("create keyspace test with replication = {'class': 'SimpleStrategy', 'replication_factor': 1}");
47+
48+ Metadata metadata = cluster.getMetadata();
49+
50+ Set<TokenRange> tokenRanges = metadata.getTokenRanges();
51+ assertThat(tokenRanges).hasSize(1);
52+ TokenRange tokenRange = tokenRanges.iterator().next();
53+ assertThat(tokenRange)
54+ .startsWith(Token.M3PToken.FACTORY.minToken())
55+ .endsWith(Token.M3PToken.FACTORY.minToken())
56+ .isNotEmpty()
57+ .isNotWrappedAround();
58+
59+ Set<Host> hostsForRange = metadata.getReplicas("test", tokenRange);
60+ Host host1 = TestUtils.findHost(cluster, 1);
61+ assertThat(hostsForRange).containsOnly(host1);
62+
63+ ByteBuffer randomPartitionKey = Bytes.fromHexString("0xCAFEBABE");
64+ Set<Host> hostsForKey = metadata.getReplicas("test", randomPartitionKey);
65+ assertThat(hostsForKey).containsOnly(host1);
66+
67+ Set<TokenRange> rangesForHost = metadata.getTokenRanges("test", host1);
68+ assertThat(rangesForHost).containsOnly(tokenRange);
69+ } finally {
70+ if (cluster != null)
71+ cluster.close();
72+ if (ccm != null)
73+ ccm.remove();
74+ }
75+ }
76+}
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")