Search in sources :

Example 6 with IPartitioner

use of org.apache.cassandra.dht.IPartitioner 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 7 with IPartitioner

use of org.apache.cassandra.dht.IPartitioner 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 8 with IPartitioner

use of org.apache.cassandra.dht.IPartitioner in project cassandra by apache.

the class RepairOptionTest method testParseOptions.

@Test
public void testParseOptions() {
    IPartitioner partitioner = Murmur3Partitioner.instance;
    Token.TokenFactory tokenFactory = partitioner.getTokenFactory();
    // parse with empty options
    RepairOption option = RepairOption.parse(new HashMap<String, String>(), partitioner);
    if (FBUtilities.isWindows && (DatabaseDescriptor.getDiskAccessMode() != Config.DiskAccessMode.standard || DatabaseDescriptor.getIndexAccessMode() != Config.DiskAccessMode.standard))
        assertTrue(option.getParallelism() == RepairParallelism.PARALLEL);
    else
        assertTrue(option.getParallelism() == RepairParallelism.SEQUENTIAL);
    assertFalse(option.isPrimaryRange());
    assertFalse(option.isIncremental());
    // parse everything except hosts (hosts cannot be combined with data centers)
    Map<String, String> options = new HashMap<>();
    options.put(RepairOption.PARALLELISM_KEY, "parallel");
    options.put(RepairOption.PRIMARY_RANGE_KEY, "false");
    options.put(RepairOption.INCREMENTAL_KEY, "false");
    options.put(RepairOption.RANGES_KEY, "0:10,11:20,21:30");
    options.put(RepairOption.COLUMNFAMILIES_KEY, "cf1,cf2,cf3");
    options.put(RepairOption.DATACENTERS_KEY, "dc1,dc2,dc3");
    option = RepairOption.parse(options, partitioner);
    assertTrue(option.getParallelism() == RepairParallelism.PARALLEL);
    assertFalse(option.isPrimaryRange());
    assertFalse(option.isIncremental());
    Set<Range<Token>> expectedRanges = new HashSet<>(3);
    expectedRanges.add(new Range<>(tokenFactory.fromString("0"), tokenFactory.fromString("10")));
    expectedRanges.add(new Range<>(tokenFactory.fromString("11"), tokenFactory.fromString("20")));
    expectedRanges.add(new Range<>(tokenFactory.fromString("21"), tokenFactory.fromString("30")));
    assertEquals(expectedRanges, option.getRanges());
    Set<String> expectedCFs = new HashSet<>(3);
    expectedCFs.add("cf1");
    expectedCFs.add("cf2");
    expectedCFs.add("cf3");
    assertEquals(expectedCFs, option.getColumnFamilies());
    Set<String> expectedDCs = new HashSet<>(3);
    expectedDCs.add("dc1");
    expectedDCs.add("dc2");
    expectedDCs.add("dc3");
    assertEquals(expectedDCs, option.getDataCenters());
    // expect an error when parsing with hosts as well
    options.put(RepairOption.HOSTS_KEY, "127.0.0.1,127.0.0.2,127.0.0.3");
    assertParseThrowsIllegalArgumentExceptionWithMessage(options, "Cannot combine -dc and -hosts options");
    // remove data centers to proceed with testing parsing hosts
    options.remove(RepairOption.DATACENTERS_KEY);
    option = RepairOption.parse(options, partitioner);
    Set<String> expectedHosts = new HashSet<>(3);
    expectedHosts.add("127.0.0.1");
    expectedHosts.add("127.0.0.2");
    expectedHosts.add("127.0.0.3");
    assertEquals(expectedHosts, option.getHosts());
}
Also used : HashMap(java.util.HashMap) Token(org.apache.cassandra.dht.Token) JUnitMatchers.containsString(org.junit.matchers.JUnitMatchers.containsString) Range(org.apache.cassandra.dht.Range) IPartitioner(org.apache.cassandra.dht.IPartitioner) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 9 with IPartitioner

use of org.apache.cassandra.dht.IPartitioner in project cassandra by apache.

the class SerializationsTest method testValidationCompleteWrite.

private void testValidationCompleteWrite() throws IOException {
    IPartitioner p = RandomPartitioner.instance;
    MerkleTrees mt = new MerkleTrees(p);
    // empty validation
    mt.addMerkleTree((int) Math.pow(2, 15), FULL_RANGE);
    Validator v0 = new Validator(DESC, FBUtilities.getBroadcastAddress(), -1);
    ValidationComplete c0 = new ValidationComplete(DESC, mt);
    // validation with a tree
    mt = new MerkleTrees(p);
    mt.addMerkleTree(Integer.MAX_VALUE, FULL_RANGE);
    for (int i = 0; i < 10; i++) mt.split(p.getRandomToken());
    Validator v1 = new Validator(DESC, FBUtilities.getBroadcastAddress(), -1);
    ValidationComplete c1 = new ValidationComplete(DESC, mt);
    // validation failed
    ValidationComplete c3 = new ValidationComplete(DESC);
    testRepairMessageWrite("service.ValidationComplete.bin", c0, c1, c3);
}
Also used : MerkleTrees(org.apache.cassandra.utils.MerkleTrees) Validator(org.apache.cassandra.repair.Validator) IPartitioner(org.apache.cassandra.dht.IPartitioner)

Example 10 with IPartitioner

use of org.apache.cassandra.dht.IPartitioner in project cassandra by apache.

the class StreamingTransferTest method testRequestEmpty.

@Test
public void testRequestEmpty() throws Exception {
    // requesting empty data should succeed
    IPartitioner p = Util.testPartitioner();
    List<Range<Token>> ranges = new ArrayList<>();
    ranges.add(new Range<>(p.getMinimumToken(), p.getToken(ByteBufferUtil.bytes("key1"))));
    ranges.add(new Range<>(p.getToken(ByteBufferUtil.bytes("key2")), p.getMinimumToken()));
    StreamResultFuture futureResult = new StreamPlan("StreamingTransferTest").requestRanges(LOCAL, LOCAL, KEYSPACE2, ranges).execute();
    UUID planId = futureResult.planId;
    StreamState result = futureResult.get();
    assert planId.equals(result.planId);
    assert result.description.equals("StreamingTransferTest");
    // we should have completed session with empty transfer
    assert result.sessions.size() == 1;
    SessionInfo session = Iterables.get(result.sessions, 0);
    assert session.peer.equals(LOCAL);
    assert session.getTotalFilesReceived() == 0;
    assert session.getTotalFilesSent() == 0;
    assert session.getTotalSizeReceived() == 0;
    assert session.getTotalSizeSent() == 0;
}
Also used : Range(org.apache.cassandra.dht.Range) IPartitioner(org.apache.cassandra.dht.IPartitioner) Test(org.junit.Test)

Aggregations

IPartitioner (org.apache.cassandra.dht.IPartitioner)53 Token (org.apache.cassandra.dht.Token)27 Test (org.junit.Test)27 InetAddress (java.net.InetAddress)15 Range (org.apache.cassandra.dht.Range)14 TokenMetadata (org.apache.cassandra.locator.TokenMetadata)10 IdentityQueryFilter (org.apache.cassandra.db.columniterator.IdentityQueryFilter)9 BigIntegerToken (org.apache.cassandra.dht.RandomPartitioner.BigIntegerToken)9 VersionedValue (org.apache.cassandra.gms.VersionedValue)9 IOException (java.io.IOException)7 ArrayList (java.util.ArrayList)6 RandomPartitioner (org.apache.cassandra.dht.RandomPartitioner)6 IOError (java.io.IOError)4 ByteBuffer (java.nio.ByteBuffer)4 IFilter (org.apache.cassandra.db.filter.IFilter)3 QueryPath (org.apache.cassandra.db.filter.QueryPath)3 AbstractReplicationStrategy (org.apache.cassandra.locator.AbstractReplicationStrategy)3 IndexExpression (org.apache.cassandra.thrift.IndexExpression)3 HashMultimap (com.google.common.collect.HashMultimap)2 Multimap (com.google.common.collect.Multimap)2