Skip to content

Commit 2bb29bc

Browse files
committed
Implement ProtocolFeatureStore
Implement class to store information about all features negotiated.
1 parent 6932f11 commit 2bb29bc

File tree

4 files changed

+52
-14
lines changed

4 files changed

+52
-14
lines changed

core/src/main/java/com/datastax/oss/driver/internal/core/channel/DriverChannel.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import com.datastax.oss.driver.internal.core.adminrequest.ThrottledAdminRequestHandler;
3333
import com.datastax.oss.driver.internal.core.pool.ChannelPool;
3434
import com.datastax.oss.driver.internal.core.protocol.LwtInfo;
35+
import com.datastax.oss.driver.internal.core.protocol.ProtocolFeatureStore;
3536
import com.datastax.oss.driver.internal.core.protocol.ShardingInfo;
3637
import com.datastax.oss.driver.internal.core.protocol.ShardingInfo.ConnectionShardingInfo;
3738
import com.datastax.oss.driver.internal.core.session.DefaultSession;
@@ -63,7 +64,6 @@ public class DriverChannel {
6364
AttributeKey.valueOf("options");
6465
static final AttributeKey<ConnectionShardingInfo> SHARDING_INFO_KEY =
6566
AttributeKey.valueOf("sharding_info");
66-
static final AttributeKey<LwtInfo> LWT_INFO_KEY = AttributeKey.valueOf("lwt_info");
6767

6868
@SuppressWarnings("RedundantStringConstructorCall")
6969
static final Object GRACEFUL_CLOSE_MESSAGE = new String("GRACEFUL_CLOSE_MESSAGE");
@@ -159,7 +159,7 @@ public ShardingInfo getShardingInfo() {
159159
}
160160

161161
public LwtInfo getLwtInfo() {
162-
return channel.attr(LWT_INFO_KEY).get();
162+
return ProtocolFeatureStore.loadFromChannel(channel).getLwtFeatureInfo();
163163
}
164164

165165
/**

core/src/main/java/com/datastax/oss/driver/internal/core/channel/ProtocolInitHandler.java

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@
2323
*/
2424
package com.datastax.oss.driver.internal.core.channel;
2525

26-
import static com.datastax.oss.driver.internal.core.channel.DriverChannel.LWT_INFO_KEY;
27-
2826
import com.datastax.oss.driver.api.core.DefaultProtocolVersion;
2927
import com.datastax.oss.driver.api.core.InvalidKeyspaceException;
3028
import com.datastax.oss.driver.api.core.ProtocolVersion;
@@ -40,7 +38,7 @@
4038
import com.datastax.oss.driver.internal.core.context.InternalDriverContext;
4139
import com.datastax.oss.driver.internal.core.protocol.BytesToSegmentDecoder;
4240
import com.datastax.oss.driver.internal.core.protocol.FrameToSegmentEncoder;
43-
import com.datastax.oss.driver.internal.core.protocol.LwtInfo;
41+
import com.datastax.oss.driver.internal.core.protocol.ProtocolFeatureStore;
4442
import com.datastax.oss.driver.internal.core.protocol.SegmentToBytesEncoder;
4543
import com.datastax.oss.driver.internal.core.protocol.SegmentToFrameDecoder;
4644
import com.datastax.oss.driver.internal.core.protocol.ShardingInfo;
@@ -96,7 +94,7 @@ class ProtocolInitHandler extends ConnectInitHandler {
9694
private String logPrefix;
9795
private ChannelHandlerContext ctx;
9896
private final boolean querySupportedOptions;
99-
private LwtInfo lwtInfo;
97+
private ProtocolFeatureStore featureStore;
10098
private TabletInfo tabletInfo;
10199

102100
/**
@@ -192,8 +190,8 @@ Message getRequest() {
192190
return request = Options.INSTANCE;
193191
case STARTUP:
194192
Map<String, String> startupOptions = new HashMap<>(context.getStartupOptions());
195-
if (lwtInfo != null) {
196-
lwtInfo.addOption(startupOptions);
193+
if (featureStore != null) {
194+
featureStore.populateStartupOptions(startupOptions);
197195
}
198196
if (tabletInfo != null && tabletInfo.isEnabled()) {
199197
TabletInfo.addOption(startupOptions);
@@ -229,15 +227,13 @@ void onResponse(Message response) {
229227
if (step == Step.OPTIONS && response instanceof Supported) {
230228
channel.attr(DriverChannel.OPTIONS_KEY).set(((Supported) response).options);
231229
Supported res = (Supported) response;
230+
featureStore = ProtocolFeatureStore.parseSupportedOptions(res.options);
232231
ConnectionShardingInfo shardingInfo = ShardingInfo.parseShardingInfo(res.options);
233232
if (shardingInfo != null) {
234233
channel.attr(DriverChannel.SHARDING_INFO_KEY).set(shardingInfo);
235234
}
236-
lwtInfo = LwtInfo.parseLwtInfo(res.options);
237-
if (lwtInfo != null) {
238-
channel.attr(LWT_INFO_KEY).set(lwtInfo);
239-
}
240235
tabletInfo = TabletInfo.parseTabletInfo(res.options);
236+
featureStore.storeInChannel(channel);
241237
step = Step.STARTUP;
242238
send();
243239
} else if (step == Step.STARTUP && response instanceof Ready) {

core/src/main/java/com/datastax/oss/driver/internal/core/protocol/LwtInfo.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public boolean isLwt(int flags) {
3737
return (flags & mask) == mask;
3838
}
3939

40-
public static LwtInfo parseLwtInfo(Map<String, List<String>> supported) {
40+
public static LwtInfo loadFromSupportedOptions(Map<String, List<String>> supported) {
4141
if (!supported.containsKey(SCYLLA_LWT_ADD_METADATA_MARK_KEY)) {
4242
return null;
4343
}
@@ -67,7 +67,7 @@ public static LwtInfo parseLwtInfo(Map<String, List<String>> supported) {
6767
return new LwtInfo((int) mask);
6868
}
6969

70-
public void addOption(Map<String, String> options) {
70+
public void populateStartupOptions(Map<String, String> options) {
7171
options.put(SCYLLA_LWT_ADD_METADATA_MARK_KEY, Integer.toString(mask));
7272
}
7373
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.datastax.oss.driver.internal.core.protocol;
2+
3+
import edu.umd.cs.findbugs.annotations.NonNull;
4+
import io.netty.channel.Channel;
5+
import io.netty.util.AttributeKey;
6+
import java.util.List;
7+
import java.util.Map;
8+
9+
public class ProtocolFeatureStore {
10+
private static final AttributeKey<ProtocolFeatureStore> CHANNEL_KEY =
11+
AttributeKey.valueOf("protocol_feature_store");
12+
13+
private final LwtInfo lwtInfo;
14+
15+
ProtocolFeatureStore(LwtInfo lwtInfo) {
16+
this.lwtInfo = lwtInfo;
17+
}
18+
19+
public LwtInfo getLwtFeatureInfo() {
20+
return lwtInfo;
21+
}
22+
23+
public static ProtocolFeatureStore parseSupportedOptions(
24+
@NonNull Map<String, List<String>> options) {
25+
LwtInfo lwtInfo = LwtInfo.loadFromSupportedOptions(options);
26+
return new ProtocolFeatureStore(lwtInfo);
27+
}
28+
29+
public void populateStartupOptions(@NonNull Map<String, String> options) {
30+
if (lwtInfo != null) {
31+
lwtInfo.populateStartupOptions(options);
32+
}
33+
}
34+
35+
public static ProtocolFeatureStore loadFromChannel(@NonNull Channel channel) {
36+
return channel.attr(ProtocolFeatureStore.CHANNEL_KEY).get();
37+
}
38+
39+
public void storeInChannel(@NonNull Channel channel) {
40+
channel.attr(ProtocolFeatureStore.CHANNEL_KEY).set(this);
41+
}
42+
}

0 commit comments

Comments
 (0)