Elffuss BraidHub

kikocis/java-driver public

Merge pull request #289 from datastax/java681

New TokenRange#intersectWith and public methods to create tokens and ranges (JAVA-681)
Changesetbccef797dff928ba34d1ac133fb74b31a85d11eb17927788d3a835091fceb713
AuthorOlivier Michallat <olim7t@users.noreply.github.com> · 2015-03-05 15:51 -08:00
CommitterOlivier Michallat <olim7t@users.noreply.github.com> · 2015-03-05 15:51 -08:00
Parents2545f7762c19, 78fd0af56d43

Browse the files at this changeset

6 files changed

modified driver-core/CHANGELOG.rst +1 −1
@@ -7,7 +7,7 @@
77 - [new feature] Add AddressTranslater for EC2 multi-region deployment (JAVA-518)
88 - [improvement] Add connection heartbeat (JAVA-533)
99 - [improvement] Reduce level of logs on missing rpc_address (JAVA-568)
10-- [improvement] Expose node token and range information (JAVA-312)
10+- [improvement] Expose node token and range information (JAVA-312, JAVA-681)
1111 - [bug] Fix cluster name mismatch check at startup (JAVA-595)
1212 - [bug] Fix guava dependency when using OSGI (JAVA-620)
1313 - [bug] Fix handling of DROP events when ks name is case-sensitive (JAVA-678)
modified driver-core/src/main/java/com/datastax/driver/core/Metadata.java +32 −0
@@ -27,6 +27,8 @@
2727 import org.slf4j.Logger;
2828 import org.slf4j.LoggerFactory;
2929
30+import com.datastax.driver.core.exceptions.DriverInternalError;
31+
3032 /**
3133 * Keeps metadata on the connected cluster, including known nodes and schema definitions.
3234 */
@@ -407,6 +409,36 @@
407409 return sb.toString();
408410 }
409411
412+ /**
413+ * Builds a new {@link Token} from its string representation, according to the partitioner
414+ * reported by the Cassandra nodes.
415+ *
416+ * @param tokenStr the string representation.
417+ * @return the token.
418+ */
419+ public Token newToken(String tokenStr) {
420+ TokenMap current = tokenMap;
421+ if (current == null)
422+ throw new DriverInternalError("Token factory not set. This should only happen at initialization time");
423+
424+ return current.factory.fromString(tokenStr);
425+ }
426+
427+ /**
428+ * Builds a new {@link TokenRange}.
429+ *
430+ * @param start the start token.
431+ * @param end the end token.
432+ * @return the range.
433+ */
434+ public TokenRange newTokenRange(Token start, Token end) {
435+ TokenMap current = tokenMap;
436+ if (current == null)
437+ throw new DriverInternalError("Token factory not set. This should only happen at initialization time");
438+
439+ return new TokenRange(start, end, current.factory);
440+ }
441+
410442 Token.Factory tokenFactory() {
411443 TokenMap current = tokenMap;
412444 return (current == null) ? null : current.factory;
modified driver-core/src/main/java/com/datastax/driver/core/Token.java +18 −11
@@ -100,8 +100,8 @@
100100
101101 private static final BigInteger RING_END = BigInteger.valueOf(Long.MAX_VALUE);
102102 private static final BigInteger RING_LENGTH = RING_END.subtract(BigInteger.valueOf(Long.MIN_VALUE));
103- public static final M3PToken MIN_TOKEN = new M3PToken(Long.MIN_VALUE);
104- public static final M3PToken MAX_TOKEN = new M3PToken(Long.MAX_VALUE);
103+ static final M3PToken MIN_TOKEN = new M3PToken(Long.MIN_VALUE);
104+ static final M3PToken MAX_TOKEN = new M3PToken(Long.MAX_VALUE);
105105
106106 private long getblock(ByteBuffer key, int offset, int index) {
107107 int i_8 = index << 3;
@@ -202,7 +202,7 @@
202202 }
203203
204204 @Override
205- public M3PToken fromString(String tokenStr) {
205+ M3PToken fromString(String tokenStr) {
206206 return new M3PToken(Long.parseLong(tokenStr));
207207 }
208208
@@ -222,7 +222,7 @@
222222 }
223223
224224 @Override
225- public M3PToken hash(ByteBuffer partitionKey) {
225+ M3PToken hash(ByteBuffer partitionKey) {
226226 long v = murmur(partitionKey);
227227 return new M3PToken(v == Long.MIN_VALUE ? Long.MAX_VALUE : v);
228228 }
@@ -305,8 +305,15 @@
305305
306306 @Override
307307 public OPPToken fromString(String tokenStr) {
308- String prefix = (tokenStr.length() % 2 == 0) ? "0x" : "0x0";
309- ByteBuffer value = Bytes.fromHexString(prefix + tokenStr);
308+ // This method must be able to parse the contents of system.peers.tokens, which do not have the "0x" prefix.
309+ // On the other hand, OPPToken#toString has the "0x" because it should be usable in a CQL query, and it's
310+ // nice to have fromString and toString symetrical.
311+ // So handle both cases:
312+ if (!tokenStr.startsWith("0x")) {
313+ String prefix = (tokenStr.length() % 2 == 0) ? "0x" : "0x0";
314+ tokenStr = prefix + tokenStr;
315+ }
316+ ByteBuffer value = Bytes.fromHexString(tokenStr);
310317 return new OPPToken(value);
311318 }
312319
@@ -326,12 +333,12 @@
326333 }
327334
328335 @Override
329- public OPPToken hash(ByteBuffer partitionKey) {
336+ OPPToken hash(ByteBuffer partitionKey) {
330337 return new OPPToken(partitionKey);
331338 }
332339
333340 @Override
334- public List<Token> split(Token startToken, Token endToken, int numberOfSplits) {
341+ List<Token> split(Token startToken, Token endToken, int numberOfSplits) {
335342 int tokenOrder = startToken.compareTo(endToken);
336343
337344 // ]min,min] means the whole ring. However, since there is no "max token" with this partitioner, we can't come up
@@ -514,7 +521,7 @@
514521 }
515522
516523 @Override
517- public RPToken fromString(String tokenStr) {
524+ RPToken fromString(String tokenStr) {
518525 return new RPToken(new BigInteger(tokenStr));
519526 }
520527
@@ -534,7 +541,7 @@
534541 }
535542
536543 @Override
537- public RPToken hash(ByteBuffer partitionKey) {
544+ RPToken hash(ByteBuffer partitionKey) {
538545 return new RPToken(md5(partitionKey));
539546 }
540547
@@ -559,7 +566,7 @@
559566 tokens.add(new RPToken(value));
560567 return tokens;
561568 }
562- };
569+ }
563570
564571 private RPToken(BigInteger value) {
565572 this.value = value;
modified driver-core/src/main/java/com/datastax/driver/core/TokenRange.java +20 −0
@@ -163,6 +163,26 @@
163163 || that.contains(this.end, false);
164164 }
165165
166+ /**
167+ * Computes the intersection of this range with another one.
168+ * <p>
169+ * This call will fail if the two ranges do not intersect, you must check by calling
170+ * {@link #intersects(TokenRange)} beforehand.
171+ *
172+ * @param that the other range.
173+ * @return the range resulting from the intersection.
174+ * @throws IllegalArgumentException if the ranges do not intersect.
175+ */
176+ public TokenRange intersectWith(TokenRange that) {
177+ if (!this.intersects(that))
178+ throw new IllegalArgumentException("The two ranges do not intersect, use intersects() before calling this method");
179+
180+ return new TokenRange(
181+ (this.contains(that.start, true)) ? that.start : this.start,
182+ (this.contains(that.end, false)) ? that.end : this.end,
183+ factory);
184+ }
185+
166186 // isStart handles the case where the token is the start of another range, for example:
167187 // * ]1,2] contains 2, but it does not contain the start of ]2,3]
168188 // * ]1,2] does not contain 1, but it contains the start of ]1,3]
modified driver-core/src/test/java/com/datastax/driver/core/TokenIntegrationTest.java +14 −1
@@ -76,7 +76,6 @@
7676
7777 @AfterClass(groups = "short", alwaysRun=true)
7878 public void teardown() {
79- System.out.println("Tearing down");
8079 if (cluster != null)
8180 cluster.close();
8281 if (ccm != null)
@@ -360,5 +359,19 @@
360359 }
361360 }
362361
362+ @Test(groups = "short")
363+ public void should_expose_token_and_range_creation_methods() {
364+ Metadata metadata = cluster.getMetadata();
365+
366+ // Pick a random range
367+ TokenRange range = metadata.getTokenRanges().iterator().next();
368+
369+ Token start = metadata.newToken(range.getStart().toString());
370+ Token end = metadata.newToken(range.getEnd().toString());
371+
372+ assertThat(metadata.newTokenRange(start, end))
373+ .isEqualTo(range);
374+ }
375+
363376 protected abstract Token.Factory tokenFactory();
364377 }
modified driver-core/src/test/java/com/datastax/driver/core/TokenRangeTest.java +27 −12
@@ -1,14 +1,11 @@
11 package com.datastax.driver.core;
22
3-import java.math.BigInteger;
43 import java.util.List;
54
65 import org.testng.annotations.Test;
76
87 import static org.testng.Assert.fail;
98
10-import com.datastax.driver.core.Token.M3PToken;
11-
129 import static com.datastax.driver.core.Assertions.assertThat;
1310
1411 public class TokenRangeTest {
@@ -76,6 +73,25 @@
7673 ;
7774 }
7875
76+ @Test(groups = "unit")
77+ public void should_compute_intersection() {
78+ assertThat(tokenRange(3, 9).intersectWith(tokenRange(2, 4)))
79+ .isEqualTo(tokenRange(3, 4));
80+ assertThat(tokenRange(3, 9).intersectWith(tokenRange(3, 5)))
81+ .isEqualTo(tokenRange(3, 5));
82+ assertThat(tokenRange(3, 9).intersectWith(tokenRange(4, 6)))
83+ .isEqualTo(tokenRange(4, 6));
84+ assertThat(tokenRange(3, 9).intersectWith(tokenRange(7, 9)))
85+ .isEqualTo(tokenRange(7, 9));
86+ assertThat(tokenRange(3, 9).intersectWith(tokenRange(8, 10)))
87+ .isEqualTo(tokenRange(8, 9));
88+ }
89+
90+ @Test(groups = "unit", expectedExceptions = IllegalArgumentException.class)
91+ public void should_fail_to_compute_intersection_when_ranges_dont_intersect() {
92+ tokenRange(1, 2).intersectWith(tokenRange(2, 3));
93+ }
94+
7995 @Test(groups = "unit")
8096 public void should_merge_with_other_range() {
8197 assertThat(tokenRange(3, 9).mergeWith(tokenRange(2, 3))).isEqualTo(tokenRange(2, 9));
@@ -128,12 +144,12 @@
128144
129145 @Test(groups = "unit", expectedExceptions = IllegalArgumentException.class)
130146 public void should_not_merge_with_nonadjacent_and_disjoint_ranges() {
131- tokenRange(0,5).mergeWith(tokenRange(7,14));
147+ tokenRange(0, 5).mergeWith(tokenRange(7, 14));
132148 }
133149
134150 @Test(groups = "unit")
135151 public void should_return_non_empty_range_if_other_range_is_empty() {
136- assertThat(tokenRange(1,5).mergeWith(tokenRange(5,5))).isEqualTo(tokenRange(1,5));
152+ assertThat(tokenRange(1, 5).mergeWith(tokenRange(5, 5))).isEqualTo(tokenRange(1, 5));
137153 }
138154
139155 @Test(groups = "unit")
@@ -155,14 +171,13 @@
155171 assertThat(splits).containsExactly(tokenRange(3, 5), tokenRange(5, 7), tokenRange(7, 9));
156172 }
157173
158-
159174 @Test(groups = "unit")
160175 public void should_throw_error_with_less_than_1_splits() {
161- for(int i = -255; i < 1; i++) {
176+ for (int i = -255; i < 1; i++) {
162177 try {
163178 tokenRange(0, 1).splitEvenly(i);
164179 fail("Expected error when providing " + i + " splits.");
165- } catch(IllegalArgumentException e) {
180+ } catch (IllegalArgumentException e) {
166181 // expected.
167182 }
168183 }
@@ -175,15 +190,15 @@
175190
176191 @Test(groups = "unit")
177192 public void should_create_empty_token_ranges_if_too_many_splits() {
178- TokenRange range = tokenRange(0,10);
193+ TokenRange range = tokenRange(0, 10);
179194
180195 List<TokenRange> ranges = range.splitEvenly(255);
181196 assertThat(ranges).hasSize(255);
182197
183- for(int i = 0; i < ranges.size(); i++) {
198+ for (int i = 0; i < ranges.size(); i++) {
184199 TokenRange tr = ranges.get(i);
185- if(i < 10) {
186- assertThat(tr).isEqualTo(tokenRange(i, i+1));
200+ if (i < 10) {
201+ assertThat(tr).isEqualTo(tokenRange(i, i + 1));
187202 } else {
188203 assertThat(tr.isEmpty());
189204 }