Search in sources :

Example 16 with Replica

use of org.apache.cassandra.locator.Replica in project cassandra by apache.

the class PendingRangesBench method searchTokenForOldPendingRanges.

@Benchmark
public void searchTokenForOldPendingRanges(final Blackhole bh) {
    int randomToken = ThreadLocalRandom.current().nextInt(maxToken * 10 + 5);
    Token searchToken = new RandomPartitioner.BigIntegerToken(Integer.toString(randomToken));
    Set<Replica> replicas = new HashSet<>();
    for (Map.Entry<Range<Token>, Collection<Replica>> entry : oldPendingRanges.asMap().entrySet()) {
        if (entry.getKey().contains(searchToken))
            replicas.addAll(entry.getValue());
    }
    bh.consume(replicas);
}
Also used : Collection(java.util.Collection) Token(org.apache.cassandra.dht.Token) Range(org.apache.cassandra.dht.Range) Replica(org.apache.cassandra.locator.Replica) Map(java.util.Map) HashSet(java.util.HashSet)

Example 17 with Replica

use of org.apache.cassandra.locator.Replica in project cassandra by apache.

the class ViewUtils method getViewNaturalEndpoint.

/**
 * Calculate the natural endpoint for the view.
 *
 * The view natural endpoint is the endpoint which has the same cardinality as this node in the replication factor.
 * The cardinality is the number at which this node would store a piece of data, given the change in replication
 * factor. If the keyspace's replication strategy is a NetworkTopologyStrategy, we filter the ring to contain only
 * nodes in the local datacenter when calculating cardinality.
 *
 * For example, if we have the following ring:
 *   {@code A, T1 -> B, T2 -> C, T3 -> A}
 *
 * For the token T1, at RF=1, A would be included, so A's cardinality for T1 is 1. For the token T1, at RF=2, B would
 * be included, so B's cardinality for token T1 is 2. For token T3, at RF = 2, A would be included, so A's cardinality
 * for T3 is 2.
 *
 * For a view whose base token is T1 and whose view token is T3, the pairings between the nodes would be:
 *  A writes to C (A's cardinality is 1 for T1, and C's cardinality is 1 for T3)
 *  B writes to A (B's cardinality is 2 for T1, and A's cardinality is 2 for T3)
 *  C writes to B (C's cardinality is 3 for T1, and B's cardinality is 3 for T3)
 *
 * @return Optional.empty() if this method is called using a base token which does not belong to this replica
 */
public static Optional<Replica> getViewNaturalEndpoint(AbstractReplicationStrategy replicationStrategy, Token baseToken, Token viewToken) {
    String localDataCenter = DatabaseDescriptor.getEndpointSnitch().getLocalDatacenter();
    EndpointsForToken naturalBaseReplicas = replicationStrategy.getNaturalReplicasForToken(baseToken);
    EndpointsForToken naturalViewReplicas = replicationStrategy.getNaturalReplicasForToken(viewToken);
    Optional<Replica> localReplica = Iterables.tryFind(naturalViewReplicas, Replica::isSelf).toJavaUtil();
    if (localReplica.isPresent())
        return localReplica;
    // We only select replicas from our own DC
    // TODO: this is poor encapsulation, leaking implementation details of replication strategy
    Predicate<Replica> isLocalDC = r -> !(replicationStrategy instanceof NetworkTopologyStrategy) || DatabaseDescriptor.getEndpointSnitch().getDatacenter(r).equals(localDataCenter);
    // We have to remove any endpoint which is shared between the base and the view, as it will select itself
    // and throw off the counts otherwise.
    EndpointsForToken baseReplicas = naturalBaseReplicas.filter(r -> !naturalViewReplicas.endpoints().contains(r.endpoint()) && isLocalDC.test(r));
    EndpointsForToken viewReplicas = naturalViewReplicas.filter(r -> !naturalBaseReplicas.endpoints().contains(r.endpoint()) && isLocalDC.test(r));
    // number of replicas for all of the tokens in the ring.
    assert baseReplicas.size() == viewReplicas.size() : "Replication strategy should have the same number of endpoints for the base and the view";
    int baseIdx = -1;
    for (int i = 0; i < baseReplicas.size(); i++) {
        if (baseReplicas.get(i).isSelf()) {
            baseIdx = i;
            break;
        }
    }
    if (baseIdx < 0)
        // This node is not a base replica of this key, so we return empty
        return Optional.empty();
    return Optional.of(viewReplicas.get(baseIdx));
}
Also used : Iterables(com.google.common.collect.Iterables) Token(org.apache.cassandra.dht.Token) AbstractReplicationStrategy(org.apache.cassandra.locator.AbstractReplicationStrategy) EndpointsForToken(org.apache.cassandra.locator.EndpointsForToken) Predicate(java.util.function.Predicate) Optional(java.util.Optional) DatabaseDescriptor(org.apache.cassandra.config.DatabaseDescriptor) Keyspace(org.apache.cassandra.db.Keyspace) NetworkTopologyStrategy(org.apache.cassandra.locator.NetworkTopologyStrategy) Replica(org.apache.cassandra.locator.Replica) EndpointsForToken(org.apache.cassandra.locator.EndpointsForToken) NetworkTopologyStrategy(org.apache.cassandra.locator.NetworkTopologyStrategy) Replica(org.apache.cassandra.locator.Replica)

Example 18 with Replica

use of org.apache.cassandra.locator.Replica in project cassandra by apache.

the class OnInstanceRepair method invokeRepair.

private static void invokeRepair(String keyspaceName, boolean repairPaxos, boolean repairOnlyPaxos, boolean primaryRangeOnly, boolean force, Runnable listener) {
    Keyspace keyspace = Keyspace.open(keyspaceName);
    TokenMetadata metadata = StorageService.instance.getTokenMetadata().cloneOnlyTokenMap();
    invokeRepair(keyspaceName, repairPaxos, repairOnlyPaxos, () -> primaryRangeOnly ? Collections.singletonList(metadata.getPrimaryRangeFor(currentToken())) : keyspace.getReplicationStrategy().getAddressReplicas(metadata).get(getBroadcastAddressAndPort()).asList(Replica::range), primaryRangeOnly, force, listener);
}
Also used : Keyspace(org.apache.cassandra.db.Keyspace) TokenMetadata(org.apache.cassandra.locator.TokenMetadata) Replica(org.apache.cassandra.locator.Replica)

Example 19 with Replica

use of org.apache.cassandra.locator.Replica in project cassandra by apache.

the class MoveTransientTest method calculateStreamAndFetchRangesMoveBackwardBetween.

public Pair<RangesAtEndpoint, RangesAtEndpoint> calculateStreamAndFetchRangesMoveBackwardBetween() throws Exception {
    Range<Token> aPrimeRange = new Range<>(elevenToken, fourteenToken);
    RangesAtEndpoint updated = RangesAtEndpoint.of(new Replica(address01, aPrimeRange, true), new Replica(address01, range_9_11, true), new Replica(address01, range_6_9, false));
    Pair<RangesAtEndpoint, RangesAtEndpoint> result = RangeRelocator.calculateStreamAndFetchRanges(current, updated);
    assertContentsIgnoreOrder(result.left, fullReplica(address01, oneToken, threeToken), fullReplica(address01, fourteenToken, oneToken));
    assertContentsIgnoreOrder(result.right, transientReplica(address01, sixToken, nineToken), fullReplica(address01, nineToken, elevenToken));
    return result;
}
Also used : RangesAtEndpoint(org.apache.cassandra.locator.RangesAtEndpoint) Token(org.apache.cassandra.dht.Token) Range(org.apache.cassandra.dht.Range) Replica.fullReplica(org.apache.cassandra.locator.Replica.fullReplica) Replica(org.apache.cassandra.locator.Replica) Replica.transientReplica(org.apache.cassandra.locator.Replica.transientReplica) EndpointsByReplica(org.apache.cassandra.locator.EndpointsByReplica)

Example 20 with Replica

use of org.apache.cassandra.locator.Replica in project cassandra by apache.

the class MoveTransientTest method calculateStreamAndFetchRangesMoveForwardBetween.

/**
 * Ring with start A 1-3 B 3-6 C 6-9 D 9-11 E 11-1
 * A's moves from 3 to 7
 *
 * @throws Exception
 */
private Pair<RangesAtEndpoint, RangesAtEndpoint> calculateStreamAndFetchRangesMoveForwardBetween() throws Exception {
    Range<Token> aPrimeRange = new Range<>(sixToken, sevenToken);
    Range<Token> bPrimeRange = new Range<>(oneToken, sixToken);
    RangesAtEndpoint updated = RangesAtEndpoint.of(new Replica(address01, aPrimeRange, true), new Replica(address01, bPrimeRange, true), new Replica(address01, range_11_1, false));
    Pair<RangesAtEndpoint, RangesAtEndpoint> result = RangeRelocator.calculateStreamAndFetchRanges(current, updated);
    assertContentsIgnoreOrder(result.left, fullReplica(address01, elevenToken, oneToken), transientReplica(address01, nineToken, elevenToken));
    assertContentsIgnoreOrder(result.right, fullReplica(address01, threeToken, sixToken), fullReplica(address01, sixToken, sevenToken));
    return result;
}
Also used : RangesAtEndpoint(org.apache.cassandra.locator.RangesAtEndpoint) Token(org.apache.cassandra.dht.Token) Range(org.apache.cassandra.dht.Range) Replica.fullReplica(org.apache.cassandra.locator.Replica.fullReplica) Replica(org.apache.cassandra.locator.Replica) Replica.transientReplica(org.apache.cassandra.locator.Replica.transientReplica) EndpointsByReplica(org.apache.cassandra.locator.EndpointsByReplica)

Aggregations

Replica (org.apache.cassandra.locator.Replica)69 InetAddressAndPort (org.apache.cassandra.locator.InetAddressAndPort)24 Token (org.apache.cassandra.dht.Token)22 Test (org.junit.Test)20 HashMap (java.util.HashMap)18 Mutation (org.apache.cassandra.db.Mutation)15 EndpointsByReplica (org.apache.cassandra.locator.EndpointsByReplica)15 Range (org.apache.cassandra.dht.Range)14 RangesAtEndpoint (org.apache.cassandra.locator.RangesAtEndpoint)13 TokenMetadata (org.apache.cassandra.locator.TokenMetadata)12 Keyspace (org.apache.cassandra.db.Keyspace)11 AbstractReplicationStrategy (org.apache.cassandra.locator.AbstractReplicationStrategy)11 EndpointsForRange (org.apache.cassandra.locator.EndpointsForRange)11 Replica.fullReplica (org.apache.cassandra.locator.Replica.fullReplica)10 Collection (java.util.Collection)8 Map (java.util.Map)8 EndpointsForToken (org.apache.cassandra.locator.EndpointsForToken)8 ReplicaPlan (org.apache.cassandra.locator.ReplicaPlan)8 ArrayList (java.util.ArrayList)7 HashSet (java.util.HashSet)6