Search in sources :

Example 16 with TokenRange

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

the class AddedNodeIT method should_signal_and_create_pool_when_node_gets_added.

@Test
public void should_signal_and_create_pool_when_node_gets_added() {
    AddListener addListener = new AddListener();
    try (CqlSession session = SessionUtils.newSession(CCM_RULE, null, addListener, null, null)) {
        assertThat(session.getMetadata().getTokenMap()).isPresent();
        Set<TokenRange> tokenRanges = session.getMetadata().getTokenMap().get().getTokenRanges();
        assertThat(tokenRanges).hasSize(3);
        CCM_RULE.getCcmBridge().add(4, "dc1");
        await().pollInterval(500, TimeUnit.MILLISECONDS).atMost(60, TimeUnit.SECONDS).until(() -> addListener.addedNode != null);
        Map<Node, ChannelPool> pools = ((DefaultSession) session).getPools();
        await().pollInterval(500, TimeUnit.MILLISECONDS).atMost(60, TimeUnit.SECONDS).until(() -> pools.containsKey(addListener.addedNode));
        await().pollInterval(500, TimeUnit.MILLISECONDS).atMost(60, TimeUnit.SECONDS).until(() -> session.getMetadata().getTokenMap().get().getTokenRanges().size() == 4);
    }
}
Also used : ChannelPool(com.datastax.oss.driver.internal.core.pool.ChannelPool) Node(com.datastax.oss.driver.api.core.metadata.Node) TokenRange(com.datastax.oss.driver.api.core.metadata.token.TokenRange) CqlSession(com.datastax.oss.driver.api.core.CqlSession) DefaultSession(com.datastax.oss.driver.internal.core.session.DefaultSession) Test(org.junit.Test)

Example 17 with TokenRange

use of com.datastax.oss.driver.api.core.metadata.token.TokenRange 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 TokenRange

use of com.datastax.oss.driver.api.core.metadata.token.TokenRange 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 TokenRange

use of com.datastax.oss.driver.api.core.metadata.token.TokenRange 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 TokenRange

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

the class TokenRangeBase method splitEvenly.

@NonNull
@Override
public List<TokenRange> splitEvenly(int numberOfSplits) {
    if (numberOfSplits < 1)
        throw new IllegalArgumentException(String.format("numberOfSplits (%d) must be greater than 0.", numberOfSplits));
    if (isEmpty()) {
        throw new IllegalArgumentException("Can't split empty range " + this);
    }
    List<TokenRange> tokenRanges = new ArrayList<>();
    List<Token> splitPoints = split(start, end, numberOfSplits);
    Token splitStart = start;
    for (Token splitEnd : splitPoints) {
        tokenRanges.add(newTokenRange(splitStart, splitEnd));
        splitStart = splitEnd;
    }
    tokenRanges.add(newTokenRange(splitStart, end));
    return tokenRanges;
}
Also used : ArrayList(java.util.ArrayList) TokenRange(com.datastax.oss.driver.api.core.metadata.token.TokenRange) Token(com.datastax.oss.driver.api.core.metadata.token.Token) NonNull(edu.umd.cs.findbugs.annotations.NonNull)

Aggregations

TokenRange (com.datastax.oss.driver.api.core.metadata.token.TokenRange)23 Node (com.datastax.oss.driver.api.core.metadata.Node)11 TokenMap (com.datastax.oss.driver.api.core.metadata.TokenMap)10 Token (com.datastax.oss.driver.api.core.metadata.token.Token)10 Test (org.junit.Test)6 ByteBuffer (java.nio.ByteBuffer)5 ArrayList (java.util.ArrayList)5 CqlSession (com.datastax.oss.driver.api.core.CqlSession)4 SimpleStatement (com.datastax.oss.driver.api.core.cql.SimpleStatement)3 EndPoint (com.datastax.oss.driver.api.core.metadata.EndPoint)3 ImmutableSet (com.datastax.oss.driver.shaded.guava.common.collect.ImmutableSet)3 NonNull (edu.umd.cs.findbugs.annotations.NonNull)3 Map (java.util.Map)3 ConsistencyLevel (com.datastax.oss.driver.api.core.ConsistencyLevel)2 V4 (com.datastax.oss.driver.api.core.DefaultProtocolVersion.V4)2 Version (com.datastax.oss.driver.api.core.Version)2 Row (com.datastax.oss.driver.api.core.cql.Row)2 Metadata (com.datastax.oss.driver.api.core.metadata.Metadata)2 TypeCodecs (com.datastax.oss.driver.api.core.type.codec.TypeCodecs)2 ChannelPool (com.datastax.oss.driver.internal.core.pool.ChannelPool)2