Elffuss BraidHub

kikocis/java-driver public

JAVA-1303: Add missing BoundStatement.setRoutingKey(ByteBuffer...) (#761)

Changeset152eef368ed0b39bceaef3e54ac6e4cc006f43c995009f35f627ee4eea3a0d7a
AuthorOlivier Michallat <olim7t@users.noreply.github.com> · 2016-10-19 10:51 -07:00
CommitterGitHub <noreply@github.com> · 2016-10-19 10:51 -07:00
Parents7e53feb3a639

Browse the files at this changeset

3 files changed

modified changelog/README.md +1 −0
@@ -3,6 +3,7 @@
33 ### 3.0.5 (in progress)
44
55 - [bug] JAVA-1312: QueryBuilder modifies selected columns when manually selected.
6+- [improvement] JAVA-1303: Add missing BoundStatement.setRoutingKey(ByteBuffer...)
67
78
89 ### 3.0.4
modified driver-core/src/main/java/com/datastax/driver/core/BoundStatement.java +18 −0
@@ -270,6 +270,24 @@
270270 return this;
271271 }
272272
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+
273291 /**
274292 * {@inheritDoc}
275293 */
modified driver-core/src/test/java/com/datastax/driver/core/policies/TokenAwarePolicyTest.java +73 −1
@@ -27,7 +27,8 @@
2727
2828 import static com.datastax.driver.core.Assertions.assertThat;
2929 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;
3132
3233 @CreateCCM(PER_METHOD)
3334 @CCMConfig(createCcm = false)
@@ -312,6 +313,77 @@
312313 }
313314 }
314315
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+
315387 /**
316388 * Ensures that {@link TokenAwarePolicy} will properly handle a routing key for a {@link PreparedStatement}
317389 * whose table uses multiple columns for its partition key.