Search in sources :

Example 6 with Cluster

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

the class CASTest method testIncompleteWriteFollowedBySuccessfulWriteWithStaleRingDuringRangeMovementFollowedByWrite.

/**
 * During a range movement, a CAS may fail leaving side effects that are not witnessed by another operation
 * being performed with stale ring information.
 * This is a particular special case of stale ring information sequencing, which probably would be resolved
 * by fixing each of the more isolated cases (but is unique, so deserving of its own test case).
 * See CASSANDRA-15745
 *
 *  - Range moves from {1, 2, 3} to {2, 3, 4}; witnessed by X (not by !X)
 *  -   X: Prepare to {2, 3, 4}
 *  -   X: Propose to {4}
 *  -  !X: Prepare and Propose to {1, 2}
 *  - Range move visible by !X
 *  - Any: Prepare and Propose to {3, 4}
 */
@Ignore
@Test
public void testIncompleteWriteFollowedBySuccessfulWriteWithStaleRingDuringRangeMovementFollowedByWrite() throws Throwable {
    try (Cluster cluster = Cluster.create(4, config -> config.set("write_request_timeout", REQUEST_TIMEOUT).set("cas_contention_timeout", CONTENTION_TIMEOUT))) {
        cluster.schemaChange("CREATE KEYSPACE " + KEYSPACE + " WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 3};");
        cluster.schemaChange("CREATE TABLE " + KEYSPACE + ".tbl (pk int, ck int, v1 int, v2 int, PRIMARY KEY (pk, ck))");
        // make it so {4} is bootstrapping, and this has not propagated to other nodes yet
        for (int i = 1; i <= 4; ++i) cluster.get(1).acceptsOnInstance(UnsafeGossipHelper::removeFromRing).accept(cluster.get(4));
        cluster.get(4).acceptsOnInstance(UnsafeGossipHelper::addToRingBootstrapping).accept(cluster.get(4));
        int pk = pk(cluster, 1, 2);
        // {4} promises and accepts on !{1} => {2, 3, 4}; commits on !{1, 2, 3} => {4}
        cluster.filters().verbs(PAXOS_PREPARE_REQ.id, READ_REQ.id).from(4).to(1).drop();
        cluster.filters().verbs(PAXOS_PROPOSE_REQ.id).from(4).to(1, 2, 3).drop();
        try {
            cluster.coordinator(4).execute("INSERT INTO " + KEYSPACE + ".tbl (pk, ck, v1) VALUES (?, 1, 1) IF NOT EXISTS", ConsistencyLevel.QUORUM, pk);
            Assert.assertTrue(false);
        } catch (RuntimeException wrapped) {
            Assert.assertEquals("Operation timed out - received only 1 responses.", wrapped.getCause().getMessage());
        }
        // {1} promises and accepts on !{3} => {1, 2}; commits on !{2, 3} => {1}
        cluster.filters().verbs(PAXOS_PREPARE_REQ.id, READ_REQ.id).from(1).to(3).drop();
        cluster.filters().verbs(PAXOS_PROPOSE_REQ.id).from(1).to(3).drop();
        cluster.filters().verbs(PAXOS_COMMIT_REQ.id).from(1).to(2, 3).drop();
        assertRows(cluster.coordinator(1).execute("INSERT INTO " + KEYSPACE + ".tbl (pk, ck, v2) VALUES (?, 1, 2) IF NOT EXISTS", ConsistencyLevel.ONE, pk), row(true));
        // finish topology change
        for (int i = 1; i <= 4; ++i) cluster.get(i).acceptsOnInstance(UnsafeGossipHelper::addToRingNormal).accept(cluster.get(4));
        // {3} reads from !{2} => {3, 4}
        cluster.filters().verbs(PAXOS_PREPARE_REQ.id, READ_REQ.id).from(3).to(2).drop();
        cluster.filters().verbs(PAXOS_PROPOSE_REQ.id).from(3).to(2).drop();
        assertRows(cluster.coordinator(3).execute("INSERT INTO " + KEYSPACE + ".tbl (pk, ck, v2) VALUES (?, 1, 2) IF NOT EXISTS", ConsistencyLevel.ONE, pk), row(false, 5, 1, null, 2));
    }
}
Also used : UnsafeGossipHelper(org.apache.cassandra.distributed.impl.UnsafeGossipHelper) Cluster(org.apache.cassandra.distributed.Cluster) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 7 with Cluster

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

the class ClearSnapshotTest method testSeqClearsSnapshot.

@Test
public void testSeqClearsSnapshot() throws IOException, TimeoutException {
    try (Cluster cluster = init(Cluster.build(3).withConfig(config -> config.with(GOSSIP).with(NETWORK)).withInstanceInitializer(BB::install).start())) {
        cluster.schemaChange(withKeyspace("create table %s.tbl (id int primary key, x int)"));
        for (int i = 0; i < 10; i++) cluster.get(1).executeInternal(withKeyspace("insert into %s.tbl (id, x) values (?, ?)"), i, i);
        cluster.get(1).nodetoolResult("repair", "-seq", "-full", KEYSPACE).asserts().success();
        cluster.get(1).logs().watchFor("Clearing snapshot");
    }
}
Also used : Uninterruptibles(com.google.common.util.concurrent.Uninterruptibles) Logger(org.slf4j.Logger) MethodDelegation(net.bytebuddy.implementation.MethodDelegation) ElementMatchers.named(net.bytebuddy.matcher.ElementMatchers.named) ByteBuddy(net.bytebuddy.ByteBuddy) LoggerFactory(org.slf4j.LoggerFactory) TimeoutException(java.util.concurrent.TimeoutException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) IOException(java.io.IOException) Callable(java.util.concurrent.Callable) Test(org.junit.Test) ConsistencyLevel(org.apache.cassandra.distributed.api.ConsistencyLevel) ClassLoadingStrategy(net.bytebuddy.dynamic.loading.ClassLoadingStrategy) ArrayList(java.util.ArrayList) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) SuperCall(net.bytebuddy.implementation.bind.annotation.SuperCall) List(java.util.List) ActiveRepairService(org.apache.cassandra.service.ActiveRepairService) Assert.assertFalse(org.junit.Assert.assertFalse) Cluster(org.apache.cassandra.distributed.Cluster) Directories(org.apache.cassandra.db.Directories) NETWORK(org.apache.cassandra.distributed.api.Feature.NETWORK) GOSSIP(org.apache.cassandra.distributed.api.Feature.GOSSIP) Cluster(org.apache.cassandra.distributed.Cluster) Test(org.junit.Test)

Example 8 with Cluster

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

the class ClearSnapshotTest method clearSnapshotSlowTest.

@Test
public void clearSnapshotSlowTest() throws IOException, InterruptedException, ExecutionException {
    try (Cluster cluster = init(Cluster.build(3).withConfig(config -> config.with(GOSSIP).with(NETWORK)).withInstanceInitializer(BB::install).start())) {
        int tableCount = 50;
        for (int i = 0; i < tableCount; i++) {
            String ksname = "ks" + i;
            cluster.schemaChange("create keyspace " + ksname + " with replication = {'class': 'SimpleStrategy', 'replication_factor': 3}");
            cluster.schemaChange("create table " + ksname + ".tbl (id int primary key, t int)");
            cluster.get(1).executeInternal("insert into " + ksname + ".tbl (id , t) values (?, ?)", i, i);
            cluster.forEach((node) -> node.flush(ksname));
        }
        List<Thread> repairThreads = new ArrayList<>();
        for (int i = 0; i < tableCount; i++) {
            String ksname = "ks" + i;
            Thread t = new Thread(() -> cluster.get(1).nodetoolResult("repair", "-full", ksname).asserts().success());
            t.start();
            repairThreads.add(t);
        }
        AtomicBoolean gotExc = new AtomicBoolean(false);
        AtomicBoolean exit = new AtomicBoolean(false);
        Thread reads = new Thread(() -> {
            while (!exit.get()) {
                try {
                    cluster.coordinator(1).execute("select * from ks1.tbl where id = 5", ConsistencyLevel.QUORUM);
                    Uninterruptibles.sleepUninterruptibly(10, TimeUnit.MILLISECONDS);
                } catch (Exception e) {
                    if (!gotExc.get())
                        logger.error("Unexpected exception querying table ks1.tbl", e);
                    gotExc.set(true);
                }
            }
        });
        reads.start();
        long activeRepairs;
        do {
            activeRepairs = cluster.get(1).callOnInstance(() -> ActiveRepairService.instance.parentRepairSessionCount());
            Thread.sleep(50);
        } while (activeRepairs < 35);
        cluster.setUncaughtExceptionsFilter((t) -> t.getMessage() != null && t.getMessage().contains("Parent repair session with id"));
        cluster.get(2).shutdown().get();
        repairThreads.forEach(t -> {
            try {
                t.join();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        });
        exit.set(true);
        reads.join();
        assertFalse(gotExc.get());
    }
}
Also used : Uninterruptibles(com.google.common.util.concurrent.Uninterruptibles) Logger(org.slf4j.Logger) MethodDelegation(net.bytebuddy.implementation.MethodDelegation) ElementMatchers.named(net.bytebuddy.matcher.ElementMatchers.named) ByteBuddy(net.bytebuddy.ByteBuddy) LoggerFactory(org.slf4j.LoggerFactory) TimeoutException(java.util.concurrent.TimeoutException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) IOException(java.io.IOException) Callable(java.util.concurrent.Callable) Test(org.junit.Test) ConsistencyLevel(org.apache.cassandra.distributed.api.ConsistencyLevel) ClassLoadingStrategy(net.bytebuddy.dynamic.loading.ClassLoadingStrategy) ArrayList(java.util.ArrayList) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) SuperCall(net.bytebuddy.implementation.bind.annotation.SuperCall) List(java.util.List) ActiveRepairService(org.apache.cassandra.service.ActiveRepairService) Assert.assertFalse(org.junit.Assert.assertFalse) Cluster(org.apache.cassandra.distributed.Cluster) Directories(org.apache.cassandra.db.Directories) NETWORK(org.apache.cassandra.distributed.api.Feature.NETWORK) GOSSIP(org.apache.cassandra.distributed.api.Feature.GOSSIP) ArrayList(java.util.ArrayList) Cluster(org.apache.cassandra.distributed.Cluster) TimeoutException(java.util.concurrent.TimeoutException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.junit.Test)

Example 9 with Cluster

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

the class FailingResponseDoesNotLogTest method dispatcherErrorDoesNotLock.

@Test
public void dispatcherErrorDoesNotLock() throws IOException {
    System.setProperty("cassandra.custom_query_handler_class", AlwaysRejectErrorQueryHandler.class.getName());
    try (Cluster cluster = Cluster.build(1).withConfig(c -> c.with(Feature.NATIVE_PROTOCOL, Feature.GOSSIP).set("client_error_reporting_exclusions", ImmutableMap.of("subnets", Collections.singletonList("127.0.0.1")))).start()) {
        try (SimpleClient client = SimpleClient.builder("127.0.0.1", 9042).build().connect(false)) {
            client.execute("SELECT * FROM system.peers", ConsistencyLevel.ONE);
            Assert.fail("Query should have failed");
        } catch (Exception e) {
        // ignore; expected
        }
        // logs happen before client response; so grep is enough
        LogAction logs = cluster.get(1).logs();
        LogResult<List<String>> matches = logs.grep("address contained in client_error_reporting_exclusions");
        Assertions.assertThat(matches.getResult()).hasSize(1);
        matches = logs.grep("Unexpected exception during request");
        Assertions.assertThat(matches.getResult()).isEmpty();
    } finally {
        System.clearProperty("cassandra.custom_query_handler_class");
    }
}
Also used : BeforeClass(org.junit.BeforeClass) LogAction(org.apache.cassandra.distributed.api.LogAction) BatchStatement(org.apache.cassandra.cql3.statements.BatchStatement) CQLStatement(org.apache.cassandra.cql3.CQLStatement) QueryHandler(org.apache.cassandra.cql3.QueryHandler) ByteBuffer(java.nio.ByteBuffer) RequestExecutionException(org.apache.cassandra.exceptions.RequestExecutionException) Map(java.util.Map) Assertions(org.assertj.core.api.Assertions) DatabaseDescriptor(org.apache.cassandra.config.DatabaseDescriptor) BatchQueryOptions(org.apache.cassandra.cql3.BatchQueryOptions) RequestValidationException(org.apache.cassandra.exceptions.RequestValidationException) ResultMessage(org.apache.cassandra.transport.messages.ResultMessage) ConsistencyLevel(org.apache.cassandra.db.ConsistencyLevel) Feature(org.apache.cassandra.distributed.api.Feature) ImmutableMap(com.google.common.collect.ImmutableMap) QueryState(org.apache.cassandra.service.QueryState) MD5Digest(org.apache.cassandra.utils.MD5Digest) ClientState(org.apache.cassandra.service.ClientState) IOException(java.io.IOException) Test(org.junit.Test) List(java.util.List) SimpleClient(org.apache.cassandra.transport.SimpleClient) Cluster(org.apache.cassandra.distributed.Cluster) Assert(org.junit.Assert) Collections(java.util.Collections) QueryOptions(org.apache.cassandra.cql3.QueryOptions) LogResult(org.apache.cassandra.distributed.api.LogResult) LogAction(org.apache.cassandra.distributed.api.LogAction) Cluster(org.apache.cassandra.distributed.Cluster) SimpleClient(org.apache.cassandra.transport.SimpleClient) List(java.util.List) RequestExecutionException(org.apache.cassandra.exceptions.RequestExecutionException) RequestValidationException(org.apache.cassandra.exceptions.RequestValidationException) IOException(java.io.IOException) Test(org.junit.Test)

Example 10 with Cluster

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

the class FrozenUDTTest method testEmptyValue.

@Test
public void testEmptyValue() throws IOException {
    try (Cluster cluster = init(Cluster.build(1).start())) {
        cluster.schemaChange("create type " + KEYSPACE + ".a (foo text)");
        cluster.schemaChange("create table " + KEYSPACE + ".x (id int, ck frozen<a>, i int, primary key (id, ck))");
        cluster.coordinator(1).execute("insert into " + KEYSPACE + ".x (id, ck, i) VALUES (1, system.fromjson('{\"foo\":\"\"}'), 1)", ConsistencyLevel.ALL);
        cluster.coordinator(1).execute("insert into " + KEYSPACE + ".x (id, ck, i) VALUES (1, system.fromjson('{\"foo\":\"a\"}'), 2)", ConsistencyLevel.ALL);
        cluster.forEach(i -> i.flush(KEYSPACE));
        Runnable check = () -> {
            assertRows(cluster.coordinator(1).execute("select i from " + KEYSPACE + ".x WHERE id = 1 and ck = system.fromjson('{\"foo\":\"\"}')", ConsistencyLevel.ALL), row(1));
            assertRows(cluster.coordinator(1).execute("select i from " + KEYSPACE + ".x WHERE id = 1 and ck = system.fromjson('{\"foo\":\"a\"}')", ConsistencyLevel.ALL), row(2));
        };
        check.run();
        cluster.schemaChange("alter type " + KEYSPACE + ".a add bar text");
        check.run();
        assertRows(cluster.coordinator(1).execute("select i from " + KEYSPACE + ".x WHERE id = 1 and ck = system.fromjson('{\"foo\":\"\",\"bar\":\"\"}')", ConsistencyLevel.ALL));
        cluster.coordinator(1).execute("insert into " + KEYSPACE + ".x (id, ck, i) VALUES (1, system.fromjson('{\"foo\":\"\",\"bar\":\"\"}'), 3)", ConsistencyLevel.ALL);
        check.run();
        assertRows(cluster.coordinator(1).execute("select i from " + KEYSPACE + ".x WHERE id = 1 and ck = system.fromjson('{\"foo\":\"\",\"bar\":\"\"}')", ConsistencyLevel.ALL), row(3));
    }
}
Also used : Cluster(org.apache.cassandra.distributed.Cluster) 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