Search in sources :

Example 16 with Token

use of com.datastax.oss.driver.api.core.metadata.token.Token in project java-driver by datastax.

the class DefaultTokenMap method buildTokenToPrimaryAndRing.

private static TokenToPrimaryAndRing buildTokenToPrimaryAndRing(Collection<Node> nodes, TokenFactory tokenFactory) {
    ImmutableMap.Builder<Token, Node> tokenToPrimaryBuilder = ImmutableMap.builder();
    SortedSet<Token> sortedTokens = new TreeSet<>();
    for (Node node : nodes) {
        for (String tokenString : ((DefaultNode) node).getRawTokens()) {
            Token token = tokenFactory.parse(tokenString);
            sortedTokens.add(token);
            tokenToPrimaryBuilder.put(token, node);
        }
    }
    return new TokenToPrimaryAndRing(tokenToPrimaryBuilder.build(), ImmutableList.copyOf(sortedTokens));
}
Also used : TreeSet(java.util.TreeSet) Node(com.datastax.oss.driver.api.core.metadata.Node) DefaultNode(com.datastax.oss.driver.internal.core.metadata.DefaultNode) DefaultNode(com.datastax.oss.driver.internal.core.metadata.DefaultNode) Token(com.datastax.oss.driver.api.core.metadata.token.Token) ImmutableMap(com.datastax.oss.driver.shaded.guava.common.collect.ImmutableMap)

Example 17 with Token

use of com.datastax.oss.driver.api.core.metadata.token.Token in project java-driver by datastax.

the class DefaultTokenMap method build.

public static DefaultTokenMap build(@NonNull Collection<Node> nodes, @NonNull Collection<KeyspaceMetadata> keyspaces, @NonNull TokenFactory tokenFactory, @NonNull ReplicationStrategyFactory replicationStrategyFactory, @NonNull String logPrefix) {
    TokenToPrimaryAndRing tmp = buildTokenToPrimaryAndRing(nodes, tokenFactory);
    Map<Token, Node> tokenToPrimary = tmp.tokenToPrimary;
    List<Token> ring = tmp.ring;
    LOG.debug("[{}] Rebuilt ring ({} tokens)", logPrefix, ring.size());
    Set<TokenRange> tokenRanges = buildTokenRanges(ring, tokenFactory);
    ImmutableSetMultimap.Builder<Node, TokenRange> tokenRangesByPrimary = ImmutableSetMultimap.builder();
    for (TokenRange range : tokenRanges) {
        if (range.isFullRing()) {
            // The full ring is always ]min, min], so getEnd() doesn't match the node's token
            assert tokenToPrimary.size() == 1;
            tokenRangesByPrimary.put(tokenToPrimary.values().iterator().next(), range);
        } else {
            tokenRangesByPrimary.put(tokenToPrimary.get(range.getEnd()), range);
        }
    }
    Map<CqlIdentifier, Map<String, String>> replicationConfigs = buildReplicationConfigs(keyspaces, logPrefix);
    ImmutableMap.Builder<Map<String, String>, KeyspaceTokenMap> keyspaceMapsBuilder = ImmutableMap.builder();
    for (Map<String, String> config : ImmutableSet.copyOf(replicationConfigs.values())) {
        LOG.debug("[{}] Computing keyspace-level data for {}", logPrefix, config);
        keyspaceMapsBuilder.put(config, KeyspaceTokenMap.build(config, tokenToPrimary, ring, tokenRanges, tokenFactory, replicationStrategyFactory, logPrefix));
    }
    return new DefaultTokenMap(tokenFactory, tokenRanges, tokenRangesByPrimary.build(), replicationConfigs, keyspaceMapsBuilder.build(), logPrefix);
}
Also used : ImmutableSetMultimap(com.datastax.oss.driver.shaded.guava.common.collect.ImmutableSetMultimap) Node(com.datastax.oss.driver.api.core.metadata.Node) DefaultNode(com.datastax.oss.driver.internal.core.metadata.DefaultNode) Token(com.datastax.oss.driver.api.core.metadata.token.Token) CqlIdentifier(com.datastax.oss.driver.api.core.CqlIdentifier) ImmutableMap(com.datastax.oss.driver.shaded.guava.common.collect.ImmutableMap) TokenRange(com.datastax.oss.driver.api.core.metadata.token.TokenRange) TokenMap(com.datastax.oss.driver.api.core.metadata.TokenMap) ImmutableMap(com.datastax.oss.driver.shaded.guava.common.collect.ImmutableMap) Map(java.util.Map)

Example 18 with Token

use of com.datastax.oss.driver.api.core.metadata.token.Token in project java-driver by datastax.

the class DefaultTokenMap method buildTokenRanges.

private static Set<TokenRange> buildTokenRanges(List<Token> ring, TokenFactory factory) {
    ImmutableSet.Builder<TokenRange> builder = ImmutableSet.builder();
    // JAVA-684: if there is only one token, return the full ring (]minToken, minToken])
    if (ring.size() == 1) {
        builder.add(factory.range(factory.minToken(), factory.minToken()));
    } else {
        for (int i = 0; i < ring.size(); i++) {
            Token start = ring.get(i);
            Token end = ring.get((i + 1) % ring.size());
            builder.add(factory.range(start, end));
        }
    }
    return builder.build();
}
Also used : ImmutableSet(com.datastax.oss.driver.shaded.guava.common.collect.ImmutableSet) TokenRange(com.datastax.oss.driver.api.core.metadata.token.TokenRange) Token(com.datastax.oss.driver.api.core.metadata.token.Token)

Example 19 with Token

use of com.datastax.oss.driver.api.core.metadata.token.Token in project java-driver by datastax.

the class KeyspaceTokenMap method build.

static KeyspaceTokenMap build(Map<String, String> replicationConfig, Map<Token, Node> tokenToPrimary, List<Token> ring, Set<TokenRange> tokenRanges, TokenFactory tokenFactory, ReplicationStrategyFactory replicationStrategyFactory, String logPrefix) {
    long start = System.nanoTime();
    try {
        ReplicationStrategy strategy = replicationStrategyFactory.newInstance(replicationConfig);
        Map<Token, Set<Node>> replicasByToken = strategy.computeReplicasByToken(tokenToPrimary, ring);
        SetMultimap<Node, TokenRange> tokenRangesByNode;
        if (ring.size() == 1) {
            // We forced the single range to ]minToken,minToken], make sure to use that instead of
            // relying
            // on the node's token
            ImmutableSetMultimap.Builder<Node, TokenRange> builder = ImmutableSetMultimap.builder();
            for (Node node : tokenToPrimary.values()) {
                builder.putAll(node, tokenRanges);
            }
            tokenRangesByNode = builder.build();
        } else {
            tokenRangesByNode = buildTokenRangesByNode(tokenRanges, replicasByToken);
        }
        return new KeyspaceTokenMap(ring, tokenRangesByNode, replicasByToken, tokenFactory);
    } finally {
        LOG.debug("[{}] Computing keyspace-level data for {} took {}", logPrefix, replicationConfig, NanoTime.formatTimeSince(start));
    }
}
Also used : Set(java.util.Set) ImmutableSetMultimap(com.datastax.oss.driver.shaded.guava.common.collect.ImmutableSetMultimap) Node(com.datastax.oss.driver.api.core.metadata.Node) Token(com.datastax.oss.driver.api.core.metadata.token.Token) TokenRange(com.datastax.oss.driver.api.core.metadata.token.TokenRange)

Example 20 with Token

use of com.datastax.oss.driver.api.core.metadata.token.Token in project java-driver by datastax.

the class Murmur3TokenRange method split.

@Override
protected List<Token> split(Token startToken, Token endToken, int numberOfSplits) {
    // edge case: ]min, min] means the whole ring
    if (startToken.equals(endToken) && startToken.equals(Murmur3TokenFactory.MIN_TOKEN)) {
        endToken = Murmur3TokenFactory.MAX_TOKEN;
    }
    BigInteger start = BigInteger.valueOf(((Murmur3Token) startToken).getValue());
    BigInteger end = BigInteger.valueOf(((Murmur3Token) endToken).getValue());
    BigInteger range = end.subtract(start);
    if (range.compareTo(BigInteger.ZERO) < 0) {
        range = range.add(RING_LENGTH);
    }
    List<BigInteger> values = super.split(start, range, RING_END, RING_LENGTH, numberOfSplits);
    List<Token> tokens = Lists.newArrayListWithExpectedSize(values.size());
    for (BigInteger value : values) {
        tokens.add(new Murmur3Token(value.longValue()));
    }
    return tokens;
}
Also used : BigInteger(java.math.BigInteger) Token(com.datastax.oss.driver.api.core.metadata.token.Token)

Aggregations

Token (com.datastax.oss.driver.api.core.metadata.token.Token)22 Node (com.datastax.oss.driver.api.core.metadata.Node)6 Test (org.junit.Test)6 TokenRange (com.datastax.oss.driver.api.core.metadata.token.TokenRange)5 TokenMap (com.datastax.oss.driver.api.core.metadata.TokenMap)4 ImmutableMap (com.datastax.oss.driver.shaded.guava.common.collect.ImmutableMap)4 ByteBuffer (java.nio.ByteBuffer)4 CqlIdentifier (com.datastax.oss.driver.api.core.CqlIdentifier)3 PreparedStatement (com.datastax.oss.driver.api.core.cql.PreparedStatement)3 Row (com.datastax.oss.driver.api.core.cql.Row)3 NonNull (edu.umd.cs.findbugs.annotations.NonNull)3 BigInteger (java.math.BigInteger)3 Set (java.util.Set)3 CqlSession (com.datastax.oss.driver.api.core.CqlSession)2 BoundStatement (com.datastax.oss.driver.api.core.cql.BoundStatement)2 DefaultNode (com.datastax.oss.driver.internal.core.metadata.DefaultNode)2 ByteOrderedToken (com.datastax.oss.driver.internal.core.metadata.token.ByteOrderedToken)2 Murmur3Token (com.datastax.oss.driver.internal.core.metadata.token.Murmur3Token)2 RandomToken (com.datastax.oss.driver.internal.core.metadata.token.RandomToken)2 ImmutableSet (com.datastax.oss.driver.shaded.guava.common.collect.ImmutableSet)2