Skip to content

Commit bbd2d53

Browse files
committed
JAVA-1738: Make WriteType pluggable
1 parent 3366ea5 commit bbd2d53

18 files changed

Lines changed: 132 additions & 21 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-alpha3 (in progress)
66

7+
- [improvement] JAVA-1738: Convert enums to allow extensibility
78
- [bug] JAVA-1727: Override DefaultUdtValue.equals
89
- [bug] JAVA-1729: Override DefaultTupleValue.equals
910
- [improvement] JAVA-1720: Merge Cluster and Session into a single interface

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
*/
2525
public interface ConsistencyLevel {
2626

27-
/** The numerical value that the level is encoded to. */
27+
/** The numerical value that the level is encoded to in protocol frames. */
2828
int getProtocolCode();
2929

3030
/** The textual representation of the level in configuration files. */

core/src/main/java/com/datastax/oss/driver/api/core/retry/DefaultRetryPolicy.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@
2020
import com.datastax.oss.driver.api.core.connection.HeartbeatException;
2121
import com.datastax.oss.driver.api.core.context.DriverContext;
2222
import com.datastax.oss.driver.api.core.servererrors.CoordinatorException;
23+
import com.datastax.oss.driver.api.core.servererrors.CoreWriteType;
2324
import com.datastax.oss.driver.api.core.servererrors.ReadFailureException;
2425
import com.datastax.oss.driver.api.core.servererrors.WriteFailureException;
26+
import com.datastax.oss.driver.api.core.servererrors.WriteType;
2527
import com.datastax.oss.driver.api.core.session.Request;
2628

2729
/**
@@ -82,7 +84,7 @@ public RetryDecision onWriteTimeout(
8284
int received,
8385
int retryCount) {
8486

85-
return (retryCount == 0 && writeType == WriteType.BATCH_LOG)
87+
return (retryCount == 0 && writeType == CoreWriteType.BATCH_LOG)
8688
? RetryDecision.RETRY_SAME
8789
: RetryDecision.RETHROW;
8890
}

core/src/main/java/com/datastax/oss/driver/api/core/retry/RetryPolicy.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import com.datastax.oss.driver.api.core.servererrors.ServerError;
3131
import com.datastax.oss.driver.api.core.servererrors.TruncateException;
3232
import com.datastax.oss.driver.api.core.servererrors.WriteFailureException;
33+
import com.datastax.oss.driver.api.core.servererrors.WriteType;
3334
import com.datastax.oss.driver.api.core.session.Request;
3435

3536
/**

core/src/main/java/com/datastax/oss/driver/api/core/retry/WriteType.java renamed to core/src/main/java/com/datastax/oss/driver/api/core/servererrors/CoreWriteType.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,11 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package com.datastax.oss.driver.api.core.retry;
16+
package com.datastax.oss.driver.api.core.servererrors;
17+
18+
/** A default write type supported by the driver out of the box. */
19+
public enum CoreWriteType implements WriteType {
1720

18-
/**
19-
* The type of a Cassandra write query.
20-
*
21-
* <p>This information is returned by Cassandra when a write timeout is raised, to indicate what
22-
* type of write timed out. It is useful to decide which retry decision to adopt.
23-
*/
24-
public enum WriteType {
2521
/** A write to a single partition key. Such writes are guaranteed to be atomic and isolated. */
2622
SIMPLE,
2723
/**

core/src/main/java/com/datastax/oss/driver/api/core/servererrors/WriteFailureException.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import com.datastax.oss.driver.api.core.DriverException;
2121
import com.datastax.oss.driver.api.core.metadata.Node;
2222
import com.datastax.oss.driver.api.core.retry.RetryPolicy;
23-
import com.datastax.oss.driver.api.core.retry.WriteType;
2423
import com.datastax.oss.driver.api.core.session.Request;
2524
import java.net.InetAddress;
2625
import java.util.Map;

core/src/main/java/com/datastax/oss/driver/api/core/servererrors/WriteTimeoutException.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import com.datastax.oss.driver.api.core.DriverException;
2121
import com.datastax.oss.driver.api.core.metadata.Node;
2222
import com.datastax.oss.driver.api.core.retry.RetryPolicy;
23-
import com.datastax.oss.driver.api.core.retry.WriteType;
2423
import com.datastax.oss.driver.api.core.session.Request;
2524

2625
/**
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright DataStax, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.datastax.oss.driver.api.core.servererrors;
17+
18+
/**
19+
* The type of a Cassandra write query.
20+
*
21+
* <p>This information is returned by Cassandra when a write timeout is raised, to indicate what
22+
* type of write timed out. It is useful to decide which retry decision to adopt.
23+
*
24+
* <p>The only reason to model this as an interface (as opposed to an enum type) is to accommodate
25+
* for custom protocol extensions. If you're connecting to a standard Apache Cassandra cluster, all
26+
* {@code WriteType}s are {@link CoreWriteType} instances.
27+
*/
28+
public interface WriteType {
29+
30+
/** The textual representation that the write type is encoded to in protocol frames. */
31+
String name();
32+
}

core/src/main/java/com/datastax/oss/driver/internal/core/context/DefaultDriverContext.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@
5252
import com.datastax.oss.driver.internal.core.metadata.token.TokenFactoryRegistry;
5353
import com.datastax.oss.driver.internal.core.pool.ChannelPoolFactory;
5454
import com.datastax.oss.driver.internal.core.protocol.ByteBufPrimitiveCodec;
55+
import com.datastax.oss.driver.internal.core.servererrors.DefaultWriteTypeRegistry;
56+
import com.datastax.oss.driver.internal.core.servererrors.WriteTypeRegistry;
5557
import com.datastax.oss.driver.internal.core.session.PoolManager;
5658
import com.datastax.oss.driver.internal.core.session.RequestProcessorRegistry;
5759
import com.datastax.oss.driver.internal.core.ssl.JdkSslHandlerFactory;
@@ -119,6 +121,8 @@ public class DefaultDriverContext implements InternalDriverContext {
119121
private final LazyReference<ConsistencyLevelRegistry> consistencyLevelRegistryRef =
120122
new LazyReference<>(
121123
"consistencyLevelRegistry", this::buildConsistencyLevelRegistry, cycleDetector);
124+
private final LazyReference<WriteTypeRegistry> writeTypeRegistryRef =
125+
new LazyReference<>("writeTypeRegistry", this::buildWriteTypeRegistry, cycleDetector);
122126
private final LazyReference<NettyOptions> nettyOptionsRef =
123127
new LazyReference<>("nettyOptions", this::buildNettyOptions, cycleDetector);
124128
private final LazyReference<WriteCoalescer> writeCoalescerRef =
@@ -262,6 +266,10 @@ protected ConsistencyLevelRegistry buildConsistencyLevelRegistry() {
262266
return new DefaultConsistencyLevelRegistry();
263267
}
264268

269+
protected WriteTypeRegistry buildWriteTypeRegistry() {
270+
return new DefaultWriteTypeRegistry();
271+
}
272+
265273
protected NettyOptions buildNettyOptions() {
266274
return new DefaultNettyOptions(this);
267275
}
@@ -419,6 +427,11 @@ public ConsistencyLevelRegistry consistencyLevelRegistry() {
419427
return consistencyLevelRegistryRef.get();
420428
}
421429

430+
@Override
431+
public WriteTypeRegistry writeTypeRegistry() {
432+
return writeTypeRegistryRef.get();
433+
}
434+
422435
@Override
423436
public NettyOptions nettyOptions() {
424437
return nettyOptionsRef.get();

core/src/main/java/com/datastax/oss/driver/internal/core/context/InternalDriverContext.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import com.datastax.oss.driver.internal.core.metadata.token.ReplicationStrategyFactory;
3131
import com.datastax.oss.driver.internal.core.metadata.token.TokenFactoryRegistry;
3232
import com.datastax.oss.driver.internal.core.pool.ChannelPoolFactory;
33+
import com.datastax.oss.driver.internal.core.servererrors.WriteTypeRegistry;
3334
import com.datastax.oss.driver.internal.core.session.PoolManager;
3435
import com.datastax.oss.driver.internal.core.session.RequestProcessorRegistry;
3536
import com.datastax.oss.driver.internal.core.ssl.SslHandlerFactory;
@@ -51,6 +52,8 @@ public interface InternalDriverContext extends DriverContext {
5152

5253
ConsistencyLevelRegistry consistencyLevelRegistry();
5354

55+
WriteTypeRegistry writeTypeRegistry();
56+
5457
NettyOptions nettyOptions();
5558

5659
WriteCoalescer writeCoalescer();

0 commit comments

Comments
 (0)