Skip to content

Commit fe1094a

Browse files
authored
JAVA-1917: Add ability to set node on statement (apache#1086)
1 parent fe1407d commit fe1094a

20 files changed

Lines changed: 440 additions & 67 deletions

File tree

changelog/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
### 4.0.0-beta2 (in progress)
66

7+
- [new feature] JAVA-1917: Add ability to set node on statement
78
- [improvement] JAVA-1916: Base TimestampCodec.parse on java.util.Date.
89
- [improvement] JAVA-1940: Clean up test resources when CCM integration tests finish
910
- [bug] JAVA-1938: Make CassandraSchemaQueries classes public

core/revapi.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,28 @@
499499
"oldArchive": "com.datastax.oss:java-driver-core:jar:4.0.0-beta1",
500500
"elementKind": "method",
501501
"justification": "Renamed context getters for uniformity"
502+
},
503+
{
504+
"code": "java.method.addedToInterface",
505+
"new": "method com.datastax.oss.driver.api.core.metadata.Node com.datastax.oss.driver.api.core.session.Request::getNode()",
506+
"package": "com.datastax.oss.driver.api.core.session",
507+
"classQualifiedName": "com.datastax.oss.driver.api.core.session.Request",
508+
"classSimpleName": "Request",
509+
"methodName": "getNode",
510+
"newArchive": "com.datastax.oss:java-driver-core:jar:4.0.0-beta2-SNAPSHOT",
511+
"elementKind": "method",
512+
"justification": "Add ability to query specific nodes for virtual tables"
513+
},
514+
{
515+
"code": "java.method.addedToInterface",
516+
"new": "method T com.datastax.oss.driver.api.core.cql.Statement<T extends com.datastax.oss.driver.api.core.cql.Statement<T extends com.datastax.oss.driver.api.core.cql.Statement<T>>>::setNode(com.datastax.oss.driver.api.core.metadata.Node)",
517+
"package": "com.datastax.oss.driver.api.core.cql",
518+
"classQualifiedName": "com.datastax.oss.driver.api.core.cql.Statement",
519+
"classSimpleName": "Statement",
520+
"methodName": "setNode",
521+
"newArchive": "com.datastax.oss:java-driver-core:jar:4.0.0-beta2-SNAPSHOT",
522+
"elementKind": "method",
523+
"justification": "Add ability to query specific nodes for virtual tables"
502524
}
503525
]
504526
}

core/src/main/java/com/datastax/oss/driver/api/core/CqlSession.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,13 @@ default CompletionStage<AsyncResultSet> executeAsync(@NonNull String query) {
112112
* <li>{@link Request#getRoutingKeyspace() boundStatement.getRoutingKeyspace()} is set from
113113
* either {@link Request#getKeyspace() simpleStatement.getKeyspace()} (if it's not {@code
114114
* null}), or {@code simpleStatement.getRoutingKeyspace()};
115-
* <li>on the other hand, {@link Statement#getTimestamp() boundStatement.getTimestamp()} is
116-
* <b>not</b> copied from the simple statement. It will be set to {@link Long#MIN_VALUE},
117-
* meaning that the value will be assigned by the session's timestamp generator.
115+
* <li>on the other hand, the following attributes are <b>not</b> propagated:
116+
* <ul>
117+
* <li>{@link Statement#getTimestamp() boundStatement.getTimestamp()} will be set to
118+
* {@link Long#MIN_VALUE}, meaning that the value will be assigned by the session's
119+
* timestamp generator.
120+
* <li>{@link Statement#getNode() boundStatement.getNode()} will always be {@code null}.
121+
* </ul>
118122
* </ul>
119123
*
120124
* If you want to customize this behavior, you can write your own implementation of {@link

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ static BatchStatement newInstance(@NonNull BatchType batchType) {
6060
Integer.MIN_VALUE,
6161
null,
6262
null,
63+
null,
6364
null);
6465
}
6566

@@ -87,6 +88,7 @@ static BatchStatement newInstance(
8788
Integer.MIN_VALUE,
8889
null,
8990
null,
91+
null,
9092
null);
9193
}
9294

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ public BatchStatement build() {
146146
pageSize,
147147
consistencyLevel,
148148
serialConsistencyLevel,
149-
timeout);
149+
timeout,
150+
node);
150151
}
151152
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ public BoundStatementBuilder(@NonNull BoundStatement template) {
9090
this.values = template.getValues().toArray(new ByteBuffer[this.variableDefinitions.size()]);
9191
this.codecRegistry = template.codecRegistry();
9292
this.protocolVersion = template.protocolVersion();
93+
this.node = template.getNode();
9394
}
9495

9596
@Override
@@ -167,6 +168,7 @@ public BoundStatement build() {
167168
serialConsistencyLevel,
168169
timeout,
169170
codecRegistry,
170-
protocolVersion);
171+
protocolVersion,
172+
node);
171173
}
172174
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ static SimpleStatement newInstance(@NonNull String cqlQuery) {
7777
Integer.MIN_VALUE,
7878
null,
7979
null,
80+
null,
8081
null);
8182
}
8283

@@ -107,6 +108,7 @@ static SimpleStatement newInstance(
107108
Integer.MIN_VALUE,
108109
null,
109110
null,
111+
null,
110112
null);
111113
}
112114

@@ -134,6 +136,7 @@ static SimpleStatement newInstance(
134136
Integer.MIN_VALUE,
135137
null,
136138
null,
139+
null,
137140
null);
138141
}
139142

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ public SimpleStatement build() {
176176
pageSize,
177177
consistencyLevel,
178178
serialConsistencyLevel,
179-
timeout);
179+
timeout,
180+
node);
180181
}
181182
}

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,13 @@
1818
import com.datastax.oss.driver.api.core.ConsistencyLevel;
1919
import com.datastax.oss.driver.api.core.CqlIdentifier;
2020
import com.datastax.oss.driver.api.core.CqlSession;
21+
import com.datastax.oss.driver.api.core.NoNodeAvailableException;
2122
import com.datastax.oss.driver.api.core.config.DefaultDriverOption;
2223
import com.datastax.oss.driver.api.core.config.DriverExecutionProfile;
2324
import com.datastax.oss.driver.api.core.context.DriverContext;
25+
import com.datastax.oss.driver.api.core.loadbalancing.LoadBalancingPolicy;
26+
import com.datastax.oss.driver.api.core.loadbalancing.NodeDistance;
27+
import com.datastax.oss.driver.api.core.metadata.Node;
2428
import com.datastax.oss.driver.api.core.metadata.token.Token;
2529
import com.datastax.oss.driver.api.core.session.Request;
2630
import com.datastax.oss.driver.api.core.session.Session;
@@ -93,6 +97,30 @@ public interface Statement<T extends Statement<T>> extends Request {
9397
@NonNull
9498
T setRoutingKeyspace(@Nullable CqlIdentifier newRoutingKeyspace);
9599

100+
/**
101+
* Sets the {@link Node} that should handle this query.
102+
*
103+
* <p>In the general case, use of this method is <em>heavily discouraged</em> and should only be
104+
* used in the following cases:
105+
*
106+
* <ol>
107+
* <li>Querying node-local tables, such as tables in the {@code system} and {@code system_views}
108+
* keyspaces.
109+
* <li>Applying a series of schema changes, where it may be advantageous to execute schema
110+
* changes in sequence on the same node.
111+
* </ol>
112+
*
113+
* <p>Configuring a specific node causes the configured {@link LoadBalancingPolicy} to be
114+
* completely bypassed. However, if the load balancing policy dictates that the node is at
115+
* distance {@link NodeDistance#IGNORED} or there is no active connectivity to the node, the
116+
* request will fail with a {@link NoNodeAvailableException}.
117+
*
118+
* @param node The node that should be used to handle executions of this statement or null to
119+
* delegate to the configured load balancing policy.
120+
*/
121+
@NonNull
122+
T setNode(@Nullable Node node);
123+
96124
/**
97125
* Shortcut for {@link #setRoutingKeyspace(CqlIdentifier)
98126
* setRoutingKeyspace(CqlIdentifier.fromCql(newRoutingKeyspaceName))}.

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import com.datastax.oss.driver.api.core.ConsistencyLevel;
1919
import com.datastax.oss.driver.api.core.CqlIdentifier;
2020
import com.datastax.oss.driver.api.core.config.DriverExecutionProfile;
21+
import com.datastax.oss.driver.api.core.metadata.Node;
2122
import com.datastax.oss.driver.api.core.metadata.token.Token;
2223
import com.datastax.oss.protocol.internal.util.collection.NullAllowingImmutableMap;
2324
import edu.umd.cs.findbugs.annotations.NonNull;
@@ -54,6 +55,7 @@ public abstract class StatementBuilder<T extends StatementBuilder<T, S>, S exten
5455
@Nullable protected ConsistencyLevel consistencyLevel;
5556
@Nullable protected ConsistencyLevel serialConsistencyLevel;
5657
@Nullable protected Duration timeout;
58+
@Nullable protected Node node;
5759

5860
protected StatementBuilder() {
5961
// nothing to do
@@ -78,6 +80,7 @@ protected StatementBuilder(S template) {
7880
this.consistencyLevel = template.getConsistencyLevel();
7981
this.serialConsistencyLevel = template.getSerialConsistencyLevel();
8082
this.timeout = template.getTimeout();
83+
this.node = template.getNode();
8184
}
8285

8386
/** @see Statement#setExecutionProfileName(String) */
@@ -199,6 +202,12 @@ public T withTimeout(@Nullable Duration timeout) {
199202
return self;
200203
}
201204

205+
/** @see Statement#setNode(Node) */
206+
public T withNode(@Nullable Node node) {
207+
this.node = node;
208+
return self;
209+
}
210+
202211
@NonNull
203212
protected Map<String, ByteBuffer> buildCustomPayload() {
204213
return (customPayloadBuilder == null)

0 commit comments

Comments
 (0)