Search in sources :

Example 21 with GOSSIP

use of org.apache.cassandra.distributed.api.Feature.GOSSIP in project cassandra by apache.

the class HintsServiceMetricsTest method testHintsServiceMetrics.

@Test
public void testHintsServiceMetrics() throws Exception {
    // setup a 3-node cluster with a bytebuddy injection that makes the writting of some hints to fail
    try (Cluster cluster = builder().withNodes(3).withConfig(config -> config.with(NETWORK, GOSSIP, NATIVE_PROTOCOL)).withInstanceInitializer(FailHints::install).start()) {
        // setup a message filter to drop some of the hint request messages from node1
        AtomicInteger hintsNode2 = new AtomicInteger();
        AtomicInteger hintsNode3 = new AtomicInteger();
        cluster.filters().verbs(Verb.HINT_REQ.id).from(1).messagesMatching((from, to, message) -> (to == 2 && hintsNode2.incrementAndGet() <= NUM_TIMEOUTS_PER_NODE) || (to == 3 && hintsNode3.incrementAndGet() <= NUM_TIMEOUTS_PER_NODE)).drop();
        // setup a message filter to drop mutations requests from node1, so it creates hints for those mutations
        AtomicBoolean dropWritesForNode2 = new AtomicBoolean(false);
        AtomicBoolean dropWritesForNode3 = new AtomicBoolean(false);
        cluster.filters().verbs(Verb.MUTATION_REQ.id).from(1).messagesMatching((from, to, message) -> (to == 2 && dropWritesForNode2.get()) || (to == 3 && dropWritesForNode3.get())).drop();
        // fix under replicated keyspaces so they don't produce hint requests while we are dropping mutations
        fixDistributedSchemas(cluster);
        cluster.schemaChange(withKeyspace("CREATE KEYSPACE %s WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 3}"));
        cluster.schemaChange(withKeyspace("CREATE TABLE %s.t (k int PRIMARY KEY, v int)"));
        ICoordinator coordinator = cluster.coordinator(1);
        IInvokableInstance node1 = cluster.get(1);
        IInvokableInstance node2 = cluster.get(2);
        IInvokableInstance node3 = cluster.get(3);
        // write the first half of the rows with the second node dropping mutation requests,
        // so some hints will be created for that node
        dropWritesForNode2.set(true);
        for (int i = 0; i < NUM_ROWS / 2; i++) coordinator.execute(withKeyspace("INSERT INTO %s.t (k, v) VALUES (?, ?)"), QUORUM, i, i);
        dropWritesForNode2.set(false);
        // write the second half of the rows with the third node dropping mutations requests,
        // so some hints will be created for that node
        dropWritesForNode3.set(true);
        for (int i = NUM_ROWS / 2; i < NUM_ROWS; i++) coordinator.execute(withKeyspace("INSERT INTO %s.t (k, v) VALUES (?, ?)"), QUORUM, i, i);
        dropWritesForNode3.set(false);
        // wait until all the hints have been successfully applied to the nodes that have been dropping mutations
        waitUntilAsserted(() -> assertThat(countRows(node2)).isEqualTo(countRows(node3)).isEqualTo(NUM_ROWS));
        // Verify the metrics for the coordinator node, which is the only one actually sending hints.
        // The hint delivery errors that we have injected should have made the service try to send them again.
        // These retries are done periodically and in pages, so the retries may send again some of the hints that
        // were already successfully sent. This way, there may be more succeeded hints than actual hints/rows.
        waitUntilAsserted(() -> assertThat(countHintsSucceeded(node1)).isGreaterThanOrEqualTo(NUM_ROWS));
        waitUntilAsserted(() -> assertThat(countHintsFailed(node1)).isEqualTo(NUM_FAILURES_PER_NODE * 2));
        waitUntilAsserted(() -> assertThat(countHintsTimedOut(node1)).isEqualTo(NUM_TIMEOUTS_PER_NODE * 2));
        // verify delay metrics
        long numGlobalDelays = countGlobalDelays(node1);
        assertThat(numGlobalDelays).isGreaterThanOrEqualTo(NUM_ROWS);
        assertThat(countEndpointDelays(node1, node1)).isEqualTo(0);
        assertThat(countEndpointDelays(node1, node2)).isGreaterThan(0).isLessThanOrEqualTo(numGlobalDelays);
        assertThat(countEndpointDelays(node1, node3)).isGreaterThan(0).isLessThanOrEqualTo(numGlobalDelays);
        assertThat(countEndpointDelays(node1, node2) + countEndpointDelays(node1, node3)).isGreaterThanOrEqualTo(numGlobalDelays);
        // verify that the metrics for the not-coordinator nodes are zero
        for (IInvokableInstance node : Arrays.asList(node2, node3)) {
            assertThat(countHintsSucceeded(node)).isEqualTo(0);
            assertThat(countHintsFailed(node)).isEqualTo(0);
            assertThat(countHintsTimedOut(node)).isEqualTo(0);
            assertThat(countGlobalDelays(node)).isEqualTo(0);
            cluster.forEach(target -> assertThat(countEndpointDelays(node, target)).isEqualTo(0));
        }
    }
}
Also used : Arrays(java.util.Arrays) MethodDelegation(net.bytebuddy.implementation.MethodDelegation) ByteBuddy(net.bytebuddy.ByteBuddy) ElementMatchers.takesArguments(net.bytebuddy.matcher.ElementMatchers.takesArguments) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Callable(java.util.concurrent.Callable) MINUTES(java.util.concurrent.TimeUnit.MINUTES) NATIVE_PROTOCOL(org.apache.cassandra.distributed.api.Feature.NATIVE_PROTOCOL) ThrowingRunnable(org.awaitility.core.ThrowingRunnable) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ICoordinator(org.apache.cassandra.distributed.api.ICoordinator) Metrics(org.apache.cassandra.distributed.shared.Metrics) TestBaseImpl(org.apache.cassandra.distributed.test.TestBaseImpl) AssertionsForClassTypes.assertThat(org.assertj.core.api.AssertionsForClassTypes.assertThat) QUORUM(org.apache.cassandra.distributed.api.ConsistencyLevel.QUORUM) NETWORK(org.apache.cassandra.distributed.api.Feature.NETWORK) Awaitility.await(org.awaitility.Awaitility.await) ElementMatchers.named(net.bytebuddy.matcher.ElementMatchers.named) HintsServiceMetrics(org.apache.cassandra.metrics.HintsServiceMetrics) Test(org.junit.Test) Hint(org.apache.cassandra.hints.Hint) Verb(org.apache.cassandra.net.Verb) ClassLoadingStrategy(net.bytebuddy.dynamic.loading.ClassLoadingStrategy) SuperCall(net.bytebuddy.implementation.bind.annotation.SuperCall) IInvokableInstance(org.apache.cassandra.distributed.api.IInvokableInstance) Future(org.apache.cassandra.utils.concurrent.Future) Cluster(org.apache.cassandra.distributed.Cluster) SECONDS(java.util.concurrent.TimeUnit.SECONDS) GOSSIP(org.apache.cassandra.distributed.api.Feature.GOSSIP) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ICoordinator(org.apache.cassandra.distributed.api.ICoordinator) IInvokableInstance(org.apache.cassandra.distributed.api.IInvokableInstance) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Cluster(org.apache.cassandra.distributed.Cluster) Hint(org.apache.cassandra.hints.Hint) Test(org.junit.Test)

Example 22 with GOSSIP

use of org.apache.cassandra.distributed.api.Feature.GOSSIP 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 23 with GOSSIP

use of org.apache.cassandra.distributed.api.Feature.GOSSIP 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 24 with GOSSIP

use of org.apache.cassandra.distributed.api.Feature.GOSSIP in project cassandra by apache.

the class ReprepareTestOldBehaviour method testReprepareUsingOldBehavior.

@Test
public void testReprepareUsingOldBehavior() throws Throwable {
    // fork of testReprepareMixedVersionWithoutReset, but makes sure oldBehavior has a clean state
    try (ICluster<IInvokableInstance> c = init(builder().withNodes(2).withConfig(config -> config.with(GOSSIP, NETWORK, NATIVE_PROTOCOL)).withInstanceInitializer(PrepareBehaviour::oldBehaviour).start())) {
        ForceHostLoadBalancingPolicy lbp = new ForceHostLoadBalancingPolicy();
        c.schemaChange(withKeyspace("CREATE TABLE %s.tbl (pk int, ck int, v int, PRIMARY KEY (pk, ck));"));
        try (com.datastax.driver.core.Cluster cluster = com.datastax.driver.core.Cluster.builder().addContactPoint("127.0.0.1").addContactPoint("127.0.0.2").withLoadBalancingPolicy(lbp).build();
            Session session = cluster.connect()) {
            session.execute(withKeyspace("USE %s"));
            lbp.setPrimary(2);
            final PreparedStatement select = session.prepare(withKeyspace("SELECT * FROM %s.tbl"));
            session.execute(select.bind());
            lbp.setPrimary(1);
            session.execute(select.bind());
        }
    }
}
Also used : PreparedStatement(com.datastax.driver.core.PreparedStatement) IInvokableInstance(org.apache.cassandra.distributed.api.IInvokableInstance) Session(com.datastax.driver.core.Session) ICluster(org.apache.cassandra.distributed.api.ICluster) Test(org.junit.Test) QueryProcessor(org.apache.cassandra.cql3.QueryProcessor) NETWORK(org.apache.cassandra.distributed.api.Feature.NETWORK) GOSSIP(org.apache.cassandra.distributed.api.Feature.GOSSIP) NATIVE_PROTOCOL(org.apache.cassandra.distributed.api.Feature.NATIVE_PROTOCOL) IInvokableInstance(org.apache.cassandra.distributed.api.IInvokableInstance) PreparedStatement(com.datastax.driver.core.PreparedStatement) Session(com.datastax.driver.core.Session) Test(org.junit.Test)

Example 25 with GOSSIP

use of org.apache.cassandra.distributed.api.Feature.GOSSIP in project cassandra by apache.

the class ReprepareTestBase method testReprepareTwoKeyspaces.

public void testReprepareTwoKeyspaces(BiConsumer<ClassLoader, Integer> instanceInitializer) throws Throwable {
    try (ICluster<IInvokableInstance> c = init(builder().withNodes(2).withConfig(config -> config.with(GOSSIP, NETWORK, NATIVE_PROTOCOL)).withInstanceInitializer(instanceInitializer).start())) {
        c.schemaChange(withKeyspace("CREATE KEYSPACE %s2 WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 2};"));
        c.schemaChange(withKeyspace("CREATE TABLE %s.tbl (pk int, ck int, v int, PRIMARY KEY (pk, ck));"));
        ForceHostLoadBalancingPolicy lbp = new ForceHostLoadBalancingPolicy();
        for (int firstContact : new int[] { 1, 2 }) try (com.datastax.driver.core.Cluster cluster = com.datastax.driver.core.Cluster.builder().addContactPoint("127.0.0.1").addContactPoint("127.0.0.2").withLoadBalancingPolicy(lbp).build();
            Session session = cluster.connect()) {
            {
                session.execute(withKeyspace("USE %s"));
                c.stream().forEach((i) -> i.runOnInstance(QueryProcessor::clearPreparedStatementsCache));
                lbp.setPrimary(firstContact);
                final PreparedStatement select = session.prepare(withKeyspace("SELECT * FROM %s.tbl"));
                session.execute(select.bind());
                c.stream().forEach((i) -> i.runOnInstance(QueryProcessor::clearPreparedStatementsCache));
                lbp.setPrimary(firstContact == 1 ? 2 : 1);
                session.execute(withKeyspace("USE %s2"));
                try {
                    session.execute(select.bind());
                } catch (DriverInternalError e) {
                    Assert.assertTrue(e.getCause().getMessage().contains("can't execute it on"));
                    continue;
                }
                fail("Should have thrown");
            }
        }
    }
}
Also used : LoadBalancingPolicy(com.datastax.driver.core.policies.LoadBalancingPolicy) MethodDelegation(net.bytebuddy.implementation.MethodDelegation) ByteBuddy(net.bytebuddy.ByteBuddy) ElementMatchers.takesArguments(net.bytebuddy.matcher.ElementMatchers.takesArguments) QueryProcessor(org.apache.cassandra.cql3.QueryProcessor) QueryHandler(org.apache.cassandra.cql3.QueryHandler) Iterators(com.google.common.collect.Iterators) NATIVE_PROTOCOL(org.apache.cassandra.distributed.api.Feature.NATIVE_PROTOCOL) PreparedStatement(com.datastax.driver.core.PreparedStatement) FixedValue(net.bytebuddy.implementation.FixedValue) Session(com.datastax.driver.core.Session) BiConsumer(java.util.function.BiConsumer) AssertUtils.fail(org.apache.cassandra.distributed.shared.AssertUtils.fail) InvalidRequestException(org.apache.cassandra.exceptions.InvalidRequestException) NETWORK(org.apache.cassandra.distributed.api.Feature.NETWORK) ResultMessage(org.apache.cassandra.transport.messages.ResultMessage) FBUtilities(org.apache.cassandra.utils.FBUtilities) Iterator(java.util.Iterator) ElementMatchers.named(net.bytebuddy.matcher.ElementMatchers.named) Collection(java.util.Collection) ClientState(org.apache.cassandra.service.ClientState) ICluster(org.apache.cassandra.distributed.api.ICluster) ClassLoadingStrategy(net.bytebuddy.dynamic.loading.ClassLoadingStrategy) DriverInternalError(com.datastax.driver.core.exceptions.DriverInternalError) List(java.util.List) IInvokableInstance(org.apache.cassandra.distributed.api.IInvokableInstance) Cluster(com.datastax.driver.core.Cluster) Host(com.datastax.driver.core.Host) HostDistance(com.datastax.driver.core.HostDistance) Comparator(java.util.Comparator) Assert(org.junit.Assert) Collections(java.util.Collections) Statement(com.datastax.driver.core.Statement) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) GOSSIP(org.apache.cassandra.distributed.api.Feature.GOSSIP) IInvokableInstance(org.apache.cassandra.distributed.api.IInvokableInstance) DriverInternalError(com.datastax.driver.core.exceptions.DriverInternalError) ICluster(org.apache.cassandra.distributed.api.ICluster) Cluster(com.datastax.driver.core.Cluster) PreparedStatement(com.datastax.driver.core.PreparedStatement) Session(com.datastax.driver.core.Session)

Aggregations

GOSSIP (org.apache.cassandra.distributed.api.Feature.GOSSIP)34 NETWORK (org.apache.cassandra.distributed.api.Feature.NETWORK)33 Test (org.junit.Test)32 Cluster (org.apache.cassandra.distributed.Cluster)27 Assert (org.junit.Assert)16 IInvokableInstance (org.apache.cassandra.distributed.api.IInvokableInstance)15 ConsistencyLevel (org.apache.cassandra.distributed.api.ConsistencyLevel)14 NATIVE_PROTOCOL (org.apache.cassandra.distributed.api.Feature.NATIVE_PROTOCOL)14 ByteBuddy (net.bytebuddy.ByteBuddy)12 ClassLoadingStrategy (net.bytebuddy.dynamic.loading.ClassLoadingStrategy)12 MethodDelegation (net.bytebuddy.implementation.MethodDelegation)12 ElementMatchers.named (net.bytebuddy.matcher.ElementMatchers.named)12 StorageService (org.apache.cassandra.service.StorageService)12 TimeUnit (java.util.concurrent.TimeUnit)11 List (java.util.List)10 Map (java.util.Map)10 ICluster (org.apache.cassandra.distributed.api.ICluster)10 NetworkTopology (org.apache.cassandra.distributed.shared.NetworkTopology)10 IOException (java.io.IOException)9 TokenSupplier (org.apache.cassandra.distributed.api.TokenSupplier)9