kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | 3c659615631699fb3c95dd73c9eaffc532335978fe0c3eb64b71f98da9803cbc |
|---|---|
| Author | olim7t <omichallat+github@gmail.com> · 2015-03-11 09:06 +01:00 |
| Committer | olim7t <omichallat+github@gmail.com> · 2015-03-11 09:06 +01:00 |
| Parents | cd9155c21b83, a30aaa55e581 |
9 files changed
modified driver-core/src/main/java/com/datastax/driver/core/Metadata.java +11 −1
| @@ -22,6 +22,7 @@ | ||
| 22 | 22 | import java.util.concurrent.ConcurrentMap; |
| 23 | 23 | import java.util.regex.Pattern; |
| 24 | 24 | |
| 25 | +import com.google.common.collect.ImmutableMap; | |
| 25 | 26 | import com.google.common.collect.ImmutableSet; |
| 26 | 27 | import com.google.common.collect.Maps; |
| 27 | 28 | import org.slf4j.Logger; |
| @@ -540,7 +541,16 @@ | ||
| 540 | 541 | |
| 541 | 542 | tokenToHosts.put(keyspace.getName(), ksTokens); |
| 542 | 543 | |
| 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 | + } | |
| 544 | 554 | hostsToRanges.put(keyspace.getName(), ksRanges); |
| 545 | 555 | } |
| 546 | 556 | 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 @@ | ||
| 19 | 19 | import java.util.Iterator; |
| 20 | 20 | import java.util.List; |
| 21 | 21 | |
| 22 | +import com.google.common.annotations.VisibleForTesting; | |
| 22 | 23 | import com.google.common.base.Objects; |
| 23 | 24 | import com.google.common.collect.ImmutableList; |
| 24 | 25 | import com.google.common.collect.Lists; |
| @@ -34,7 +35,8 @@ | ||
| 34 | 35 | public final class TokenRange implements Comparable<TokenRange> { |
| 35 | 36 | private final Token start; |
| 36 | 37 | private final Token end; |
| 37 | - private final Token.Factory factory; | |
| 38 | + @VisibleForTesting | |
| 39 | + final Token.Factory factory; | |
| 38 | 40 | |
| 39 | 41 | TokenRange(Token start, Token end, Token.Factory factory) { |
| 40 | 42 | this.start = start; |
modified driver-core/src/test/java/com/datastax/driver/core/CCMBridge.java +2 −2
| @@ -177,9 +177,9 @@ | ||
| 177 | 177 | |
| 178 | 178 | public void bootstrapNode(int n, String dc) { |
| 179 | 179 | 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); | |
| 181 | 181 | 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); | |
| 183 | 183 | execute("ccm node%d start --wait-other-notice --wait-for-binary-proto", n); |
| 184 | 184 | } |
| 185 | 185 |
modified driver-core/src/test/java/com/datastax/driver/core/RequestHandlerTest.java +2 −1
| @@ -31,7 +31,8 @@ | ||
| 31 | 31 | cluster = Cluster.builder().addContactPoint("127.0.0.1").withPort(scassandra.getBinaryPort()) |
| 32 | 32 | .withPoolingOptions(new PoolingOptions() |
| 33 | 33 | .setCoreConnectionsPerHost(HostDistance.LOCAL, 1) |
| 34 | - .setMaxConnectionsPerHost(HostDistance.LOCAL, 1)) | |
| 34 | + .setMaxConnectionsPerHost(HostDistance.LOCAL, 1) | |
| 35 | + .setHeartbeatIntervalSeconds(0)) | |
| 35 | 36 | .build(); |
| 36 | 37 | |
| 37 | 38 | 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 @@ | ||
| 352 | 352 | .isNull(); |
| 353 | 353 | wrappedRange = range; |
| 354 | 354 | |
| 355 | - assertThat(range).unwrapsOverMinToken(tokenFactory()); | |
| 355 | + assertThat(range).isWrappedAround(); // this also checks the unwrapped ranges | |
| 356 | 356 | } else { |
| 357 | - assertThat(range).unwrapsToItself(); | |
| 357 | + assertThat(range).isNotWrappedAround(); | |
| 358 | 358 | } |
| 359 | 359 | } |
| 360 | 360 | } |
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 @@ | ||
| 5 | 5 | |
| 6 | 6 | import org.assertj.core.api.AbstractAssert; |
| 7 | 7 | |
| 8 | -import static org.assertj.core.api.Assertions.assertThat; | |
| 8 | +import static com.datastax.driver.core.Assertions.assertThat; | |
| 9 | 9 | |
| 10 | 10 | public class TokenRangeAssert extends AbstractAssert<TokenRangeAssert, TokenRange> { |
| 11 | 11 | protected TokenRangeAssert(TokenRange actual) { |
| 12 | 12 | super(actual, TokenRangeAssert.class); |
| 13 | 13 | } |
| 14 | 14 | |
| 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); | |
| 22 | 17 | return this; |
| 23 | 18 | } |
| 24 | 19 | |
| 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); | |
| 34 | 22 | return this; |
| 35 | 23 | } |
| 36 | 24 | |
| 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(); | |
| 39 | 32 | return this; |
| 40 | 33 | } |
| 41 | 34 | |
| 42 | - public TokenRangeAssert unwrapsOverMinToken(Token.Factory factory) { | |
| 35 | + public TokenRangeAssert isWrappedAround() { | |
| 36 | + assertThat(actual.isWrappedAround()).isTrue(); | |
| 37 | + | |
| 38 | + Token.Factory factory = actual.factory; | |
| 39 | + | |
| 43 | 40 | List<TokenRange> unwrapped = actual.unwrap(); |
| 44 | 41 | assertThat(unwrapped.size()) |
| 45 | 42 | .as("%s should unwrap to two ranges, but unwrapped to %s", actual, unwrapped) |
| @@ -47,11 +44,17 @@ | ||
| 47 | 44 | |
| 48 | 45 | Iterator<TokenRange> unwrappedIt = unwrapped.iterator(); |
| 49 | 46 | TokenRange firstRange = unwrappedIt.next(); |
| 50 | - assertThat(firstRange.getEnd()).isEqualTo(factory.minToken()); | |
| 47 | + assertThat(firstRange).endsWith(factory.minToken()); | |
| 51 | 48 | |
| 52 | 49 | TokenRange secondRange = unwrappedIt.next(); |
| 53 | - assertThat(secondRange.getStart()).isEqualTo(factory.minToken()); | |
| 50 | + assertThat(secondRange).startsWith(factory.minToken()); | |
| 51 | + | |
| 52 | + return this; | |
| 53 | + } | |
| 54 | 54 | |
| 55 | + public TokenRangeAssert isNotWrappedAround() { | |
| 56 | + assertThat(actual.isWrappedAround()).isFalse(); | |
| 57 | + assertThat(actual.unwrap()).containsExactly(actual); | |
| 55 | 58 | return this; |
| 56 | 59 | } |
| 57 | 60 | |
| @@ -59,4 +62,26 @@ | ||
| 59 | 62 | assertThat(actual.unwrap()).containsExactly(subRanges); |
| 60 | 63 | return this; |
| 61 | 64 | } |
| 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 | + } | |
| 62 | 87 | } |
modified driver-core/src/test/java/com/datastax/driver/core/TokenRangeTest.java +5 −5
| @@ -171,11 +171,11 @@ | ||
| 171 | 171 | @Test(groups = "unit") |
| 172 | 172 | public void should_unwrap_to_non_wrapping_ranges() { |
| 173 | 173 | 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(); | |
| 179 | 179 | } |
| 180 | 180 | |
| 181 | 181 | @Test(groups = "unit") |