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