Skip to content

Commit 0f96183

Browse files
committed
Fix inferred request routing copy in statement builders
1 parent dfab4d0 commit 0f96183

6 files changed

Lines changed: 143 additions & 4 deletions

File tree

core/src/main/java/com/datastax/oss/driver/api/core/cql/StatementBuilder.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import com.datastax.oss.driver.api.core.config.DriverExecutionProfile;
2424
import com.datastax.oss.driver.api.core.metadata.Node;
2525
import com.datastax.oss.driver.api.core.metadata.token.Token;
26+
import com.datastax.oss.driver.internal.core.cql.RequestRoutingTypeAccessor;
2627
import com.datastax.oss.driver.internal.core.util.RoutingKey;
2728
import com.datastax.oss.protocol.internal.util.collection.NullAllowingImmutableMap;
2829
import edu.umd.cs.findbugs.annotations.NonNull;
@@ -89,7 +90,15 @@ protected StatementBuilder(StatementT template) {
8990
this.timeout = template.getTimeout();
9091
this.node = template.getNode();
9192
this.nowInSeconds = template.getNowInSeconds();
92-
this.requestRoutingType = template.getRequestRoutingType();
93+
this.requestRoutingType = getConfiguredRequestRoutingType(template);
94+
}
95+
96+
@Nullable
97+
private RequestRoutingType getConfiguredRequestRoutingType(StatementT template) {
98+
if (template instanceof RequestRoutingTypeAccessor) {
99+
return ((RequestRoutingTypeAccessor) template).getConfiguredRequestRoutingType();
100+
}
101+
return template.getRequestRoutingType();
93102
}
94103

95104
/** @see Statement#setExecutionProfileName(String) */

core/src/main/java/com/datastax/oss/driver/internal/core/cql/DefaultBatchStatement.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
import org.slf4j.LoggerFactory;
5050

5151
@Immutable
52-
public class DefaultBatchStatement implements BatchStatement {
52+
public class DefaultBatchStatement implements BatchStatement, RequestRoutingTypeAccessor {
5353
private static final Logger LOG = LoggerFactory.getLogger(DefaultBatchStatement.class);
5454

5555
private final BatchType batchType;
@@ -873,6 +873,12 @@ public RequestRoutingType getRequestRoutingType() {
873873
return cachedStatementsRequestRoutingType;
874874
}
875875

876+
@Nullable
877+
@Override
878+
public RequestRoutingType getConfiguredRequestRoutingType() {
879+
return requestRoutingType;
880+
}
881+
876882
@NonNull
877883
@Override
878884
public BatchStatement setRequestRoutingType(RequestRoutingType requestRoutingType) {

core/src/main/java/com/datastax/oss/driver/internal/core/cql/DefaultBoundStatement.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
import net.jcip.annotations.Immutable;
4848

4949
@Immutable
50-
public class DefaultBoundStatement implements BoundStatement {
50+
public class DefaultBoundStatement implements BoundStatement, RequestRoutingTypeAccessor {
5151

5252
private final PreparedStatement preparedStatement;
5353
private final ColumnDefinitions variableDefinitions;
@@ -816,6 +816,12 @@ public RequestRoutingType getRequestRoutingType() {
816816
return preparedStatement.getRequestRoutingType();
817817
}
818818

819+
@Nullable
820+
@Override
821+
public RequestRoutingType getConfiguredRequestRoutingType() {
822+
return requestRoutingType;
823+
}
824+
819825
@NonNull
820826
@Override
821827
public BoundStatement setRequestRoutingType(@Nullable RequestRoutingType requestRoutingType) {

core/src/main/java/com/datastax/oss/driver/internal/core/cql/DefaultSimpleStatement.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
import net.jcip.annotations.Immutable;
4343

4444
@Immutable
45-
public class DefaultSimpleStatement implements SimpleStatement {
45+
public class DefaultSimpleStatement implements SimpleStatement, RequestRoutingTypeAccessor {
4646

4747
private final String query;
4848
private final List<Object> positionalValues;
@@ -786,6 +786,12 @@ public RequestRoutingType getRequestRoutingType() {
786786
return null;
787787
}
788788

789+
@Nullable
790+
@Override
791+
public RequestRoutingType getConfiguredRequestRoutingType() {
792+
return requestRoutingType;
793+
}
794+
789795
@NonNull
790796
@Override
791797
public SimpleStatement setRequestRoutingType(@Nullable RequestRoutingType requestRoutingType) {
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
package com.datastax.oss.driver.internal.core.cql;
19+
20+
import com.datastax.oss.driver.api.core.RequestRoutingType;
21+
import edu.umd.cs.findbugs.annotations.Nullable;
22+
23+
/** Internal hook to distinguish explicitly configured routing type from inferred routing type. */
24+
public interface RequestRoutingTypeAccessor {
25+
@Nullable
26+
RequestRoutingType getConfiguredRequestRoutingType();
27+
}

core/src/test/java/com/datastax/oss/driver/api/core/cql/StatementBuilderTest.java

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,15 @@
2121
import static org.mockito.Mockito.mock;
2222
import static org.mockito.Mockito.when;
2323

24+
import com.datastax.oss.driver.api.core.DefaultConsistencyLevel;
25+
import com.datastax.oss.driver.api.core.DefaultProtocolVersion;
26+
import com.datastax.oss.driver.api.core.RequestRoutingType;
27+
import com.datastax.oss.driver.api.core.type.codec.registry.CodecRegistry;
28+
import com.datastax.oss.driver.internal.core.cql.DefaultBoundStatement;
2429
import com.datastax.oss.driver.shaded.guava.common.base.Charsets;
2530
import edu.umd.cs.findbugs.annotations.NonNull;
2631
import java.nio.ByteBuffer;
32+
import java.util.Collections;
2733
import org.junit.Test;
2834

2935
public class StatementBuilderTest {
@@ -103,4 +109,83 @@ public void should_match_set_routing_key_vararg() {
103109
builderStmt = builder.setRoutingKey(buff2, buff1).build();
104110
assertThat(expectedStmt.getRoutingKey()).isNotEqualTo(builderStmt.getRoutingKey());
105111
}
112+
113+
@Test
114+
public void should_not_copy_inferred_simple_routing_type_as_explicit() {
115+
SimpleStatement serialStatement =
116+
SimpleStatement.builder("select * from test.foo")
117+
.setConsistencyLevel(DefaultConsistencyLevel.LOCAL_SERIAL)
118+
.build();
119+
120+
assertThat(serialStatement.getRequestRoutingType()).isEqualTo(RequestRoutingType.LWT);
121+
122+
SimpleStatement regularStatement =
123+
SimpleStatement.builder(serialStatement)
124+
.setConsistencyLevel(DefaultConsistencyLevel.ONE)
125+
.build();
126+
127+
assertThat(regularStatement.getRequestRoutingType()).isNull();
128+
}
129+
130+
@Test
131+
public void should_not_copy_inferred_bound_routing_type_as_explicit() {
132+
BoundStatement serialStatement = newRegularBoundStatement(DefaultConsistencyLevel.LOCAL_SERIAL);
133+
134+
assertThat(serialStatement.getRequestRoutingType()).isEqualTo(RequestRoutingType.LWT);
135+
136+
BoundStatement regularStatement =
137+
new BoundStatementBuilder(serialStatement)
138+
.setConsistencyLevel(DefaultConsistencyLevel.ONE)
139+
.build();
140+
141+
assertThat(regularStatement.getRequestRoutingType()).isEqualTo(RequestRoutingType.REGULAR);
142+
}
143+
144+
@Test
145+
public void should_not_copy_inferred_batch_routing_type_as_explicit() {
146+
BatchStatement serialStatement =
147+
BatchStatement.builder(BatchType.LOGGED)
148+
.setConsistencyLevel(DefaultConsistencyLevel.LOCAL_SERIAL)
149+
.build();
150+
151+
assertThat(serialStatement.getRequestRoutingType()).isEqualTo(RequestRoutingType.LWT);
152+
153+
BatchStatement regularStatement =
154+
BatchStatement.builder(serialStatement)
155+
.setConsistencyLevel(DefaultConsistencyLevel.ONE)
156+
.build();
157+
158+
assertThat(regularStatement.getRequestRoutingType()).isEqualTo(RequestRoutingType.REGULAR);
159+
}
160+
161+
private BoundStatement newRegularBoundStatement(DefaultConsistencyLevel consistencyLevel) {
162+
PreparedStatement preparedStatement = mock(PreparedStatement.class);
163+
ColumnDefinitions variableDefinitions = mock(ColumnDefinitions.class);
164+
when(preparedStatement.isLWT()).thenReturn(false);
165+
when(preparedStatement.getRequestRoutingType()).thenReturn(RequestRoutingType.REGULAR);
166+
when(preparedStatement.getVariableDefinitions()).thenReturn(variableDefinitions);
167+
return new DefaultBoundStatement(
168+
preparedStatement,
169+
variableDefinitions,
170+
new ByteBuffer[0],
171+
null,
172+
null,
173+
null,
174+
null,
175+
null,
176+
Collections.emptyMap(),
177+
null,
178+
false,
179+
Statement.NO_DEFAULT_TIMESTAMP,
180+
null,
181+
Integer.MIN_VALUE,
182+
consistencyLevel,
183+
null,
184+
null,
185+
CodecRegistry.DEFAULT,
186+
DefaultProtocolVersion.DEFAULT,
187+
null,
188+
Statement.NO_NOW_IN_SECONDS,
189+
null);
190+
}
106191
}

0 commit comments

Comments
 (0)