Search in sources :

Example 51 with IInvokableInstance

use of org.apache.cassandra.distributed.api.IInvokableInstance in project cassandra by apache.

the class PrepareBatchStatementsTest method testPreparedBatch.

@Test
public void testPreparedBatch() throws Exception {
    try (ICluster<IInvokableInstance> c = init(builder().withNodes(1).withConfig(config -> config.with(GOSSIP, NETWORK, NATIVE_PROTOCOL)).start())) {
        try (com.datastax.driver.core.Cluster cluster = com.datastax.driver.core.Cluster.builder().addContactPoint("127.0.0.1").build();
            Session s = cluster.connect()) {
            c.schemaChange(withKeyspace("CREATE KEYSPACE ks1 WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 1};"));
            c.schemaChange(withKeyspace("CREATE TABLE ks1.tbl (pk int, ck int, v int, PRIMARY KEY (pk, ck));"));
            c.schemaChange(withKeyspace("CREATE KEYSPACE ks2 WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 1};"));
            c.schemaChange(withKeyspace("CREATE TABLE ks2.tbl (pk int, ck int, v int, PRIMARY KEY (pk, ck));"));
            String batch1 = "BEGIN BATCH\n" + "UPDATE ks1.tbl SET v = ? where pk = ? and ck = ?;\n" + "UPDATE ks2.tbl SET v = ? where pk = ? and ck = ?;\n" + "APPLY BATCH;";
            String batch2 = "BEGIN BATCH\n" + "INSERT INTO ks1.tbl (pk, ck, v) VALUES (?, ?, ?);\n" + "INSERT INTO tbl (pk, ck, v) VALUES (?, ?, ?);\n" + "APPLY BATCH;";
            PreparedStatement prepared;
            prepared = s.prepare(batch1);
            s.execute(prepared.bind(1, 1, 1, 1, 1, 1));
            c.get(1).runOnInstance(() -> {
                // no USE here, only a fully qualified batch - should get stored ONCE
                List<String> stmts = StorageService.instance.getPreparedStatements().stream().map(p -> p.right).collect(Collectors.toList());
                assertEquals(Lists.newArrayList(batch1), stmts);
                QueryProcessor.clearPreparedStatements(false);
            });
            s.execute("use ks2");
            prepared = s.prepare(batch1);
            s.execute(prepared.bind(1, 1, 1, 1, 1, 1));
            c.get(1).runOnInstance(() -> {
                // after USE, fully qualified - should get stored twice! Once with null keyspace (new behaviour) once with ks2 keyspace (old behaviour)
                List<String> stmts = StorageService.instance.getPreparedStatements().stream().map(p -> p.right).collect(Collectors.toList());
                assertEquals(Lists.newArrayList(batch1, batch1), stmts);
                QueryProcessor.clearPreparedStatements(false);
            });
            prepared = s.prepare(batch2);
            s.execute(prepared.bind(1, 1, 1, 1, 1, 1));
            c.get(1).runOnInstance(() -> {
                // after USE, should get stored twice, once with keyspace, once without
                List<String> stmts = StorageService.instance.getPreparedStatements().stream().map(p -> p.right).collect(Collectors.toList());
                assertEquals(Lists.newArrayList(batch2, batch2), stmts);
                QueryProcessor.clearPreparedStatements(false);
            });
        }
    }
}
Also used : ICluster(org.apache.cassandra.distributed.api.ICluster) StorageService(org.apache.cassandra.service.StorageService) Test(org.junit.Test) QueryProcessor(org.apache.cassandra.cql3.QueryProcessor) Collectors(java.util.stream.Collectors) NATIVE_PROTOCOL(org.apache.cassandra.distributed.api.Feature.NATIVE_PROTOCOL) PreparedStatement(com.datastax.driver.core.PreparedStatement) List(java.util.List) IInvokableInstance(org.apache.cassandra.distributed.api.IInvokableInstance) Lists(org.assertj.core.util.Lists) Session(com.datastax.driver.core.Session) NETWORK(org.apache.cassandra.distributed.api.Feature.NETWORK) Assert.assertEquals(org.junit.Assert.assertEquals) GOSSIP(org.apache.cassandra.distributed.api.Feature.GOSSIP) IInvokableInstance(org.apache.cassandra.distributed.api.IInvokableInstance) PreparedStatement(com.datastax.driver.core.PreparedStatement) Session(com.datastax.driver.core.Session) Test(org.junit.Test)

Example 52 with IInvokableInstance

use of org.apache.cassandra.distributed.api.IInvokableInstance in project cassandra by apache.

the class BootstrapBinaryDisabledTest method bootstrap.

private static void bootstrap(Cluster cluster, Map<String, Object> config, boolean isWriteSurvey) throws TimeoutException {
    IInstanceConfig nodeConfig = cluster.newInstanceConfig();
    nodeConfig.set("auto_bootstrap", true);
    config.forEach(nodeConfig::set);
    // TODO can we make this more isolated?
    System.setProperty("cassandra.ring_delay_ms", "5000");
    if (isWriteSurvey)
        System.setProperty("cassandra.write_survey", "true");
    RewriteEnabled.enable();
    cluster.bootstrap(nodeConfig).startup();
    IInvokableInstance node = cluster.get(cluster.size());
    assertLogHas(node, "Some data streaming failed");
    assertLogHas(node, isWriteSurvey ? "Not starting client transports in write_survey mode as it's bootstrapping or auth is enabled" : "Node is not yet bootstrapped completely");
    node.nodetoolResult("join").asserts().failure().errorContains("Cannot join the ring until bootstrap completes");
    RewriteEnabled.disable();
    node.nodetoolResult("bootstrap", "resume").asserts().success();
    if (isWriteSurvey)
        assertLogHas(node, "Not starting client transports in write_survey mode as it's bootstrapping or auth is enabled");
    if (isWriteSurvey) {
        node.nodetoolResult("join").asserts().success();
        assertLogHas(node, "Leaving write survey mode and joining ring at operator request");
    }
    node.logs().watchFor("Starting listening for CQL clients");
    assertBootstrapState(node, "COMPLETED");
}
Also used : IInvokableInstance(org.apache.cassandra.distributed.api.IInvokableInstance) IInstanceConfig(org.apache.cassandra.distributed.api.IInstanceConfig)

Example 53 with IInvokableInstance

use of org.apache.cassandra.distributed.api.IInvokableInstance in project cassandra by apache.

the class OnClusterJoin method performSimple.

public ActionList performSimple() {
    IInvokableInstance joinInstance = actions.cluster.get(joining);
    before(joinInstance);
    return ActionList.of(// setup the node's own gossip state for pending ownership, and return gossip actions to disseminate
    new OnClusterUpdateGossip(actions, joining, new OnInstanceSetBootstrapping(actions, joining)), new OnInstanceSyncSchemaForBootstrap(actions, joining), // stream/repair from a peer
    new OnInstanceBootstrap(actions, joinInstance), // setup the node's own gossip state for natural ownership, and return gossip actions to disseminate
    new OnClusterUpdateGossip(actions, joining, new OnInstanceSetNormal(actions, joining)));
}
Also used : IInvokableInstance(org.apache.cassandra.distributed.api.IInvokableInstance)

Example 54 with IInvokableInstance

use of org.apache.cassandra.distributed.api.IInvokableInstance in project cassandra by apache.

the class OnClusterReplace method performSimple.

public ActionList performSimple() {
    // need to mark it as DOWN, and perhaps shut it down
    Map<InetSocketAddress, IInvokableInstance> lookup = Cluster.getUniqueAddressLookup(actions.cluster);
    IInvokableInstance leaveInstance = actions.cluster.get(leaving);
    IInvokableInstance joinInstance = actions.cluster.get(joining);
    before(leaveInstance);
    UUID hostId = leaveInstance.unsafeCallOnThisThread(SystemKeyspace::getLocalHostId);
    String movingToken = leaveInstance.unsafeCallOnThisThread(() -> Utils.currentToken().toString());
    List<Map.Entry<String, String>> repairRanges = leaveInstance.unsafeApplyOnThisThread((String keyspaceName) -> StorageService.instance.getLocalAndPendingRanges(keyspaceName).stream().map(OnClusterReplace::toStringEntry).collect(Collectors.toList()), actions.keyspace);
    int[] others = repairRanges.stream().mapToInt(repairRange -> lookup.get(leaveInstance.unsafeApplyOnThisThread((String keyspaceName, String tk) -> Keyspace.open(keyspaceName).getReplicationStrategy().getNaturalReplicasForToken(Utils.parseToken(tk)).stream().map(Replica::endpoint).filter(i -> !i.equals(getBroadcastAddressAndPort())).findFirst().orElseThrow(IllegalStateException::new), actions.keyspace, repairRange.getValue())).config().num()).toArray();
    return ActionList.of(// first sync gossip so that newly joined nodes are known by all, so that when we markdown we do not throw UnavailableException
    ReliableAction.transitively("Sync Gossip", () -> actions.gossipWithAll(leaving)), // "shutdown" the leaving instance
    new OnClusterUpdateGossip(actions, ActionList.of(new OnInstanceMarkShutdown(actions, leaving), new OnClusterMarkDown(actions, leaving)), new OnInstanceSendShutdownToAll(actions, leaving)), new OnClusterRepairRanges(actions, others, true, false, repairRanges), // stream/repair from a peer
    new OnClusterUpdateGossip(actions, joining, new OnInstanceSetBootstrapReplacing(actions, joining, leaving, hostId, movingToken)), new OnInstanceSyncSchemaForBootstrap(actions, joining), new OnInstanceBootstrap(actions, joinInstance, movingToken, true), // setup the node's own gossip state for natural ownership, and return gossip actions to disseminate
    new OnClusterUpdateGossip(actions, joining, new OnInstanceSetNormal(actions, joining, hostId, movingToken)));
}
Also used : LazyToString.lazy(org.apache.cassandra.utils.LazyToString.lazy) NONE(org.apache.cassandra.simulator.Action.Modifiers.NONE) ReliableAction(org.apache.cassandra.simulator.Actions.ReliableAction) Range(org.apache.cassandra.dht.Range) StorageService(org.apache.cassandra.service.StorageService) ActionList(org.apache.cassandra.simulator.ActionList) UUID(java.util.UUID) InetSocketAddress(java.net.InetSocketAddress) Collectors(java.util.stream.Collectors) SystemKeyspace(org.apache.cassandra.db.SystemKeyspace) Replica(org.apache.cassandra.locator.Replica) AbstractMap(java.util.AbstractMap) List(java.util.List) Token(org.apache.cassandra.dht.Token) FBUtilities.getBroadcastAddressAndPort(org.apache.cassandra.utils.FBUtilities.getBroadcastAddressAndPort) IInvokableInstance(org.apache.cassandra.distributed.api.IInvokableInstance) Map(java.util.Map) STRICT(org.apache.cassandra.simulator.Action.Modifiers.STRICT) Cluster(org.apache.cassandra.distributed.Cluster) Keyspace(org.apache.cassandra.db.Keyspace) IInvokableInstance(org.apache.cassandra.distributed.api.IInvokableInstance) SystemKeyspace(org.apache.cassandra.db.SystemKeyspace) InetSocketAddress(java.net.InetSocketAddress) UUID(java.util.UUID)

Example 55 with IInvokableInstance

use of org.apache.cassandra.distributed.api.IInvokableInstance in project cassandra by apache.

the class OnClusterLeave method performSimple.

public ActionList performSimple() {
    IInvokableInstance leaveInstance = actions.cluster.get(leaving);
    before(leaveInstance);
    AtomicReference<Supplier<? extends Future<?>>> preparedUnbootstrap = new AtomicReference<>();
    return ActionList.of(// setup the node's own gossip state for pending ownership, and return gossip actions to disseminate
    new OnClusterUpdateGossip(actions, leaving, new OnInstanceSetLeaving(actions, leaving)), new SimulatedActionConsumer<>("Prepare unbootstrap on " + leaving, RELIABLE_NO_TIMEOUTS, RELIABLE_NO_TIMEOUTS, actions, leaveInstance, ref -> ref.set(StorageService.instance.prepareUnbootstrapStreaming()), preparedUnbootstrap), new SimulatedActionConsumer<>("Execute unbootstrap on " + leaving, RELIABLE_NO_TIMEOUTS, RELIABLE_NO_TIMEOUTS, actions, leaveInstance, ref -> FBUtilities.waitOnFuture(ref.get().get()), preparedUnbootstrap), // setup the node's own gossip state for natural ownership, and return gossip actions to disseminate
    new OnClusterUpdateGossip(actions, leaving, new OnInstanceSetLeft(actions, leaving)));
}
Also used : SimulatedActionConsumer(org.apache.cassandra.simulator.systems.SimulatedActionConsumer) RELIABLE_NO_TIMEOUTS(org.apache.cassandra.simulator.Action.Modifiers.RELIABLE_NO_TIMEOUTS) FBUtilities(org.apache.cassandra.utils.FBUtilities) LazyToString.lazy(org.apache.cassandra.utils.LazyToString.lazy) IInvokableInstance(org.apache.cassandra.distributed.api.IInvokableInstance) Future(org.apache.cassandra.utils.concurrent.Future) StorageService(org.apache.cassandra.service.StorageService) ActionList(org.apache.cassandra.simulator.ActionList) AtomicReference(java.util.concurrent.atomic.AtomicReference) Supplier(java.util.function.Supplier) IInvokableInstance(org.apache.cassandra.distributed.api.IInvokableInstance) Future(org.apache.cassandra.utils.concurrent.Future) Supplier(java.util.function.Supplier) AtomicReference(java.util.concurrent.atomic.AtomicReference)

Aggregations

IInvokableInstance (org.apache.cassandra.distributed.api.IInvokableInstance)55 Test (org.junit.Test)36 Cluster (org.apache.cassandra.distributed.Cluster)31 List (java.util.List)16 IOException (java.io.IOException)15 ConsistencyLevel (org.apache.cassandra.distributed.api.ConsistencyLevel)14 Feature (org.apache.cassandra.distributed.api.Feature)13 GOSSIP (org.apache.cassandra.distributed.api.Feature.GOSSIP)13 NETWORK (org.apache.cassandra.distributed.api.Feature.NETWORK)13 ICluster (org.apache.cassandra.distributed.api.ICluster)13 TestBaseImpl (org.apache.cassandra.distributed.test.TestBaseImpl)13 TokenSupplier (org.apache.cassandra.distributed.api.TokenSupplier)12 Session (com.datastax.driver.core.Session)11 Arrays (java.util.Arrays)11 Assertions (org.assertj.core.api.Assertions)10 Set (java.util.Set)9 NATIVE_PROTOCOL (org.apache.cassandra.distributed.api.Feature.NATIVE_PROTOCOL)9 Assert (org.junit.Assert)9 PreparedStatement (com.datastax.driver.core.PreparedStatement)8 Map (java.util.Map)8