use of com.datastax.oss.driver.internal.core.metadata.token.DefaultTokenMap in project java-driver by datastax.
the class PreparedStatementIT method should_infer_routing_information_when_partition_key_is_bound.
private void should_infer_routing_information_when_partition_key_is_bound(String queryString) {
CqlSession session = sessionRule.session();
TokenFactory tokenFactory = ((DefaultTokenMap) session.getMetadata().getTokenMap().orElseThrow(AssertionError::new)).getTokenFactory();
// We'll bind a=1 in the query, check what token this is supposed to produce
Token expectedToken = session.execute("SELECT token(a) FROM prepared_statement_test WHERE a = 1").one().getToken(0);
BoundStatement boundStatement = session.prepare(queryString).bind().setInt("a", 1);
assertThat(boundStatement.getRoutingKeyspace()).isEqualTo(sessionRule.keyspace());
assertThat(tokenFactory.hash(boundStatement.getRoutingKey())).isEqualTo(expectedToken);
}
use of com.datastax.oss.driver.internal.core.metadata.token.DefaultTokenMap in project java-driver by datastax.
the class DefaultMetadata method rebuildTokenMap.
@Nullable
protected TokenMap rebuildTokenMap(Map<UUID, Node> newNodes, Map<CqlIdentifier, KeyspaceMetadata> newKeyspaces, boolean tokenMapEnabled, boolean forceFullRebuild, TokenFactory tokenFactory, InternalDriverContext context) {
String logPrefix = context.getSessionName();
ReplicationStrategyFactory replicationStrategyFactory = context.getReplicationStrategyFactory();
if (!tokenMapEnabled) {
LOG.debug("[{}] Token map is disabled, skipping", logPrefix);
return this.tokenMap;
}
long start = System.nanoTime();
try {
DefaultTokenMap oldTokenMap = (DefaultTokenMap) this.tokenMap;
if (oldTokenMap == null) {
// Initial build, we need the token factory
if (tokenFactory == null) {
LOG.debug("[{}] Building initial token map but the token factory is missing, skipping", logPrefix);
return null;
} else {
LOG.debug("[{}] Building initial token map", logPrefix);
return DefaultTokenMap.build(newNodes.values(), newKeyspaces.values(), tokenFactory, replicationStrategyFactory, logPrefix);
}
} else if (forceFullRebuild) {
LOG.debug("[{}] Updating token map but some nodes/tokens have changed, full rebuild", logPrefix);
return DefaultTokenMap.build(newNodes.values(), newKeyspaces.values(), oldTokenMap.getTokenFactory(), replicationStrategyFactory, logPrefix);
} else {
LOG.debug("[{}] Refreshing token map (only schema has changed)", logPrefix);
return oldTokenMap.refresh(newNodes.values(), newKeyspaces.values(), replicationStrategyFactory);
}
} catch (Throwable t) {
Loggers.warnWithException(LOG, "[{}] Unexpected error while refreshing token map, keeping previous version", logPrefix, t);
return this.tokenMap;
} finally {
LOG.debug("[{}] Rebuilding token map took {}", logPrefix, NanoTime.formatTimeSince(start));
}
}
use of com.datastax.oss.driver.internal.core.metadata.token.DefaultTokenMap in project java-driver by datastax.
the class FullNodeListRefresh method compute.
@Override
public Result compute(DefaultMetadata oldMetadata, boolean tokenMapEnabled, InternalDriverContext context) {
String logPrefix = context.getSessionName();
TokenFactoryRegistry tokenFactoryRegistry = context.getTokenFactoryRegistry();
Map<UUID, Node> oldNodes = oldMetadata.getNodes();
Map<UUID, Node> added = new HashMap<>();
Set<UUID> seen = new HashSet<>();
TokenFactory tokenFactory = oldMetadata.getTokenMap().map(m -> ((DefaultTokenMap) m).getTokenFactory()).orElse(null);
boolean tokensChanged = false;
for (NodeInfo nodeInfo : nodeInfos) {
UUID id = nodeInfo.getHostId();
if (seen.contains(id)) {
LOG.warn("[{}] Found duplicate entries with host_id {} in system.peers, " + "keeping only the first one", logPrefix, id);
} else {
seen.add(id);
DefaultNode node = (DefaultNode) oldNodes.get(id);
if (node == null) {
node = new DefaultNode(nodeInfo.getEndPoint(), context);
LOG.debug("[{}] Adding new node {}", logPrefix, node);
added.put(id, node);
}
if (tokenFactory == null && nodeInfo.getPartitioner() != null) {
tokenFactory = tokenFactoryRegistry.tokenFactoryFor(nodeInfo.getPartitioner());
}
tokensChanged |= copyInfos(nodeInfo, node, context);
}
}
Set<UUID> removed = Sets.difference(oldNodes.keySet(), seen);
if (added.isEmpty() && removed.isEmpty()) {
// The list didn't change
if (!oldMetadata.getTokenMap().isPresent() && tokenFactory != null) {
// token map rebuild:
return new Result(oldMetadata.withNodes(oldMetadata.getNodes(), tokenMapEnabled, true, tokenFactory, context));
} else {
// No need to create a new metadata instance
return new Result(oldMetadata);
}
} else {
ImmutableMap.Builder<UUID, Node> newNodesBuilder = ImmutableMap.builder();
ImmutableList.Builder<Object> eventsBuilder = ImmutableList.builder();
newNodesBuilder.putAll(added);
for (Map.Entry<UUID, Node> entry : oldNodes.entrySet()) {
if (!removed.contains(entry.getKey())) {
newNodesBuilder.put(entry.getKey(), entry.getValue());
}
}
for (Node node : added.values()) {
eventsBuilder.add(NodeStateEvent.added((DefaultNode) node));
}
for (UUID id : removed) {
Node node = oldNodes.get(id);
eventsBuilder.add(NodeStateEvent.removed((DefaultNode) node));
}
return new Result(oldMetadata.withNodes(newNodesBuilder.build(), tokenMapEnabled, tokensChanged, tokenFactory, context), eventsBuilder.build());
}
}
Aggregations