org.apache.cassandra.db.ConsistencyLevel Java Examples

The following examples show how to use org.apache.cassandra.db.ConsistencyLevel. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TriggersTest.java    From stratio-cassandra with Apache License 2.0 6 votes vote down vote up
@Test
public void executeTriggerOnThriftCASOperation() throws Exception
{
    Cassandra.Client client = new Cassandra.Client(
            new TBinaryProtocol(
                    new TFramedTransportFactory().openTransport(
                            InetAddress.getLocalHost().getHostName(), 9170)));
    client.set_keyspace(ksName);
    client.cas(bytes(6),
               cfName,
               Collections.<Column>emptyList(),
               Collections.singletonList(getColumnForInsert("v1", 6)),
               org.apache.cassandra.thrift.ConsistencyLevel.LOCAL_SERIAL,
               org.apache.cassandra.thrift.ConsistencyLevel.ONE);

    assertUpdateIsAugmented(6);
}
 
Example #2
Source File: QueryOptions.java    From stratio-cassandra with Apache License 2.0 6 votes vote down vote up
private EnumSet<Flag> gatherFlags(QueryOptions options)
{
    EnumSet<Flag> flags = EnumSet.noneOf(Flag.class);
    if (options.getValues().size() > 0)
        flags.add(Flag.VALUES);
    if (options.skipMetadata())
        flags.add(Flag.SKIP_METADATA);
    if (options.getPageSize() >= 0)
        flags.add(Flag.PAGE_SIZE);
    if (options.getPagingState() != null)
        flags.add(Flag.PAGING_STATE);
    if (options.getSerialConsistency() != ConsistencyLevel.SERIAL)
        flags.add(Flag.SERIAL_CONSISTENCY);
    if (options.getSpecificOptions().timestamp != Long.MIN_VALUE)
        flags.add(Flag.TIMESTAMP);
    return flags;
}
 
Example #3
Source File: DatacenterSyncWriteResponseHandler.java    From stratio-cassandra with Apache License 2.0 6 votes vote down vote up
public DatacenterSyncWriteResponseHandler(Collection<InetAddress> naturalEndpoints,
                                          Collection<InetAddress> pendingEndpoints,
                                          ConsistencyLevel consistencyLevel,
                                          Keyspace keyspace,
                                          Runnable callback,
                                          WriteType writeType)
{
    // Response is been managed by the map so make it 1 for the superclass.
    super(keyspace, naturalEndpoints, pendingEndpoints, consistencyLevel, callback, writeType);
    assert consistencyLevel == ConsistencyLevel.EACH_QUORUM;

    strategy = (NetworkTopologyStrategy) keyspace.getReplicationStrategy();

    for (String dc : strategy.getDatacenters())
    {
        int rf = strategy.getReplicationFactor(dc);
        responses.put(dc, new AtomicInteger((rf / 2) + 1));
    }

    // During bootstrap, we have to include the pending endpoints or we may fail the consistency level
    // guarantees (see #833)
    for (InetAddress pending : pendingEndpoints)
    {
        responses.get(snitch.getDatacenter(pending)).incrementAndGet();
    }
}
 
Example #4
Source File: TriggersTest.java    From stratio-cassandra with Apache License 2.0 6 votes vote down vote up
@Test(expected=InvalidRequestException.class)
public void onThriftCASRejectGeneratedUpdatesForDifferentCF() throws Exception
{
    String cf = "cf" + System.nanoTime();
    try
    {
        setupTableWithTrigger(cf, CrossTableTrigger.class);
        Cassandra.Client client = new Cassandra.Client(
                new TBinaryProtocol(
                        new TFramedTransportFactory().openTransport(
                                InetAddress.getLocalHost().getHostName(), 9170)));
        client.set_keyspace(ksName);
        client.cas(bytes(10),
                   cf,
                   Collections.<Column>emptyList(),
                   Collections.singletonList(getColumnForInsert("v1", 10)),
                   org.apache.cassandra.thrift.ConsistencyLevel.LOCAL_SERIAL,
                   org.apache.cassandra.thrift.ConsistencyLevel.ONE);
    }
    finally
    {
        assertUpdateNotExecuted(cf, 10);
    }
}
 
Example #5
Source File: AbstractWriteResponseHandler.java    From stratio-cassandra with Apache License 2.0 6 votes vote down vote up
/**
 * @param callback A callback to be called when the write is successful.
 */
protected AbstractWriteResponseHandler(Keyspace keyspace,
                                       Collection<InetAddress> naturalEndpoints,
                                       Collection<InetAddress> pendingEndpoints,
                                       ConsistencyLevel consistencyLevel,
                                       Runnable callback,
                                       WriteType writeType)
{
    this.keyspace = keyspace;
    this.pendingEndpoints = pendingEndpoints;
    this.start = System.nanoTime();
    this.consistencyLevel = consistencyLevel;
    this.naturalEndpoints = naturalEndpoints;
    this.callback = callback;
    this.writeType = writeType;
}
 
Example #6
Source File: PasswordAuthenticator.java    From stratio-cassandra with Apache License 2.0 6 votes vote down vote up
private void setupDefaultUser()
{
    try
    {
        // insert the default superuser if AUTH_KS.CREDENTIALS_CF is empty.
        if (!hasExistingUsers())
        {
            process(String.format("INSERT INTO %s.%s (username, salted_hash) VALUES ('%s', '%s') USING TIMESTAMP 0",
                                  Auth.AUTH_KS,
                                  CREDENTIALS_CF,
                                  DEFAULT_USER_NAME,
                                  escape(hashpw(DEFAULT_USER_PASSWORD))),
                    ConsistencyLevel.ONE);
            logger.info("PasswordAuthenticator created default user '{}'", DEFAULT_USER_NAME);
        }
    }
    catch (RequestExecutionException e)
    {
        logger.warn("PasswordAuthenticator skipped default user setup: some nodes were not ready");
    }
}
 
Example #7
Source File: Auth.java    From stratio-cassandra with Apache License 2.0 6 votes vote down vote up
private static void setupDefaultSuperuser()
{
    try
    {
        // insert a default superuser if AUTH_KS.USERS_CF is empty.
        if (!hasExistingUsers())
        {
            QueryProcessor.process(String.format("INSERT INTO %s.%s (name, super) VALUES ('%s', %s) USING TIMESTAMP 0",
                                                 AUTH_KS,
                                                 USERS_CF,
                                                 DEFAULT_SUPERUSER_NAME,
                                                 true),
                                   ConsistencyLevel.ONE);
            logger.info("Created default superuser '{}'", DEFAULT_SUPERUSER_NAME);
        }
    }
    catch (RequestExecutionException e)
    {
        logger.warn("Skipped default superuser setup: some nodes were not ready");
    }
}
 
Example #8
Source File: SchemaStatement.java    From stratio-cassandra with Apache License 2.0 6 votes vote down vote up
public SchemaStatement(Timer timer, StressSettings settings, DataSpec spec,
                       PreparedStatement statement, Integer thriftId, ConsistencyLevel cl, ValidationType validationType)
{
    super(timer, settings, spec);
    this.statement = statement;
    this.thriftId = thriftId;
    this.cl = cl;
    this.validationType = validationType;
    argumentIndex = new int[statement.getVariables().size()];
    bindBuffer = new Object[argumentIndex.length];
    int i = 0;
    for (ColumnDefinitions.Definition definition : statement.getVariables())
        argumentIndex[i++] = spec.partitionGenerator.indexOf(definition.getName());

    statement.setConsistencyLevel(JavaDriverClient.from(cl));
}
 
Example #9
Source File: ClientOnlyExample.java    From stratio-cassandra with Apache License 2.0 6 votes vote down vote up
private static void testWriting() throws Exception
{
    // do some writing.
    for (int i = 0; i < 100; i++)
    {
        QueryProcessor.process(String.format("INSERT INTO %s.%s (id, name, value) VALUES ( 'key%d', 'colb', 'value%d')",
                                             KEYSPACE,
                                             COLUMN_FAMILY,
                                             i,
                                             i),
                               ConsistencyLevel.QUORUM);

        System.out.println("wrote key" + i);
    }
    System.out.println("Done writing.");
}
 
Example #10
Source File: SSTableAttachedSecondaryIndexTest.java    From sasi with Apache License 2.0 6 votes vote down vote up
private Set<String> executeCQL(String rawStatement) throws Exception
{
    SelectStatement statement = (SelectStatement) QueryProcessor.parseStatement(rawStatement).prepare().statement;
    ResultMessage.Rows cqlRows = statement.executeInternal(QueryState.forInternalCalls(), new QueryOptions(ConsistencyLevel.LOCAL_ONE, Collections.<ByteBuffer>emptyList()));

    Set<String> results = new TreeSet<>();
    for (CqlRow row : cqlRows.toThriftResult().getRows())
    {
        for (org.apache.cassandra.thrift.Column col : row.columns)
        {
            String columnName = UTF8Type.instance.getString(col.bufferForName());
            if (columnName.equals("key"))
                results.add(AsciiType.instance.getString(col.bufferForValue()));
        }
    }

    return results;
}
 
Example #11
Source File: CassandraEmbeddedStoreManager.java    From titan1withtp3.1 with Apache License 2.0 6 votes vote down vote up
private void mutate(List<org.apache.cassandra.db.Mutation> cmds, org.apache.cassandra.db.ConsistencyLevel clvl) throws BackendException {
    try {
        schedule(DatabaseDescriptor.getRpcTimeout());
        try {
            if (atomicBatch) {
                StorageProxy.mutateAtomically(cmds, clvl);
            } else {
                StorageProxy.mutate(cmds, clvl);
            }
        } catch (RequestExecutionException e) {
            throw new TemporaryBackendException(e);
        } finally {
            release();
        }
    } catch (TimeoutException ex) {
        log.debug("Cassandra TimeoutException", ex);
        throw new TemporaryBackendException(ex);
    }
}
 
Example #12
Source File: AbstractReadExecutor.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
public AlwaysSpeculatingReadExecutor(ColumnFamilyStore cfs,
                                     ReadCommand command,
                                     ConsistencyLevel consistencyLevel,
                                     List<InetAddress> targetReplicas)
{
    super(command, consistencyLevel, targetReplicas);
    this.cfs = cfs;
}
 
Example #13
Source File: Auth.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
private static ConsistencyLevel consistencyForUser(String username)
{
    if (username.equals(DEFAULT_SUPERUSER_NAME))
        return ConsistencyLevel.QUORUM;
    else
        return ConsistencyLevel.LOCAL_ONE;
}
 
Example #14
Source File: WriteCallbackInfo.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
public WriteCallbackInfo(InetAddress target,
                         IAsyncCallback callback,
                         MessageOut message,
                         IVersionedSerializer<?> serializer,
                         ConsistencyLevel consistencyLevel,
                         boolean allowHints)
{
    super(target, callback, serializer);
    assert message != null;
    this.sentMessage = message;
    this.consistencyLevel = consistencyLevel;
    this.allowHints = allowHints;
}
 
Example #15
Source File: WriteCallbackInfo.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
public boolean shouldHint()
{
    return allowHints
        && sentMessage.verb != MessagingService.Verb.COUNTER_MUTATION
        && consistencyLevel != ConsistencyLevel.ANY
        && StorageProxy.shouldHint(target);
}
 
Example #16
Source File: NonNativeTimestampTest.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
@Test
public void setServerTimestampForNonCqlNativeStatements() throws RequestValidationException, RequestExecutionException, CharacterCodingException, UnsupportedEncodingException
{
    String createKsCQL = "CREATE KEYSPACE non_native_ts_test" +
                         " WITH REPLICATION = { 'class' : 'SimpleStrategy', 'replication_factor' : 1 };";
    String createTableCQL = "CREATE TABLE non_native_ts_test.table_0 (k int PRIMARY KEY, v int)";
    String insertCQL = "INSERT INTO non_native_ts_test.table_0 (k, v) values (1, ?)";
    String selectCQL = "SELECT v, writetime(v) AS wt FROM non_native_ts_test.table_0 WHERE k = 1";

    QueryProcessor.instance.process(createKsCQL,
                                    QueryState.forInternalCalls(),
                                    QueryOptions.forInternalCalls(Collections.<ByteBuffer>emptyList()));
    QueryProcessor.instance.process(createTableCQL,
                                    QueryState.forInternalCalls(),
                                    QueryOptions.forInternalCalls(Collections.<ByteBuffer>emptyList()));
    QueryProcessor.instance.process(insertCQL,
                                    QueryState.forInternalCalls(),
                                    QueryOptions.forInternalCalls(ConsistencyLevel.ONE,
                                                                  Arrays.asList(ByteBufferUtil.bytes(2))));
    UntypedResultSet.Row row = QueryProcessor.instance.executeInternal(selectCQL).one();
    assertEquals(2, row.getInt("v"));
    long timestamp1 = row.getLong("wt");
    assertFalse(timestamp1 == -1l);

    // per CASSANDRA-8246 the two updates will have the same (incorrect)
    // timestamp, so reconcilliation is by value and the "older" update wins
    QueryProcessor.instance.process(insertCQL,
                                    QueryState.forInternalCalls(),
                                    QueryOptions.forInternalCalls(ConsistencyLevel.ONE,
                                                                  Arrays.asList(ByteBufferUtil.bytes(1))));
    row = QueryProcessor.executeInternal(selectCQL).one();
    assertEquals(1, row.getInt("v"));
    assertTrue(row.getLong("wt") > timestamp1);
}
 
Example #17
Source File: AntiEntropyServiceCounterTest.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
public List<IMutation> getWriteData()
{
    List<IMutation> rms = new LinkedList<IMutation>();
    Mutation rm = new Mutation(keyspaceName, ByteBufferUtil.bytes("key1"));
    rm.addCounter(cfname, CellNames.simpleDense(ByteBufferUtil.bytes("Column1")), 42);
    rms.add(new CounterMutation(rm, ConsistencyLevel.ONE));
    return rms;
}
 
Example #18
Source File: TriggersTest.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
@Test(expected=RuntimeException.class)
public void onCqlUpdateWithConditionsRejectGeneratedUpdatesForDifferentTable() throws Exception
{
    String cf = "cf" + System.nanoTime();
    try
    {
        setupTableWithTrigger(cf, CrossTableTrigger.class);
        String cql = String.format("INSERT INTO %s.%s (k, v1) VALUES (8, 8) IF NOT EXISTS", ksName, cf);
        QueryProcessor.process(cql, ConsistencyLevel.ONE);
    }
    finally
    {
        assertUpdateNotExecuted(cf, 7);
    }
}
 
Example #19
Source File: TriggersTest.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
@Before
public void setup() throws Exception
{
    StorageService.instance.initServer(0);
    if (thriftServer == null || ! thriftServer.isRunning())
    {
        thriftServer = new ThriftServer(InetAddress.getLocalHost(), 9170, 50);
        thriftServer.start();
    }

    String cql = String.format("CREATE KEYSPACE IF NOT EXISTS %s " +
                               "WITH REPLICATION = {'class': 'SimpleStrategy', 'replication_factor': 1}",
                               ksName);
    QueryProcessor.process(cql, ConsistencyLevel.ONE);

    cql = String.format("CREATE TABLE IF NOT EXISTS %s.%s (k int, v1 int, v2 int, PRIMARY KEY (k))", ksName, cfName);
    QueryProcessor.process(cql, ConsistencyLevel.ONE);

    cql = String.format("CREATE TABLE IF NOT EXISTS %s.%s (k int, v1 int, v2 int, PRIMARY KEY (k))", ksName, otherCf);
    QueryProcessor.process(cql, ConsistencyLevel.ONE);

    // no conditional execution of create trigger stmt yet
    if (! triggerCreated)
    {
        cql = String.format("CREATE TRIGGER trigger_1 ON %s.%s USING '%s'",
                            ksName, cfName, TestTrigger.class.getName());
        QueryProcessor.process(cql, ConsistencyLevel.ONE);
        triggerCreated = true;
    }
}
 
Example #20
Source File: ExecuteMessage.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
public ExecuteMessage decode(ByteBuf body, int version)
{
    byte[] id = CBUtil.readBytes(body);
    if (version == 1)
    {
        List<ByteBuffer> values = CBUtil.readValueList(body);
        ConsistencyLevel consistency = CBUtil.readConsistencyLevel(body);
        return new ExecuteMessage(MD5Digest.wrap(id), QueryOptions.fromProtocolV1(consistency, values));
    }
    else
    {
        return new ExecuteMessage(MD5Digest.wrap(id), QueryOptions.codec.decode(body, version));
    }
}
 
Example #21
Source File: TriggersTest.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
@Test
public void executeTriggerOnCqlBatchInsert() throws Exception
{
    String cql = String.format("BEGIN BATCH " +
                               "    INSERT INTO %s.%s (k, v1) VALUES (1, 1); " +
                               "APPLY BATCH",
                               ksName, cfName);
    QueryProcessor.process(cql, ConsistencyLevel.ONE);
    assertUpdateIsAugmented(1);
}
 
Example #22
Source File: AbstractReadExecutor.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
public SpeculatingReadExecutor(ColumnFamilyStore cfs,
                               ReadCommand command,
                               ConsistencyLevel consistencyLevel,
                               List<InetAddress> targetReplicas)
{
    super(command, consistencyLevel, targetReplicas);
    this.cfs = cfs;
}
 
Example #23
Source File: PrepareCallback.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
public PrepareCallback(ByteBuffer key, CFMetaData metadata, int targets, ConsistencyLevel consistency)
{
    super(targets, consistency);
    // need to inject the right key in the empty commit so comparing with empty commits in the reply works as expected
    mostRecentCommit = Commit.emptyCommit(key, metadata);
    mostRecentInProgressCommit = Commit.emptyCommit(key, metadata);
    mostRecentInProgressCommitWithUpdate = Commit.emptyCommit(key, metadata);
}
 
Example #24
Source File: DatacenterWriteResponseHandler.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
public DatacenterWriteResponseHandler(Collection<InetAddress> naturalEndpoints,
                                      Collection<InetAddress> pendingEndpoints,
                                      ConsistencyLevel consistencyLevel,
                                      Keyspace keyspace,
                                      Runnable callback,
                                      WriteType writeType)
{
    super(naturalEndpoints, pendingEndpoints, consistencyLevel, keyspace, callback, writeType);
    assert consistencyLevel.isDatacenterLocal();
}
 
Example #25
Source File: TriggersTest.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
@Test
public void executeTriggerOnCqlInsertWithConditions() throws Exception
{
    String cql = String.format("INSERT INTO %s.%s (k, v1) VALUES (4, 4) IF NOT EXISTS", ksName, cfName);
    QueryProcessor.process(cql, ConsistencyLevel.ONE);
    assertUpdateIsAugmented(4);
}
 
Example #26
Source File: UnavailableException.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
public UnavailableException(String msg, ConsistencyLevel consistency, int required, int alive)
{
    super(ExceptionCode.UNAVAILABLE, msg);
    this.consistency = consistency;
    this.required = required;
    this.alive = alive;
}
 
Example #27
Source File: PasswordAuthenticator.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
private static ConsistencyLevel consistencyForUser(String username)
{
    if (username.equals(DEFAULT_USER_NAME))
        return ConsistencyLevel.QUORUM;
    else
        return ConsistencyLevel.LOCAL_ONE;
}
 
Example #28
Source File: PasswordAuthenticator.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
private static boolean hasExistingUsers() throws RequestExecutionException
{
    // Try looking up the 'cassandra' default user first, to avoid the range query if possible.
    String defaultSUQuery = String.format("SELECT * FROM %s.%s WHERE username = '%s'", Auth.AUTH_KS, CREDENTIALS_CF, DEFAULT_USER_NAME);
    String allUsersQuery = String.format("SELECT * FROM %s.%s LIMIT 1", Auth.AUTH_KS, CREDENTIALS_CF);
    return !process(defaultSUQuery, ConsistencyLevel.ONE).isEmpty()
        || !process(defaultSUQuery, ConsistencyLevel.QUORUM).isEmpty()
        || !process(allUsersQuery, ConsistencyLevel.QUORUM).isEmpty();
}
 
Example #29
Source File: Auth.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
private static boolean hasExistingUsers() throws RequestExecutionException
{
    // Try looking up the 'cassandra' default super user first, to avoid the range query if possible.
    String defaultSUQuery = String.format("SELECT * FROM %s.%s WHERE name = '%s'", AUTH_KS, USERS_CF, DEFAULT_SUPERUSER_NAME);
    String allUsersQuery = String.format("SELECT * FROM %s.%s LIMIT 1", AUTH_KS, USERS_CF);
    return !QueryProcessor.process(defaultSUQuery, ConsistencyLevel.ONE).isEmpty()
        || !QueryProcessor.process(defaultSUQuery, ConsistencyLevel.QUORUM).isEmpty()
        || !QueryProcessor.process(allUsersQuery, ConsistencyLevel.QUORUM).isEmpty();
}
 
Example #30
Source File: RequestTimeoutException.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
protected RequestTimeoutException(ExceptionCode code, ConsistencyLevel consistency, int received, int blockFor)
{
    super(code, String.format("Operation timed out - received only %d responses.", received));
    this.consistency = consistency;
    this.received = received;
    this.blockFor = blockFor;
}