Search in sources :

Example 36 with TokenMetadata

use of org.apache.cassandra.locator.TokenMetadata 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"), InetAddress.getByName("127.0.0.1"));
    metadata.updateNormalToken(new StringToken("C"), InetAddress.getByName("127.0.0.2"));
    // DC2
    metadata.updateNormalToken(new StringToken("B"), InetAddress.getByName("127.0.0.4"));
    metadata.updateNormalToken(new StringToken("D"), InetAddress.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");
    Keyspace.clear("Keyspace1");
    KeyspaceMetadata meta = KeyspaceMetadata.create("Keyspace1", KeyspaceParams.create(false, replicationMap));
    Schema.instance.load(meta);
    Optional<InetAddress> naturalEndpoint = ViewUtils.getViewNaturalEndpoint("Keyspace1", new StringToken("CA"), new StringToken("BB"));
    Assert.assertTrue(naturalEndpoint.isPresent());
    Assert.assertEquals(InetAddress.getByName("127.0.0.2"), naturalEndpoint.get());
}
Also used : NetworkTopologyStrategy(org.apache.cassandra.locator.NetworkTopologyStrategy) HashMap(java.util.HashMap) TokenMetadata(org.apache.cassandra.locator.TokenMetadata) KeyspaceMetadata(org.apache.cassandra.schema.KeyspaceMetadata) InetAddress(java.net.InetAddress) StringToken(org.apache.cassandra.dht.OrderPreservingPartitioner.StringToken) Test(org.junit.Test)

Example 37 with TokenMetadata

use of org.apache.cassandra.locator.TokenMetadata 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"), InetAddress.getByName("127.0.0.1"));
    metadata.updateNormalToken(new StringToken("C"), InetAddress.getByName("127.0.0.2"));
    // DC2
    metadata.updateNormalToken(new StringToken("B"), InetAddress.getByName("127.0.0.4"));
    metadata.updateNormalToken(new StringToken("D"), InetAddress.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");
    Keyspace.clear("Keyspace1");
    KeyspaceMetadata meta = KeyspaceMetadata.create("Keyspace1", KeyspaceParams.create(false, replicationMap));
    Schema.instance.load(meta);
    Optional<InetAddress> naturalEndpoint = ViewUtils.getViewNaturalEndpoint("Keyspace1", new StringToken("CA"), new StringToken("BB"));
    Assert.assertTrue(naturalEndpoint.isPresent());
    Assert.assertEquals(InetAddress.getByName("127.0.0.1"), naturalEndpoint.get());
}
Also used : NetworkTopologyStrategy(org.apache.cassandra.locator.NetworkTopologyStrategy) HashMap(java.util.HashMap) TokenMetadata(org.apache.cassandra.locator.TokenMetadata) KeyspaceMetadata(org.apache.cassandra.schema.KeyspaceMetadata) InetAddress(java.net.InetAddress) StringToken(org.apache.cassandra.dht.OrderPreservingPartitioner.StringToken) Test(org.junit.Test)

Example 38 with TokenMetadata

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

the class BootStrapperTest method testSourceTargetComputation.

private RangeStreamer testSourceTargetComputation(String keyspaceName, int numOldNodes, int replicationFactor) throws UnknownHostException {
    StorageService ss = StorageService.instance;
    TokenMetadata tmd = ss.getTokenMetadata();
    generateFakeEndpoints(numOldNodes);
    Token myToken = tmd.partitioner.getRandomToken();
    InetAddress myEndpoint = InetAddress.getByName("127.0.0.1");
    assertEquals(numOldNodes, tmd.sortedTokens().size());
    RangeStreamer s = new RangeStreamer(tmd, null, myEndpoint, "Bootstrap", true, DatabaseDescriptor.getEndpointSnitch(), new StreamStateStore(), false, 1);
    IFailureDetector mockFailureDetector = new IFailureDetector() {

        public boolean isAlive(InetAddress ep) {
            return true;
        }

        public void interpret(InetAddress ep) {
            throw new UnsupportedOperationException();
        }

        public void report(InetAddress ep) {
            throw new UnsupportedOperationException();
        }

        public void registerFailureDetectionEventListener(IFailureDetectionEventListener listener) {
            throw new UnsupportedOperationException();
        }

        public void unregisterFailureDetectionEventListener(IFailureDetectionEventListener listener) {
            throw new UnsupportedOperationException();
        }

        public void remove(InetAddress ep) {
            throw new UnsupportedOperationException();
        }

        public void forceConviction(InetAddress ep) {
            throw new UnsupportedOperationException();
        }
    };
    s.addSourceFilter(new RangeStreamer.FailureDetectorSourceFilter(mockFailureDetector));
    s.addRanges(keyspaceName, Keyspace.open(keyspaceName).getReplicationStrategy().getPendingAddressRanges(tmd, myToken, myEndpoint));
    Collection<Map.Entry<InetAddress, Collection<Range<Token>>>> toFetch = s.toFetch().get(keyspaceName);
    // Check we get get RF new ranges in total
    Set<Range<Token>> ranges = new HashSet<>();
    for (Map.Entry<InetAddress, Collection<Range<Token>>> e : toFetch) ranges.addAll(e.getValue());
    assertEquals(replicationFactor, ranges.size());
    // is used, they will vary.
    assert toFetch.iterator().next().getValue().size() > 0;
    assert !toFetch.iterator().next().getKey().equals(myEndpoint);
    return s;
}
Also used : TokenMetadata(org.apache.cassandra.locator.TokenMetadata) StorageService(org.apache.cassandra.service.StorageService) IFailureDetector(org.apache.cassandra.gms.IFailureDetector) Collection(java.util.Collection) IFailureDetectionEventListener(org.apache.cassandra.gms.IFailureDetectionEventListener) InetAddress(java.net.InetAddress) Map(java.util.Map) HashSet(java.util.HashSet)

Example 39 with TokenMetadata

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

the class BootStrapperTest method testAllocateTokens.

@Test
public void testAllocateTokens() throws UnknownHostException {
    int vn = 16;
    String ks = "BootStrapperTestKeyspace3";
    TokenMetadata tm = new TokenMetadata();
    generateFakeEndpoints(tm, 10, vn);
    InetAddress addr = FBUtilities.getBroadcastAddress();
    allocateTokensForNode(vn, ks, tm, addr);
}
Also used : TokenMetadata(org.apache.cassandra.locator.TokenMetadata) InetAddress(java.net.InetAddress) Test(org.junit.Test)

Example 40 with TokenMetadata

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

the class BootStrapperTest method testAllocateTokensNetworkStrategy.

public void testAllocateTokensNetworkStrategy(int rackCount, int replicas) throws UnknownHostException {
    IEndpointSnitch oldSnitch = DatabaseDescriptor.getEndpointSnitch();
    try {
        DatabaseDescriptor.setEndpointSnitch(new RackInferringSnitch());
        int vn = 16;
        String ks = "BootStrapperTestNTSKeyspace" + rackCount + replicas;
        String dc = "1";
        // Register peers with expected DC for NetworkTopologyStrategy.
        TokenMetadata metadata = StorageService.instance.getTokenMetadata();
        metadata.clearUnsafe();
        metadata.updateHostId(UUID.randomUUID(), InetAddress.getByName("127.1.0.99"));
        metadata.updateHostId(UUID.randomUUID(), InetAddress.getByName("127.15.0.99"));
        SchemaLoader.createKeyspace(ks, KeyspaceParams.nts(dc, replicas, "15", 15), SchemaLoader.standardCFMD(ks, "Standard1"));
        TokenMetadata tm = StorageService.instance.getTokenMetadata();
        tm.clearUnsafe();
        for (int i = 0; i < rackCount; ++i) generateFakeEndpoints(tm, 10, vn, dc, Integer.toString(i));
        InetAddress addr = InetAddress.getByName("127." + dc + ".0.99");
        allocateTokensForNode(vn, ks, tm, addr);
    // Note: Not matching replication factor in second datacentre, but this should not affect us.
    } finally {
        DatabaseDescriptor.setEndpointSnitch(oldSnitch);
    }
}
Also used : TokenMetadata(org.apache.cassandra.locator.TokenMetadata) InetAddress(java.net.InetAddress) IEndpointSnitch(org.apache.cassandra.locator.IEndpointSnitch) RackInferringSnitch(org.apache.cassandra.locator.RackInferringSnitch)

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