Search in sources :

Example 96 with Cluster

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

the class BootstrapTest method readWriteDuringBootstrapTest.

@Test
public void readWriteDuringBootstrapTest() throws Throwable {
    int originalNodeCount = 2;
    int expandedNodeCount = originalNodeCount + 1;
    try (Cluster cluster = builder().withNodes(originalNodeCount).withTokenSupplier(TokenSupplier.evenlyDistributedTokens(expandedNodeCount)).withNodeIdTopology(NetworkTopology.singleDcNetworkTopology(expandedNodeCount, "dc0", "rack0")).withConfig(config -> config.with(NETWORK, GOSSIP)).start()) {
        IInstanceConfig config = cluster.newInstanceConfig();
        IInvokableInstance newInstance = cluster.bootstrap(config);
        withProperty("cassandra.join_ring", false, () -> newInstance.startup(cluster));
        cluster.forEach(statusToBootstrap(newInstance));
        populate(cluster, 0, 100);
        Assert.assertEquals(100, newInstance.executeInternal("SELECT *FROM " + KEYSPACE + ".tbl").length);
    }
}
Also used : IntStream(java.util.stream.IntStream) ICluster(org.apache.cassandra.distributed.api.ICluster) Test(org.junit.Test) ConsistencyLevel(org.apache.cassandra.distributed.api.ConsistencyLevel) Collectors(java.util.stream.Collectors) GossipHelper.bootstrap(org.apache.cassandra.distributed.action.GossipHelper.bootstrap) TokenSupplier(org.apache.cassandra.distributed.api.TokenSupplier) IInvokableInstance(org.apache.cassandra.distributed.api.IInvokableInstance) GossipHelper.pullSchemaFrom(org.apache.cassandra.distributed.action.GossipHelper.pullSchemaFrom) Arrays.asList(java.util.Arrays.asList) Map(java.util.Map) TestBaseImpl(org.apache.cassandra.distributed.test.TestBaseImpl) Cluster(org.apache.cassandra.distributed.Cluster) GossipHelper.withProperty(org.apache.cassandra.distributed.action.GossipHelper.withProperty) Assert(org.junit.Assert) IInstanceConfig(org.apache.cassandra.distributed.api.IInstanceConfig) GossipHelper.statusToBootstrap(org.apache.cassandra.distributed.action.GossipHelper.statusToBootstrap) NetworkTopology(org.apache.cassandra.distributed.shared.NetworkTopology) NETWORK(org.apache.cassandra.distributed.api.Feature.NETWORK) GOSSIP(org.apache.cassandra.distributed.api.Feature.GOSSIP) IInvokableInstance(org.apache.cassandra.distributed.api.IInvokableInstance) IInstanceConfig(org.apache.cassandra.distributed.api.IInstanceConfig) ICluster(org.apache.cassandra.distributed.api.ICluster) Cluster(org.apache.cassandra.distributed.Cluster) Test(org.junit.Test)

Example 97 with Cluster

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

the class BootstrapTest method bootstrapTest.

@Test
public void bootstrapTest() throws Throwable {
    int originalNodeCount = 2;
    int expandedNodeCount = originalNodeCount + 1;
    try (Cluster cluster = builder().withNodes(originalNodeCount).withTokenSupplier(TokenSupplier.evenlyDistributedTokens(expandedNodeCount)).withNodeIdTopology(NetworkTopology.singleDcNetworkTopology(expandedNodeCount, "dc0", "rack0")).withConfig(config -> config.with(NETWORK, GOSSIP)).start()) {
        populate(cluster, 0, 100);
        IInstanceConfig config = cluster.newInstanceConfig();
        IInvokableInstance newInstance = cluster.bootstrap(config);
        withProperty("cassandra.join_ring", false, () -> newInstance.startup(cluster));
        cluster.forEach(statusToBootstrap(newInstance));
        cluster.run(asList(pullSchemaFrom(cluster.get(1)), bootstrap()), newInstance.config().num());
        for (Map.Entry<Integer, Long> e : count(cluster).entrySet()) Assert.assertEquals("Node " + e.getKey() + " has incorrect row state", 100L, e.getValue().longValue());
    }
}
Also used : IntStream(java.util.stream.IntStream) ICluster(org.apache.cassandra.distributed.api.ICluster) Test(org.junit.Test) ConsistencyLevel(org.apache.cassandra.distributed.api.ConsistencyLevel) Collectors(java.util.stream.Collectors) GossipHelper.bootstrap(org.apache.cassandra.distributed.action.GossipHelper.bootstrap) TokenSupplier(org.apache.cassandra.distributed.api.TokenSupplier) IInvokableInstance(org.apache.cassandra.distributed.api.IInvokableInstance) GossipHelper.pullSchemaFrom(org.apache.cassandra.distributed.action.GossipHelper.pullSchemaFrom) Arrays.asList(java.util.Arrays.asList) Map(java.util.Map) TestBaseImpl(org.apache.cassandra.distributed.test.TestBaseImpl) Cluster(org.apache.cassandra.distributed.Cluster) GossipHelper.withProperty(org.apache.cassandra.distributed.action.GossipHelper.withProperty) Assert(org.junit.Assert) IInstanceConfig(org.apache.cassandra.distributed.api.IInstanceConfig) GossipHelper.statusToBootstrap(org.apache.cassandra.distributed.action.GossipHelper.statusToBootstrap) NetworkTopology(org.apache.cassandra.distributed.shared.NetworkTopology) NETWORK(org.apache.cassandra.distributed.api.Feature.NETWORK) GOSSIP(org.apache.cassandra.distributed.api.Feature.GOSSIP) IInvokableInstance(org.apache.cassandra.distributed.api.IInvokableInstance) IInstanceConfig(org.apache.cassandra.distributed.api.IInstanceConfig) ICluster(org.apache.cassandra.distributed.api.ICluster) Cluster(org.apache.cassandra.distributed.Cluster) Map(java.util.Map) Test(org.junit.Test)

Example 98 with Cluster

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

the class ReadsDuringBootstrapTest method readsDuringBootstrapTest.

@Test
public void readsDuringBootstrapTest() throws IOException, ExecutionException, InterruptedException, TimeoutException {
    int originalNodeCount = 3;
    int expandedNodeCount = originalNodeCount + 1;
    ExecutorService es = Executors.newSingleThreadExecutor();
    try (Cluster cluster = builder().withNodes(originalNodeCount).withTokenSupplier(TokenSupplier.evenlyDistributedTokens(expandedNodeCount)).withNodeIdTopology(NetworkTopology.singleDcNetworkTopology(expandedNodeCount, "dc0", "rack0")).withConfig(config -> config.with(NETWORK, GOSSIP).set("read_request_timeout", String.format("%dms", Integer.MAX_VALUE)).set("request_timeout", String.format("%dms", Integer.MAX_VALUE))).withInstanceInitializer(BB::install).start()) {
        String query = withKeyspace("SELECT * FROM %s.tbl WHERE id = ?");
        cluster.schemaChange(withKeyspace("CREATE KEYSPACE IF NOT EXISTS %s WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 2};"));
        cluster.schemaChange(withKeyspace("CREATE TABLE %s.tbl (id int PRIMARY KEY)"));
        cluster.get(1).runOnInstance(() -> BB.block.set(true));
        Future<?> read = es.submit(() -> cluster.coordinator(1).execute(query, ConsistencyLevel.QUORUM, 3));
        long mark = cluster.get(1).logs().mark();
        bootstrapAndJoinNode(cluster);
        cluster.get(1).logs().watchFor(mark, "New node /127.0.0.4");
        cluster.get(1).runOnInstance(() -> BB.block.set(false));
        // populate cache
        for (int i = 0; i < 10; i++) cluster.coordinator(1).execute(query, ConsistencyLevel.QUORUM, i);
        cluster.get(1).runOnInstance(() -> BB.latch.countDown());
        read.get();
    } finally {
        es.shutdown();
    }
}
Also used : ExecutorService(java.util.concurrent.ExecutorService) Cluster(org.apache.cassandra.distributed.Cluster) Test(org.junit.Test)

Example 99 with Cluster

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

the class HostReplacementAbruptDownedInstanceTest method hostReplaceAbruptShutdown.

/**
 * Can we maybe also test with an abrupt shutdown, that is when the shutdown state is not broadcast and the node to be replaced is on NORMAL state?
 */
@Test
public void hostReplaceAbruptShutdown() throws IOException {
    int numStartNodes = 3;
    TokenSupplier even = TokenSupplier.evenlyDistributedTokens(numStartNodes);
    try (Cluster cluster = Cluster.build(numStartNodes).withConfig(c -> c.with(Feature.GOSSIP, Feature.NETWORK)).withTokenSupplier(node -> even.token(node == (numStartNodes + 1) ? 2 : node)).start()) {
        IInvokableInstance seed = cluster.get(1);
        IInvokableInstance nodeToRemove = cluster.get(2);
        IInvokableInstance peer = cluster.get(3);
        List<IInvokableInstance> peers = Arrays.asList(seed, peer);
        setupCluster(cluster);
        // collect rows/tokens to detect issues later on if the state doesn't match
        SimpleQueryResult expectedState = nodeToRemove.coordinator().executeWithResult("SELECT * FROM " + KEYSPACE + ".tbl", ConsistencyLevel.ALL);
        stopAbrupt(cluster, nodeToRemove);
        // at this point node 2 should still be NORMAL on all other nodes
        peers.forEach(p -> assertRingState(p, nodeToRemove, "Normal"));
        // node is down, but queries should still work
        // TODO failing, but shouldn't!
        // peers.forEach(p -> validateRows(p.coordinator(), expectedState));
        // now create a new node to replace the other node
        long startNanos = nanoTime();
        IInvokableInstance replacingNode = replaceHostAndStart(cluster, nodeToRemove, properties -> {
            // since node2 was killed abruptly its possible that node2's gossip state has an old schema version
            // if this happens then bootstrap will fail waiting for a schema version it will never see; to avoid
            // this, setting this property to log the warning rather than fail bootstrap
            properties.set(BOOTSTRAP_SKIP_SCHEMA_CHECK, true);
        });
        logger.info("Host replacement of {} with {} took {}", nodeToRemove, replacingNode, Duration.ofNanos(nanoTime() - startNanos));
        peers.forEach(p -> awaitRingJoin(p, replacingNode));
        // make sure all nodes are healthy
        awaitRingHealthy(seed);
        List<IInvokableInstance> expectedRing = Arrays.asList(seed, peer, replacingNode);
        expectedRing.forEach(p -> assertRingIs(p, expectedRing));
        expectedRing.forEach(p -> validateRows(p.coordinator(), expectedState));
    }
}
Also used : Arrays(java.util.Arrays) Feature(org.apache.cassandra.distributed.api.Feature) Logger(org.slf4j.Logger) ClusterUtils.stopAbrupt(org.apache.cassandra.distributed.shared.ClusterUtils.stopAbrupt) HostReplacementTest.setupCluster(org.apache.cassandra.distributed.test.hostreplacement.HostReplacementTest.setupCluster) LoggerFactory(org.slf4j.LoggerFactory) ClusterUtils.awaitRingJoin(org.apache.cassandra.distributed.shared.ClusterUtils.awaitRingJoin) HostReplacementTest.validateRows(org.apache.cassandra.distributed.test.hostreplacement.HostReplacementTest.validateRows) IOException(java.io.IOException) Test(org.junit.Test) ConsistencyLevel(org.apache.cassandra.distributed.api.ConsistencyLevel) Global.nanoTime(org.apache.cassandra.utils.Clock.Global.nanoTime) BOOTSTRAP_SKIP_SCHEMA_CHECK(org.apache.cassandra.config.CassandraRelevantProperties.BOOTSTRAP_SKIP_SCHEMA_CHECK) TokenSupplier(org.apache.cassandra.distributed.api.TokenSupplier) ClusterUtils.replaceHostAndStart(org.apache.cassandra.distributed.shared.ClusterUtils.replaceHostAndStart) List(java.util.List) IInvokableInstance(org.apache.cassandra.distributed.api.IInvokableInstance) Duration(java.time.Duration) TestBaseImpl(org.apache.cassandra.distributed.test.TestBaseImpl) SimpleQueryResult(org.apache.cassandra.distributed.api.SimpleQueryResult) Cluster(org.apache.cassandra.distributed.Cluster) ClusterUtils.assertRingState(org.apache.cassandra.distributed.shared.ClusterUtils.assertRingState) ClusterUtils.assertRingIs(org.apache.cassandra.distributed.shared.ClusterUtils.assertRingIs) ClusterUtils.awaitRingHealthy(org.apache.cassandra.distributed.shared.ClusterUtils.awaitRingHealthy) IInvokableInstance(org.apache.cassandra.distributed.api.IInvokableInstance) SimpleQueryResult(org.apache.cassandra.distributed.api.SimpleQueryResult) TokenSupplier(org.apache.cassandra.distributed.api.TokenSupplier) HostReplacementTest.setupCluster(org.apache.cassandra.distributed.test.hostreplacement.HostReplacementTest.setupCluster) Cluster(org.apache.cassandra.distributed.Cluster) Test(org.junit.Test)

Example 100 with Cluster

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

the class UnableToParseClientMessageFromBlockedSubnetTest method badMessageCausesProtocolExceptionFromExcludeList.

@Test
public void badMessageCausesProtocolExceptionFromExcludeList() throws IOException, TimeoutException {
    Cluster cluster = getCluster();
    // write gibberish to the native protocol
    IInvokableInstance node = cluster.get(1);
    // make sure everything is fine at the start
    Assertions.assertThat(node.metrics().getCounter("org.apache.cassandra.metrics.Client.ProtocolException")).isEqualTo(0);
    Assertions.assertThat(node.metrics().getCounter("org.apache.cassandra.metrics.Client.UnknownException")).isEqualTo(0);
    LogAction logs = node.logs();
    long mark = logs.mark();
    try (SimpleClient client = SimpleClient.builder("127.0.0.1", 9042).protocolVersion(version).useBeta().build()) {
        client.connect(false, true);
        // this should return a failed response
        // disable waiting on procol errors as that logic was reverted until we can figure out its 100% safe
        // right now ProtocolException is thrown for fatal and non-fatal issues, so closing the channel
        // on non-fatal issues could cause other issues for the cluster
        byte expectedVersion = (byte) (80 + version.asInt());
        Message.Response response = client.execute(new UnableToParseClientMessageTest.CustomHeaderMessage(new byte[] { expectedVersion, 1, 2, 3, 4, 5, 6, 7, 8, 9 }), false);
        Assertions.assertThat(response).isInstanceOf(ErrorMessage.class);
        logs.watchFor(mark, "address contained in client_error_reporting_exclusions");
        Assertions.assertThat(node.metrics().getCounter("org.apache.cassandra.metrics.Client.ProtocolException")).isEqualTo(0);
        Assertions.assertThat(node.metrics().getCounter("org.apache.cassandra.metrics.Client.UnknownException")).isEqualTo(0);
        Assertions.assertThat(logs.grep(mark, "Excluding client exception fo").getResult()).hasSize(1);
        Assertions.assertThat(logs.grep(mark, "Unexpected exception during request").getResult()).isEmpty();
    }
}
Also used : IInvokableInstance(org.apache.cassandra.distributed.api.IInvokableInstance) LogAction(org.apache.cassandra.distributed.api.LogAction) ErrorMessage(org.apache.cassandra.transport.messages.ErrorMessage) Message(org.apache.cassandra.transport.Message) Cluster(org.apache.cassandra.distributed.Cluster) SimpleClient(org.apache.cassandra.transport.SimpleClient) 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