use of com.palantir.atlasdb.keyvalue.cassandra.LightweightOppToken in project atlasdb by palantir.
the class CassandraService method refreshTokenRanges.
public Set<InetSocketAddress> refreshTokenRanges() {
Set<InetSocketAddress> servers = Sets.newHashSet();
try {
ImmutableRangeMap.Builder<LightweightOppToken, List<InetSocketAddress>> newTokenRing = ImmutableRangeMap.builder();
// grab latest token ring view from a random node in the cluster
List<TokenRange> tokenRanges = getTokenRanges();
// RangeMap needs a little help with weird 1-node, 1-vnode, this-entire-feature-is-useless case
if (tokenRanges.size() == 1) {
String onlyEndpoint = Iterables.getOnlyElement(Iterables.getOnlyElement(tokenRanges).getEndpoints());
InetSocketAddress onlyHost = getAddressForHost(onlyEndpoint);
newTokenRing.put(Range.all(), ImmutableList.of(onlyHost));
} else {
// normal case, large cluster with many vnodes
for (TokenRange tokenRange : tokenRanges) {
List<InetSocketAddress> hosts = tokenRange.getEndpoints().stream().map(this::getAddressForHostThrowUnchecked).collect(Collectors.toList());
servers.addAll(hosts);
LightweightOppToken startToken = new LightweightOppToken(BaseEncoding.base16().decode(tokenRange.getStart_token().toUpperCase()));
LightweightOppToken endToken = new LightweightOppToken(BaseEncoding.base16().decode(tokenRange.getEnd_token().toUpperCase()));
if (startToken.compareTo(endToken) <= 0) {
newTokenRing.put(Range.openClosed(startToken, endToken), hosts);
} else {
// Handle wrap-around
newTokenRing.put(Range.greaterThan(startToken), hosts);
newTokenRing.put(Range.atMost(endToken), hosts);
}
}
}
tokenMap = newTokenRing.build();
tokenRangeWritesLogger.updateTokenRanges(tokenMap.asMapOfRanges().keySet());
return servers;
} catch (Exception e) {
log.error("Couldn't grab new token ranges for token aware cassandra mapping!", e);
// return the set of servers we knew about last time we successfully constructed the tokenMap
return tokenMap.asMapOfRanges().values().stream().flatMap(Collection::stream).collect(Collectors.toSet());
}
}
use of com.palantir.atlasdb.keyvalue.cassandra.LightweightOppToken in project atlasdb by palantir.
the class ClusterMetadataUtils method getMinimalSetOfRangesForTokens.
@VisibleForTesting
static // Needs to be Set<Range<>> as we don't want to merge ranges yet
Set<Range<LightweightOppToken>> getMinimalSetOfRangesForTokens(Set<LightweightOppToken> partitionKeyTokens, SortedMap<LightweightOppToken, Range<LightweightOppToken>> tokenRangesByStart) {
Map<LightweightOppToken, Range<LightweightOppToken>> tokenRangesByStartToken = new HashMap<>();
for (LightweightOppToken token : partitionKeyTokens) {
Range<LightweightOppToken> minimalTokenRange = findTokenRange(token, tokenRangesByStart);
tokenRangesByStartToken.merge(LightweightOppToken.getLowerExclusive(minimalTokenRange), minimalTokenRange, ClusterMetadataUtils::findLatestEndingRange);
if (!minimalTokenRange.hasLowerBound()) {
// handle wraparound
Optional<Range<LightweightOppToken>> wraparoundRange = tokenRangesByStart.values().stream().filter(Predicate.not(Range::hasUpperBound)).findFirst();
wraparoundRange.ifPresent(range -> tokenRangesByStartToken.merge(LightweightOppToken.getLowerExclusive(range), range, ClusterMetadataUtils::findLatestEndingRange));
}
}
return ImmutableSet.copyOf(tokenRangesByStartToken.values());
}
use of com.palantir.atlasdb.keyvalue.cassandra.LightweightOppToken in project atlasdb by palantir.
the class CassandraRepairEteTest method testTokenRangeCoversFullRing.
@Test
public void testTokenRangeCoversFullRing() {
CqlMetadata cqlMetadata = new CqlMetadata(cluster.getMetadata());
Set<Range<LightweightOppToken>> tokenRanges = cqlMetadata.getTokenRanges();
// Turning this into a RangeSet should give the complete range (-inf, +inf)
RangeSet<LightweightOppToken> fullTokenRing = tokenRanges.stream().collect(toImmutableRangeSet());
assertThat(fullTokenRing.asRanges()).containsExactly(Range.all());
}
use of com.palantir.atlasdb.keyvalue.cassandra.LightweightOppToken in project atlasdb by palantir.
the class CassandraRepairEteTest method tokenRangesToRepairShouldBeSubsetsOfTokenMap.
@Test
public void tokenRangesToRepairShouldBeSubsetsOfTokenMap() {
Map<CassandraServer, Set<Range<LightweightOppToken>>> fullTokenMap = getFullTokenMap();
RangesForRepair rangesToRepair = CassandraRepairHelper.getRangesToRepair(cqlCluster, ATLAS_SERVICE, TABLE_1);
KeyedStream.stream(rangesToRepair.tokenMap()).forEach((address, cqlRangesForHost) -> assertRangesToRepairAreSubsetsOfRangesFromTokenMap(fullTokenMap, address, cqlRangesForHost));
}
use of com.palantir.atlasdb.keyvalue.cassandra.LightweightOppToken in project atlasdb by palantir.
the class ClusterMetadataUtilsTest method testMinTokenIsStart.
@Test
public void testMinTokenIsStart() {
LightweightOppToken nestedEndKey = getToken("0001");
LightweightOppToken outerEndKey = getToken("0002");
Range<LightweightOppToken> nested = Range.atMost(nestedEndKey);
Range<LightweightOppToken> outer = Range.atMost(outerEndKey);
assertThat(ClusterMetadataUtils.findLatestEndingRange(nested, outer)).isEqualTo(outer);
}
Aggregations