Search in sources :

Example 56 with Cluster

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

the class MigrationCoordinatorTest method replaceNode.

/**
 * We shouldn't wait on versions only available from a node being replaced
 * see CASSANDRA-
 */
@Test
public void replaceNode() 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 replacementAddress = 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("cassandra.replace_address", replacementAddress.getHostAddress());
        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)

Example 57 with Cluster

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

the class NativeMixedVersionTest method v4ConnectionCleansUpThreadLocalState.

@Test
public void v4ConnectionCleansUpThreadLocalState() throws IOException {
    // make sure to limit the netty thread pool to size 1, this will make the test determanistic as all work
    // will happen on the single thread.
    System.setProperty("io.netty.eventLoopThreads", "1");
    try (Cluster cluster = Cluster.build(1).withConfig(c -> c.with(Feature.values()).set("track_warnings", ImmutableMap.of("enabled", true, "local_read_size", ImmutableMap.of("warn_threshold_kb", 1)))).start()) {
        init(cluster);
        cluster.schemaChange(withKeyspace("CREATE TABLE %s.tbl (pk int, ck1 int, value blob, PRIMARY KEY (pk, ck1))"));
        IInvokableInstance node = cluster.get(1);
        ByteBuffer blob = ByteBuffer.wrap("This is just some large string to get some number of bytes".getBytes(StandardCharsets.UTF_8));
        for (int i = 0; i < 100; i++) node.executeInternal(withKeyspace("INSERT INTO %s.tbl (pk, ck1, value) VALUES (?, ?, ?)"), 0, i, blob);
        // does not support warnings)
        try (com.datastax.driver.core.Cluster driver = JavaDriverUtils.create(cluster, ProtocolVersion.V5);
            Session session = driver.connect()) {
            ResultSet rs = session.execute(withKeyspace("SELECT * FROM %s.tbl"));
            Assertions.assertThat(rs.getExecutionInfo().getWarnings()).isNotEmpty();
        }
        try (com.datastax.driver.core.Cluster driver = JavaDriverUtils.create(cluster, ProtocolVersion.V3);
            Session session = driver.connect()) {
            ResultSet rs = session.execute(withKeyspace("SELECT * FROM %s.tbl"));
            Assertions.assertThat(rs.getExecutionInfo().getWarnings()).isEmpty();
        }
        // this should not happen; so make sure no logs are found
        List<String> result = node.logs().grep("Warnings present in message with version less than").getResult();
        Assertions.assertThat(result).isEmpty();
    } finally {
        System.clearProperty("io.netty.eventLoopThreads");
    }
}
Also used : Feature(org.apache.cassandra.distributed.api.Feature) ImmutableMap(com.google.common.collect.ImmutableMap) IOException(java.io.IOException) Test(org.junit.Test) ByteBuffer(java.nio.ByteBuffer) StandardCharsets(java.nio.charset.StandardCharsets) ProtocolVersion(com.datastax.driver.core.ProtocolVersion) List(java.util.List) ResultSet(com.datastax.driver.core.ResultSet) IInvokableInstance(org.apache.cassandra.distributed.api.IInvokableInstance) Session(com.datastax.driver.core.Session) Assertions(org.assertj.core.api.Assertions) Cluster(org.apache.cassandra.distributed.Cluster) IInvokableInstance(org.apache.cassandra.distributed.api.IInvokableInstance) ResultSet(com.datastax.driver.core.ResultSet) Cluster(org.apache.cassandra.distributed.Cluster) ByteBuffer(java.nio.ByteBuffer) Session(com.datastax.driver.core.Session) Test(org.junit.Test)

Example 58 with Cluster

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

the class NativeTransportEncryptionOptionsTest method optionalTlsConnectionAllowedWithKeystoreTest.

@Test
public void optionalTlsConnectionAllowedWithKeystoreTest() throws Throwable {
    try (Cluster cluster = builder().withNodes(1).withConfig(c -> {
        c.with(Feature.NATIVE_PROTOCOL);
        c.set("client_encryption_options", validKeystore);
    }).createWithoutStarting()) {
        InetAddress address = cluster.get(1).config().broadcastAddress().getAddress();
        int port = (int) cluster.get(1).config().get("native_transport_port");
        TlsConnection tlsConnection = new TlsConnection(address.getHostAddress(), port);
        tlsConnection.assertCannotConnect();
        cluster.startup();
        Assert.assertEquals("TLS native connection should be possible with keystore by default", ConnectResult.NEGOTIATED, tlsConnection.connect());
    }
}
Also used : InetAddress(java.net.InetAddress) Feature(org.apache.cassandra.distributed.api.Feature) ImmutableMap(com.google.common.collect.ImmutableMap) Test(org.junit.Test) Cluster(org.apache.cassandra.distributed.Cluster) Assert(org.junit.Assert) Collections(java.util.Collections) Cluster(org.apache.cassandra.distributed.Cluster) InetAddress(java.net.InetAddress) Test(org.junit.Test)

Example 59 with Cluster

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

the class NativeTransportEncryptionOptionsTest method optionalTlsConnectionAllowedToRegularPortTest.

@Test
public void optionalTlsConnectionAllowedToRegularPortTest() throws Throwable {
    try (Cluster cluster = builder().withNodes(1).withConfig(c -> {
        c.with(Feature.NATIVE_PROTOCOL);
        c.set("native_transport_port_ssl", 9043);
        c.set("client_encryption_options", ImmutableMap.builder().putAll(validKeystore).put("enabled", false).put("optional", true).build());
    }).createWithoutStarting()) {
        InetAddress address = cluster.get(1).config().broadcastAddress().getAddress();
        int unencrypted_port = (int) cluster.get(1).config().get("native_transport_port");
        int ssl_port = (int) cluster.get(1).config().get("native_transport_port_ssl");
        // Create the connections and prove they cannot connect before server start
        TlsConnection connectionToUnencryptedPort = new TlsConnection(address.getHostAddress(), unencrypted_port);
        connectionToUnencryptedPort.assertCannotConnect();
        TlsConnection connectionToEncryptedPort = new TlsConnection(address.getHostAddress(), ssl_port);
        connectionToEncryptedPort.assertCannotConnect();
        cluster.startup();
        Assert.assertEquals("TLS native connection should be possible to native_transport_port_ssl", ConnectResult.NEGOTIATED, connectionToEncryptedPort.connect());
        Assert.assertEquals("TLS native connection should not be possible on the regular port if an SSL port is specified", ConnectResult.FAILED_TO_NEGOTIATE, // but did connect
        connectionToUnencryptedPort.connect());
    }
}
Also used : InetAddress(java.net.InetAddress) Feature(org.apache.cassandra.distributed.api.Feature) ImmutableMap(com.google.common.collect.ImmutableMap) Test(org.junit.Test) Cluster(org.apache.cassandra.distributed.Cluster) Assert(org.junit.Assert) Collections(java.util.Collections) Cluster(org.apache.cassandra.distributed.Cluster) InetAddress(java.net.InetAddress) Test(org.junit.Test)

Example 60 with Cluster

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

the class PreviewRepairTest method testStartNonIntersectingPreviewRepair.

/**
 * Makes sure we can start a non-intersecting preview repair while there are other pending sstables on disk
 */
@Test
public void testStartNonIntersectingPreviewRepair() throws IOException, InterruptedException, ExecutionException {
    ExecutorService es = Executors.newSingleThreadExecutor();
    try (Cluster cluster = init(Cluster.build(2).withConfig(config -> config.with(GOSSIP).with(NETWORK)).start())) {
        cluster.schemaChange("create table " + KEYSPACE + ".tbl (id int primary key, t int)");
        insert(cluster.coordinator(1), 0, 100);
        cluster.forEach((node) -> node.flush(KEYSPACE));
        cluster.get(1).nodetoolResult("repair", KEYSPACE, "tbl").asserts().success();
        insert(cluster.coordinator(1), 100, 100);
        cluster.forEach((node) -> node.flush(KEYSPACE));
        // pause inc repair validation messages on node2 until node1 has finished
        Condition incRepairStarted = newOneTimeCondition();
        Condition continueIncRepair = newOneTimeCondition();
        DelayFirstRepairTypeMessageFilter filter = DelayFirstRepairTypeMessageFilter.validationRequest(incRepairStarted, continueIncRepair);
        cluster.filters().outbound().verbs(Verb.VALIDATION_REQ.id).from(1).to(2).messagesMatching(filter).drop();
        // get local ranges to repair two separate ranges:
        List<String> localRanges = cluster.get(1).callOnInstance(() -> {
            List<String> res = new ArrayList<>();
            for (Range<Token> r : StorageService.instance.getLocalReplicas(KEYSPACE).ranges()) res.add(r.left.getTokenValue() + ":" + r.right.getTokenValue());
            return res;
        });
        assertEquals(2, localRanges.size());
        String[] previewedRange = localRanges.get(0).split(":");
        String[] repairedRange = localRanges.get(1).split(":");
        Future<NodeToolResult> repairStatusFuture = es.submit(() -> cluster.get(1).nodetoolResult("repair", "-st", repairedRange[0], "-et", repairedRange[1], KEYSPACE, "tbl"));
        // wait for node1 to start validation compaction
        incRepairStarted.await();
        // now we have pending sstables in range "repairedRange", make sure we can preview "previewedRange"
        cluster.get(1).nodetoolResult("repair", "-vd", "-st", previewedRange[0], "-et", previewedRange[1], KEYSPACE, "tbl").asserts().success().notificationContains("Repaired data is in sync");
        continueIncRepair.signalAll();
        repairStatusFuture.get().asserts().success();
    } finally {
        es.shutdown();
    }
}
Also used : Condition.newOneTimeCondition(org.apache.cassandra.utils.concurrent.Condition.newOneTimeCondition) Condition(org.apache.cassandra.utils.concurrent.Condition) ExecutorService(java.util.concurrent.ExecutorService) ArrayList(java.util.ArrayList) Cluster(org.apache.cassandra.distributed.Cluster) Token(org.apache.cassandra.dht.Token) NodeToolResult(org.apache.cassandra.distributed.api.NodeToolResult) 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