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