Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@

import com.datastax.dse.driver.api.core.graph.FluentGraphStatement;
import com.datastax.oss.driver.api.core.ConsistencyLevel;
import com.datastax.oss.driver.api.core.RequestRoutingType;
import com.datastax.oss.driver.api.core.config.DriverExecutionProfile;
import com.datastax.oss.driver.api.core.cql.Statement;
import com.datastax.oss.driver.api.core.metadata.Node;
import edu.umd.cs.findbugs.annotations.NonNull;
import java.nio.ByteBuffer;
import java.time.Duration;
import java.util.Collections;
Expand Down Expand Up @@ -127,4 +129,10 @@ protected BytecodeGraphStatement newInstance(
readConsistencyLevel,
writeConsistencyLevel);
}

@NonNull
@Override
public RequestRoutingType getRequestRoutingType() {
return RequestRoutingType.REGULAR;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import com.datastax.dse.driver.api.core.graph.BatchGraphStatement;
import com.datastax.oss.driver.api.core.ConsistencyLevel;
import com.datastax.oss.driver.api.core.RequestRoutingType;
import com.datastax.oss.driver.api.core.config.DriverExecutionProfile;
import com.datastax.oss.driver.api.core.metadata.Node;
import com.datastax.oss.driver.shaded.guava.common.collect.ImmutableList;
Expand Down Expand Up @@ -151,4 +152,10 @@ protected BatchGraphStatement newInstance(
public Iterator<GraphTraversal> iterator() {
return this.traversals.iterator();
}

@NonNull
@Override
public RequestRoutingType getRequestRoutingType() {
return RequestRoutingType.REGULAR;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import com.datastax.dse.driver.api.core.graph.FluentGraphStatement;
import com.datastax.oss.driver.api.core.ConsistencyLevel;
import com.datastax.oss.driver.api.core.RequestRoutingType;
import com.datastax.oss.driver.api.core.config.DriverExecutionProfile;
import com.datastax.oss.driver.api.core.metadata.Node;
import edu.umd.cs.findbugs.annotations.NonNull;
Expand Down Expand Up @@ -103,4 +104,10 @@ protected FluentGraphStatement newInstance(
public GraphTraversal<?, ?> getTraversal() {
return traversal;
}

@NonNull
@Override
public RequestRoutingType getRequestRoutingType() {
return RequestRoutingType.REGULAR;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import com.datastax.dse.driver.api.core.graph.ScriptGraphStatement;
import com.datastax.oss.driver.api.core.ConsistencyLevel;
import com.datastax.oss.driver.api.core.RequestRoutingType;
import com.datastax.oss.driver.api.core.config.DriverExecutionProfile;
import com.datastax.oss.driver.api.core.metadata.Node;
import com.datastax.oss.protocol.internal.util.collection.NullAllowingImmutableMap;
Expand Down Expand Up @@ -204,4 +205,10 @@ protected ScriptGraphStatement newInstance(
public String toString() {
return String.format("ScriptGraphStatement['%s', params: %s]", this.script, this.queryParams);
}

@NonNull
@Override
public RequestRoutingType getRequestRoutingType() {
return RequestRoutingType.REGULAR;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.datastax.oss.driver.api.core;

/** The type of routing for a given request. */
public enum RequestRoutingType {
/** A regular (non-LWT) request. */
REGULAR,
/** A lightweight transaction (LWT) request. */
LWT
}
Original file line number Diff line number Diff line change
Expand Up @@ -718,7 +718,7 @@ public enum DefaultDriverOption implements DriverOption {

/**
* CQL 4.x has a known issue where prepared statement invalidation may be bypassed on the client
* side. Reference: https://github.com/scylladb/scylladb/issues/20860
* side. Reference: <a href="https://github.com/scylladb/scylladb/issues/20860">link</a>
*
* <p>When this occurs, the client's metadata can become outdated, leading to various
* deserialization errors.
Expand Down Expand Up @@ -1063,7 +1063,17 @@ public enum DefaultDriverOption implements DriverOption {
* <p>Value type: {@link java.util.List List}&#60;{@link String}&#62;
*/
LOAD_BALANCING_DC_FAILOVER_PREFERRED_REMOTE_DCS(
"advanced.load-balancing-policy.dc-failover.preferred-remote-dcs");
"advanced.load-balancing-policy.dc-failover.preferred-remote-dcs"),

/**
* The default routing method to use for LWT (Lightweight Transaction) requests. REGULAR uses the
* standard load balancing algorithm with slow replica avoidance and shuffling.
* PRESERVE_REPLICA_ORDER maintains the replica order from the partitioner.
*
* <p>Value-type: string
*/
LOAD_BALANCING_DEFAULT_LWT_REQUEST_ROUTING_METHOD(
"advanced.load-balancing-policy.default-lwt-request-routing-method");

private final String path;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,9 @@ protected static void fillWithDriverDefaults(OptionsMap map) {
map.put(TypedDriverOption.METRICS_GENERATE_AGGREGABLE_HISTOGRAMS, true);
map.put(
TypedDriverOption.LOAD_BALANCING_DC_FAILOVER_PREFERRED_REMOTE_DCS, ImmutableList.of(""));
map.put(
TypedDriverOption.LOAD_BALANCING_DEFAULT_LWT_REQUEST_ROUTING_METHOD,
"PRESERVE_REPLICA_ORDER");
}

@Immutable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -933,6 +933,12 @@ public String toString() {
DefaultDriverOption.LOAD_BALANCING_DC_FAILOVER_PREFERRED_REMOTE_DCS,
GenericType.listOf(String.class));

/** The request routing method to use in the request routing load balancing policy. */
public static final TypedDriverOption<String> LOAD_BALANCING_DEFAULT_LWT_REQUEST_ROUTING_METHOD =
new TypedDriverOption<>(
DefaultDriverOption.LOAD_BALANCING_DEFAULT_LWT_REQUEST_ROUTING_METHOD,
GenericType.STRING);

private static Iterable<TypedDriverOption<?>> introspectBuiltInValues() {
try {
ImmutableList.Builder<TypedDriverOption<?>> result = ImmutableList.builder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -280,13 +280,4 @@ default int computeSizeInBytes(@NonNull DriverContext context) {

return size;
}

/**
* Overrides LWT state to a specific value. If unset or set to {@code null} the {@link
* Statement#isLWT()} method will infer result from the statments in the batch.
*
* @param newIsLWT new Boolean to set
* @return new BatchStatement with updated isLWT field.
*/
BatchStatement setIsLWT(Boolean newIsLWT);
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ public class BatchStatementBuilder extends StatementBuilder<BatchStatementBuilde
@Nullable private CqlIdentifier keyspace;
@NonNull private ImmutableList.Builder<BatchableStatement<?>> statementsBuilder;
private int statementsCount;
@Nullable private Boolean isLWT = null;

public BatchStatementBuilder(@NonNull BatchType batchType) {
this.batchType = batchType;
Expand Down Expand Up @@ -76,19 +75,6 @@ public BatchStatementBuilder setKeyspace(@NonNull String keyspaceName) {
return setKeyspace(CqlIdentifier.fromCql(keyspaceName));
}

/**
* Forces driver to see this batch as LWT or non-LWT. Note that if never explicitly set or set to
* {@code null}, the resulting {@code DefaultBatchStatement} will decide its LWT state based on
* contained statements.
*
* @return this builder; never {@code null}.
*/
@NonNull
public BatchStatementBuilder setIsLWT(Boolean newIsLWT) {
this.isLWT = newIsLWT;
return this;
}

/**
* Adds a new statement to the batch.
*
Expand Down Expand Up @@ -172,7 +158,7 @@ public BatchStatement build() {
timeout,
node,
nowInSeconds,
isLWT);
requestRoutingType);
}

public int getStatementsCount() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.datastax.oss.driver.api.core.ConsistencyLevel;
import com.datastax.oss.driver.api.core.CqlIdentifier;
import com.datastax.oss.driver.api.core.ProtocolVersion;
import com.datastax.oss.driver.api.core.RequestRoutingType;
import com.datastax.oss.driver.api.core.config.DriverExecutionProfile;
import com.datastax.oss.driver.api.core.metadata.token.Token;
import com.datastax.oss.driver.api.core.type.DataType;
Expand Down Expand Up @@ -67,7 +68,8 @@ public BoundStatementBuilder(
@Nullable ConsistencyLevel serialConsistencyLevel,
@Nullable Duration timeout,
@NonNull CodecRegistry codecRegistry,
@NonNull ProtocolVersion protocolVersion) {
@NonNull ProtocolVersion protocolVersion,
@Nullable RequestRoutingType requestRoutingType) {
this.preparedStatement = preparedStatement;
this.variableDefinitions = variableDefinitions;
this.values = values;
Expand All @@ -89,6 +91,7 @@ public BoundStatementBuilder(
this.timeout = timeout;
this.codecRegistry = codecRegistry;
this.protocolVersion = protocolVersion;
this.requestRoutingType = requestRoutingType;
}

public BoundStatementBuilder(@NonNull BoundStatement template) {
Expand Down Expand Up @@ -204,6 +207,7 @@ public BoundStatement build() {
codecRegistry,
protocolVersion,
node,
nowInSeconds);
nowInSeconds,
requestRoutingType);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import com.datastax.oss.driver.api.core.CqlSession;
import com.datastax.oss.driver.api.core.DefaultProtocolVersion;
import com.datastax.oss.driver.api.core.RequestRoutingType;
import com.datastax.oss.driver.api.core.metadata.token.Partitioner;
import edu.umd.cs.findbugs.annotations.NonNull;
import edu.umd.cs.findbugs.annotations.Nullable;
Expand Down Expand Up @@ -133,6 +134,10 @@ public interface PreparedStatement {
*/
boolean isLWT();

/** Returns the request routing type for this prepared statement. */
@Nullable
RequestRoutingType getRequestRoutingType();

/**
* Updates {@link #getResultMetadataId()} and {@link #getResultSetDefinitions()} atomically.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.datastax.oss.driver.api.core.CqlIdentifier;
import com.datastax.oss.driver.api.core.CqlSession;
import com.datastax.oss.driver.api.core.DefaultProtocolVersion;
import com.datastax.oss.driver.api.core.RequestRoutingType;
import com.datastax.oss.driver.api.core.context.DriverContext;
import com.datastax.oss.driver.api.core.session.Request;
import com.datastax.oss.driver.internal.core.cql.DefaultSimpleStatement;
Expand Down Expand Up @@ -84,7 +85,8 @@ static SimpleStatement newInstance(@NonNull String cqlQuery) {
null,
null,
null,
Statement.NO_NOW_IN_SECONDS);
Statement.NO_NOW_IN_SECONDS,
RequestRoutingType.REGULAR);
}

/**
Expand Down Expand Up @@ -118,7 +120,8 @@ static SimpleStatement newInstance(
null,
null,
null,
Statement.NO_NOW_IN_SECONDS);
Statement.NO_NOW_IN_SECONDS,
RequestRoutingType.REGULAR);
}

/**
Expand Down Expand Up @@ -149,7 +152,8 @@ static SimpleStatement newInstance(
null,
null,
null,
Statement.NO_NOW_IN_SECONDS);
Statement.NO_NOW_IN_SECONDS,
RequestRoutingType.REGULAR);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ public SimpleStatement build() {
serialConsistencyLevel,
timeout,
node,
nowInSeconds);
nowInSeconds,
requestRoutingType);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.datastax.oss.driver.api.core.CqlIdentifier;
import com.datastax.oss.driver.api.core.CqlSession;
import com.datastax.oss.driver.api.core.NoNodeAvailableException;
import com.datastax.oss.driver.api.core.RequestRoutingType;
import com.datastax.oss.driver.api.core.config.DefaultDriverOption;
import com.datastax.oss.driver.api.core.config.DriverExecutionProfile;
import com.datastax.oss.driver.api.core.context.DriverContext;
Expand Down Expand Up @@ -525,6 +526,20 @@ default SelfT setNowInSeconds(int nowInSeconds) {
return (SelfT) this;
}

/**
* Sets the request routing type to use when applying the request (for testing purposes).
*
* <p>This method's default implementation returns the statement unchanged. The only reason it
* exists is to preserve binary compatibility. Internally, the driver overrides it to record the
* new value.
*/
@NonNull
@CheckReturnValue
@SuppressWarnings("unchecked")
default SelfT setRequestRoutingType(@Nullable RequestRoutingType requestRoutingType) {
return (SelfT) this;
}

/**
* Informs if this is a prepared LWT query.
*
Expand All @@ -540,7 +555,9 @@ default SelfT setNowInSeconds(int nowInSeconds) {
*
* @see <a href="https://docs.scylladb.com/using-scylla/lwt/">Docs about LWT</a>
*/
boolean isLWT();
default boolean isLWT() {
return getRequestRoutingType() == RequestRoutingType.LWT; // treating null as non-LWT
}

/**
* Calculates the approximate size in bytes that the statement will have when encoded.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import com.datastax.oss.driver.api.core.ConsistencyLevel;
import com.datastax.oss.driver.api.core.CqlIdentifier;
import com.datastax.oss.driver.api.core.RequestRoutingType;
import com.datastax.oss.driver.api.core.config.DriverExecutionProfile;
import com.datastax.oss.driver.api.core.metadata.Node;
import com.datastax.oss.driver.api.core.metadata.token.Token;
Expand Down Expand Up @@ -61,6 +62,7 @@ public abstract class StatementBuilder<
@Nullable protected Duration timeout;
@Nullable protected Node node;
protected int nowInSeconds = Statement.NO_NOW_IN_SECONDS;
@Nullable protected RequestRoutingType requestRoutingType;

protected StatementBuilder() {
// nothing to do
Expand All @@ -87,6 +89,7 @@ protected StatementBuilder(StatementT template) {
this.timeout = template.getTimeout();
this.node = template.getNode();
this.nowInSeconds = template.getNowInSeconds();
this.requestRoutingType = template.getRequestRoutingType();
}

/** @see Statement#setExecutionProfileName(String) */
Expand Down Expand Up @@ -282,6 +285,12 @@ public SelfT setNowInSeconds(int nowInSeconds) {
return self;
}

@NonNull
public SelfT setRequestRoutingType(@Nullable RequestRoutingType requestRoutingType) {
this.requestRoutingType = requestRoutingType;
return self;
}

@NonNull
protected Map<String, ByteBuffer> buildCustomPayload() {
return (customPayloadBuilder == null)
Expand Down
Loading
Loading