Skip to content

Commit 6425a4f

Browse files
committed
temporary
1 parent 4c8eec7 commit 6425a4f

File tree

12 files changed

+576
-1
lines changed

12 files changed

+576
-1
lines changed

core/src/main/java/com/datastax/oss/driver/api/core/config/DefaultDriverOption.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -641,6 +641,13 @@ public enum DefaultDriverOption implements DriverOption {
641641
*/
642642
METADATA_TOKEN_MAP_ENABLED("advanced.metadata.token-map.enabled"),
643643

644+
/**
645+
* Whether tablet metadata is enabled.
646+
*
647+
* <p>Value-type: boolean
648+
*/
649+
METADATA_TABLET_MAP_ENABLED("advanced.metadata.tablet-map.enabled"),
650+
644651
/**
645652
* How long the driver waits for responses to control queries.
646653
*

core/src/main/java/com/datastax/oss/driver/api/core/config/OptionsMap.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,7 @@ protected static void fillWithDriverDefaults(OptionsMap map) {
354354
map.put(TypedDriverOption.METADATA_SCHEMA_WINDOW, Duration.ofSeconds(1));
355355
map.put(TypedDriverOption.METADATA_SCHEMA_MAX_EVENTS, 20);
356356
map.put(TypedDriverOption.METADATA_TOKEN_MAP_ENABLED, true);
357+
map.put(TypedDriverOption.METADATA_TABLET_MAP_ENABLED, false);
357358
map.put(TypedDriverOption.CONTROL_CONNECTION_TIMEOUT, initQueryTimeout);
358359
map.put(TypedDriverOption.CONTROL_CONNECTION_AGREEMENT_INTERVAL, Duration.ofMillis(200));
359360
map.put(TypedDriverOption.CONTROL_CONNECTION_AGREEMENT_TIMEOUT, Duration.ofSeconds(10));

core/src/main/java/com/datastax/oss/driver/api/core/config/TypedDriverOption.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,9 @@ public String toString() {
527527
/** Whether token metadata is enabled. */
528528
public static final TypedDriverOption<Boolean> METADATA_TOKEN_MAP_ENABLED =
529529
new TypedDriverOption<>(DefaultDriverOption.METADATA_TOKEN_MAP_ENABLED, GenericType.BOOLEAN);
530+
/** Whether tablet metadata is enabled. */
531+
public static final TypedDriverOption<Boolean> METADATA_TABLET_MAP_ENABLED =
532+
new TypedDriverOption<>(DefaultDriverOption.METADATA_TABLET_MAP_ENABLED, GenericType.BOOLEAN);
530533
/** How long the driver waits for responses to control queries. */
531534
public static final TypedDriverOption<Duration> CONTROL_CONNECTION_TIMEOUT =
532535
new TypedDriverOption<>(DefaultDriverOption.CONTROL_CONNECTION_TIMEOUT, GenericType.DURATION);

core/src/main/java/com/datastax/oss/driver/api/core/metadata/Metadata.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,17 @@ default Optional<KeyspaceMetadata> getKeyspace(@NonNull String keyspaceName) {
115115
@NonNull
116116
Optional<TokenMap> getTokenMap();
117117

118+
/**
119+
* The tablet map for this cluster.
120+
*
121+
* <p>Note that this property might be absent if tablet metadata was disabled, or if there was a
122+
* runtime error while computing the map (this would generate a warning log).
123+
*
124+
* @see DefaultDriverOption#NEW_OPTION
125+
*/
126+
@NonNull
127+
Optional<TabletMap> getTabletMap();
128+
118129
/**
119130
* The cluster name to which this session is connected. The Optional returned should contain the
120131
* value from the server for <b>system.local.cluster_name</b>.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.datastax.oss.driver.api.core.metadata;
2+
3+
import com.datastax.oss.driver.internal.core.metadata.DefaultTabletMap;
4+
5+
import java.util.Map;
6+
import java.util.NavigableSet;
7+
import java.util.Set;
8+
9+
public interface TabletMap {
10+
public Map<DefaultTabletMap.KeyspaceTableNamePair, NavigableSet<DefaultTabletMap.Tablet>> getRows();
11+
public Set<String> getKeyspaces();
12+
}

core/src/main/java/com/datastax/oss/driver/internal/core/control/ControlConnection.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,16 @@ private void onSuccessfulReconnect() {
499499
}
500500
}
501501
});
502+
context
503+
.getMetadataManager()
504+
.fullTabletMapRefresh()
505+
.whenComplete(
506+
(result, error) -> {
507+
if (error != null) {
508+
LOG.debug("[{}] Error while refreshing tablet map", logPrefix, error);
509+
}
510+
}
511+
);
502512
}
503513
}
504514

core/src/main/java/com/datastax/oss/driver/internal/core/metadata/DefaultMetadata.java

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@
1616
package com.datastax.oss.driver.internal.core.metadata;
1717

1818
import com.datastax.oss.driver.api.core.CqlIdentifier;
19+
import com.datastax.oss.driver.api.core.cql.Row;
1920
import com.datastax.oss.driver.api.core.metadata.Metadata;
2021
import com.datastax.oss.driver.api.core.metadata.Node;
22+
import com.datastax.oss.driver.api.core.metadata.TabletMap;
2123
import com.datastax.oss.driver.api.core.metadata.TokenMap;
2224
import com.datastax.oss.driver.api.core.metadata.schema.KeyspaceMetadata;
2325
import com.datastax.oss.driver.internal.core.context.InternalDriverContext;
@@ -30,6 +32,7 @@
3032
import edu.umd.cs.findbugs.annotations.NonNull;
3133
import edu.umd.cs.findbugs.annotations.Nullable;
3234
import java.util.Collections;
35+
import java.util.List;
3336
import java.util.Map;
3437
import java.util.Optional;
3538
import java.util.UUID;
@@ -46,21 +49,24 @@
4649
public class DefaultMetadata implements Metadata {
4750
private static final Logger LOG = LoggerFactory.getLogger(DefaultMetadata.class);
4851
public static DefaultMetadata EMPTY =
49-
new DefaultMetadata(Collections.emptyMap(), Collections.emptyMap(), null, null);
52+
new DefaultMetadata(Collections.emptyMap(), Collections.emptyMap(), null, null, null);
5053

5154
protected final Map<UUID, Node> nodes;
5255
protected final Map<CqlIdentifier, KeyspaceMetadata> keyspaces;
5356
protected final TokenMap tokenMap;
57+
protected final TabletMap tabletMap;
5458
protected final String clusterName;
5559

5660
protected DefaultMetadata(
5761
Map<UUID, Node> nodes,
5862
Map<CqlIdentifier, KeyspaceMetadata> keyspaces,
5963
TokenMap tokenMap,
64+
TabletMap tabletMap,
6065
String clusterName) {
6166
this.nodes = nodes;
6267
this.keyspaces = keyspaces;
6368
this.tokenMap = tokenMap;
69+
this.tabletMap = tabletMap;
6470
this.clusterName = clusterName;
6571
}
6672

@@ -82,6 +88,12 @@ public Optional<TokenMap> getTokenMap() {
8288
return Optional.ofNullable(tokenMap);
8389
}
8490

91+
@NonNull
92+
@Override
93+
public Optional<TabletMap> getTabletMap() {
94+
return Optional.ofNullable(tabletMap);
95+
}
96+
8597
@NonNull
8698
@Override
8799
public Optional<String> getClusterName() {
@@ -114,6 +126,7 @@ public DefaultMetadata withNodes(
114126
this.keyspaces,
115127
rebuildTokenMap(
116128
newNodes, keyspaces, tokenMapEnabled, forceFullRebuild, tokenFactory, context),
129+
tabletMap,
117130
context.getChannelFactory().getClusterName());
118131
}
119132

@@ -125,9 +138,23 @@ public DefaultMetadata withSchema(
125138
this.nodes,
126139
ImmutableMap.copyOf(newKeyspaces),
127140
rebuildTokenMap(nodes, newKeyspaces, tokenMapEnabled, false, null, context),
141+
tabletMap,
128142
context.getChannelFactory().getClusterName());
129143
}
130144

145+
public DefaultMetadata withTablets(
146+
Iterable<DefaultTabletMap.Tablet> tablets,
147+
boolean tabletMapEnabled,
148+
InternalDriverContext context) {
149+
return new DefaultMetadata(
150+
this.nodes,
151+
this.keyspaces,
152+
this.tokenMap,
153+
rebuildTabletMap(tablets, tabletMapEnabled, context),
154+
context.getChannelFactory().getClusterName());
155+
}
156+
157+
131158
@Nullable
132159
protected TokenMap rebuildTokenMap(
133160
Map<UUID, Node> newNodes,
@@ -188,4 +215,31 @@ protected TokenMap rebuildTokenMap(
188215
LOG.debug("[{}] Rebuilding token map took {}", logPrefix, NanoTime.formatTimeSince(start));
189216
}
190217
}
218+
219+
@Nullable
220+
protected TabletMap rebuildTabletMap(
221+
Iterable<DefaultTabletMap.Tablet> tablets,
222+
boolean tabletMapEnabled,
223+
InternalDriverContext context) {
224+
225+
String logPrefix = context.getSessionName();
226+
227+
if (!tabletMapEnabled) {
228+
LOG.debug("[{}] Tablet map is disabled, skipping", logPrefix);
229+
return this.tabletMap;
230+
}
231+
long start = System.nanoTime();
232+
try {
233+
return new DefaultTabletMap(tablets);
234+
} catch (Throwable t) {
235+
Loggers.warnWithException(
236+
LOG,
237+
"[{}] Unexpected error while refreshing tablet map, keeping previous version",
238+
logPrefix,
239+
t);
240+
return this.tabletMap;
241+
} finally {
242+
LOG.debug("[{}] Rebuilding tablet map took {}", logPrefix, NanoTime.formatTimeSince(start));
243+
}
244+
}
191245
}

0 commit comments

Comments
 (0)