Skip to content

Commit 857526f

Browse files
committed
Fix prepared routing inference override
1 parent 0f96183 commit 857526f

4 files changed

Lines changed: 104 additions & 2 deletions

File tree

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -813,6 +813,9 @@ public RequestRoutingType getRequestRoutingType() {
813813
if (serialConsistencyLevel != null && serialConsistencyLevel.isSerial()) {
814814
return RequestRoutingType.LWT;
815815
}
816+
if (preparedStatement instanceof RequestRoutingTypeAccessor) {
817+
return ((RequestRoutingTypeAccessor) preparedStatement).getConfiguredRequestRoutingType();
818+
}
816819
return preparedStatement.getRequestRoutingType();
817820
}
818821

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
import org.slf4j.LoggerFactory;
5959

6060
@ThreadSafe
61-
public class DefaultPreparedStatement implements PreparedStatement {
61+
public class DefaultPreparedStatement implements PreparedStatement, RequestRoutingTypeAccessor {
6262
private static final Logger LOGGER = LoggerFactory.getLogger(DefaultPreparedStatement.class);
6363
private static final Splitter SPACE_SPLITTER = Splitter.onPattern("\\s+");
6464
private static final Splitter COMMA_SPLITTER = Splitter.onPattern(",");
@@ -211,6 +211,12 @@ public RequestRoutingType getRequestRoutingType() {
211211
return null;
212212
}
213213

214+
@Nullable
215+
@Override
216+
public RequestRoutingType getConfiguredRequestRoutingType() {
217+
return requestRoutingType;
218+
}
219+
214220
@Override
215221
public void setResultMetadata(
216222
@NonNull ByteBuffer newResultMetadataId, @NonNull ColumnDefinitions newResultSetDefinitions) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
import com.datastax.oss.driver.api.core.RequestRoutingType;
2121
import edu.umd.cs.findbugs.annotations.Nullable;
2222

23-
/** Internal hook to distinguish explicitly configured routing type from inferred routing type. */
23+
/** Internal hook to distinguish stored routing type from consistency-inferred routing type. */
2424
public interface RequestRoutingTypeAccessor {
2525
@Nullable
2626
RequestRoutingType getConfiguredRequestRoutingType();
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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 static org.assertj.core.api.Assertions.assertThat;
21+
22+
import com.datastax.oss.driver.api.core.ConsistencyLevel;
23+
import com.datastax.oss.driver.api.core.DefaultConsistencyLevel;
24+
import com.datastax.oss.driver.api.core.DefaultProtocolVersion;
25+
import com.datastax.oss.driver.api.core.RequestRoutingType;
26+
import com.datastax.oss.driver.api.core.cql.BoundStatement;
27+
import com.datastax.oss.driver.api.core.cql.ColumnDefinitions;
28+
import com.datastax.oss.driver.api.core.type.codec.registry.CodecRegistry;
29+
import com.datastax.oss.protocol.internal.util.Bytes;
30+
import java.util.Collections;
31+
import org.junit.Test;
32+
33+
public class DefaultPreparedStatementTest {
34+
35+
@Test
36+
public void should_not_keep_inferred_routing_type_after_bound_consistency_override() {
37+
DefaultPreparedStatement preparedStatement =
38+
newPreparedStatement(DefaultConsistencyLevel.LOCAL_SERIAL, null, null);
39+
40+
assertThat(preparedStatement.getRequestRoutingType()).isEqualTo(RequestRoutingType.LWT);
41+
42+
BoundStatement boundStatement =
43+
preparedStatement.bind().setConsistencyLevel(DefaultConsistencyLevel.ONE);
44+
45+
assertThat(boundStatement.getConsistencyLevel()).isEqualTo(DefaultConsistencyLevel.ONE);
46+
assertThat(boundStatement.getRequestRoutingType()).isNull();
47+
}
48+
49+
@Test
50+
public void should_keep_detected_lwt_routing_type_after_bound_consistency_override() {
51+
DefaultPreparedStatement preparedStatement =
52+
newPreparedStatement(DefaultConsistencyLevel.LOCAL_SERIAL, null, RequestRoutingType.LWT);
53+
54+
BoundStatement boundStatement =
55+
preparedStatement.bind().setConsistencyLevel(DefaultConsistencyLevel.ONE);
56+
57+
assertThat(boundStatement.getRequestRoutingType()).isEqualTo(RequestRoutingType.LWT);
58+
}
59+
60+
private DefaultPreparedStatement newPreparedStatement(
61+
ConsistencyLevel consistencyLevel,
62+
ConsistencyLevel serialConsistencyLevel,
63+
RequestRoutingType requestRoutingType) {
64+
ColumnDefinitions variableDefinitions =
65+
DefaultColumnDefinitions.valueOf(Collections.emptyList());
66+
return new DefaultPreparedStatement(
67+
Bytes.fromHexString("0x"),
68+
"SELECT * FROM test.foo WHERE pk = ?",
69+
variableDefinitions,
70+
Collections.emptyList(),
71+
null,
72+
null,
73+
null,
74+
null,
75+
Collections.emptyMap(),
76+
null,
77+
null,
78+
null,
79+
null,
80+
null,
81+
Collections.emptyMap(),
82+
null,
83+
null,
84+
null,
85+
Integer.MIN_VALUE,
86+
consistencyLevel,
87+
serialConsistencyLevel,
88+
false,
89+
CodecRegistry.DEFAULT,
90+
DefaultProtocolVersion.DEFAULT,
91+
requestRoutingType);
92+
}
93+
}

0 commit comments

Comments
 (0)