Search in sources :

Example 11 with TokenMetadata

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

the class MoveTest method testMoveWithPendingRangesNetworkStrategyTenNode.

@Test
public void testMoveWithPendingRangesNetworkStrategyTenNode() throws Exception {
    StorageService ss = StorageService.instance;
    final int RING_SIZE = 14;
    TokenMetadata tmd = ss.getTokenMetadata();
    tmd.clearUnsafe();
    VersionedValue.VersionedValueFactory valueFactory = new VersionedValue.VersionedValueFactory(partitioner);
    ArrayList<Token> endpointTokens = new ArrayList<>();
    ArrayList<Token> keyTokens = new ArrayList<>();
    List<InetAddress> hosts = new ArrayList<>();
    List<UUID> hostIds = new ArrayList<>();
    for (int i = 0; i < RING_SIZE / 2; i++) {
        endpointTokens.add(new BigIntegerToken(String.valueOf(10 * i)));
        endpointTokens.add(new BigIntegerToken(String.valueOf((10 * i) + 1)));
    }
    Util.createInitialRing(ss, partitioner, endpointTokens, keyTokens, hosts, hostIds, RING_SIZE);
    PendingRangeCalculatorService.instance.blockUntilFinished();
    int MOVING_NODE = 0;
    moveHost(hosts.get(MOVING_NODE), 5, tmd, valueFactory);
    assertPendingRanges(tmd, generatePendingRanges(generatePendingMapEntry(0, 1, "127.0.0.1"), generatePendingMapEntry(1, 5, "127.0.0.1")), Network_11_KeyspaceName);
    assertPendingRanges(tmd, generatePendingRanges(generatePendingMapEntry(0, 1, "127.0.0.1"), generatePendingMapEntry(1, 5, "127.0.0.1")), Network_22_KeyspaceName);
    assertPendingRanges(tmd, generatePendingRanges(generatePendingMapEntry(0, 1, "127.0.0.1"), generatePendingMapEntry(1, 5, "127.0.0.1")), Network_33_KeyspaceName);
    finishMove(hosts.get(MOVING_NODE), 5, tmd);
    moveHost(hosts.get(MOVING_NODE), 0, tmd, valueFactory);
    assertPendingRanges(tmd, generatePendingRanges(generatePendingMapEntry(0, 1, "127.0.0.3"), generatePendingMapEntry(1, 5, "127.0.0.3")), Network_11_KeyspaceName);
    assertPendingRanges(tmd, generatePendingRanges(generatePendingMapEntry(0, 1, "127.0.0.5"), generatePendingMapEntry(1, 5, "127.0.0.5")), Network_22_KeyspaceName);
    assertPendingRanges(tmd, generatePendingRanges(generatePendingMapEntry(0, 1, "127.0.0.7"), generatePendingMapEntry(1, 5, "127.0.0.7")), Network_33_KeyspaceName);
    finishMove(hosts.get(MOVING_NODE), 0, tmd);
    MOVING_NODE = 1;
    moveHost(hosts.get(MOVING_NODE), 5, tmd, valueFactory);
    assertPendingRanges(tmd, generatePendingRanges(generatePendingMapEntry(1, 5, "127.0.0.2")), Network_11_KeyspaceName);
    assertPendingRanges(tmd, generatePendingRanges(generatePendingMapEntry(1, 5, "127.0.0.2")), Network_22_KeyspaceName);
    assertPendingRanges(tmd, generatePendingRanges(generatePendingMapEntry(1, 5, "127.0.0.2")), Network_33_KeyspaceName);
    finishMove(hosts.get(MOVING_NODE), 5, tmd);
    moveHost(hosts.get(MOVING_NODE), 1, tmd, valueFactory);
    assertPendingRanges(tmd, generatePendingRanges(generatePendingMapEntry(1, 5, "127.0.0.4")), Network_11_KeyspaceName);
    assertPendingRanges(tmd, generatePendingRanges(generatePendingMapEntry(1, 5, "127.0.0.6")), Network_22_KeyspaceName);
    assertPendingRanges(tmd, generatePendingRanges(generatePendingMapEntry(1, 5, "127.0.0.8")), Network_33_KeyspaceName);
    finishMove(hosts.get(MOVING_NODE), 1, tmd);
    MOVING_NODE = 3;
    moveHost(hosts.get(MOVING_NODE), 25, tmd, valueFactory);
    assertPendingRanges(tmd, generatePendingRanges(generatePendingMapEntry(1, 10, "127.0.0.6"), generatePendingMapEntry(10, 11, "127.0.0.6"), generatePendingMapEntry(21, 25, "127.0.0.4")), Network_11_KeyspaceName);
    assertPendingRanges(tmd, generatePendingRanges(generatePendingMapEntry(61, 0, "127.0.0.6"), generatePendingMapEntry(0, 1, "127.0.0.6"), generatePendingMapEntry(21, 25, "127.0.0.4"), generatePendingMapEntry(11, 20, "127.0.0.4"), generatePendingMapEntry(20, 21, "127.0.0.4")), Network_22_KeyspaceName);
    assertPendingRanges(tmd, generatePendingRanges(generatePendingMapEntry(51, 60, "127.0.0.6"), generatePendingMapEntry(60, 61, "127.0.0.6"), generatePendingMapEntry(21, 25, "127.0.0.4"), generatePendingMapEntry(11, 20, "127.0.0.4"), generatePendingMapEntry(20, 21, "127.0.0.4")), Network_33_KeyspaceName);
    finishMove(hosts.get(MOVING_NODE), 25, tmd);
    moveHost(hosts.get(MOVING_NODE), 11, tmd, valueFactory);
    assertPendingRanges(tmd, generatePendingRanges(generatePendingMapEntry(1, 10, "127.0.0.4"), generatePendingMapEntry(10, 11, "127.0.0.4"), generatePendingMapEntry(21, 25, "127.0.0.8")), Network_11_KeyspaceName);
    assertPendingRanges(tmd, generatePendingRanges(generatePendingMapEntry(61, 0, "127.0.0.4"), generatePendingMapEntry(0, 1, "127.0.0.4"), generatePendingMapEntry(11, 20, "127.0.0.8"), generatePendingMapEntry(20, 21, "127.0.0.8"), generatePendingMapEntry(21, 25, "127.0.0.10")), Network_22_KeyspaceName);
    assertPendingRanges(tmd, generatePendingRanges(generatePendingMapEntry(51, 60, "127.0.0.4"), generatePendingMapEntry(60, 61, "127.0.0.4"), generatePendingMapEntry(21, 25, "127.0.0.12"), generatePendingMapEntry(11, 20, "127.0.0.10"), generatePendingMapEntry(20, 21, "127.0.0.10")), Network_33_KeyspaceName);
    finishMove(hosts.get(MOVING_NODE), 11, tmd);
}
Also used : VersionedValue(org.apache.cassandra.gms.VersionedValue) BigIntegerToken(org.apache.cassandra.dht.RandomPartitioner.BigIntegerToken) BigIntegerToken(org.apache.cassandra.dht.RandomPartitioner.BigIntegerToken) Token(org.apache.cassandra.dht.Token) TokenMetadata(org.apache.cassandra.locator.TokenMetadata) InetAddress(java.net.InetAddress) Test(org.junit.Test)

Example 12 with TokenMetadata

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

the class MoveTest method testMoveWithPendingRangesSimpleStrategyTenNode.

@Test
public void testMoveWithPendingRangesSimpleStrategyTenNode() throws Exception {
    StorageService ss = StorageService.instance;
    final int RING_SIZE = 10;
    TokenMetadata tmd = ss.getTokenMetadata();
    tmd.clearUnsafe();
    VersionedValue.VersionedValueFactory valueFactory = new VersionedValue.VersionedValueFactory(partitioner);
    ArrayList<Token> endpointTokens = new ArrayList<>();
    ArrayList<Token> keyTokens = new ArrayList<>();
    List<InetAddress> 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
    final 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), 1000, tmd, valueFactory);
    assertPendingRanges(tmd, generatePendingRanges(generatePendingMapEntry(1000, 0, "127.0.0.2")), Simple_RF1_KeyspaceName);
    assertPendingRanges(tmd, generatePendingRanges(generatePendingMapEntry(1000, 0, "127.0.0.3")), Simple_RF2_KeyspaceName);
    assertPendingRanges(tmd, generatePendingRanges(generatePendingMapEntry(1000, 0, "127.0.0.4")), Simple_RF3_KeyspaceName);
    finishMove(hosts.get(MOVING_NODE), 1000, tmd);
    moveHost(hosts.get(MOVING_NODE), 0, tmd, valueFactory);
    assertPendingRanges(tmd, generatePendingRanges(generatePendingMapEntry(1000, 0, "127.0.0.1")), Simple_RF1_KeyspaceName);
    assertPendingRanges(tmd, generatePendingRanges(generatePendingMapEntry(1000, 0, "127.0.0.1")), Simple_RF2_KeyspaceName);
    assertPendingRanges(tmd, generatePendingRanges(generatePendingMapEntry(1000, 0, "127.0.0.1")), Simple_RF3_KeyspaceName);
    finishMove(hosts.get(MOVING_NODE), 0, tmd);
    moveHost(hosts.get(MOVING_NODE), 35, tmd, valueFactory);
    assertPendingRanges(tmd, generatePendingRanges(generatePendingMapEntry(30, 35, "127.0.0.1"), generatePendingMapEntry(90, 0, "127.0.0.2")), Simple_RF1_KeyspaceName);
    assertPendingRanges(tmd, generatePendingRanges(generatePendingMapEntry(30, 35, "127.0.0.1"), generatePendingMapEntry(20, 30, "127.0.0.1"), generatePendingMapEntry(80, 90, "127.0.0.2"), generatePendingMapEntry(90, 0, "127.0.0.3")), Simple_RF2_KeyspaceName);
    assertPendingRanges(tmd, generatePendingRanges(generatePendingMapEntry(30, 35, "127.0.0.1"), generatePendingMapEntry(20, 30, "127.0.0.1"), generatePendingMapEntry(80, 90, "127.0.0.3"), generatePendingMapEntry(90, 0, "127.0.0.4"), generatePendingMapEntry(10, 20, "127.0.0.1"), generatePendingMapEntry(70, 80, "127.0.0.2")), Simple_RF3_KeyspaceName);
    finishMove(hosts.get(MOVING_NODE), 35, tmd);
}
Also used : VersionedValue(org.apache.cassandra.gms.VersionedValue) BigIntegerToken(org.apache.cassandra.dht.RandomPartitioner.BigIntegerToken) Token(org.apache.cassandra.dht.Token) TokenMetadata(org.apache.cassandra.locator.TokenMetadata) InetAddress(java.net.InetAddress) Test(org.junit.Test)

Example 13 with TokenMetadata

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

the class MoveTest method newTestWriteEndpointsDuringMove.

/*
     * Test whether write endpoints is correct when the node is moving. Uses
     * StorageService.onChange and does not manipulate token metadata directly.
     */
@Test
public void newTestWriteEndpointsDuringMove() throws Exception {
    StorageService ss = StorageService.instance;
    final int RING_SIZE = 10;
    // index of the moving node
    final int MOVING_NODE = 3;
    TokenMetadata tmd = ss.getTokenMetadata();
    VersionedValue.VersionedValueFactory valueFactory = new VersionedValue.VersionedValueFactory(partitioner);
    ArrayList<Token> endpointTokens = new ArrayList<Token>();
    ArrayList<Token> keyTokens = new ArrayList<Token>();
    List<InetAddress> hosts = new ArrayList<InetAddress>();
    List<UUID> hostIds = new ArrayList<UUID>();
    Util.createInitialRing(ss, partitioner, endpointTokens, keyTokens, hosts, hostIds, RING_SIZE);
    Map<Token, List<InetAddress>> expectedEndpoints = new HashMap<Token, List<InetAddress>>();
    for (Token token : keyTokens) {
        List<InetAddress> endpoints = new ArrayList<InetAddress>();
        Iterator<Token> tokenIter = TokenMetadata.ringIterator(tmd.sortedTokens(), token, false);
        while (tokenIter.hasNext()) {
            endpoints.add(tmd.getEndpoint(tokenIter.next()));
        }
        expectedEndpoints.put(token, endpoints);
    }
    // node LEAVING_NODE should move to this token
    Token newToken = positionToken(MOVING_NODE);
    // Third node leaves
    ss.onChange(hosts.get(MOVING_NODE), ApplicationState.STATUS, valueFactory.moving(newToken));
    PendingRangeCalculatorService.instance.blockUntilFinished();
    assertTrue(tmd.isMoving(hosts.get(MOVING_NODE)));
    AbstractReplicationStrategy strategy;
    for (String keyspaceName : Schema.instance.getNonLocalStrategyKeyspaces()) {
        strategy = getStrategy(keyspaceName, tmd);
        if (strategy instanceof NetworkTopologyStrategy)
            continue;
        int numMoved = 0;
        for (Token token : keyTokens) {
            int replicationFactor = strategy.getReplicationFactor();
            HashSet<InetAddress> actual = new HashSet<InetAddress>(tmd.getWriteEndpoints(token, keyspaceName, strategy.calculateNaturalEndpoints(token, tmd.cloneOnlyTokenMap())));
            HashSet<InetAddress> expected = new HashSet<InetAddress>();
            for (int i = 0; i < replicationFactor; i++) {
                expected.add(expectedEndpoints.get(token).get(i));
            }
            if (expected.size() == actual.size()) {
                assertEquals("mismatched endpoint sets", expected, actual);
            } else {
                expected.add(hosts.get(MOVING_NODE));
                assertEquals("mismatched endpoint sets", expected, actual);
                numMoved++;
            }
        }
        assertEquals("mismatched number of moved token", 1, numMoved);
    }
    // moving endpoint back to the normal state
    ss.onChange(hosts.get(MOVING_NODE), ApplicationState.STATUS, valueFactory.normal(Collections.singleton(newToken)));
}
Also used : VersionedValue(org.apache.cassandra.gms.VersionedValue) BigIntegerToken(org.apache.cassandra.dht.RandomPartitioner.BigIntegerToken) Token(org.apache.cassandra.dht.Token) TokenMetadata(org.apache.cassandra.locator.TokenMetadata) NetworkTopologyStrategy(org.apache.cassandra.locator.NetworkTopologyStrategy) AbstractReplicationStrategy(org.apache.cassandra.locator.AbstractReplicationStrategy) InetAddress(java.net.InetAddress) Test(org.junit.Test)

Example 14 with TokenMetadata

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

the class ActiveRepairServiceTest method testGetNeighborsTimesTwoInLocalDC.

@Test
public void testGetNeighborsTimesTwoInLocalDC() throws Throwable {
    TokenMetadata tmd = StorageService.instance.getTokenMetadata();
    // generate rf*2 nodes, and ensure that only neighbors specified by the ARS are returned
    addTokens(2 * Keyspace.open(KEYSPACE5).getReplicationStrategy().getReplicationFactor());
    AbstractReplicationStrategy ars = Keyspace.open(KEYSPACE5).getReplicationStrategy();
    Set<InetAddress> expected = new HashSet<>();
    for (Range<Token> replicaRange : ars.getAddressRanges().get(FBUtilities.getBroadcastAddress())) {
        expected.addAll(ars.getRangeAddresses(tmd.cloneOnlyTokenMap()).get(replicaRange));
    }
    expected.remove(FBUtilities.getBroadcastAddress());
    // remove remote endpoints
    TokenMetadata.Topology topology = tmd.cloneOnlyTokenMap().getTopology();
    HashSet<InetAddress> localEndpoints = Sets.newHashSet(topology.getDatacenterEndpoints().get(DatabaseDescriptor.getLocalDataCenter()));
    expected = Sets.intersection(expected, localEndpoints);
    Collection<Range<Token>> ranges = StorageService.instance.getLocalRanges(KEYSPACE5);
    Set<InetAddress> neighbors = new HashSet<>();
    for (Range<Token> range : ranges) {
        neighbors.addAll(ActiveRepairService.getNeighbors(KEYSPACE5, ranges, range, Arrays.asList(DatabaseDescriptor.getLocalDataCenter()), null));
    }
    assertEquals(expected, neighbors);
}
Also used : AbstractReplicationStrategy(org.apache.cassandra.locator.AbstractReplicationStrategy) Token(org.apache.cassandra.dht.Token) TokenMetadata(org.apache.cassandra.locator.TokenMetadata) Range(org.apache.cassandra.dht.Range) InetAddress(java.net.InetAddress) Test(org.junit.Test)

Example 15 with TokenMetadata

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

the class ActiveRepairServiceTest method testGetNeighborsTimesTwoInSpecifiedHosts.

@Test
public void testGetNeighborsTimesTwoInSpecifiedHosts() throws Throwable {
    TokenMetadata tmd = StorageService.instance.getTokenMetadata();
    // generate rf*2 nodes, and ensure that only neighbors specified by the hosts are returned
    addTokens(2 * Keyspace.open(KEYSPACE5).getReplicationStrategy().getReplicationFactor());
    AbstractReplicationStrategy ars = Keyspace.open(KEYSPACE5).getReplicationStrategy();
    List<InetAddress> expected = new ArrayList<>();
    for (Range<Token> replicaRange : ars.getAddressRanges().get(FBUtilities.getBroadcastAddress())) {
        expected.addAll(ars.getRangeAddresses(tmd.cloneOnlyTokenMap()).get(replicaRange));
    }
    expected.remove(FBUtilities.getBroadcastAddress());
    Collection<String> hosts = Arrays.asList(FBUtilities.getBroadcastAddress().getCanonicalHostName(), expected.get(0).getCanonicalHostName());
    Collection<Range<Token>> ranges = StorageService.instance.getLocalRanges(KEYSPACE5);
    assertEquals(expected.get(0), ActiveRepairService.getNeighbors(KEYSPACE5, ranges, ranges.iterator().next(), null, hosts).iterator().next());
}
Also used : AbstractReplicationStrategy(org.apache.cassandra.locator.AbstractReplicationStrategy) Token(org.apache.cassandra.dht.Token) TokenMetadata(org.apache.cassandra.locator.TokenMetadata) Range(org.apache.cassandra.dht.Range) InetAddress(java.net.InetAddress) Test(org.junit.Test)

Aggregations

TokenMetadata (org.apache.cassandra.locator.TokenMetadata)72 InetAddress (java.net.InetAddress)50 Test (org.junit.Test)50 Token (org.apache.cassandra.dht.Token)27 VersionedValue (org.apache.cassandra.gms.VersionedValue)22 Range (org.apache.cassandra.dht.Range)16 AbstractReplicationStrategy (org.apache.cassandra.locator.AbstractReplicationStrategy)14 BigIntegerToken (org.apache.cassandra.dht.RandomPartitioner.BigIntegerToken)13 StringToken (org.apache.cassandra.dht.OrderPreservingPartitioner.StringToken)12 KeyspaceMetadata (org.apache.cassandra.schema.KeyspaceMetadata)12 IPartitioner (org.apache.cassandra.dht.IPartitioner)10 LongToken (org.apache.cassandra.dht.Murmur3Partitioner.LongToken)7 HashMap (java.util.HashMap)5 HashMultimap (com.google.common.collect.HashMultimap)4 Multimap (com.google.common.collect.Multimap)4 BytesToken (org.apache.cassandra.dht.ByteOrderedPartitioner.BytesToken)4 NetworkTopologyStrategy (org.apache.cassandra.locator.NetworkTopologyStrategy)4 StorageService (org.apache.cassandra.service.StorageService)4 UUID (java.util.UUID)3 Before (org.junit.Before)3