Skip to content

Commit 30f3b4b

Browse files
committed
refactor: Streamline LWT handling in batch statements
1 parent f78df04 commit 30f3b4b

File tree

5 files changed

+39
-61
lines changed

5 files changed

+39
-61
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package com.datastax.oss.driver.api.core;
22

3+
/** The type of routing for a given request. */
34
public enum RequestRoutingType {
5+
/** A regular (non-LWT) request. */
46
REGULAR,
7+
/** A lightweight transaction (LWT) request. */
58
LWT
69
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ default int computeSizeInBytes(@NonNull DriverContext context) {
283283

284284
/**
285285
* Overrides LWT state to a specific value. If unset or set to {@code null} the {@link
286-
* Statement#isLWT()} method will infer result from the statments in the batch.
286+
* Statement#isLWT()} method will infer result from the statements in the batch.
287287
*
288288
* @param newIsLWT new Boolean to set
289289
* @return new BatchStatement with updated isLWT field.

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
package com.datastax.oss.driver.api.core.cql;
1919

2020
import com.datastax.oss.driver.api.core.CqlIdentifier;
21-
import com.datastax.oss.driver.api.core.RequestRoutingType;
2221
import com.datastax.oss.driver.internal.core.cql.DefaultBatchStatement;
2322
import com.datastax.oss.driver.shaded.guava.common.collect.ImmutableList;
2423
import com.datastax.oss.driver.shaded.guava.common.collect.Iterables;
@@ -153,8 +152,6 @@ public BatchStatementBuilder clearStatements() {
153152
@NonNull
154153
public BatchStatement build() {
155154
List<BatchableStatement<?>> statements = statementsBuilder.build();
156-
RequestRoutingType routingType =
157-
isLWT != null ? (isLWT ? RequestRoutingType.LWT : RequestRoutingType.REGULAR) : null;
158155
return new DefaultBatchStatement(
159156
batchType,
160157
statements,
@@ -175,7 +172,7 @@ public BatchStatement build() {
175172
timeout,
176173
node,
177174
nowInSeconds,
178-
routingType);
175+
isLWT);
179176
}
180177

181178
public int getStatementsCount() {

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

Lines changed: 32 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import java.util.Iterator;
4444
import java.util.List;
4545
import java.util.Map;
46+
import java.util.Objects;
4647
import net.jcip.annotations.Immutable;
4748
import org.slf4j.Logger;
4849
import org.slf4j.LoggerFactory;
@@ -70,7 +71,7 @@ public class DefaultBatchStatement implements BatchStatement {
7071
private final Duration timeout;
7172
private final Node node;
7273
private final int nowInSeconds;
73-
@NonNull private final RequestRoutingType routingType;
74+
private final Boolean isExplicitLWTSet;
7475

7576
public DefaultBatchStatement(
7677
BatchType batchType,
@@ -92,7 +93,7 @@ public DefaultBatchStatement(
9293
Duration timeout,
9394
Node node,
9495
int nowInSeconds,
95-
@NonNull RequestRoutingType routingType) {
96+
Boolean isExplicitLWTSet) {
9697
for (BatchableStatement<?> statement : statements) {
9798
if (statement != null
9899
&& (statement.getConsistencyLevel() != null
@@ -124,7 +125,7 @@ public DefaultBatchStatement(
124125
this.timeout = timeout;
125126
this.node = node;
126127
this.nowInSeconds = nowInSeconds;
127-
this.routingType = routingType;
128+
this.isExplicitLWTSet = isExplicitLWTSet;
128129
}
129130

130131
@NonNull
@@ -156,7 +157,7 @@ public BatchStatement setBatchType(@NonNull BatchType newBatchType) {
156157
timeout,
157158
node,
158159
nowInSeconds,
159-
routingType);
160+
isExplicitLWTSet);
160161
}
161162

162163
@NonNull
@@ -182,7 +183,7 @@ public BatchStatement setKeyspace(@Nullable CqlIdentifier newKeyspace) {
182183
timeout,
183184
node,
184185
nowInSeconds,
185-
routingType);
186+
isExplicitLWTSet);
186187
}
187188

188189
@NonNull
@@ -212,7 +213,7 @@ public BatchStatement add(@NonNull BatchableStatement<?> statement) {
212213
timeout,
213214
node,
214215
nowInSeconds,
215-
routingType);
216+
isExplicitLWTSet);
216217
}
217218
}
218219

@@ -246,7 +247,7 @@ public BatchStatement addAll(@NonNull Iterable<? extends BatchableStatement<?>>
246247
timeout,
247248
node,
248249
nowInSeconds,
249-
routingType);
250+
isExplicitLWTSet);
250251
}
251252
}
252253

@@ -278,7 +279,7 @@ public BatchStatement clear() {
278279
timeout,
279280
node,
280281
nowInSeconds,
281-
routingType);
282+
isExplicitLWTSet);
282283
}
283284

284285
@NonNull
@@ -315,7 +316,7 @@ public BatchStatement setPagingState(ByteBuffer newPagingState) {
315316
timeout,
316317
node,
317318
nowInSeconds,
318-
routingType);
319+
isExplicitLWTSet);
319320
}
320321

321322
@Override
@@ -346,7 +347,7 @@ public BatchStatement setPageSize(int newPageSize) {
346347
timeout,
347348
node,
348349
nowInSeconds,
349-
routingType);
350+
isExplicitLWTSet);
350351
}
351352

352353
@Nullable
@@ -378,7 +379,7 @@ public BatchStatement setConsistencyLevel(@Nullable ConsistencyLevel newConsiste
378379
timeout,
379380
node,
380381
nowInSeconds,
381-
routingType);
382+
isExplicitLWTSet);
382383
}
383384

384385
@Nullable
@@ -411,7 +412,7 @@ public BatchStatement setSerialConsistencyLevel(
411412
timeout,
412413
node,
413414
nowInSeconds,
414-
routingType);
415+
isExplicitLWTSet);
415416
}
416417

417418
@Override
@@ -442,7 +443,7 @@ public BatchStatement setExecutionProfileName(@Nullable String newConfigProfileN
442443
timeout,
443444
node,
444445
nowInSeconds,
445-
routingType);
446+
isExplicitLWTSet);
446447
}
447448

448449
@Override
@@ -473,7 +474,7 @@ public DefaultBatchStatement setExecutionProfile(@Nullable DriverExecutionProfil
473474
timeout,
474475
node,
475476
nowInSeconds,
476-
routingType);
477+
isExplicitLWTSet);
477478
}
478479

479480
@Override
@@ -539,7 +540,7 @@ public BatchStatement setRoutingKeyspace(CqlIdentifier newRoutingKeyspace) {
539540
timeout,
540541
node,
541542
nowInSeconds,
542-
routingType);
543+
isExplicitLWTSet);
543544
}
544545

545546
@NonNull
@@ -565,7 +566,7 @@ public BatchStatement setNode(@Nullable Node newNode) {
565566
timeout,
566567
newNode,
567568
nowInSeconds,
568-
routingType);
569+
isExplicitLWTSet);
569570
}
570571

571572
@Nullable
@@ -612,7 +613,7 @@ public BatchStatement setRoutingKey(ByteBuffer newRoutingKey) {
612613
timeout,
613614
node,
614615
nowInSeconds,
615-
routingType);
616+
isExplicitLWTSet);
616617
}
617618

618619
@Override
@@ -653,7 +654,7 @@ public BatchStatement setRoutingToken(Token newRoutingToken) {
653654
timeout,
654655
node,
655656
nowInSeconds,
656-
routingType);
657+
isExplicitLWTSet);
657658
}
658659

659660
@NonNull
@@ -685,7 +686,7 @@ public DefaultBatchStatement setCustomPayload(@NonNull Map<String, ByteBuffer> n
685686
timeout,
686687
node,
687688
nowInSeconds,
688-
routingType);
689+
isExplicitLWTSet);
689690
}
690691

691692
@Override
@@ -722,7 +723,7 @@ public DefaultBatchStatement setIdempotent(Boolean newIdempotence) {
722723
timeout,
723724
node,
724725
nowInSeconds,
725-
routingType);
726+
isExplicitLWTSet);
726727
}
727728

728729
@Override
@@ -753,7 +754,7 @@ public BatchStatement setTracing(boolean newTracing) {
753754
timeout,
754755
node,
755756
nowInSeconds,
756-
routingType);
757+
isExplicitLWTSet);
757758
}
758759

759760
@Override
@@ -784,7 +785,7 @@ public BatchStatement setQueryTimestamp(long newTimestamp) {
784785
timeout,
785786
node,
786787
nowInSeconds,
787-
routingType);
788+
isExplicitLWTSet);
788789
}
789790

790791
@NonNull
@@ -810,7 +811,7 @@ public BatchStatement setTimeout(@Nullable Duration newTimeout) {
810811
newTimeout,
811812
node,
812813
nowInSeconds,
813-
routingType);
814+
isExplicitLWTSet);
814815
}
815816

816817
@Override
@@ -841,18 +842,17 @@ public BatchStatement setNowInSeconds(int newNowInSeconds) {
841842
timeout,
842843
node,
843844
newNowInSeconds,
844-
routingType);
845+
isExplicitLWTSet);
845846
}
846847

847848
@NonNull
848-
@Override
849-
public RequestRoutingType getRequestRoutingType() {
850-
return routingType;
849+
public Boolean getIsExplicitLWTSet() {
850+
return isExplicitLWTSet;
851851
}
852852

853853
@NonNull
854854
@Override
855-
public BatchStatement setRequestRoutingType(RequestRoutingType requestRoutingType) {
855+
public BatchStatement setIsLWT(Boolean newIsLWT) {
856856
return new DefaultBatchStatement(
857857
batchType,
858858
statements,
@@ -873,40 +873,18 @@ public BatchStatement setRequestRoutingType(RequestRoutingType requestRoutingTyp
873873
timeout,
874874
node,
875875
nowInSeconds,
876-
requestRoutingType);
876+
newIsLWT);
877877
}
878878

879879
@NonNull
880880
@Override
881-
public BatchStatement setIsLWT(Boolean newIsLWT) {
882-
RequestRoutingType routingType =
883-
newIsLWT != null ? (newIsLWT ? RequestRoutingType.LWT : RequestRoutingType.REGULAR) : null;
884-
return new DefaultBatchStatement(
885-
batchType,
886-
statements,
887-
executionProfileName,
888-
executionProfile,
889-
keyspace,
890-
routingKeyspace,
891-
routingKey,
892-
routingToken,
893-
customPayload,
894-
idempotent,
895-
tracing,
896-
timestamp,
897-
pagingState,
898-
pageSize,
899-
consistencyLevel,
900-
serialConsistencyLevel,
901-
timeout,
902-
node,
903-
nowInSeconds,
904-
routingType);
881+
public RequestRoutingType getRequestRoutingType() {
882+
return isLWT() ? RequestRoutingType.LWT : RequestRoutingType.REGULAR;
905883
}
906884

907885
@Override
908886
public boolean isLWT() {
909-
if (routingType != null) return routingType == RequestRoutingType.LWT;
887+
if (Objects.nonNull(isExplicitLWTSet)) return isExplicitLWTSet;
910888
return statements.stream().anyMatch(Statement::isLWT);
911889
}
912890
}

core/src/main/java/com/datastax/oss/driver/internal/core/loadbalancing/DefaultLoadBalancingPolicy.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ public Queue<Node> newQueryPlan(@Nullable Request request, @Nullable Session ses
171171
switch (getDefaultLWTRequestRoutingMethod(request)) {
172172
case PRESERVE_REPLICA_ORDER:
173173
return newQueryPlanPreserveReplicas(request, session);
174+
case REGULAR:
174175
default:
175176
return newQueryPlanRegular(request, session);
176177
}
@@ -416,8 +417,7 @@ protected class NodeResponseRateSample {
416417
@VisibleForTesting protected final OptionalLong newest;
417418

418419
private NodeResponseRateSample() {
419-
long now = nanoTime();
420-
this.oldest = now;
420+
this.oldest = nanoTime();
421421
this.newest = OptionalLong.empty();
422422
}
423423

0 commit comments

Comments
 (0)