kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | 152eef368ed0b39bceaef3e54ac6e4cc006f43c995009f35f627ee4eea3a0d7a |
|---|---|
| Author | Olivier Michallat <olim7t@users.noreply.github.com> · 2016-10-19 10:51 -07:00 |
| Committer | GitHub <noreply@github.com> · 2016-10-19 10:51 -07:00 |
| Parents | 7e53feb3a639 |
3 files changed
modified changelog/README.md +1 −0
| @@ -3,6 +3,7 @@ | ||
| 3 | 3 | ### 3.0.5 (in progress) |
| 4 | 4 | |
| 5 | 5 | - [bug] JAVA-1312: QueryBuilder modifies selected columns when manually selected. |
| 6 | +- [improvement] JAVA-1303: Add missing BoundStatement.setRoutingKey(ByteBuffer...) | |
| 6 | 7 | |
| 7 | 8 | |
| 8 | 9 | ### 3.0.4 |
modified driver-core/src/main/java/com/datastax/driver/core/BoundStatement.java +18 −0
| @@ -270,6 +270,24 @@ | ||
| 270 | 270 | return this; |
| 271 | 271 | } |
| 272 | 272 | |
| 273 | + /** | |
| 274 | + * Sets the routing key for this bound statement, when the query partition key is composite and the routing key must | |
| 275 | + * be built from multiple values. | |
| 276 | + * <p/> | |
| 277 | + * This is useful when the routing key can neither be set on the {@code PreparedStatement} this bound statement | |
| 278 | + * was built from, nor automatically computed from bound variables. In particular, this is the case if the | |
| 279 | + * partition key is composite and only some of its components are bound. | |
| 280 | + * | |
| 281 | + * @param routingKeyComponents the raw (binary) values to compose to obtain | |
| 282 | + * the routing key. | |
| 283 | + * @return this {@code BoundStatement} object. | |
| 284 | + * @see BoundStatement#getRoutingKey | |
| 285 | + */ | |
| 286 | + public BoundStatement setRoutingKey(ByteBuffer... routingKeyComponents) { | |
| 287 | + this.routingKey = SimpleStatement.compose(routingKeyComponents); | |
| 288 | + return this; | |
| 289 | + } | |
| 290 | + | |
| 273 | 291 | /** |
| 274 | 292 | * {@inheritDoc} |
| 275 | 293 | */ |
modified driver-core/src/test/java/com/datastax/driver/core/policies/TokenAwarePolicyTest.java +73 −1
| @@ -27,7 +27,8 @@ | ||
| 27 | 27 | |
| 28 | 28 | import static com.datastax.driver.core.Assertions.assertThat; |
| 29 | 29 | import static com.datastax.driver.core.CreateCCM.TestMode.PER_METHOD; |
| 30 | -import static com.datastax.driver.core.TestUtils.*; | |
| 30 | +import static com.datastax.driver.core.TestUtils.CREATE_KEYSPACE_SIMPLE_FORMAT; | |
| 31 | +import static com.datastax.driver.core.TestUtils.nonQuietClusterCloseOptions; | |
| 31 | 32 | |
| 32 | 33 | @CreateCCM(PER_METHOD) |
| 33 | 34 | @CCMConfig(createCcm = false) |
| @@ -312,6 +313,77 @@ | ||
| 312 | 313 | } |
| 313 | 314 | } |
| 314 | 315 | |
| 316 | + /** | |
| 317 | + * Validates that when overriding a routing key on a {@link BoundStatement} | |
| 318 | + * using {@link BoundStatement#setRoutingKey(ByteBuffer...)} and | |
| 319 | + * {@link BoundStatement#setRoutingKey(ByteBuffer)} that this routing key is used to determine | |
| 320 | + * which hosts to route queries to. | |
| 321 | + * | |
| 322 | + * @test_category load_balancing:token_aware | |
| 323 | + */ | |
| 324 | + @Test(groups = "short") | |
| 325 | + public void should_use_provided_routing_key_boundstatement() { | |
| 326 | + // given: A 4 node cluster using TokenAwarePolicy with a replication factor of 1. | |
| 327 | + ScassandraCluster sCluster = ScassandraCluster.builder() | |
| 328 | + .withNodes(4) | |
| 329 | + .withSimpleKeyspace("keyspace", 1) | |
| 330 | + .build(); | |
| 331 | + Cluster cluster = Cluster.builder() | |
| 332 | + .addContactPoints(sCluster.address(2).getAddress()) | |
| 333 | + .withPort(sCluster.getBinaryPort()) | |
| 334 | + .withNettyOptions(nonQuietClusterCloseOptions) | |
| 335 | + // Don't shuffle replicas just to keep test deterministic. | |
| 336 | + .withLoadBalancingPolicy(new TokenAwarePolicy(new RoundRobinPolicy(), false)) | |
| 337 | + .build(); | |
| 338 | + | |
| 339 | + try { | |
| 340 | + sCluster.init(); | |
| 341 | + | |
| 342 | + Session session = cluster.connect("keyspace"); | |
| 343 | + | |
| 344 | + PreparedStatement preparedStatement = session.prepare("insert into tbl (k0, v) values (?, ?)"); | |
| 345 | + // bind text values since scassandra defaults to use varchar if not primed. | |
| 346 | + // this is inconsequential in this case since we are explicitly providing the routing key. | |
| 347 | + BoundStatement bs = preparedStatement.bind("a", "b"); | |
| 348 | + | |
| 349 | + // Derive a routing key for single routing key component, this should resolve to | |
| 350 | + // '4891967783720036163' | |
| 351 | + ByteBuffer routingKey = TypeCodec.bigint().serialize(33L, ProtocolVersion.NEWEST_SUPPORTED); | |
| 352 | + bs.setRoutingKey(routingKey); | |
| 353 | + | |
| 354 | + queryTracker.query(session, 10, bs); | |
| 355 | + | |
| 356 | + // Expect only node 3 to have been queried, give it has ownership of that partition | |
| 357 | + // (token range is (4611686018427387902, 6917529027641081853]) | |
| 358 | + queryTracker.assertQueried(sCluster, 1, 1, 0); | |
| 359 | + queryTracker.assertQueried(sCluster, 1, 2, 0); | |
| 360 | + queryTracker.assertQueried(sCluster, 1, 3, 0); | |
| 361 | + queryTracker.assertQueried(sCluster, 1, 4, 10); | |
| 362 | + | |
| 363 | + // reset counts. | |
| 364 | + queryTracker.reset(); | |
| 365 | + | |
| 366 | + // Derive a routing key for multiple routing key components, this should resolve to | |
| 367 | + // '3735658072872431718' | |
| 368 | + bs = preparedStatement.bind("a", "b"); | |
| 369 | + ByteBuffer routingKeyK0Part = TypeCodec.bigint().serialize(42L, ProtocolVersion.NEWEST_SUPPORTED); | |
| 370 | + ByteBuffer routingKeyK1Part = TypeCodec.varchar().serialize("hello_world", ProtocolVersion.NEWEST_SUPPORTED); | |
| 371 | + bs.setRoutingKey(routingKeyK0Part, routingKeyK1Part); | |
| 372 | + | |
| 373 | + queryTracker.query(session, 10, bs); | |
| 374 | + | |
| 375 | + // Expect only node 3 to have been queried, give it has ownership of that partition | |
| 376 | + // (token range is (2305843009213693951, 4611686018427387902]) | |
| 377 | + queryTracker.assertQueried(sCluster, 1, 1, 0); | |
| 378 | + queryTracker.assertQueried(sCluster, 1, 2, 0); | |
| 379 | + queryTracker.assertQueried(sCluster, 1, 3, 10); | |
| 380 | + queryTracker.assertQueried(sCluster, 1, 4, 0); | |
| 381 | + } finally { | |
| 382 | + cluster.close(); | |
| 383 | + sCluster.stop(); | |
| 384 | + } | |
| 385 | + } | |
| 386 | + | |
| 315 | 387 | /** |
| 316 | 388 | * Ensures that {@link TokenAwarePolicy} will properly handle a routing key for a {@link PreparedStatement} |
| 317 | 389 | * whose table uses multiple columns for its partition key. |