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);
}
}
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);
}
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();
}
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));
}
}
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;
}
Aggregations