Search in sources :

Example 1 with LightweightOppToken

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());
    }
}
Also used : LightweightOppToken(com.palantir.atlasdb.keyvalue.cassandra.LightweightOppToken) InetSocketAddress(java.net.InetSocketAddress) ImmutableRangeMap(com.google.common.collect.ImmutableRangeMap) Collection(java.util.Collection) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) TokenRange(org.apache.cassandra.thrift.TokenRange) UnknownHostException(java.net.UnknownHostException)

Example 2 with LightweightOppToken

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());
}
Also used : LightweightOppToken(com.palantir.atlasdb.keyvalue.cassandra.LightweightOppToken) HashMap(java.util.HashMap) Range(com.google.common.collect.Range) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 3 with LightweightOppToken

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());
}
Also used : CqlMetadata(com.palantir.atlasdb.cassandra.backup.CqlMetadata) LightweightOppToken(com.palantir.atlasdb.keyvalue.cassandra.LightweightOppToken) FullyBoundedTimestampRange(com.palantir.timestamp.FullyBoundedTimestampRange) Range(com.google.common.collect.Range) Test(org.junit.Test)

Example 4 with LightweightOppToken

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));
}
Also used : RangesForRepair(com.palantir.atlasdb.cassandra.backup.RangesForRepair) CassandraServer(com.palantir.atlasdb.keyvalue.cassandra.pool.CassandraServer) RangeSet(com.google.common.collect.RangeSet) ImmutableRangeSet.toImmutableRangeSet(com.google.common.collect.ImmutableRangeSet.toImmutableRangeSet) Set(java.util.Set) HashSet(java.util.HashSet) LightweightOppToken(com.palantir.atlasdb.keyvalue.cassandra.LightweightOppToken) Test(org.junit.Test)

Example 5 with LightweightOppToken

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);
}
Also used : LightweightOppToken(com.palantir.atlasdb.keyvalue.cassandra.LightweightOppToken) Test(org.junit.Test)

Aggregations

LightweightOppToken (com.palantir.atlasdb.keyvalue.cassandra.LightweightOppToken)31 Test (org.junit.Test)25 TokenRange (com.datastax.driver.core.TokenRange)10 Map (java.util.Map)9 Range (com.google.common.collect.Range)8 TransactionsTableInteraction (com.palantir.atlasdb.cassandra.backup.transaction.TransactionsTableInteraction)8 Transactions2TableInteraction (com.palantir.atlasdb.cassandra.backup.transaction.Transactions2TableInteraction)6 VisibleForTesting (com.google.common.annotations.VisibleForTesting)3 ImmutableList (com.google.common.collect.ImmutableList)3 Transactions1TableInteraction (com.palantir.atlasdb.cassandra.backup.transaction.Transactions1TableInteraction)3 Set (java.util.Set)3 Token (com.datastax.driver.core.Token)2 ImmutableRangeMap (com.google.common.collect.ImmutableRangeMap)2 RangeSet (com.google.common.collect.RangeSet)2 InetSocketAddress (java.net.InetSocketAddress)2 UnknownHostException (java.net.UnknownHostException)2 Collection (java.util.Collection)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 List (java.util.List)2