Elffuss BraidHub

kikocis/java-driver public

Merge pull request #293 from datastax/java684

Empty TokenRange returned in a one token cluster (JAVA-684).
Changesetf19dee63aee46dad907ecf958b1aebfce333843cf21384d93058c2a272365fee
AuthorOlivier Michallat <olim7t@users.noreply.github.com> · 2015-03-10 17:54 +01:00
CommitterOlivier Michallat <olim7t@users.noreply.github.com> · 2015-03-10 17:54 +01:00
Parentsa15d1f0cf4da, f9ed03ba0d7a

Browse the files at this changeset

3 files changed

modified driver-core/CHANGELOG.rst +1 −0
@@ -16,6 +16,7 @@
1616 - [improvement] Exclude Netty POM from META-INF in shaded JAR (JAVA-654)
1717 - [bug] Quote single quotes contained in table comments in asCQLQuery method
1818 (JAVA-655)
19+- [bug] Empty TokenRange returned in a one token cluster (JAVA-684)
1920
2021 Merged from 2.0.9_fixes branch:
2122
modified driver-core/src/main/java/com/datastax/driver/core/Metadata.java +9 −4
@@ -547,10 +547,15 @@
547547
548548 private static Set<TokenRange> makeTokenRanges(List<Token> ring, Token.Factory factory) {
549549 ImmutableSet.Builder<TokenRange> builder = ImmutableSet.builder();
550- for (int i = 0; i < ring.size(); i++) {
551- Token start = ring.get(i);
552- Token end = ring.get((i + 1) % ring.size());
553- builder.add(new TokenRange(start, end, factory));
550+ // JAVA-684: if there is only one token, return the range ]minToken, minToken]
551+ if(ring.size() == 1) {
552+ builder.add(new TokenRange(factory.minToken(), factory.minToken(), factory));
553+ } else {
554+ for (int i = 0; i < ring.size(); i++) {
555+ Token start = ring.get(i);
556+ Token end = ring.get((i + 1) % ring.size());
557+ builder.add(new TokenRange(start, end, factory));
558+ }
554559 }
555560 return builder.build();
556561 }
modified driver-core/src/test/java/com/datastax/driver/core/TokenMapTest.java +36 −1
@@ -16,16 +16,26 @@
1616 package com.datastax.driver.core;
1717
1818 import java.nio.ByteBuffer;
19+import java.util.Collection;
20+import java.util.Map;
1921 import java.util.Set;
2022
23+import com.google.common.collect.ImmutableMap;
24+import com.google.common.collect.Lists;
25+import com.google.common.collect.Sets;
2126 import org.testng.annotations.*;
2227
28+import static org.mockito.Mockito.mock;
29+import static org.testng.Assert.assertEquals;
2330 import static org.testng.Assert.assertFalse;
31+import static org.assertj.core.api.Assertions.*;
2432
2533 import com.datastax.driver.core.CCMBridge.CCMCluster;
2634
35+import static com.datastax.driver.core.Token.M3PToken.FACTORY;
36+
2737 /**
28- * Tests that the token map is correctly initialized at startup (JAVA-415).
38+ * Tests for {@link com.datastax.driver.core.Metadata.TokenMap}.
2939 */
3040 public class TokenMapTest {
3141 CCMCluster ccmCluster = null;
@@ -37,6 +47,9 @@
3747 cluster = ccmCluster.cluster;
3848 }
3949
50+ /**
51+ * Tests that the token map is correctly initialized at startup (JAVA-415).
52+ */
4053 @Test(groups = "short")
4154 public void initTest() {
4255 // If the token map is initialized correctly, we should get replicas for any partition key
@@ -45,6 +58,28 @@
4558 assertFalse(replicas.isEmpty());
4659 }
4760
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+
4883 @AfterMethod(groups = "short")
4984 public void teardown() {
5085 ccmCluster.discard();