kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | bccef797dff928ba34d1ac133fb74b31a85d11eb17927788d3a835091fceb713 |
|---|---|
| Author | Olivier Michallat <olim7t@users.noreply.github.com> · 2015-03-05 15:51 -08:00 |
| Committer | Olivier Michallat <olim7t@users.noreply.github.com> · 2015-03-05 15:51 -08:00 |
| Parents | 2545f7762c19, 78fd0af56d43 |
6 files changed
modified driver-core/CHANGELOG.rst +1 −1
| @@ -7,7 +7,7 @@ | ||
| 7 | 7 | - [new feature] Add AddressTranslater for EC2 multi-region deployment (JAVA-518) |
| 8 | 8 | - [improvement] Add connection heartbeat (JAVA-533) |
| 9 | 9 | - [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) | |
| 11 | 11 | - [bug] Fix cluster name mismatch check at startup (JAVA-595) |
| 12 | 12 | - [bug] Fix guava dependency when using OSGI (JAVA-620) |
| 13 | 13 | - [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 @@ | ||
| 27 | 27 | import org.slf4j.Logger; |
| 28 | 28 | import org.slf4j.LoggerFactory; |
| 29 | 29 | |
| 30 | +import com.datastax.driver.core.exceptions.DriverInternalError; | |
| 31 | + | |
| 30 | 32 | /** |
| 31 | 33 | * Keeps metadata on the connected cluster, including known nodes and schema definitions. |
| 32 | 34 | */ |
| @@ -407,6 +409,36 @@ | ||
| 407 | 409 | return sb.toString(); |
| 408 | 410 | } |
| 409 | 411 | |
| 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 | + | |
| 410 | 442 | Token.Factory tokenFactory() { |
| 411 | 443 | TokenMap current = tokenMap; |
| 412 | 444 | return (current == null) ? null : current.factory; |
modified driver-core/src/main/java/com/datastax/driver/core/Token.java +18 −11
| @@ -100,8 +100,8 @@ | ||
| 100 | 100 | |
| 101 | 101 | private static final BigInteger RING_END = BigInteger.valueOf(Long.MAX_VALUE); |
| 102 | 102 | 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); | |
| 105 | 105 | |
| 106 | 106 | private long getblock(ByteBuffer key, int offset, int index) { |
| 107 | 107 | int i_8 = index << 3; |
| @@ -202,7 +202,7 @@ | ||
| 202 | 202 | } |
| 203 | 203 | |
| 204 | 204 | @Override |
| 205 | - public M3PToken fromString(String tokenStr) { | |
| 205 | + M3PToken fromString(String tokenStr) { | |
| 206 | 206 | return new M3PToken(Long.parseLong(tokenStr)); |
| 207 | 207 | } |
| 208 | 208 | |
| @@ -222,7 +222,7 @@ | ||
| 222 | 222 | } |
| 223 | 223 | |
| 224 | 224 | @Override |
| 225 | - public M3PToken hash(ByteBuffer partitionKey) { | |
| 225 | + M3PToken hash(ByteBuffer partitionKey) { | |
| 226 | 226 | long v = murmur(partitionKey); |
| 227 | 227 | return new M3PToken(v == Long.MIN_VALUE ? Long.MAX_VALUE : v); |
| 228 | 228 | } |
| @@ -305,8 +305,15 @@ | ||
| 305 | 305 | |
| 306 | 306 | @Override |
| 307 | 307 | 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); | |
| 310 | 317 | return new OPPToken(value); |
| 311 | 318 | } |
| 312 | 319 | |
| @@ -326,12 +333,12 @@ | ||
| 326 | 333 | } |
| 327 | 334 | |
| 328 | 335 | @Override |
| 329 | - public OPPToken hash(ByteBuffer partitionKey) { | |
| 336 | + OPPToken hash(ByteBuffer partitionKey) { | |
| 330 | 337 | return new OPPToken(partitionKey); |
| 331 | 338 | } |
| 332 | 339 | |
| 333 | 340 | @Override |
| 334 | - public List<Token> split(Token startToken, Token endToken, int numberOfSplits) { | |
| 341 | + List<Token> split(Token startToken, Token endToken, int numberOfSplits) { | |
| 335 | 342 | int tokenOrder = startToken.compareTo(endToken); |
| 336 | 343 | |
| 337 | 344 | // ]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 @@ | ||
| 514 | 521 | } |
| 515 | 522 | |
| 516 | 523 | @Override |
| 517 | - public RPToken fromString(String tokenStr) { | |
| 524 | + RPToken fromString(String tokenStr) { | |
| 518 | 525 | return new RPToken(new BigInteger(tokenStr)); |
| 519 | 526 | } |
| 520 | 527 | |
| @@ -534,7 +541,7 @@ | ||
| 534 | 541 | } |
| 535 | 542 | |
| 536 | 543 | @Override |
| 537 | - public RPToken hash(ByteBuffer partitionKey) { | |
| 544 | + RPToken hash(ByteBuffer partitionKey) { | |
| 538 | 545 | return new RPToken(md5(partitionKey)); |
| 539 | 546 | } |
| 540 | 547 | |
| @@ -559,7 +566,7 @@ | ||
| 559 | 566 | tokens.add(new RPToken(value)); |
| 560 | 567 | return tokens; |
| 561 | 568 | } |
| 562 | - }; | |
| 569 | + } | |
| 563 | 570 | |
| 564 | 571 | private RPToken(BigInteger value) { |
| 565 | 572 | this.value = value; |
modified driver-core/src/main/java/com/datastax/driver/core/TokenRange.java +20 −0
| @@ -163,6 +163,26 @@ | ||
| 163 | 163 | || that.contains(this.end, false); |
| 164 | 164 | } |
| 165 | 165 | |
| 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 | + | |
| 166 | 186 | // isStart handles the case where the token is the start of another range, for example: |
| 167 | 187 | // * ]1,2] contains 2, but it does not contain the start of ]2,3] |
| 168 | 188 | // * ]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 @@ | ||
| 76 | 76 | |
| 77 | 77 | @AfterClass(groups = "short", alwaysRun=true) |
| 78 | 78 | public void teardown() { |
| 79 | - System.out.println("Tearing down"); | |
| 80 | 79 | if (cluster != null) |
| 81 | 80 | cluster.close(); |
| 82 | 81 | if (ccm != null) |
| @@ -360,5 +359,19 @@ | ||
| 360 | 359 | } |
| 361 | 360 | } |
| 362 | 361 | |
| 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 | + | |
| 363 | 376 | protected abstract Token.Factory tokenFactory(); |
| 364 | 377 | } |
modified driver-core/src/test/java/com/datastax/driver/core/TokenRangeTest.java +27 −12
| @@ -1,14 +1,11 @@ | ||
| 1 | 1 | package com.datastax.driver.core; |
| 2 | 2 | |
| 3 | -import java.math.BigInteger; | |
| 4 | 3 | import java.util.List; |
| 5 | 4 | |
| 6 | 5 | import org.testng.annotations.Test; |
| 7 | 6 | |
| 8 | 7 | import static org.testng.Assert.fail; |
| 9 | 8 | |
| 10 | -import com.datastax.driver.core.Token.M3PToken; | |
| 11 | - | |
| 12 | 9 | import static com.datastax.driver.core.Assertions.assertThat; |
| 13 | 10 | |
| 14 | 11 | public class TokenRangeTest { |
| @@ -76,6 +73,25 @@ | ||
| 76 | 73 | ; |
| 77 | 74 | } |
| 78 | 75 | |
| 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 | + | |
| 79 | 95 | @Test(groups = "unit") |
| 80 | 96 | public void should_merge_with_other_range() { |
| 81 | 97 | assertThat(tokenRange(3, 9).mergeWith(tokenRange(2, 3))).isEqualTo(tokenRange(2, 9)); |
| @@ -128,12 +144,12 @@ | ||
| 128 | 144 | |
| 129 | 145 | @Test(groups = "unit", expectedExceptions = IllegalArgumentException.class) |
| 130 | 146 | 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)); | |
| 132 | 148 | } |
| 133 | 149 | |
| 134 | 150 | @Test(groups = "unit") |
| 135 | 151 | 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)); | |
| 137 | 153 | } |
| 138 | 154 | |
| 139 | 155 | @Test(groups = "unit") |
| @@ -155,14 +171,13 @@ | ||
| 155 | 171 | assertThat(splits).containsExactly(tokenRange(3, 5), tokenRange(5, 7), tokenRange(7, 9)); |
| 156 | 172 | } |
| 157 | 173 | |
| 158 | - | |
| 159 | 174 | @Test(groups = "unit") |
| 160 | 175 | 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++) { | |
| 162 | 177 | try { |
| 163 | 178 | tokenRange(0, 1).splitEvenly(i); |
| 164 | 179 | fail("Expected error when providing " + i + " splits."); |
| 165 | - } catch(IllegalArgumentException e) { | |
| 180 | + } catch (IllegalArgumentException e) { | |
| 166 | 181 | // expected. |
| 167 | 182 | } |
| 168 | 183 | } |
| @@ -175,15 +190,15 @@ | ||
| 175 | 190 | |
| 176 | 191 | @Test(groups = "unit") |
| 177 | 192 | public void should_create_empty_token_ranges_if_too_many_splits() { |
| 178 | - TokenRange range = tokenRange(0,10); | |
| 193 | + TokenRange range = tokenRange(0, 10); | |
| 179 | 194 | |
| 180 | 195 | List<TokenRange> ranges = range.splitEvenly(255); |
| 181 | 196 | assertThat(ranges).hasSize(255); |
| 182 | 197 | |
| 183 | - for(int i = 0; i < ranges.size(); i++) { | |
| 198 | + for (int i = 0; i < ranges.size(); i++) { | |
| 184 | 199 | 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)); | |
| 187 | 202 | } else { |
| 188 | 203 | assertThat(tr.isEmpty()); |
| 189 | 204 | } |