Search in sources :

Example 51 with Cluster

use of org.apache.cassandra.distributed.Cluster in project cassandra by apache.

the class UnableToParseClientMessageFromBlockedSubnetTest method getCluster.

private Cluster getCluster() {
    if (CLUSTER == null || CLUSTER_EXCLUDED_SUBNETS != excludeSubnets) {
        if (CLUSTER != null) {
            CLUSTER.close();
            CLUSTER = null;
        }
        try {
            CLUSTER = init(Cluster.build(1).withConfig(c -> c.with(Feature.values()).set("client_error_reporting_exclusions", ImmutableMap.of("subnets", excludeSubnets))).start());
            CLUSTER_EXCLUDED_SUBNETS = excludeSubnets;
        } catch (IOException e) {
            throw new UncheckedIOException(e);
        }
    }
    return CLUSTER;
}
Also used : Arrays(java.util.Arrays) AfterClass(org.junit.AfterClass) Feature(org.apache.cassandra.distributed.api.Feature) BeforeClass(org.junit.BeforeClass) LogAction(org.apache.cassandra.distributed.api.LogAction) ImmutableMap(com.google.common.collect.ImmutableMap) RunWith(org.junit.runner.RunWith) TimeoutException(java.util.concurrent.TimeoutException) IOException(java.io.IOException) Test(org.junit.Test) ArrayList(java.util.ArrayList) UncheckedIOException(java.io.UncheckedIOException) List(java.util.List) IInvokableInstance(org.apache.cassandra.distributed.api.IInvokableInstance) SimpleClient(org.apache.cassandra.transport.SimpleClient) ErrorMessage(org.apache.cassandra.transport.messages.ErrorMessage) Message(org.apache.cassandra.transport.Message) Assertions(org.assertj.core.api.Assertions) Cluster(org.apache.cassandra.distributed.Cluster) ProtocolVersion(org.apache.cassandra.transport.ProtocolVersion) Collections(java.util.Collections) DatabaseDescriptor(org.apache.cassandra.config.DatabaseDescriptor) Parameterized(org.junit.runners.Parameterized) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException)

Example 52 with Cluster

use of org.apache.cassandra.distributed.Cluster in project cassandra by apache.

the class ResourceLeakTest method doTest.

void doTest(int numClusterNodes, Consumer<IInstanceConfig> updater) throws Throwable {
    for (int loop = 0; loop < numTestLoops; loop++) {
        System.out.println(String.format("========== Starting loop %03d ========", loop));
        try (Cluster cluster = (Cluster) builder().withNodes(numClusterNodes).withConfig(updater).start()) {
            init(cluster);
            String tableName = "tbl" + loop;
            cluster.schemaChange("CREATE TABLE " + KEYSPACE + "." + tableName + " (pk int, ck int, v int, PRIMARY KEY (pk, ck))");
            cluster.coordinator(1).execute("INSERT INTO " + KEYSPACE + "." + tableName + "(pk,ck,v) VALUES (0,0,0)", ConsistencyLevel.ALL);
            cluster.get(1).flush(KEYSPACE);
            if (dumpEveryLoop) {
                dumpResources(String.format("loop%03d", loop));
            }
        } catch (Throwable tr) {
            System.out.println("Dumping resources for exception: " + tr.getMessage());
            tr.printStackTrace();
            dumpResources("exception");
        }
        if (forceCollection) {
            System.runFinalization();
            System.gc();
        }
        System.out.println(String.format("========== Completed loop %03d ========", loop));
    }
}
Also used : Cluster(org.apache.cassandra.distributed.Cluster)

Example 53 with Cluster

use of org.apache.cassandra.distributed.Cluster in project cassandra by apache.

the class SchemaDisagreementTest method writeWithInconsequentialSchemaDisagreement.

/**
 * If a node isn't aware of a column, but receives a mutation without that column, the write should succeed.
 */
@Test
public void writeWithInconsequentialSchemaDisagreement() throws Throwable {
    try (Cluster cluster = init(builder().withNodes(3).withConfig(config -> config.with(NETWORK)).start())) {
        cluster.schemaChange(withKeyspace("CREATE TABLE %s.tbl (pk int, ck int, v1 int, PRIMARY KEY (pk, ck))"));
        cluster.get(1).executeInternal(withKeyspace("INSERT INTO %s.tbl (pk, ck, v1) VALUES (1, 1, 1)"));
        cluster.get(2).executeInternal(withKeyspace("INSERT INTO %s.tbl (pk, ck, v1) VALUES (1, 1, 1)"));
        cluster.get(3).executeInternal(withKeyspace("INSERT INTO %s.tbl (pk, ck, v1) VALUES (1, 1, 1)"));
        // Introduce schema disagreement
        cluster.schemaChange(withKeyspace("ALTER TABLE %s.tbl ADD v2 int"), 1);
        // this write shouldn't cause any problems because it doesn't write to the new column
        cluster.coordinator(1).execute(withKeyspace("INSERT INTO %s.tbl (pk, ck, v1) VALUES (2, 2, 2)"), ALL);
    }
}
Also used : Cluster(org.apache.cassandra.distributed.Cluster) Test(org.junit.Test)

Example 54 with Cluster

use of org.apache.cassandra.distributed.Cluster in project cassandra by apache.

the class MessageForwardingTest method mutationsForwardedToAllReplicasTest.

@Test
public void mutationsForwardedToAllReplicasTest() {
    String originalTraceTimeout = TracingUtil.setWaitForTracingEventTimeoutSecs("1");
    final int numInserts = 100;
    Map<InetAddress, Integer> forwardFromCounts = new HashMap<>();
    Map<InetAddress, Integer> commitCounts = new HashMap<>();
    try (Cluster cluster = (Cluster) init(builder().withDC("dc0", 1).withDC("dc1", 3).start())) {
        cluster.schemaChange("CREATE TABLE " + KEYSPACE + ".tbl (pk int, ck int, v text, PRIMARY KEY (pk, ck))");
        cluster.forEach(instance -> commitCounts.put(instance.broadcastAddress().getAddress(), 0));
        final UUID sessionId = UUIDGen.getTimeUUID();
        Stream<Future<Object[][]>> inserts = IntStream.range(0, numInserts).mapToObj((idx) -> {
            return cluster.coordinator(1).asyncExecuteWithTracing(sessionId, "INSERT INTO " + KEYSPACE + ".tbl(pk,ck,v) VALUES (1, 1, 'x')", ConsistencyLevel.ALL);
        });
        // Wait for each of the futures to complete before checking the traces, don't care
        // about the result so
        // noinspection ResultOfMethodCallIgnored
        inserts.map(IsolatedExecutor::waitOn).collect(Collectors.toList());
        cluster.stream("dc1").forEach(instance -> forwardFromCounts.put(instance.broadcastAddress().getAddress(), 0));
        cluster.forEach(instance -> commitCounts.put(instance.broadcastAddress().getAddress(), 0));
        List<TracingUtil.TraceEntry> traces = TracingUtil.getTrace(cluster, sessionId, ConsistencyLevel.ALL);
        traces.forEach(traceEntry -> {
            if (traceEntry.activity.contains("Appending to commitlog")) {
                commitCounts.compute(traceEntry.source, (k, v) -> (v != null ? v : 0) + 1);
            } else if (traceEntry.activity.contains("Enqueuing forwarded write to ")) {
                forwardFromCounts.compute(traceEntry.source, (k, v) -> (v != null ? v : 0) + 1);
            }
        });
        // Check that each node in dc1 was the forwarder at least once.  There is a (1/3)^numInserts chance
        // that the same node will be picked, but the odds of that are ~2e-48.
        forwardFromCounts.forEach((source, count) -> Assert.assertTrue(source + " should have been randomized to forward messages", count > 0));
        // Check that each node received the forwarded messages once (and only once)
        commitCounts.forEach((source, count) -> Assert.assertEquals(source + " appending to commitlog traces", (long) numInserts, (long) count));
    } catch (IOException e) {
        Assert.fail("Threw exception: " + e);
    } finally {
        TracingUtil.setWaitForTracingEventTimeoutSecs(originalTraceTimeout);
    }
}
Also used : IntStream(java.util.stream.IntStream) IsolatedExecutor(org.apache.cassandra.distributed.impl.IsolatedExecutor) IOException(java.io.IOException) HashMap(java.util.HashMap) Test(org.junit.Test) UUID(java.util.UUID) ConsistencyLevel(org.apache.cassandra.distributed.api.ConsistencyLevel) Collectors(java.util.stream.Collectors) UUIDGen(org.apache.cassandra.utils.UUIDGen) InetAddress(java.net.InetAddress) List(java.util.List) Future(java.util.concurrent.Future) Stream(java.util.stream.Stream) Map(java.util.Map) Cluster(org.apache.cassandra.distributed.Cluster) Assert(org.junit.Assert) TracingUtil(org.apache.cassandra.distributed.impl.TracingUtil) HashMap(java.util.HashMap) Cluster(org.apache.cassandra.distributed.Cluster) IOException(java.io.IOException) Future(java.util.concurrent.Future) UUID(java.util.UUID) InetAddress(java.net.InetAddress) Test(org.junit.Test)

Example 55 with Cluster

use of org.apache.cassandra.distributed.Cluster in project cassandra by apache.

the class MigrationCoordinatorTest method explicitEndpointIgnore.

@Test
public void explicitEndpointIgnore() throws Throwable {
    try (Cluster cluster = Cluster.build(2).withTokenSupplier(TokenSupplier.evenlyDistributedTokens(3)).withNodeIdTopology(NetworkTopology.singleDcNetworkTopology(3, "dc0", "rack0")).withConfig(config -> config.with(NETWORK, GOSSIP)).start()) {
        cluster.schemaChange("CREATE KEYSPACE ks with replication={'class':'SimpleStrategy', 'replication_factor':2}");
        InetAddress ignoredEndpoint = cluster.get(2).broadcastAddress().getAddress();
        cluster.get(2).shutdown(false);
        cluster.schemaChangeIgnoringStoppedInstances("CREATE TABLE ks.tbl (k int primary key, v int)");
        IInstanceConfig config = cluster.newInstanceConfig();
        config.set("auto_bootstrap", true);
        System.setProperty(MigrationCoordinator.IGNORED_ENDPOINTS_PROP, ignoredEndpoint.getHostAddress());
        System.setProperty("cassandra.consistent.rangemovement", "false");
        cluster.bootstrap(config).startup();
    }
}
Also used : InetAddress(java.net.InetAddress) TokenSupplier(org.apache.cassandra.distributed.api.TokenSupplier) Test(org.junit.Test) Cluster(org.apache.cassandra.distributed.Cluster) UUID(java.util.UUID) MigrationCoordinator(org.apache.cassandra.schema.MigrationCoordinator) IInstanceConfig(org.apache.cassandra.distributed.api.IInstanceConfig) NetworkTopology(org.apache.cassandra.distributed.shared.NetworkTopology) NETWORK(org.apache.cassandra.distributed.api.Feature.NETWORK) Before(org.junit.Before) Schema(org.apache.cassandra.schema.Schema) GOSSIP(org.apache.cassandra.distributed.api.Feature.GOSSIP) IInstanceConfig(org.apache.cassandra.distributed.api.IInstanceConfig) Cluster(org.apache.cassandra.distributed.Cluster) InetAddress(java.net.InetAddress) Test(org.junit.Test)

Aggregations

Cluster (org.apache.cassandra.distributed.Cluster)161 Test (org.junit.Test)151 IInvokableInstance (org.apache.cassandra.distributed.api.IInvokableInstance)37 Assert (org.junit.Assert)37 IOException (java.io.IOException)36 Feature (org.apache.cassandra.distributed.api.Feature)34 GOSSIP (org.apache.cassandra.distributed.api.Feature.GOSSIP)30 NETWORK (org.apache.cassandra.distributed.api.Feature.NETWORK)30 ConsistencyLevel (org.apache.cassandra.distributed.api.ConsistencyLevel)29 List (java.util.List)22 ImmutableMap (com.google.common.collect.ImmutableMap)21 InetAddress (java.net.InetAddress)20 TokenSupplier (org.apache.cassandra.distributed.api.TokenSupplier)20 StorageService (org.apache.cassandra.service.StorageService)18 Arrays (java.util.Arrays)17 Collections (java.util.Collections)17 Assertions (org.assertj.core.api.Assertions)17 Map (java.util.Map)16 TestBaseImpl (org.apache.cassandra.distributed.test.TestBaseImpl)15 ICoordinator (org.apache.cassandra.distributed.api.ICoordinator)14