-
Notifications
You must be signed in to change notification settings - Fork 40
3.x: Route SELECT at SERIAL/LOCAL_SERIAL consistency like LWT #891
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: scylla-3.x
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,6 +22,7 @@ | |
| import com.datastax.driver.core.LatencyTracker; | ||
| import com.datastax.driver.core.Metrics; | ||
| import com.datastax.driver.core.MetricsUtil; | ||
| import com.datastax.driver.core.QueryOptions; | ||
| import com.datastax.driver.core.Statement; | ||
| import com.datastax.driver.core.exceptions.BootstrappingException; | ||
| import com.datastax.driver.core.exceptions.DriverException; | ||
|
|
@@ -102,6 +103,7 @@ public class LatencyAwarePolicy implements ChainableLoadBalancingPolicy { | |
| private final long retryPeriod; | ||
| private final long minMeasure; | ||
| private volatile Metrics metrics; | ||
| private volatile QueryOptions queryOptions; | ||
|
|
||
| private LatencyAwarePolicy( | ||
| LoadBalancingPolicy childPolicy, | ||
|
|
@@ -218,6 +220,7 @@ public void init(Cluster cluster, Collection<Host> hosts) { | |
| } | ||
| cluster.register(latencyTracker); | ||
| metrics = cluster.getMetrics(); | ||
| queryOptions = cluster.getConfiguration().getQueryOptions(); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since you use it only for defaultConsistency level, probably it would be better to extract just that, instead of whole |
||
| if (metrics != null) { | ||
| metrics | ||
| .getRegistry() | ||
|
|
@@ -258,10 +261,13 @@ public HostDistance distance(Host host) { | |
| */ | ||
| @Override | ||
| public Iterator<Host> newQueryPlan(String loggedKeyspace, Statement statement) { | ||
| // For LWT queries, preserve the child policy's ordering. | ||
| // For LWT queries or serial consistency queries, preserve the child policy's ordering. | ||
| // LWT routing can rely on deterministic replica ordering (e.g. by TokenAwarePolicy), and | ||
| // latency-based reordering can undermine those assumptions. | ||
| if (statement != null && statement.isLWT()) { | ||
| if (statement != null | ||
| && (statement.isLWT() | ||
| || Statement.hasSerialConsistency( | ||
| statement, queryOptions != null ? queryOptions.getConsistencyLevel() : null))) { | ||
| return childPolicy.newQueryPlan(loggedKeyspace, statement); | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -26,6 +26,7 @@ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import com.datastax.driver.core.Cluster; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import com.datastax.driver.core.CodecRegistry; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import com.datastax.driver.core.ColumnDefinitions; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import com.datastax.driver.core.ConsistencyLevel; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import com.datastax.driver.core.Host; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import com.datastax.driver.core.HostDistance; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import com.datastax.driver.core.Metadata; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -266,6 +267,7 @@ private class PreserveReplicaOrderIterator extends AbstractIterator<Host> { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private final List<Host> replicas; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private final String keyspace; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private final Statement statement; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private final boolean localOnly; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private List<Host> nonLocalReplicas; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private Iterator<Host> nonLocalReplicasIterator; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private Set<Host> returnedHosts; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -276,6 +278,11 @@ public PreserveReplicaOrderIterator(String keyspace, Statement statement, List<H | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| this.statement = statement; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| this.replicas = replicas; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| this.replicasIterator = replicas.iterator(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ConsistencyLevel cl = statement.getConsistencyLevel(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (cl == null && queryOptions != null) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| cl = queryOptions.getConsistencyLevel(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| this.localOnly = cl == ConsistencyLevel.LOCAL_SERIAL; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @Override | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -307,7 +314,7 @@ protected Host computeNext() { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Second pass: return remote replicas that are UP and not IGNORED | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (nonLocalReplicas != null) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (nonLocalReplicas != null && !localOnly) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can't just ignore non-local replicas, you need to put them into the tail of the query plan. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (nonLocalReplicasIterator == null) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| nonLocalReplicasIterator = nonLocalReplicas.iterator(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -347,6 +354,7 @@ protected Host computeNext() { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private volatile Metadata clusterMetadata; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private volatile ProtocolVersion protocolVersion; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private volatile CodecRegistry codecRegistry; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private volatile QueryOptions queryOptions; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private volatile QueryOptions.RequestRoutingMethod defaultLwtRequestRoutingMethod; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -395,8 +403,8 @@ public void init(Cluster cluster, Collection<Host> hosts) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| clusterMetadata = cluster.getMetadata(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| protocolVersion = cluster.getConfiguration().getProtocolOptions().getProtocolVersion(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| codecRegistry = cluster.getConfiguration().getCodecRegistry(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| defaultLwtRequestRoutingMethod = | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| cluster.getConfiguration().getQueryOptions().getLoadBalancingLwtRequestRoutingMethod(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| queryOptions = cluster.getConfiguration().getQueryOptions(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| defaultLwtRequestRoutingMethod = queryOptions.getLoadBalancingLwtRequestRoutingMethod(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| childPolicy.init(cluster, hosts); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -468,10 +476,14 @@ private ColumnDefinitions getRoutingVariables(Statement statement) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private QueryOptions.RequestRoutingMethod getRequestRouting(Statement statement) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!statement.isLWT() || defaultLwtRequestRoutingMethod == null) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (defaultLwtRequestRoutingMethod == null) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return QueryOptions.RequestRoutingMethod.REGULAR; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return defaultLwtRequestRoutingMethod; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (statement.isLWT() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| || Statement.hasSerialConsistency(statement, queryOptions.getConsistencyLevel())) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return defaultLwtRequestRoutingMethod; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return QueryOptions.RequestRoutingMethod.REGULAR; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
472
to
484
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's don't optimize it for a wrong configuration:
Suggested change
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think I'm getting your point. The problem we are solving is that server is not able to set LWT flag on with just added extra treatment for in other words we return
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. True, my bad, it should have been:
Suggested change
And null |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private Iterator<Host> newQueryPlanRegular( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package com.datastax.driver.core.policies; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
|
|
||
| import com.datastax.driver.core.BoundStatement; | ||
| import com.datastax.driver.core.CCMConfig; | ||
| import com.datastax.driver.core.CCMTestsSupport; | ||
| import com.datastax.driver.core.Cluster; | ||
| import com.datastax.driver.core.ConsistencyLevel; | ||
| import com.datastax.driver.core.Host; | ||
| import com.datastax.driver.core.PreparedStatement; | ||
| import com.datastax.driver.core.ResultSet; | ||
| import com.datastax.driver.core.Session; | ||
| import com.datastax.driver.core.SimpleStatement; | ||
| import java.net.InetSocketAddress; | ||
| import java.util.HashSet; | ||
| import java.util.Set; | ||
| import org.testng.annotations.Test; | ||
|
|
||
| /** | ||
| * Integration tests verifying that statements with SERIAL/LOCAL_SERIAL consistency level are routed | ||
| * through the LWT load-balancing path (PRESERVE_REPLICA_ORDER). | ||
| */ | ||
| @CCMConfig(numberOfNodes = 3) | ||
| public class LWTLoadBalancingIT extends CCMTestsSupport { | ||
|
|
||
| @Override | ||
| public Cluster.Builder createClusterBuilder() { | ||
| return Cluster.builder() | ||
| .withLoadBalancingPolicy( | ||
| new TokenAwarePolicy(new RoundRobinPolicy(), TokenAwarePolicy.ReplicaOrdering.RANDOM)); | ||
| } | ||
|
|
||
| @Override | ||
| public void onTestContextInitialized() { | ||
| execute("CREATE TABLE IF NOT EXISTS test_lwt (pk int, ck int, v int, PRIMARY KEY (pk, ck))"); | ||
| for (int i = 0; i < 10; i++) { | ||
| execute(String.format("INSERT INTO test_lwt (pk, ck, v) VALUES (%d, %d, %d)", i, 0, i)); | ||
| } | ||
| } | ||
|
|
||
| @Test(groups = "short") | ||
| public void should_route_local_serial_select_through_lwt_path() { | ||
| Session session = session(); | ||
|
|
||
| SimpleStatement simpleSelect = | ||
| new SimpleStatement("SELECT * FROM test_lwt WHERE pk = ? AND ck = ?", 1, 0); | ||
| simpleSelect.setConsistencyLevel(ConsistencyLevel.LOCAL_SERIAL); | ||
|
|
||
| PreparedStatement preparedSelect = session.prepare(simpleSelect); | ||
| BoundStatement boundSelect = preparedSelect.bind(1, 0); | ||
|
|
||
| // Verify statement properties | ||
| assertThat(simpleSelect.isLWT()).isFalse(); | ||
| assertThat(simpleSelect.getConsistencyLevel()).isEqualTo(ConsistencyLevel.LOCAL_SERIAL); | ||
|
|
||
| // Execute multiple times and collect coordinators — with PRESERVE_REPLICA_ORDER routing, | ||
| // the same partition key should always be routed to the same first replica. | ||
| Set<InetSocketAddress> coordinators = new HashSet<>(); | ||
| for (int i = 0; i < 30; i++) { | ||
| ResultSet rs = session.execute(boundSelect); | ||
| Host coordinator = rs.getExecutionInfo().getQueriedHost(); | ||
| assertThat(coordinator).isNotNull(); | ||
| coordinators.add(coordinator.getEndPoint().resolve()); | ||
| } | ||
|
|
||
| // With PRESERVE_REPLICA_ORDER, the first replica is deterministic for a given partition key, | ||
| // so all 30 executions should hit the same coordinator. | ||
| assertThat(coordinators).hasSize(1); | ||
| } | ||
|
|
||
| @Test(groups = "short") | ||
| public void should_route_serial_select_through_lwt_path() { | ||
| Session session = session(); | ||
|
|
||
| SimpleStatement simpleSelect = | ||
| new SimpleStatement("SELECT * FROM test_lwt WHERE pk = ? AND ck = ?", 2, 0); | ||
| simpleSelect.setConsistencyLevel(ConsistencyLevel.SERIAL); | ||
|
|
||
| PreparedStatement preparedSelect = session.prepare(simpleSelect); | ||
| BoundStatement boundSelect = preparedSelect.bind(2, 0); | ||
|
|
||
| // Execute multiple times and collect coordinators | ||
| Set<InetSocketAddress> coordinators = new HashSet<>(); | ||
| for (int i = 0; i < 30; i++) { | ||
| ResultSet rs = session.execute(boundSelect); | ||
| Host coordinator = rs.getExecutionInfo().getQueriedHost(); | ||
| assertThat(coordinator).isNotNull(); | ||
| coordinators.add(coordinator.getEndPoint().resolve()); | ||
| } | ||
|
|
||
| // With PRESERVE_REPLICA_ORDER, the first replica is deterministic for a given partition key. | ||
| assertThat(coordinators).hasSize(1); | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is no good reason for this API, it is too specific