Search in sources :

Example 1 with ConsistencyLevel

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

the class Coordinator method executeInternal.

private SimpleQueryResult executeInternal(String query, ConsistencyLevel consistencyLevelOrigin, Object[] boundValues) {
    ClientState clientState = makeFakeClientState();
    CQLStatement prepared = QueryProcessor.getStatement(query, clientState);
    List<ByteBuffer> boundBBValues = new ArrayList<>();
    ConsistencyLevel consistencyLevel = ConsistencyLevel.valueOf(consistencyLevelOrigin.name());
    for (Object boundValue : boundValues) boundBBValues.add(ByteBufferUtil.objectToBytes(boundValue));
    prepared.validate(QueryState.forInternalCalls().getClientState());
    // Start capturing warnings on this thread. Note that this will implicitly clear out any previous
    // warnings as it sets a new State instance on the ThreadLocal.
    ClientWarn.instance.captureWarnings();
    CoordinatorWarnings.init();
    try {
        ResultMessage res = prepared.execute(QueryState.forInternalCalls(), QueryOptions.create(toCassandraCL(consistencyLevel), boundBBValues, false, Integer.MAX_VALUE, null, null, ProtocolVersion.CURRENT, null), nanoTime());
        // Collect warnings reported during the query.
        CoordinatorWarnings.done();
        if (res != null)
            res.setWarnings(ClientWarn.instance.getWarnings());
        return RowUtil.toQueryResult(res);
    } catch (Exception | Error e) {
        CoordinatorWarnings.done();
        throw e;
    } finally {
        CoordinatorWarnings.reset();
        ClientWarn.instance.resetWarnings();
    }
}
Also used : ClientState(org.apache.cassandra.service.ClientState) CQLStatement(org.apache.cassandra.cql3.CQLStatement) ConsistencyLevel(org.apache.cassandra.distributed.api.ConsistencyLevel) ArrayList(java.util.ArrayList) ResultMessage(org.apache.cassandra.transport.messages.ResultMessage) ByteBuffer(java.nio.ByteBuffer)

Example 2 with ConsistencyLevel

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

the class CountersTest method testUpdateCounter.

private static void testUpdateCounter(boolean droppedCompactStorage) throws Throwable {
    try (Cluster cluster = Cluster.build(2).withConfig(c -> c.with(GOSSIP, NATIVE_PROTOCOL).set("drop_compact_storage_enabled", true)).start()) {
        cluster.schemaChange("CREATE KEYSPACE k WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 1}");
        String createTable = "CREATE TABLE k.t ( k int, c int, total counter, PRIMARY KEY (k, c))";
        if (droppedCompactStorage) {
            cluster.schemaChange(createTable + " WITH COMPACT STORAGE");
            cluster.schemaChange("ALTER TABLE k.t DROP COMPACT STORAGE");
        } else {
            cluster.schemaChange(createTable);
        }
        ConsistencyLevel cl = ConsistencyLevel.ONE;
        String select = "SELECT total FROM k.t WHERE k = 1 AND c = ?";
        for (int i = 1; i <= cluster.size(); i++) {
            ICoordinator coordinator = cluster.coordinator(i);
            coordinator.execute("UPDATE k.t SET total = total + 1 WHERE k = 1 AND c = ?", cl, i);
            assertRows(coordinator.execute(select, cl, i), row(1L));
            coordinator.execute("UPDATE k.t SET total = total - 4 WHERE k = 1 AND c = ?", cl, i);
            assertRows(coordinator.execute(select, cl, i), row(-3L));
        }
    }
}
Also used : AssertUtils.assertRows(org.apache.cassandra.distributed.shared.AssertUtils.assertRows) ICoordinator(org.apache.cassandra.distributed.api.ICoordinator) Test(org.junit.Test) Cluster(org.apache.cassandra.distributed.Cluster) ConsistencyLevel(org.apache.cassandra.distributed.api.ConsistencyLevel) AssertUtils.row(org.apache.cassandra.distributed.shared.AssertUtils.row) GOSSIP(org.apache.cassandra.distributed.api.Feature.GOSSIP) NATIVE_PROTOCOL(org.apache.cassandra.distributed.api.Feature.NATIVE_PROTOCOL) ConsistencyLevel(org.apache.cassandra.distributed.api.ConsistencyLevel) ICoordinator(org.apache.cassandra.distributed.api.ICoordinator) Cluster(org.apache.cassandra.distributed.Cluster)

Aggregations

ConsistencyLevel (org.apache.cassandra.distributed.api.ConsistencyLevel)2 ByteBuffer (java.nio.ByteBuffer)1 ArrayList (java.util.ArrayList)1 CQLStatement (org.apache.cassandra.cql3.CQLStatement)1 Cluster (org.apache.cassandra.distributed.Cluster)1 GOSSIP (org.apache.cassandra.distributed.api.Feature.GOSSIP)1 NATIVE_PROTOCOL (org.apache.cassandra.distributed.api.Feature.NATIVE_PROTOCOL)1 ICoordinator (org.apache.cassandra.distributed.api.ICoordinator)1 AssertUtils.assertRows (org.apache.cassandra.distributed.shared.AssertUtils.assertRows)1 AssertUtils.row (org.apache.cassandra.distributed.shared.AssertUtils.row)1 ClientState (org.apache.cassandra.service.ClientState)1 ResultMessage (org.apache.cassandra.transport.messages.ResultMessage)1 Test (org.junit.Test)1