Search in sources :

Example 1 with DefaultTokenMap

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);
}
Also used : DefaultTokenMap(com.datastax.oss.driver.internal.core.metadata.token.DefaultTokenMap) Token(com.datastax.oss.driver.api.core.metadata.token.Token) TokenFactory(com.datastax.oss.driver.internal.core.metadata.token.TokenFactory) CqlSession(com.datastax.oss.driver.api.core.CqlSession) BoundStatement(com.datastax.oss.driver.api.core.cql.BoundStatement)

Example 2 with DefaultTokenMap

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));
    }
}
Also used : ReplicationStrategyFactory(com.datastax.oss.driver.internal.core.metadata.token.ReplicationStrategyFactory) DefaultTokenMap(com.datastax.oss.driver.internal.core.metadata.token.DefaultTokenMap) Nullable(edu.umd.cs.findbugs.annotations.Nullable)

Example 3 with DefaultTokenMap

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());
    }
}
Also used : Node(com.datastax.oss.driver.api.core.metadata.Node) Sets(com.datastax.oss.driver.shaded.guava.common.collect.Sets) Logger(org.slf4j.Logger) LoggerFactory(org.slf4j.LoggerFactory) TokenFactory(com.datastax.oss.driver.internal.core.metadata.token.TokenFactory) Set(java.util.Set) ThreadSafe(net.jcip.annotations.ThreadSafe) InternalDriverContext(com.datastax.oss.driver.internal.core.context.InternalDriverContext) HashMap(java.util.HashMap) UUID(java.util.UUID) ImmutableList(com.datastax.oss.driver.shaded.guava.common.collect.ImmutableList) ImmutableMap(com.datastax.oss.driver.shaded.guava.common.collect.ImmutableMap) HashSet(java.util.HashSet) TokenFactoryRegistry(com.datastax.oss.driver.internal.core.metadata.token.TokenFactoryRegistry) Map(java.util.Map) VisibleForTesting(com.datastax.oss.driver.shaded.guava.common.annotations.VisibleForTesting) DefaultTokenMap(com.datastax.oss.driver.internal.core.metadata.token.DefaultTokenMap) DefaultTokenMap(com.datastax.oss.driver.internal.core.metadata.token.DefaultTokenMap) HashMap(java.util.HashMap) ImmutableList(com.datastax.oss.driver.shaded.guava.common.collect.ImmutableList) Node(com.datastax.oss.driver.api.core.metadata.Node) TokenFactoryRegistry(com.datastax.oss.driver.internal.core.metadata.token.TokenFactoryRegistry) ImmutableMap(com.datastax.oss.driver.shaded.guava.common.collect.ImmutableMap) TokenFactory(com.datastax.oss.driver.internal.core.metadata.token.TokenFactory) UUID(java.util.UUID) HashMap(java.util.HashMap) ImmutableMap(com.datastax.oss.driver.shaded.guava.common.collect.ImmutableMap) Map(java.util.Map) DefaultTokenMap(com.datastax.oss.driver.internal.core.metadata.token.DefaultTokenMap) HashSet(java.util.HashSet)

Aggregations

DefaultTokenMap (com.datastax.oss.driver.internal.core.metadata.token.DefaultTokenMap)3 TokenFactory (com.datastax.oss.driver.internal.core.metadata.token.TokenFactory)2 CqlSession (com.datastax.oss.driver.api.core.CqlSession)1 BoundStatement (com.datastax.oss.driver.api.core.cql.BoundStatement)1 Node (com.datastax.oss.driver.api.core.metadata.Node)1 Token (com.datastax.oss.driver.api.core.metadata.token.Token)1 InternalDriverContext (com.datastax.oss.driver.internal.core.context.InternalDriverContext)1 ReplicationStrategyFactory (com.datastax.oss.driver.internal.core.metadata.token.ReplicationStrategyFactory)1 TokenFactoryRegistry (com.datastax.oss.driver.internal.core.metadata.token.TokenFactoryRegistry)1 VisibleForTesting (com.datastax.oss.driver.shaded.guava.common.annotations.VisibleForTesting)1 ImmutableList (com.datastax.oss.driver.shaded.guava.common.collect.ImmutableList)1 ImmutableMap (com.datastax.oss.driver.shaded.guava.common.collect.ImmutableMap)1 Sets (com.datastax.oss.driver.shaded.guava.common.collect.Sets)1 Nullable (edu.umd.cs.findbugs.annotations.Nullable)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 Set (java.util.Set)1 UUID (java.util.UUID)1 ThreadSafe (net.jcip.annotations.ThreadSafe)1