Search in sources :

Example 51 with Replica

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

the class ClusterActions method replicasForPrimaryKey.

// assumes every node knows the correct topology
static List<InetSocketAddress> replicasForPrimaryKey(String keyspaceName, String table, int primaryKey) {
    Keyspace keyspace = Keyspace.open(keyspaceName);
    TableMetadata metadata = keyspace.getColumnFamilyStore(table).metadata.get();
    DecoratedKey key = metadata.partitioner.decorateKey(Int32Type.instance.decompose(primaryKey));
    // we return a Set because simulator can easily encounter point where nodes are both natural and pending
    return ReplicaLayout.forTokenWriteLiveAndDown(keyspace, key.getToken()).all().asList(Replica::endpoint);
}
Also used : TableMetadata(org.apache.cassandra.schema.TableMetadata) Keyspace(org.apache.cassandra.db.Keyspace) DecoratedKey(org.apache.cassandra.db.DecoratedKey) Replica(org.apache.cassandra.locator.Replica)

Example 52 with Replica

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

the class OnClusterReplace method performSimple.

public ActionList performSimple() {
    // need to mark it as DOWN, and perhaps shut it down
    Map<InetSocketAddress, IInvokableInstance> lookup = Cluster.getUniqueAddressLookup(actions.cluster);
    IInvokableInstance leaveInstance = actions.cluster.get(leaving);
    IInvokableInstance joinInstance = actions.cluster.get(joining);
    before(leaveInstance);
    UUID hostId = leaveInstance.unsafeCallOnThisThread(SystemKeyspace::getLocalHostId);
    String movingToken = leaveInstance.unsafeCallOnThisThread(() -> Utils.currentToken().toString());
    List<Map.Entry<String, String>> repairRanges = leaveInstance.unsafeApplyOnThisThread((String keyspaceName) -> StorageService.instance.getLocalAndPendingRanges(keyspaceName).stream().map(OnClusterReplace::toStringEntry).collect(Collectors.toList()), actions.keyspace);
    int[] others = repairRanges.stream().mapToInt(repairRange -> lookup.get(leaveInstance.unsafeApplyOnThisThread((String keyspaceName, String tk) -> Keyspace.open(keyspaceName).getReplicationStrategy().getNaturalReplicasForToken(Utils.parseToken(tk)).stream().map(Replica::endpoint).filter(i -> !i.equals(getBroadcastAddressAndPort())).findFirst().orElseThrow(IllegalStateException::new), actions.keyspace, repairRange.getValue())).config().num()).toArray();
    return ActionList.of(// first sync gossip so that newly joined nodes are known by all, so that when we markdown we do not throw UnavailableException
    ReliableAction.transitively("Sync Gossip", () -> actions.gossipWithAll(leaving)), // "shutdown" the leaving instance
    new OnClusterUpdateGossip(actions, ActionList.of(new OnInstanceMarkShutdown(actions, leaving), new OnClusterMarkDown(actions, leaving)), new OnInstanceSendShutdownToAll(actions, leaving)), new OnClusterRepairRanges(actions, others, true, false, repairRanges), // stream/repair from a peer
    new OnClusterUpdateGossip(actions, joining, new OnInstanceSetBootstrapReplacing(actions, joining, leaving, hostId, movingToken)), new OnInstanceSyncSchemaForBootstrap(actions, joining), new OnInstanceBootstrap(actions, joinInstance, movingToken, true), // setup the node's own gossip state for natural ownership, and return gossip actions to disseminate
    new OnClusterUpdateGossip(actions, joining, new OnInstanceSetNormal(actions, joining, hostId, movingToken)));
}
Also used : LazyToString.lazy(org.apache.cassandra.utils.LazyToString.lazy) NONE(org.apache.cassandra.simulator.Action.Modifiers.NONE) ReliableAction(org.apache.cassandra.simulator.Actions.ReliableAction) Range(org.apache.cassandra.dht.Range) StorageService(org.apache.cassandra.service.StorageService) ActionList(org.apache.cassandra.simulator.ActionList) UUID(java.util.UUID) InetSocketAddress(java.net.InetSocketAddress) Collectors(java.util.stream.Collectors) SystemKeyspace(org.apache.cassandra.db.SystemKeyspace) Replica(org.apache.cassandra.locator.Replica) AbstractMap(java.util.AbstractMap) List(java.util.List) Token(org.apache.cassandra.dht.Token) FBUtilities.getBroadcastAddressAndPort(org.apache.cassandra.utils.FBUtilities.getBroadcastAddressAndPort) IInvokableInstance(org.apache.cassandra.distributed.api.IInvokableInstance) Map(java.util.Map) STRICT(org.apache.cassandra.simulator.Action.Modifiers.STRICT) Cluster(org.apache.cassandra.distributed.Cluster) Keyspace(org.apache.cassandra.db.Keyspace) IInvokableInstance(org.apache.cassandra.distributed.api.IInvokableInstance) SystemKeyspace(org.apache.cassandra.db.SystemKeyspace) InetSocketAddress(java.net.InetSocketAddress) UUID(java.util.UUID)

Example 53 with Replica

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

the class ViewUtilsTest method testLocalHostPreference.

@Test
public void testLocalHostPreference() throws Exception {
    TokenMetadata metadata = StorageService.instance.getTokenMetadata();
    metadata.clearUnsafe();
    // DC1
    metadata.updateNormalToken(new StringToken("A"), InetAddressAndPort.getByName("127.0.0.1"));
    metadata.updateNormalToken(new StringToken("C"), InetAddressAndPort.getByName("127.0.0.2"));
    // DC2
    metadata.updateNormalToken(new StringToken("B"), InetAddressAndPort.getByName("127.0.0.4"));
    metadata.updateNormalToken(new StringToken("D"), InetAddressAndPort.getByName("127.0.0.5"));
    Map<String, String> replicationMap = new HashMap<>();
    replicationMap.put(ReplicationParams.CLASS, NetworkTopologyStrategy.class.getName());
    replicationMap.put("DC1", "2");
    replicationMap.put("DC2", "2");
    Schema.instance.maybeRemoveKeyspaceInstance("Keyspace1");
    KeyspaceMetadata meta = KeyspaceMetadata.create("Keyspace1", KeyspaceParams.create(false, replicationMap));
    Schema.instance.load(meta);
    Optional<Replica> naturalEndpoint = ViewUtils.getViewNaturalEndpoint(Keyspace.open("Keyspace1").getReplicationStrategy(), new StringToken("CA"), new StringToken("BB"));
    Assert.assertTrue(naturalEndpoint.isPresent());
    Assert.assertEquals(InetAddressAndPort.getByName("127.0.0.1"), naturalEndpoint.get().endpoint());
}
Also used : NetworkTopologyStrategy(org.apache.cassandra.locator.NetworkTopologyStrategy) HashMap(java.util.HashMap) TokenMetadata(org.apache.cassandra.locator.TokenMetadata) KeyspaceMetadata(org.apache.cassandra.schema.KeyspaceMetadata) Replica(org.apache.cassandra.locator.Replica) StringToken(org.apache.cassandra.dht.OrderPreservingPartitioner.StringToken) Test(org.junit.Test)

Example 54 with Replica

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

the class ViewUtilsTest method testGetIndexNaturalEndpoint.

@Test
public void testGetIndexNaturalEndpoint() throws Exception {
    TokenMetadata metadata = StorageService.instance.getTokenMetadata();
    metadata.clearUnsafe();
    // DC1
    metadata.updateNormalToken(new StringToken("A"), InetAddressAndPort.getByName("127.0.0.1"));
    metadata.updateNormalToken(new StringToken("C"), InetAddressAndPort.getByName("127.0.0.2"));
    // DC2
    metadata.updateNormalToken(new StringToken("B"), InetAddressAndPort.getByName("127.0.0.4"));
    metadata.updateNormalToken(new StringToken("D"), InetAddressAndPort.getByName("127.0.0.5"));
    Map<String, String> replicationMap = new HashMap<>();
    replicationMap.put(ReplicationParams.CLASS, NetworkTopologyStrategy.class.getName());
    replicationMap.put("DC1", "1");
    replicationMap.put("DC2", "1");
    Schema.instance.maybeRemoveKeyspaceInstance("Keyspace1");
    KeyspaceMetadata meta = KeyspaceMetadata.create("Keyspace1", KeyspaceParams.create(false, replicationMap));
    Schema.instance.load(meta);
    Optional<Replica> naturalEndpoint = ViewUtils.getViewNaturalEndpoint(Keyspace.open("Keyspace1").getReplicationStrategy(), new StringToken("CA"), new StringToken("BB"));
    Assert.assertTrue(naturalEndpoint.isPresent());
    Assert.assertEquals(InetAddressAndPort.getByName("127.0.0.2"), naturalEndpoint.get().endpoint());
}
Also used : NetworkTopologyStrategy(org.apache.cassandra.locator.NetworkTopologyStrategy) HashMap(java.util.HashMap) TokenMetadata(org.apache.cassandra.locator.TokenMetadata) KeyspaceMetadata(org.apache.cassandra.schema.KeyspaceMetadata) Replica(org.apache.cassandra.locator.Replica) StringToken(org.apache.cassandra.dht.OrderPreservingPartitioner.StringToken) Test(org.junit.Test)

Example 55 with Replica

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

the class ViewUtilsTest method testBaseTokenDoesNotBelongToLocalReplicaShouldReturnEmpty.

@Test
public void testBaseTokenDoesNotBelongToLocalReplicaShouldReturnEmpty() throws Exception {
    TokenMetadata metadata = StorageService.instance.getTokenMetadata();
    metadata.clearUnsafe();
    // DC1
    metadata.updateNormalToken(new StringToken("A"), InetAddressAndPort.getByName("127.0.0.1"));
    metadata.updateNormalToken(new StringToken("C"), InetAddressAndPort.getByName("127.0.0.2"));
    // DC2
    metadata.updateNormalToken(new StringToken("B"), InetAddressAndPort.getByName("127.0.0.4"));
    metadata.updateNormalToken(new StringToken("D"), InetAddressAndPort.getByName("127.0.0.5"));
    Map<String, String> replicationMap = new HashMap<>();
    replicationMap.put(ReplicationParams.CLASS, NetworkTopologyStrategy.class.getName());
    replicationMap.put("DC1", "1");
    replicationMap.put("DC2", "1");
    Schema.instance.maybeRemoveKeyspaceInstance("Keyspace1");
    KeyspaceMetadata meta = KeyspaceMetadata.create("Keyspace1", KeyspaceParams.create(false, replicationMap));
    Schema.instance.load(meta);
    Optional<Replica> naturalEndpoint = ViewUtils.getViewNaturalEndpoint(Keyspace.open("Keyspace1").getReplicationStrategy(), new StringToken("AB"), new StringToken("BB"));
    Assert.assertFalse(naturalEndpoint.isPresent());
}
Also used : NetworkTopologyStrategy(org.apache.cassandra.locator.NetworkTopologyStrategy) HashMap(java.util.HashMap) TokenMetadata(org.apache.cassandra.locator.TokenMetadata) KeyspaceMetadata(org.apache.cassandra.schema.KeyspaceMetadata) Replica(org.apache.cassandra.locator.Replica) StringToken(org.apache.cassandra.dht.OrderPreservingPartitioner.StringToken) Test(org.junit.Test)

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