Search in sources :

Example 81 with TokenMetadata

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

the class LeaveAndBootstrapTest method testStateJumpToLeaving.

@Test
public void testStateJumpToLeaving() throws UnknownHostException {
    StorageService ss = StorageService.instance;
    TokenMetadata tmd = ss.getTokenMetadata();
    tmd.clearUnsafe();
    Gossiper.instance.clearUnsafe();
    IPartitioner partitioner = RandomPartitioner.instance;
    VersionedValue.VersionedValueFactory valueFactory = new VersionedValue.VersionedValueFactory(partitioner);
    ArrayList<Token> endpointTokens = new ArrayList<Token>();
    ArrayList<Token> keyTokens = new ArrayList<Token>();
    List<InetAddressAndPort> hosts = new ArrayList<>();
    List<UUID> hostIds = new ArrayList<UUID>();
    // create a ring or 5 nodes
    Util.createInitialRing(ss, partitioner, endpointTokens, keyTokens, hosts, hostIds, 6);
    // node 2 leaves with _different_ token
    Gossiper.instance.injectApplicationState(hosts.get(2), ApplicationState.TOKENS, valueFactory.tokens(Collections.singleton(keyTokens.get(0))));
    ss.onChange(hosts.get(2), ApplicationState.STATUS, valueFactory.leaving(Collections.singleton(keyTokens.get(0))));
    assertEquals(keyTokens.get(0), tmd.getToken(hosts.get(2)));
    assertTrue(tmd.isLeaving(hosts.get(2)));
    assertNull(tmd.getEndpoint(endpointTokens.get(2)));
    // go to boostrap
    Gossiper.instance.injectApplicationState(hosts.get(2), ApplicationState.TOKENS, valueFactory.tokens(Collections.singleton(keyTokens.get(1))));
    ss.onChange(hosts.get(2), ApplicationState.STATUS, valueFactory.bootstrapping(Collections.<Token>singleton(keyTokens.get(1))));
    assertFalse(tmd.isLeaving(hosts.get(2)));
    assertEquals(1, tmd.getBootstrapTokens().size());
    assertEquals(hosts.get(2), tmd.getBootstrapTokens().get(keyTokens.get(1)));
    // jump to leaving again
    ss.onChange(hosts.get(2), ApplicationState.STATUS, valueFactory.leaving(Collections.singleton(keyTokens.get(1))));
    assertEquals(hosts.get(2), tmd.getEndpoint(keyTokens.get(1)));
    assertTrue(tmd.isLeaving(hosts.get(2)));
    assertTrue(tmd.getBootstrapTokens().isEmpty());
    // go to state left
    ss.onChange(hosts.get(2), ApplicationState.STATUS, valueFactory.left(Collections.singleton(keyTokens.get(1)), Gossiper.computeExpireTime()));
    assertFalse(tmd.isMember(hosts.get(2)));
    assertFalse(tmd.isLeaving(hosts.get(2)));
}
Also used : VersionedValue(org.apache.cassandra.gms.VersionedValue) InetAddressAndPort(org.apache.cassandra.locator.InetAddressAndPort) BigIntegerToken(org.apache.cassandra.dht.RandomPartitioner.BigIntegerToken) Token(org.apache.cassandra.dht.Token) TokenMetadata(org.apache.cassandra.locator.TokenMetadata) IPartitioner(org.apache.cassandra.dht.IPartitioner) Test(org.junit.Test)

Example 82 with TokenMetadata

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

the class LeaveAndBootstrapTest method testStateJumpToNormal.

@Test
public void testStateJumpToNormal() throws UnknownHostException {
    StorageService ss = StorageService.instance;
    TokenMetadata tmd = ss.getTokenMetadata();
    tmd.clearUnsafe();
    Gossiper.instance.clearUnsafe();
    IPartitioner partitioner = RandomPartitioner.instance;
    VersionedValue.VersionedValueFactory valueFactory = new VersionedValue.VersionedValueFactory(partitioner);
    ArrayList<Token> endpointTokens = new ArrayList<Token>();
    ArrayList<Token> keyTokens = new ArrayList<Token>();
    List<InetAddressAndPort> hosts = new ArrayList<>();
    List<UUID> hostIds = new ArrayList<UUID>();
    // create a ring or 5 nodes
    Util.createInitialRing(ss, partitioner, endpointTokens, keyTokens, hosts, hostIds, 6);
    // node 2 leaves
    ss.onChange(hosts.get(2), ApplicationState.STATUS, valueFactory.leaving(Collections.singleton(endpointTokens.get(2))));
    assertTrue(tmd.isLeaving(hosts.get(2)));
    assertEquals(endpointTokens.get(2), tmd.getToken(hosts.get(2)));
    // back to normal
    Gossiper.instance.injectApplicationState(hosts.get(2), ApplicationState.TOKENS, valueFactory.tokens(Collections.singleton(keyTokens.get(2))));
    ss.onChange(hosts.get(2), ApplicationState.STATUS, valueFactory.normal(Collections.singleton(keyTokens.get(2))));
    assertTrue(tmd.getSizeOfLeavingEndpoints() == 0);
    assertEquals(keyTokens.get(2), tmd.getToken(hosts.get(2)));
    // node 3 goes through leave and left and then jumps to normal at its new token
    ss.onChange(hosts.get(2), ApplicationState.STATUS, valueFactory.leaving(Collections.singleton(keyTokens.get(2))));
    ss.onChange(hosts.get(2), ApplicationState.STATUS, valueFactory.left(Collections.singleton(keyTokens.get(2)), Gossiper.computeExpireTime()));
    Gossiper.instance.injectApplicationState(hosts.get(2), ApplicationState.TOKENS, valueFactory.tokens(Collections.singleton(keyTokens.get(4))));
    ss.onChange(hosts.get(2), ApplicationState.STATUS, valueFactory.normal(Collections.singleton(keyTokens.get(4))));
    assertTrue(tmd.getBootstrapTokens().isEmpty());
    assertTrue(tmd.getSizeOfLeavingEndpoints() == 0);
    assertEquals(keyTokens.get(4), tmd.getToken(hosts.get(2)));
}
Also used : VersionedValue(org.apache.cassandra.gms.VersionedValue) InetAddressAndPort(org.apache.cassandra.locator.InetAddressAndPort) BigIntegerToken(org.apache.cassandra.dht.RandomPartitioner.BigIntegerToken) Token(org.apache.cassandra.dht.Token) TokenMetadata(org.apache.cassandra.locator.TokenMetadata) IPartitioner(org.apache.cassandra.dht.IPartitioner) Test(org.junit.Test)

Example 83 with TokenMetadata

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

the class MoveTest method testSimultaneousMove.

/*
     * Test ranges and write endpoints when multiple nodes are on the move simultaneously
     */
@Test
public void testSimultaneousMove() throws UnknownHostException {
    StorageService ss = StorageService.instance;
    final int RING_SIZE = 10;
    TokenMetadata tmd = ss.getTokenMetadata();
    IPartitioner partitioner = RandomPartitioner.instance;
    VersionedValue.VersionedValueFactory valueFactory = new VersionedValue.VersionedValueFactory(partitioner);
    ArrayList<Token> endpointTokens = new ArrayList<Token>();
    ArrayList<Token> keyTokens = new ArrayList<Token>();
    List<InetAddressAndPort> hosts = new ArrayList<>();
    List<UUID> hostIds = new ArrayList<UUID>();
    // create a ring or 10 nodes
    Util.createInitialRing(ss, partitioner, endpointTokens, keyTokens, hosts, hostIds, RING_SIZE);
    // nodes 6, 8 and 9 leave
    final int[] MOVING = new int[] { 6, 8, 9 };
    Map<Integer, Token> newTokens = new HashMap<Integer, Token>();
    for (int movingIndex : MOVING) {
        Token newToken = positionToken(movingIndex);
        ss.onChange(hosts.get(movingIndex), ApplicationState.STATUS, valueFactory.moving(newToken));
        // storing token associated with a node index
        newTokens.put(movingIndex, newToken);
    }
    tmd = tmd.cloneAfterAllSettled();
    ss.setTokenMetadataUnsafe(tmd);
    // boot two new nodes with keyTokens.get(5) and keyTokens.get(7)
    InetAddressAndPort boot1 = InetAddressAndPort.getByName("127.0.1.1");
    Gossiper.instance.initializeNodeUnsafe(boot1, UUID.randomUUID(), 1);
    Gossiper.instance.injectApplicationState(boot1, ApplicationState.TOKENS, valueFactory.tokens(Collections.singleton(keyTokens.get(5))));
    ss.onChange(boot1, ApplicationState.STATUS, valueFactory.bootstrapping(Collections.<Token>singleton(keyTokens.get(5))));
    PendingRangeCalculatorService.instance.blockUntilFinished();
    InetAddressAndPort boot2 = InetAddressAndPort.getByName("127.0.1.2");
    Gossiper.instance.initializeNodeUnsafe(boot2, UUID.randomUUID(), 1);
    Gossiper.instance.injectApplicationState(boot2, ApplicationState.TOKENS, valueFactory.tokens(Collections.singleton(keyTokens.get(7))));
    ss.onChange(boot2, ApplicationState.STATUS, valueFactory.bootstrapping(Collections.<Token>singleton(keyTokens.get(7))));
    PendingRangeCalculatorService.instance.blockUntilFinished();
    // don't require test update every time a new keyspace is added to test/conf/cassandra.yaml
    Map<String, AbstractReplicationStrategy> keyspaceStrategyMap = new HashMap<String, AbstractReplicationStrategy>();
    for (int i = 1; i <= 4; i++) {
        keyspaceStrategyMap.put("MoveTestKeyspace" + i, getStrategy("MoveTestKeyspace" + i, tmd));
    }
    /**
     *  Keyspace1 & Keyspace2 RF=1
     *  {
     *      /127.0.0.1=[(97,0]],
     *      /127.0.0.2=[(0,10]],
     *      /127.0.0.3=[(10,20]],
     *      /127.0.0.4=[(20,30]],
     *      /127.0.0.5=[(30,40]],
     *      /127.0.0.6=[(40,50]],
     *      /127.0.0.7=[(50,67]],
     *      /127.0.0.8=[(67,70]],
     *      /127.0.0.9=[(70,87]],
     *      /127.0.0.10=[(87,97]]
     *  }
     */
    RangesByEndpoint keyspace1ranges = keyspaceStrategyMap.get(Simple_RF1_KeyspaceName).getAddressReplicas();
    assertRanges(keyspace1ranges, "127.0.0.1", 97, 0);
    assertRanges(keyspace1ranges, "127.0.0.2", 0, 10);
    assertRanges(keyspace1ranges, "127.0.0.3", 10, 20);
    assertRanges(keyspace1ranges, "127.0.0.4", 20, 30);
    assertRanges(keyspace1ranges, "127.0.0.5", 30, 40);
    assertRanges(keyspace1ranges, "127.0.0.6", 40, 50);
    assertRanges(keyspace1ranges, "127.0.0.7", 50, 67);
    assertRanges(keyspace1ranges, "127.0.0.8", 67, 70);
    assertRanges(keyspace1ranges, "127.0.0.9", 70, 87);
    assertRanges(keyspace1ranges, "127.0.0.10", 87, 97);
    /**
     * Keyspace3 RF=5
     * {
     *      /127.0.0.1=[(97,0], (70,87], (50,67], (87,97], (67,70]],
     *      /127.0.0.2=[(97,0], (70,87], (87,97], (0,10], (67,70]],
     *      /127.0.0.3=[(97,0], (70,87], (87,97], (0,10], (10,20]],
     *      /127.0.0.4=[(97,0], (20,30], (87,97], (0,10], (10,20]],
     *      /127.0.0.5=[(97,0], (30,40], (20,30], (0,10], (10,20]],
     *      /127.0.0.6=[(40,50], (30,40], (20,30], (0,10], (10,20]],
     *      /127.0.0.7=[(40,50], (30,40], (50,67], (20,30], (10,20]],
     *      /127.0.0.8=[(40,50], (30,40], (50,67], (20,30], (67,70]],
     *      /127.0.0.9=[(40,50], (70,87], (30,40], (50,67], (67,70]],
     *      /127.0.0.10=[(40,50], (70,87], (50,67], (87,97], (67,70]]
     * }
     */
    RangesByEndpoint keyspace3ranges = keyspaceStrategyMap.get(KEYSPACE3).getAddressReplicas();
    assertRanges(keyspace3ranges, "127.0.0.1", 97, 0, 70, 87, 50, 67, 87, 97, 67, 70);
    assertRanges(keyspace3ranges, "127.0.0.2", 97, 0, 70, 87, 87, 97, 0, 10, 67, 70);
    assertRanges(keyspace3ranges, "127.0.0.3", 97, 0, 70, 87, 87, 97, 0, 10, 10, 20);
    assertRanges(keyspace3ranges, "127.0.0.4", 97, 0, 20, 30, 87, 97, 0, 10, 10, 20);
    assertRanges(keyspace3ranges, "127.0.0.5", 97, 0, 30, 40, 20, 30, 0, 10, 10, 20);
    assertRanges(keyspace3ranges, "127.0.0.6", 40, 50, 30, 40, 20, 30, 0, 10, 10, 20);
    assertRanges(keyspace3ranges, "127.0.0.7", 40, 50, 30, 40, 50, 67, 20, 30, 10, 20);
    assertRanges(keyspace3ranges, "127.0.0.8", 40, 50, 30, 40, 50, 67, 20, 30, 67, 70);
    assertRanges(keyspace3ranges, "127.0.0.9", 40, 50, 70, 87, 30, 40, 50, 67, 67, 70);
    assertRanges(keyspace3ranges, "127.0.0.10", 40, 50, 70, 87, 50, 67, 87, 97, 67, 70);
    /**
     * Keyspace4 RF=3
     * {
     *      /127.0.0.1=[(97,0], (70,87], (87,97]],
     *      /127.0.0.2=[(97,0], (87,97], (0,10]],
     *      /127.0.0.3=[(97,0], (0,10], (10,20]],
     *      /127.0.0.4=[(20,30], (0,10], (10,20]],
     *      /127.0.0.5=[(30,40], (20,30], (10,20]],
     *      /127.0.0.6=[(40,50], (30,40], (20,30]],
     *      /127.0.0.7=[(40,50], (30,40], (50,67]],
     *      /127.0.0.8=[(40,50], (50,67], (67,70]],
     *      /127.0.0.9=[(70,87], (50,67], (67,70]],
     *      /127.0.0.10=[(70,87], (87,97], (67,70]]
     *  }
     */
    RangesByEndpoint keyspace4ranges = keyspaceStrategyMap.get(Simple_RF3_KeyspaceName).getAddressReplicas();
    assertRanges(keyspace4ranges, "127.0.0.1", 97, 0, 70, 87, 87, 97);
    assertRanges(keyspace4ranges, "127.0.0.2", 97, 0, 87, 97, 0, 10);
    assertRanges(keyspace4ranges, "127.0.0.3", 97, 0, 0, 10, 10, 20);
    assertRanges(keyspace4ranges, "127.0.0.4", 20, 30, 0, 10, 10, 20);
    assertRanges(keyspace4ranges, "127.0.0.5", 30, 40, 20, 30, 10, 20);
    assertRanges(keyspace4ranges, "127.0.0.6", 40, 50, 30, 40, 20, 30);
    assertRanges(keyspace4ranges, "127.0.0.7", 40, 50, 30, 40, 50, 67);
    assertRanges(keyspace4ranges, "127.0.0.8", 40, 50, 50, 67, 67, 70);
    assertRanges(keyspace4ranges, "127.0.0.9", 70, 87, 50, 67, 67, 70);
    assertRanges(keyspace4ranges, "127.0.0.10", 70, 87, 87, 97, 67, 70);
    // pre-calculate the results.
    Map<String, Multimap<Token, InetAddressAndPort>> expectedEndpoints = new HashMap<>();
    expectedEndpoints.put(Simple_RF1_KeyspaceName, HashMultimap.<Token, InetAddressAndPort>create());
    expectedEndpoints.get(Simple_RF1_KeyspaceName).putAll(new BigIntegerToken("5"), makeAddrs("127.0.0.2"));
    expectedEndpoints.get(Simple_RF1_KeyspaceName).putAll(new BigIntegerToken("15"), makeAddrs("127.0.0.3"));
    expectedEndpoints.get(Simple_RF1_KeyspaceName).putAll(new BigIntegerToken("25"), makeAddrs("127.0.0.4"));
    expectedEndpoints.get(Simple_RF1_KeyspaceName).putAll(new BigIntegerToken("35"), makeAddrs("127.0.0.5"));
    expectedEndpoints.get(Simple_RF1_KeyspaceName).putAll(new BigIntegerToken("45"), makeAddrs("127.0.0.6"));
    expectedEndpoints.get(Simple_RF1_KeyspaceName).putAll(new BigIntegerToken("55"), makeAddrs("127.0.0.7", "127.0.1.1"));
    expectedEndpoints.get(Simple_RF1_KeyspaceName).putAll(new BigIntegerToken("65"), makeAddrs("127.0.0.7"));
    expectedEndpoints.get(Simple_RF1_KeyspaceName).putAll(new BigIntegerToken("75"), makeAddrs("127.0.0.9", "127.0.1.2"));
    expectedEndpoints.get(Simple_RF1_KeyspaceName).putAll(new BigIntegerToken("85"), makeAddrs("127.0.0.9"));
    expectedEndpoints.get(Simple_RF1_KeyspaceName).putAll(new BigIntegerToken("95"), makeAddrs("127.0.0.10"));
    expectedEndpoints.put(KEYSPACE2, HashMultimap.<Token, InetAddressAndPort>create());
    expectedEndpoints.get(KEYSPACE2).putAll(new BigIntegerToken("5"), makeAddrs("127.0.0.2"));
    expectedEndpoints.get(KEYSPACE2).putAll(new BigIntegerToken("15"), makeAddrs("127.0.0.3"));
    expectedEndpoints.get(KEYSPACE2).putAll(new BigIntegerToken("25"), makeAddrs("127.0.0.4"));
    expectedEndpoints.get(KEYSPACE2).putAll(new BigIntegerToken("35"), makeAddrs("127.0.0.5"));
    expectedEndpoints.get(KEYSPACE2).putAll(new BigIntegerToken("45"), makeAddrs("127.0.0.6"));
    expectedEndpoints.get(KEYSPACE2).putAll(new BigIntegerToken("55"), makeAddrs("127.0.0.7", "127.0.1.1"));
    expectedEndpoints.get(KEYSPACE2).putAll(new BigIntegerToken("65"), makeAddrs("127.0.0.7"));
    expectedEndpoints.get(KEYSPACE2).putAll(new BigIntegerToken("75"), makeAddrs("127.0.0.9", "127.0.1.2"));
    expectedEndpoints.get(KEYSPACE2).putAll(new BigIntegerToken("85"), makeAddrs("127.0.0.9"));
    expectedEndpoints.get(KEYSPACE2).putAll(new BigIntegerToken("95"), makeAddrs("127.0.0.10"));
    expectedEndpoints.put(KEYSPACE3, HashMultimap.<Token, InetAddressAndPort>create());
    expectedEndpoints.get(KEYSPACE3).putAll(new BigIntegerToken("5"), makeAddrs("127.0.0.2", "127.0.0.3", "127.0.0.4", "127.0.0.5", "127.0.0.6"));
    expectedEndpoints.get(KEYSPACE3).putAll(new BigIntegerToken("15"), makeAddrs("127.0.0.3", "127.0.0.4", "127.0.0.5", "127.0.0.6", "127.0.0.7", "127.0.1.1"));
    expectedEndpoints.get(KEYSPACE3).putAll(new BigIntegerToken("25"), makeAddrs("127.0.0.4", "127.0.0.5", "127.0.0.6", "127.0.0.7", "127.0.0.8", "127.0.1.1"));
    expectedEndpoints.get(KEYSPACE3).putAll(new BigIntegerToken("35"), makeAddrs("127.0.0.5", "127.0.0.6", "127.0.0.7", "127.0.0.8", "127.0.0.9", "127.0.1.1", "127.0.1.2"));
    expectedEndpoints.get(KEYSPACE3).putAll(new BigIntegerToken("45"), makeAddrs("127.0.0.6", "127.0.0.7", "127.0.0.8", "127.0.0.9", "127.0.0.10", "127.0.1.1", "127.0.1.2"));
    expectedEndpoints.get(KEYSPACE3).putAll(new BigIntegerToken("55"), makeAddrs("127.0.0.7", "127.0.0.8", "127.0.0.9", "127.0.0.10", "127.0.0.1", "127.0.1.1", "127.0.1.2"));
    expectedEndpoints.get(KEYSPACE3).putAll(new BigIntegerToken("65"), makeAddrs("127.0.0.7", "127.0.0.8", "127.0.0.9", "127.0.0.10", "127.0.0.1", "127.0.1.2"));
    expectedEndpoints.get(KEYSPACE3).putAll(new BigIntegerToken("75"), makeAddrs("127.0.0.9", "127.0.0.10", "127.0.0.1", "127.0.0.2", "127.0.0.3", "127.0.1.2"));
    expectedEndpoints.get(KEYSPACE3).putAll(new BigIntegerToken("85"), makeAddrs("127.0.0.9", "127.0.0.10", "127.0.0.1", "127.0.0.2", "127.0.0.3"));
    expectedEndpoints.get(KEYSPACE3).putAll(new BigIntegerToken("95"), makeAddrs("127.0.0.10", "127.0.0.1", "127.0.0.2", "127.0.0.3", "127.0.0.4"));
    expectedEndpoints.put(Simple_RF3_KeyspaceName, HashMultimap.<Token, InetAddressAndPort>create());
    expectedEndpoints.get(Simple_RF3_KeyspaceName).putAll(new BigIntegerToken("5"), makeAddrs("127.0.0.2", "127.0.0.3", "127.0.0.4"));
    expectedEndpoints.get(Simple_RF3_KeyspaceName).putAll(new BigIntegerToken("15"), makeAddrs("127.0.0.3", "127.0.0.4", "127.0.0.5"));
    expectedEndpoints.get(Simple_RF3_KeyspaceName).putAll(new BigIntegerToken("25"), makeAddrs("127.0.0.4", "127.0.0.5", "127.0.0.6"));
    expectedEndpoints.get(Simple_RF3_KeyspaceName).putAll(new BigIntegerToken("35"), makeAddrs("127.0.0.5", "127.0.0.6", "127.0.0.7", "127.0.1.1"));
    expectedEndpoints.get(Simple_RF3_KeyspaceName).putAll(new BigIntegerToken("45"), makeAddrs("127.0.0.6", "127.0.0.7", "127.0.0.8", "127.0.1.1"));
    expectedEndpoints.get(Simple_RF3_KeyspaceName).putAll(new BigIntegerToken("55"), makeAddrs("127.0.0.7", "127.0.0.8", "127.0.0.9", "127.0.1.1", "127.0.1.2"));
    expectedEndpoints.get(Simple_RF3_KeyspaceName).putAll(new BigIntegerToken("65"), makeAddrs("127.0.0.7", "127.0.0.8", "127.0.0.9", "127.0.1.2"));
    expectedEndpoints.get(Simple_RF3_KeyspaceName).putAll(new BigIntegerToken("75"), makeAddrs("127.0.0.9", "127.0.0.10", "127.0.0.1", "127.0.1.2"));
    expectedEndpoints.get(Simple_RF3_KeyspaceName).putAll(new BigIntegerToken("85"), makeAddrs("127.0.0.9", "127.0.0.10", "127.0.0.1"));
    expectedEndpoints.get(Simple_RF3_KeyspaceName).putAll(new BigIntegerToken("95"), makeAddrs("127.0.0.10", "127.0.0.1", "127.0.0.2"));
    for (Map.Entry<String, AbstractReplicationStrategy> keyspaceStrategy : keyspaceStrategyMap.entrySet()) {
        String keyspaceName = keyspaceStrategy.getKey();
        AbstractReplicationStrategy strategy = keyspaceStrategy.getValue();
        for (Token token : keyTokens) {
            Collection<InetAddressAndPort> endpoints = tmd.getWriteEndpoints(token, keyspaceName, strategy.getNaturalReplicasForToken(token)).endpoints();
            assertEquals(expectedEndpoints.get(keyspaceName).get(token).size(), endpoints.size());
            assertTrue(expectedEndpoints.get(keyspaceName).get(token).containsAll(endpoints));
        }
        // just to be sure that things still work according to the old tests, run them:
        if (strategy.getReplicationFactor().allReplicas != 3)
            continue;
        ReplicaCollection<?> replicas = null;
        // tokens 5, 15 and 25 should go three nodes
        for (int i = 0; i < 3; i++) {
            replicas = tmd.getWriteEndpoints(keyTokens.get(i), keyspaceName, strategy.getNaturalReplicasForToken(keyTokens.get(i)));
            assertEquals(3, replicas.size());
            assertTrue(replicas.endpoints().contains(hosts.get(i + 1)));
            assertTrue(replicas.endpoints().contains(hosts.get(i + 2)));
            assertTrue(replicas.endpoints().contains(hosts.get(i + 3)));
        }
        // token 35 should go to nodes 4, 5, 6 and boot1
        replicas = tmd.getWriteEndpoints(keyTokens.get(3), keyspaceName, strategy.getNaturalReplicasForToken(keyTokens.get(3)));
        assertEquals(4, replicas.size());
        assertTrue(replicas.endpoints().contains(hosts.get(4)));
        assertTrue(replicas.endpoints().contains(hosts.get(5)));
        assertTrue(replicas.endpoints().contains(hosts.get(6)));
        assertTrue(replicas.endpoints().contains(boot1));
        // token 45 should go to nodes 5, 6, 7 boot1
        replicas = tmd.getWriteEndpoints(keyTokens.get(4), keyspaceName, strategy.getNaturalReplicasForToken(keyTokens.get(4)));
        assertEquals(4, replicas.size());
        assertTrue(replicas.endpoints().contains(hosts.get(5)));
        assertTrue(replicas.endpoints().contains(hosts.get(6)));
        assertTrue(replicas.endpoints().contains(hosts.get(7)));
        assertTrue(replicas.endpoints().contains(boot1));
        // token 55 should go to nodes 6, 7, 8 boot1 and boot2
        replicas = tmd.getWriteEndpoints(keyTokens.get(5), keyspaceName, strategy.getNaturalReplicasForToken(keyTokens.get(5)));
        assertEquals(5, replicas.size());
        assertTrue(replicas.endpoints().contains(hosts.get(6)));
        assertTrue(replicas.endpoints().contains(hosts.get(7)));
        assertTrue(replicas.endpoints().contains(hosts.get(8)));
        assertTrue(replicas.endpoints().contains(boot1));
        assertTrue(replicas.endpoints().contains(boot2));
        // token 65 should go to nodes 6, 7, 8 and boot2
        replicas = tmd.getWriteEndpoints(keyTokens.get(6), keyspaceName, strategy.getNaturalReplicasForToken(keyTokens.get(6)));
        assertEquals(4, replicas.size());
        assertTrue(replicas.endpoints().contains(hosts.get(6)));
        assertTrue(replicas.endpoints().contains(hosts.get(7)));
        assertTrue(replicas.endpoints().contains(hosts.get(8)));
        assertTrue(replicas.endpoints().contains(boot2));
        // token 75 should to go nodes 8, 9, 0 and boot2
        replicas = tmd.getWriteEndpoints(keyTokens.get(7), keyspaceName, strategy.getNaturalReplicasForToken(keyTokens.get(7)));
        assertEquals(4, replicas.size());
        assertTrue(replicas.endpoints().contains(hosts.get(8)));
        assertTrue(replicas.endpoints().contains(hosts.get(9)));
        assertTrue(replicas.endpoints().contains(hosts.get(0)));
        assertTrue(replicas.endpoints().contains(boot2));
        // token 85 should go to nodes 8, 9 and 0
        replicas = tmd.getWriteEndpoints(keyTokens.get(8), keyspaceName, strategy.getNaturalReplicasForToken(keyTokens.get(8)));
        assertEquals(3, replicas.size());
        assertTrue(replicas.endpoints().contains(hosts.get(8)));
        assertTrue(replicas.endpoints().contains(hosts.get(9)));
        assertTrue(replicas.endpoints().contains(hosts.get(0)));
        // token 95 should go to nodes 9, 0 and 1
        replicas = tmd.getWriteEndpoints(keyTokens.get(9), keyspaceName, strategy.getNaturalReplicasForToken(keyTokens.get(9)));
        assertEquals(3, replicas.size());
        assertTrue(replicas.endpoints().contains(hosts.get(9)));
        assertTrue(replicas.endpoints().contains(hosts.get(0)));
        assertTrue(replicas.endpoints().contains(hosts.get(1)));
    }
    // all moving nodes are back to the normal state
    for (Integer movingIndex : MOVING) {
        ss.onChange(hosts.get(movingIndex), ApplicationState.STATUS, valueFactory.normal(Collections.singleton(newTokens.get(movingIndex))));
    }
}
Also used : InetAddressAndPort(org.apache.cassandra.locator.InetAddressAndPort) EndpointsForToken(org.apache.cassandra.locator.EndpointsForToken) BigIntegerToken(org.apache.cassandra.dht.RandomPartitioner.BigIntegerToken) Token(org.apache.cassandra.dht.Token) IPartitioner(org.apache.cassandra.dht.IPartitioner) VersionedValue(org.apache.cassandra.gms.VersionedValue) BigIntegerToken(org.apache.cassandra.dht.RandomPartitioner.BigIntegerToken) TokenMetadata(org.apache.cassandra.locator.TokenMetadata) RangesAtEndpoint(org.apache.cassandra.locator.RangesAtEndpoint) RangesByEndpoint(org.apache.cassandra.locator.RangesByEndpoint) HashMultimap(com.google.common.collect.HashMultimap) Multimap(com.google.common.collect.Multimap) AbstractReplicationStrategy(org.apache.cassandra.locator.AbstractReplicationStrategy) RangesByEndpoint(org.apache.cassandra.locator.RangesByEndpoint) Test(org.junit.Test)

Example 84 with TokenMetadata

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

the class MoveTest method testMoveWithPendingRangesForSimpleStrategyFourNode.

@Test
public void testMoveWithPendingRangesForSimpleStrategyFourNode() throws Exception {
    StorageService ss = StorageService.instance;
    final int RING_SIZE = 4;
    TokenMetadata tmd = ss.getTokenMetadata();
    tmd.clearUnsafe();
    VersionedValue.VersionedValueFactory valueFactory = new VersionedValue.VersionedValueFactory(partitioner);
    ArrayList<Token> endpointTokens = new ArrayList<>();
    ArrayList<Token> keyTokens = new ArrayList<>();
    List<InetAddressAndPort> hosts = new ArrayList<>();
    List<UUID> hostIds = new ArrayList<>();
    Util.createInitialRing(ss, partitioner, endpointTokens, keyTokens, hosts, hostIds, RING_SIZE);
    PendingRangeCalculatorService.instance.blockUntilFinished();
    // index of the moving node
    int MOVING_NODE = 0;
    moveHost(hosts.get(MOVING_NODE), 2, tmd, valueFactory);
    assertPendingRanges(tmd, generatePendingRanges(generatePendingMapEntry(0, 2, "127.0.0.1")), Simple_RF1_KeyspaceName);
    assertPendingRanges(tmd, generatePendingRanges(generatePendingMapEntry(0, 2, "127.0.0.1")), Simple_RF2_KeyspaceName);
    assertPendingRanges(tmd, generatePendingRanges(generatePendingMapEntry(0, 2, "127.0.0.1")), Simple_RF3_KeyspaceName);
    finishMove(hosts.get(MOVING_NODE), 2, tmd);
    moveHost(hosts.get(MOVING_NODE), 0, tmd, valueFactory);
    assertPendingRanges(tmd, generatePendingRanges(generatePendingMapEntry(0, 2, "127.0.0.2")), Simple_RF1_KeyspaceName);
    assertPendingRanges(tmd, generatePendingRanges(generatePendingMapEntry(0, 2, "127.0.0.3")), Simple_RF2_KeyspaceName);
    assertPendingRanges(tmd, generatePendingRanges(generatePendingMapEntry(0, 2, "127.0.0.4")), Simple_RF3_KeyspaceName);
    finishMove(hosts.get(MOVING_NODE), 0, tmd);
    moveHost(hosts.get(MOVING_NODE), 1500, tmd, valueFactory);
    assertPendingRanges(tmd, generatePendingRanges(generatePendingMapEntry(1500, 0, "127.0.0.2")), Simple_RF1_KeyspaceName);
    assertPendingRanges(tmd, generatePendingRanges(generatePendingMapEntry(1500, 0, "127.0.0.3")), Simple_RF2_KeyspaceName);
    assertPendingRanges(tmd, generatePendingRanges(generatePendingMapEntry(1500, 0, "127.0.0.4")), Simple_RF3_KeyspaceName);
    finishMove(hosts.get(MOVING_NODE), 1500, tmd);
    moveHost(hosts.get(MOVING_NODE), 0, tmd, valueFactory);
    assertPendingRanges(tmd, generatePendingRanges(generatePendingMapEntry(1500, 0, "127.0.0.1")), Simple_RF1_KeyspaceName);
    assertPendingRanges(tmd, generatePendingRanges(generatePendingMapEntry(1500, 0, "127.0.0.1")), Simple_RF2_KeyspaceName);
    assertPendingRanges(tmd, generatePendingRanges(generatePendingMapEntry(1500, 0, "127.0.0.1")), Simple_RF3_KeyspaceName);
    finishMove(hosts.get(MOVING_NODE), 0, tmd);
    moveHost(hosts.get(MOVING_NODE), 15, tmd, valueFactory);
    assertPendingRanges(tmd, generatePendingRanges(generatePendingMapEntry(10, 15, "127.0.0.1"), generatePendingMapEntry(30, 0, "127.0.0.2")), Simple_RF1_KeyspaceName);
    assertPendingRanges(tmd, generatePendingRanges(generatePendingMapEntry(20, 30, "127.0.0.2"), generatePendingMapEntry(10, 15, "127.0.0.1"), generatePendingMapEntry(0, 10, "127.0.0.1")), Simple_RF2_KeyspaceName);
    assertPendingRanges(tmd, generatePendingRanges(generatePendingMapEntry(15, 20, "127.0.0.2"), generatePendingMapEntry(0, 10, "127.0.0.1")), Simple_RF3_KeyspaceName);
    finishMove(hosts.get(MOVING_NODE), 15, tmd);
    moveHost(hosts.get(MOVING_NODE), 0, tmd, valueFactory);
    assertPendingRanges(tmd, generatePendingRanges(generatePendingMapEntry(30, 0, "127.0.0.1"), generatePendingMapEntry(10, 15, "127.0.0.3")), Simple_RF1_KeyspaceName);
    assertPendingRanges(tmd, generatePendingRanges(generatePendingMapEntry(20, 30, "127.0.0.1"), generatePendingMapEntry(10, 15, "127.0.0.4"), generatePendingMapEntry(0, 10, "127.0.0.3")), Simple_RF2_KeyspaceName);
    assertPendingRanges(tmd, generatePendingRanges(generatePendingMapEntry(15, 20, "127.0.0.1"), generatePendingMapEntry(0, 10, "127.0.0.4")), Simple_RF3_KeyspaceName);
    finishMove(hosts.get(MOVING_NODE), 0, tmd);
    moveHost(hosts.get(MOVING_NODE), 26, tmd, valueFactory);
    assertPendingRanges(tmd, generatePendingRanges(generatePendingMapEntry(20, 26, "127.0.0.1"), generatePendingMapEntry(30, 0, "127.0.0.2")), Simple_RF1_KeyspaceName);
    assertPendingRanges(tmd, generatePendingRanges(generatePendingMapEntry(26, 30, "127.0.0.2"), generatePendingMapEntry(30, 0, "127.0.0.3"), generatePendingMapEntry(10, 20, "127.0.0.1")), Simple_RF2_KeyspaceName);
    assertPendingRanges(tmd, generatePendingRanges(generatePendingMapEntry(0, 10, "127.0.0.1"), generatePendingMapEntry(26, 30, "127.0.0.3")), Simple_RF3_KeyspaceName);
    finishMove(hosts.get(MOVING_NODE), 26, tmd);
    moveHost(hosts.get(MOVING_NODE), 0, tmd, valueFactory);
    assertPendingRanges(tmd, generatePendingRanges(generatePendingMapEntry(20, 26, "127.0.0.4"), generatePendingMapEntry(30, 0, "127.0.0.1")), Simple_RF1_KeyspaceName);
    assertPendingRanges(tmd, generatePendingRanges(generatePendingMapEntry(30, 0, "127.0.0.1"), generatePendingMapEntry(26, 30, "127.0.0.1"), generatePendingMapEntry(10, 20, "127.0.0.4")), Simple_RF2_KeyspaceName);
    assertPendingRanges(tmd, generatePendingRanges(generatePendingMapEntry(26, 30, "127.0.0.1"), generatePendingMapEntry(0, 10, "127.0.0.4")), Simple_RF3_KeyspaceName);
    finishMove(hosts.get(MOVING_NODE), 0, tmd);
    MOVING_NODE = 3;
    moveHost(hosts.get(MOVING_NODE), 33, tmd, valueFactory);
    assertPendingRanges(tmd, generatePendingRanges(generatePendingMapEntry(30, 33, "127.0.0.4")), Simple_RF1_KeyspaceName);
    assertPendingRanges(tmd, generatePendingRanges(generatePendingMapEntry(30, 33, "127.0.0.4")), Simple_RF2_KeyspaceName);
    assertPendingRanges(tmd, generatePendingRanges(generatePendingMapEntry(30, 33, "127.0.0.4")), Simple_RF3_KeyspaceName);
    finishMove(hosts.get(MOVING_NODE), 33, tmd);
    moveHost(hosts.get(MOVING_NODE), 30, tmd, valueFactory);
    assertPendingRanges(tmd, generatePendingRanges(generatePendingMapEntry(30, 33, "127.0.0.1")), Simple_RF1_KeyspaceName);
    assertPendingRanges(tmd, generatePendingRanges(generatePendingMapEntry(30, 33, "127.0.0.2")), Simple_RF2_KeyspaceName);
    assertPendingRanges(tmd, generatePendingRanges(generatePendingMapEntry(30, 33, "127.0.0.3")), Simple_RF3_KeyspaceName);
    finishMove(hosts.get(MOVING_NODE), 30, tmd);
}
Also used : VersionedValue(org.apache.cassandra.gms.VersionedValue) InetAddressAndPort(org.apache.cassandra.locator.InetAddressAndPort) EndpointsForToken(org.apache.cassandra.locator.EndpointsForToken) BigIntegerToken(org.apache.cassandra.dht.RandomPartitioner.BigIntegerToken) Token(org.apache.cassandra.dht.Token) TokenMetadata(org.apache.cassandra.locator.TokenMetadata) RangesAtEndpoint(org.apache.cassandra.locator.RangesAtEndpoint) RangesByEndpoint(org.apache.cassandra.locator.RangesByEndpoint) Test(org.junit.Test)

Example 85 with TokenMetadata

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

the class MoveTest method testStateJumpToNormal.

@Test
public void testStateJumpToNormal() throws UnknownHostException {
    StorageService ss = StorageService.instance;
    TokenMetadata tmd = ss.getTokenMetadata();
    IPartitioner partitioner = RandomPartitioner.instance;
    VersionedValue.VersionedValueFactory valueFactory = new VersionedValue.VersionedValueFactory(partitioner);
    ArrayList<Token> endpointTokens = new ArrayList<Token>();
    ArrayList<Token> keyTokens = new ArrayList<Token>();
    List<InetAddressAndPort> hosts = new ArrayList<>();
    List<UUID> hostIds = new ArrayList<UUID>();
    // create a ring or 6 nodes
    Util.createInitialRing(ss, partitioner, endpointTokens, keyTokens, hosts, hostIds, 6);
    // node 2 leaves
    Token newToken = positionToken(7);
    ss.onChange(hosts.get(2), ApplicationState.STATUS, valueFactory.moving(newToken));
    assertTrue(tmd.isMoving(hosts.get(2)));
    assertEquals(endpointTokens.get(2), tmd.getToken(hosts.get(2)));
    // back to normal
    Gossiper.instance.injectApplicationState(hosts.get(2), ApplicationState.TOKENS, valueFactory.tokens(Collections.singleton(newToken)));
    ss.onChange(hosts.get(2), ApplicationState.STATUS, valueFactory.normal(Collections.singleton(newToken)));
    assertTrue(tmd.getSizeOfMovingEndpoints() == 0);
    assertEquals(newToken, tmd.getToken(hosts.get(2)));
    newToken = positionToken(8);
    // node 2 goes through leave and left and then jumps to normal at its new token
    ss.onChange(hosts.get(2), ApplicationState.STATUS, valueFactory.moving(newToken));
    Gossiper.instance.injectApplicationState(hosts.get(2), ApplicationState.TOKENS, valueFactory.tokens(Collections.singleton(newToken)));
    ss.onChange(hosts.get(2), ApplicationState.STATUS, valueFactory.normal(Collections.singleton(newToken)));
    assertTrue(tmd.getBootstrapTokens().isEmpty());
    assertTrue(tmd.getSizeOfMovingEndpoints() == 0);
    assertEquals(newToken, tmd.getToken(hosts.get(2)));
}
Also used : VersionedValue(org.apache.cassandra.gms.VersionedValue) InetAddressAndPort(org.apache.cassandra.locator.InetAddressAndPort) EndpointsForToken(org.apache.cassandra.locator.EndpointsForToken) BigIntegerToken(org.apache.cassandra.dht.RandomPartitioner.BigIntegerToken) Token(org.apache.cassandra.dht.Token) TokenMetadata(org.apache.cassandra.locator.TokenMetadata) IPartitioner(org.apache.cassandra.dht.IPartitioner) Test(org.junit.Test)

Aggregations

TokenMetadata (org.apache.cassandra.locator.TokenMetadata)109 Test (org.junit.Test)66 InetAddressAndPort (org.apache.cassandra.locator.InetAddressAndPort)43 Token (org.apache.cassandra.dht.Token)37 VersionedValue (org.apache.cassandra.gms.VersionedValue)22 InetAddress (java.net.InetAddress)21 Range (org.apache.cassandra.dht.Range)19 AbstractReplicationStrategy (org.apache.cassandra.locator.AbstractReplicationStrategy)19 StringToken (org.apache.cassandra.dht.OrderPreservingPartitioner.StringToken)13 BigIntegerToken (org.apache.cassandra.dht.RandomPartitioner.BigIntegerToken)13 Replica (org.apache.cassandra.locator.Replica)13 RangesAtEndpoint (org.apache.cassandra.locator.RangesAtEndpoint)11 IPartitioner (org.apache.cassandra.dht.IPartitioner)10 LongToken (org.apache.cassandra.dht.Murmur3Partitioner.LongToken)10 SSTableReader (org.apache.cassandra.io.sstable.format.SSTableReader)8 IEndpointSnitch (org.apache.cassandra.locator.IEndpointSnitch)8 RangesByEndpoint (org.apache.cassandra.locator.RangesByEndpoint)8 BeforeClass (org.junit.BeforeClass)8 Multimap (com.google.common.collect.Multimap)7 EndpointsForToken (org.apache.cassandra.locator.EndpointsForToken)7