Search in sources :

Example 6 with TokenMetadata

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

the class LeaveAndBootstrapTest method testStateJumpToBootstrap.

@Test
public void testStateJumpToBootstrap() throws UnknownHostException {
    StorageService ss = StorageService.instance;
    TokenMetadata tmd = ss.getTokenMetadata();
    tmd.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<InetAddress> hosts = new ArrayList<InetAddress>();
    List<UUID> hostIds = new ArrayList<UUID>();
    // create a ring or 5 nodes
    Util.createInitialRing(ss, partitioner, endpointTokens, keyTokens, hosts, hostIds, 7);
    // node 2 leaves
    ss.onChange(hosts.get(2), ApplicationState.STATUS, valueFactory.leaving(Collections.singleton(endpointTokens.get(2))));
    // don't bother to test pending ranges here, that is extensively tested by other
    // tests. Just check that the node is in appropriate lists.
    assertTrue(tmd.isMember(hosts.get(2)));
    assertTrue(tmd.isLeaving(hosts.get(2)));
    assertTrue(tmd.getBootstrapTokens().isEmpty());
    // Bootstrap the node immedidiately to keyTokens.get(4) without going through STATE_LEFT
    Gossiper.instance.injectApplicationState(hosts.get(2), ApplicationState.TOKENS, valueFactory.tokens(Collections.singleton(keyTokens.get(4))));
    ss.onChange(hosts.get(2), ApplicationState.STATUS, valueFactory.bootstrapping(Collections.<Token>singleton(keyTokens.get(4))));
    assertFalse(tmd.isMember(hosts.get(2)));
    assertFalse(tmd.isLeaving(hosts.get(2)));
    assertEquals(hosts.get(2), tmd.getBootstrapTokens().get(keyTokens.get(4)));
    // Bootstrap node hosts.get(3) to keyTokens.get(1)
    Gossiper.instance.injectApplicationState(hosts.get(3), ApplicationState.TOKENS, valueFactory.tokens(Collections.singleton(keyTokens.get(1))));
    ss.onChange(hosts.get(3), ApplicationState.STATUS, valueFactory.bootstrapping(Collections.<Token>singleton(keyTokens.get(1))));
    assertFalse(tmd.isMember(hosts.get(3)));
    assertFalse(tmd.isLeaving(hosts.get(3)));
    assertEquals(hosts.get(2), tmd.getBootstrapTokens().get(keyTokens.get(4)));
    assertEquals(hosts.get(3), tmd.getBootstrapTokens().get(keyTokens.get(1)));
    // Bootstrap node hosts.get(2) further to keyTokens.get(3)
    Gossiper.instance.injectApplicationState(hosts.get(2), ApplicationState.TOKENS, valueFactory.tokens(Collections.singleton(keyTokens.get(3))));
    ss.onChange(hosts.get(2), ApplicationState.STATUS, valueFactory.bootstrapping(Collections.<Token>singleton(keyTokens.get(3))));
    assertFalse(tmd.isMember(hosts.get(2)));
    assertFalse(tmd.isLeaving(hosts.get(2)));
    assertEquals(hosts.get(2), tmd.getBootstrapTokens().get(keyTokens.get(3)));
    assertNull(tmd.getBootstrapTokens().get(keyTokens.get(4)));
    assertEquals(hosts.get(3), tmd.getBootstrapTokens().get(keyTokens.get(1)));
    // Go to normal again for both nodes
    Gossiper.instance.injectApplicationState(hosts.get(3), ApplicationState.TOKENS, valueFactory.tokens(Collections.singleton(keyTokens.get(2))));
    Gossiper.instance.injectApplicationState(hosts.get(2), ApplicationState.TOKENS, valueFactory.tokens(Collections.singleton(keyTokens.get(3))));
    ss.onChange(hosts.get(2), ApplicationState.STATUS, valueFactory.normal(Collections.singleton(keyTokens.get(3))));
    ss.onChange(hosts.get(3), ApplicationState.STATUS, valueFactory.normal(Collections.singleton(keyTokens.get(2))));
    assertTrue(tmd.isMember(hosts.get(2)));
    assertFalse(tmd.isLeaving(hosts.get(2)));
    assertEquals(keyTokens.get(3), tmd.getToken(hosts.get(2)));
    assertTrue(tmd.isMember(hosts.get(3)));
    assertFalse(tmd.isLeaving(hosts.get(3)));
    assertEquals(keyTokens.get(2), tmd.getToken(hosts.get(3)));
    assertTrue(tmd.getBootstrapTokens().isEmpty());
}
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) IPartitioner(org.apache.cassandra.dht.IPartitioner) Test(org.junit.Test)

Example 7 with TokenMetadata

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

the class LeaveAndBootstrapTest method newTestWriteEndpointsDuringLeave.

/**
     * Test whether write endpoints is correct when the node is leaving. Uses
     * StorageService.onChange and does not manipulate token metadata directly.
     */
@Test
public void newTestWriteEndpointsDuringLeave() throws Exception {
    StorageService ss = StorageService.instance;
    final int RING_SIZE = 6;
    final int LEAVING_NODE = 3;
    TokenMetadata tmd = ss.getTokenMetadata();
    tmd.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<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 (String keyspaceName : Schema.instance.getNonLocalStrategyKeyspaces()) {
        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);
        }
    }
    // Third node leaves
    ss.onChange(hosts.get(LEAVING_NODE), ApplicationState.STATUS, valueFactory.leaving(Collections.singleton(endpointTokens.get(LEAVING_NODE))));
    assertTrue(tmd.isLeaving(hosts.get(LEAVING_NODE)));
    // because there is a tight race between submit and blockUntilFinished
    Thread.sleep(100);
    PendingRangeCalculatorService.instance.blockUntilFinished();
    AbstractReplicationStrategy strategy;
    for (String keyspaceName : Schema.instance.getNonLocalStrategyKeyspaces()) {
        strategy = getStrategy(keyspaceName, tmd);
        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));
            }
            // then we should expect it plus one extra for when it's gone
            if (expected.contains(hosts.get(LEAVING_NODE)))
                expected.add(expectedEndpoints.get(token).get(replicationFactor));
            assertEquals("mismatched endpoint sets", expected, actual);
        }
    }
}
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) AbstractReplicationStrategy(org.apache.cassandra.locator.AbstractReplicationStrategy) InetAddress(java.net.InetAddress) IPartitioner(org.apache.cassandra.dht.IPartitioner) Test(org.junit.Test)

Example 8 with TokenMetadata

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

the class LeaveAndBootstrapTest method testStateJumpToLeft.

@Test
public void testStateJumpToLeft() throws UnknownHostException {
    StorageService ss = StorageService.instance;
    TokenMetadata tmd = ss.getTokenMetadata();
    tmd.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<InetAddress> hosts = new ArrayList<InetAddress>();
    List<UUID> hostIds = new ArrayList<UUID>();
    // create a ring of 6 nodes
    Util.createInitialRing(ss, partitioner, endpointTokens, keyTokens, hosts, hostIds, 7);
    // node hosts.get(2) goes jumps to left
    ss.onChange(hosts.get(2), ApplicationState.STATUS, valueFactory.left(Collections.singleton(endpointTokens.get(2)), Gossiper.computeExpireTime()));
    assertFalse(tmd.isMember(hosts.get(2)));
    // node hosts.get(4) goes to bootstrap
    Gossiper.instance.injectApplicationState(hosts.get(3), ApplicationState.TOKENS, valueFactory.tokens(Collections.singleton(keyTokens.get(1))));
    ss.onChange(hosts.get(3), ApplicationState.STATUS, valueFactory.bootstrapping(Collections.<Token>singleton(keyTokens.get(1))));
    assertFalse(tmd.isMember(hosts.get(3)));
    assertEquals(1, tmd.getBootstrapTokens().size());
    assertEquals(hosts.get(3), tmd.getBootstrapTokens().get(keyTokens.get(1)));
    // and then directly to 'left'
    Gossiper.instance.injectApplicationState(hosts.get(2), ApplicationState.TOKENS, valueFactory.tokens(Collections.singleton(keyTokens.get(1))));
    ss.onChange(hosts.get(2), ApplicationState.STATUS, valueFactory.left(Collections.singleton(keyTokens.get(1)), Gossiper.computeExpireTime()));
    assertTrue(tmd.getBootstrapTokens().size() == 0);
    assertFalse(tmd.isMember(hosts.get(2)));
    assertFalse(tmd.isLeaving(hosts.get(2)));
}
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) IPartitioner(org.apache.cassandra.dht.IPartitioner) Test(org.junit.Test)

Example 9 with TokenMetadata

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

the class MoveTest method addNetworkTopologyKeyspace.

private static void addNetworkTopologyKeyspace(String keyspaceName, Integer... replicas) throws Exception {
    DatabaseDescriptor.setEndpointSnitch(new AbstractNetworkTopologySnitch() {

        //Odd IPs are in DC1 and Even are in DC2. Endpoints upto .14 will have unique racks and
        // then will be same for a set of three.
        @Override
        public String getRack(InetAddress endpoint) {
            int ipLastPart = getIPLastPart(endpoint);
            if (ipLastPart <= 14)
                return UUID.randomUUID().toString();
            else
                return "RAC" + (ipLastPart % 3);
        }

        @Override
        public String getDatacenter(InetAddress endpoint) {
            if (getIPLastPart(endpoint) % 2 == 0)
                return "DC2";
            else
                return "DC1";
        }

        private int getIPLastPart(InetAddress endpoint) {
            String str = endpoint.toString();
            int index = str.lastIndexOf(".");
            return Integer.parseInt(str.substring(index + 1).trim());
        }
    });
    final TokenMetadata tmd = StorageService.instance.getTokenMetadata();
    tmd.clearUnsafe();
    tmd.updateHostId(UUID.randomUUID(), InetAddress.getByName("127.0.0.1"));
    tmd.updateHostId(UUID.randomUUID(), InetAddress.getByName("127.0.0.2"));
    KeyspaceMetadata keyspace = KeyspaceMetadata.create(keyspaceName, KeyspaceParams.nts(configOptions(replicas)), Tables.of(TableMetadata.builder(keyspaceName, "CF1").addPartitionKeyColumn("key", BytesType.instance).build()));
    MigrationManager.announceNewKeyspace(keyspace);
}
Also used : TokenMetadata(org.apache.cassandra.locator.TokenMetadata) InetAddress(java.net.InetAddress) KeyspaceMetadata(org.apache.cassandra.schema.KeyspaceMetadata) AbstractNetworkTopologySnitch(org.apache.cassandra.locator.AbstractNetworkTopologySnitch)

Example 10 with TokenMetadata

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

the class MoveTest method testMoveWithPendingRangesNetworkStrategyRackAwareThirtyNodes.

@Test
public void testMoveWithPendingRangesNetworkStrategyRackAwareThirtyNodes() throws Exception {
    StorageService ss = StorageService.instance;
    final int RING_SIZE = 60;
    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();
    //Moving Endpoint 127.0.0.37 in RAC1 with current token 180
    int MOVING_NODE = 36;
    moveHost(hosts.get(MOVING_NODE), 215, tmd, valueFactory);
    assertPendingRanges(tmd, generatePendingRanges(generatePendingMapEntry(150, 151, "127.0.0.43"), generatePendingMapEntry(151, 160, "127.0.0.43"), generatePendingMapEntry(160, 161, "127.0.0.43"), generatePendingMapEntry(161, 170, "127.0.0.43"), generatePendingMapEntry(170, 171, "127.0.0.43"), generatePendingMapEntry(171, 180, "127.0.0.43"), generatePendingMapEntry(210, 211, "127.0.0.37"), generatePendingMapEntry(211, 215, "127.0.0.37")), Network_33_KeyspaceName);
    finishMove(hosts.get(MOVING_NODE), 215, tmd);
    //Moving it back to original spot
    moveHost(hosts.get(MOVING_NODE), 180, tmd, valueFactory);
    finishMove(hosts.get(MOVING_NODE), 180, 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)

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