Search in sources :

Example 1 with BatchStatement

use of com.datastax.driver.core.BatchStatement in project ignite by apache.

the class CassandraSessionImpl method execute.

/**
 * {@inheritDoc}
 */
@Override
public void execute(List<Mutation> mutations) {
    if (mutations == null || mutations.isEmpty())
        return;
    Throwable error = null;
    String errorMsg = "Failed to apply " + mutations.size() + " mutations performed withing Ignite " + "transaction into Cassandra";
    int attempt = 0;
    boolean tableExistenceRequired = false;
    Map<String, PreparedStatement> statements = new HashMap<>();
    Map<String, KeyValuePersistenceSettings> tableSettings = new HashMap<>();
    RandomSleeper sleeper = newSleeper();
    incrementSessionRefs();
    try {
        while (attempt < CQL_EXECUTION_ATTEMPTS_COUNT) {
            error = null;
            if (attempt != 0) {
                log.warning("Trying " + (attempt + 1) + " attempt to apply " + mutations.size() + " mutations " + "performed withing Ignite transaction into Cassandra");
            }
            try {
                BatchStatement batch = new BatchStatement();
                // accumulating all the mutations into one Cassandra logged batch
                for (Mutation mutation : mutations) {
                    String key = mutation.getTable() + mutation.getClass().getName();
                    PreparedStatement st = statements.get(key);
                    if (st == null) {
                        st = prepareStatement(mutation.getTable(), mutation.getStatement(), mutation.getPersistenceSettings(), mutation.tableExistenceRequired());
                        if (st != null)
                            statements.put(key, st);
                    }
                    if (st != null)
                        batch.add(mutation.bindStatement(st));
                    if (attempt == 0) {
                        if (mutation.tableExistenceRequired()) {
                            tableExistenceRequired = true;
                            if (!tableSettings.containsKey(mutation.getTable()))
                                tableSettings.put(mutation.getTable(), mutation.getPersistenceSettings());
                        }
                    }
                }
                // committing logged batch into Cassandra
                if (batch.size() > 0)
                    session().execute(tuneStatementExecutionOptions(batch));
                return;
            } catch (Throwable e) {
                error = e;
                if (CassandraHelper.isTableAbsenceError(e)) {
                    if (tableExistenceRequired) {
                        for (Map.Entry<String, KeyValuePersistenceSettings> entry : tableSettings.entrySet()) handleTableAbsenceError(entry.getKey(), entry.getValue());
                    } else
                        return;
                } else if (CassandraHelper.isHostsAvailabilityError(e)) {
                    if (handleHostsAvailabilityError(e, attempt, errorMsg))
                        statements.clear();
                } else if (CassandraHelper.isPreparedStatementClusterError(e)) {
                    handlePreparedStatementClusterError(e);
                    statements.clear();
                } else {
                    // For an error which we don't know how to handle, we will not try next attempts and terminate.
                    throw new IgniteException(errorMsg, e);
                }
            }
            if (!CassandraHelper.isTableAbsenceError(error))
                sleeper.sleep();
            attempt++;
        }
    } catch (Throwable e) {
        error = e;
    } finally {
        decrementSessionRefs();
    }
    log.error(errorMsg, error);
    throw new IgniteException(errorMsg, error);
}
Also used : HashMap(java.util.HashMap) PreparedStatement(com.datastax.driver.core.PreparedStatement) KeyValuePersistenceSettings(org.apache.ignite.cache.store.cassandra.persistence.KeyValuePersistenceSettings) IgniteException(org.apache.ignite.IgniteException) BatchStatement(com.datastax.driver.core.BatchStatement) RandomSleeper(org.apache.ignite.cache.store.cassandra.common.RandomSleeper) Mutation(org.apache.ignite.cache.store.cassandra.session.transaction.Mutation)

Example 2 with BatchStatement

use of com.datastax.driver.core.BatchStatement in project cassandra by apache.

the class AuditLoggerTest method testCqlBatchAuditing.

@Test
public void testCqlBatchAuditing() throws Throwable {
    createTable("CREATE TABLE %s (id int primary key, v1 text, v2 text)");
    Session session = sessionNet();
    BatchStatement batchStatement = new BatchStatement();
    String cqlInsert = "INSERT INTO " + KEYSPACE + "." + currentTable() + " (id, v1, v2) VALUES (?, ?, ?)";
    PreparedStatement prep = session.prepare(cqlInsert);
    AuditLogEntry logEntry = ((InMemoryAuditLogger) AuditLogManager.instance.getLogger()).inMemQueue.poll();
    assertLogEntry(cqlInsert, AuditLogEntryType.PREPARE_STATEMENT, logEntry, false);
    batchStatement.add(prep.bind(1, "Apapche", "Cassandra"));
    batchStatement.add(prep.bind(2, "Apapche1", "Cassandra1"));
    String cqlUpdate = "UPDATE " + KEYSPACE + "." + currentTable() + " SET v1 = ? WHERE id = ?";
    prep = session.prepare(cqlUpdate);
    logEntry = ((InMemoryAuditLogger) AuditLogManager.instance.getLogger()).inMemQueue.poll();
    assertLogEntry(cqlUpdate, AuditLogEntryType.PREPARE_STATEMENT, logEntry, false);
    batchStatement.add(prep.bind("Apache Cassandra", 1));
    String cqlDelete = "DELETE FROM " + KEYSPACE + "." + currentTable() + " WHERE id = ?";
    prep = session.prepare(cqlDelete);
    logEntry = ((InMemoryAuditLogger) AuditLogManager.instance.getLogger()).inMemQueue.poll();
    assertLogEntry(cqlDelete, AuditLogEntryType.PREPARE_STATEMENT, logEntry, false);
    batchStatement.add(prep.bind(1));
    ResultSet rs = session.execute(batchStatement);
    assertEquals(5, ((InMemoryAuditLogger) AuditLogManager.instance.getLogger()).inMemQueue.size());
    logEntry = ((InMemoryAuditLogger) AuditLogManager.instance.getLogger()).inMemQueue.poll();
    assertEquals(AuditLogEntryType.BATCH, logEntry.getType());
    assertTrue(logEntry.getOperation().contains("BatchId"));
    assertNotEquals(0, logEntry.getTimestamp());
    logEntry = ((InMemoryAuditLogger) AuditLogManager.instance.getLogger()).inMemQueue.poll();
    assertLogEntry(cqlInsert, AuditLogEntryType.UPDATE, logEntry, false);
    logEntry = ((InMemoryAuditLogger) AuditLogManager.instance.getLogger()).inMemQueue.poll();
    assertLogEntry(cqlInsert, AuditLogEntryType.UPDATE, logEntry, false);
    logEntry = ((InMemoryAuditLogger) AuditLogManager.instance.getLogger()).inMemQueue.poll();
    assertLogEntry(cqlUpdate, AuditLogEntryType.UPDATE, logEntry, false);
    logEntry = ((InMemoryAuditLogger) AuditLogManager.instance.getLogger()).inMemQueue.poll();
    assertLogEntry(cqlDelete, AuditLogEntryType.DELETE, logEntry, false);
    int size = rs.all().size();
    assertEquals(0, size);
}
Also used : BatchStatement(com.datastax.driver.core.BatchStatement) ResultSet(com.datastax.driver.core.ResultSet) PreparedStatement(com.datastax.driver.core.PreparedStatement) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Session(com.datastax.driver.core.Session) Test(org.junit.Test)

Example 3 with BatchStatement

use of com.datastax.driver.core.BatchStatement in project cassandra by apache.

the class BatchMetricsTest method executeLoggerBatch.

private void executeLoggerBatch(BatchStatement.Type batchStatementType, int distinctPartitions, int statementsPerPartition) {
    BatchStatement batch = new BatchStatement(batchStatementType);
    for (int i = 0; i < distinctPartitions; i++) {
        for (int j = 0; j < statementsPerPartition; j++) {
            if (batchStatementType == BatchStatement.Type.UNLOGGED || batchStatementType == BatchStatement.Type.LOGGED)
                batch.add(psLogger.bind(i, "aaaaaaaa"));
            else if (batchStatementType == BatchStatement.Type.COUNTER)
                batch.add(psCounter.bind(i));
            else
                throw new IllegalStateException("There is no a case for BatchStatement.Type." + batchStatementType.name());
        }
    }
    session.execute(batch);
}
Also used : BatchStatement(com.datastax.driver.core.BatchStatement)

Example 4 with BatchStatement

use of com.datastax.driver.core.BatchStatement in project cassandra by apache.

the class ClientRequestMetricsTest method executeBatch.

private void executeBatch(int distinctPartitions, int numClusteringKeys) {
    BatchStatement batch = new BatchStatement();
    for (int i = 0; i < distinctPartitions; i++) {
        for (int y = 0; y < numClusteringKeys; y++) {
            batch.add(writePS.bind(i, y, "aaaaaaaa"));
        }
    }
    session.execute(batch);
}
Also used : BatchStatement(com.datastax.driver.core.BatchStatement)

Example 5 with BatchStatement

use of com.datastax.driver.core.BatchStatement in project aroma-data-operations by RedRoma.

the class CassandraUserRepository method createQueryToDeleteUser.

private Statement createQueryToDeleteUser(User user) {
    UUID userUuuid = UUID.fromString(user.userId);
    BatchStatement batch = new BatchStatement();
    Statement deleteFromUsersTable = QueryBuilder.delete().all().from(Users.TABLE_NAME).where(eq(Users.USER_ID, userUuuid));
    batch.add(deleteFromUsersTable);
    Statement deleteFromRecentUsersTable = QueryBuilder.delete().all().from(Users.TABLE_NAME_RECENT).where(eq(Users.USER_ID, userUuuid));
    batch.add(deleteFromRecentUsersTable);
    Statement deleteFromUserEmailsTable = QueryBuilder.delete().all().from(Users.TABLE_NAME_BY_EMAIL).where(eq(Users.EMAIL, user.email));
    batch.add(deleteFromUserEmailsTable);
    if (!isNullOrEmpty(user.githubProfile)) {
        Statement deleteFromGithubTable = QueryBuilder.delete().all().from(Users.TABLE_NAME_BY_GITHUB_PROFILE).where(eq(Users.GITHUB_PROFILE, user.githubProfile));
        batch.add(deleteFromGithubTable);
    }
    return batch;
}
Also used : BatchStatement(com.datastax.driver.core.BatchStatement) Statement(com.datastax.driver.core.Statement) BatchStatement(com.datastax.driver.core.BatchStatement) StringAssertions.validUUID(tech.sirwellington.alchemy.arguments.assertions.StringAssertions.validUUID) UUID(java.util.UUID)

Aggregations

BatchStatement (com.datastax.driver.core.BatchStatement)22 Statement (com.datastax.driver.core.Statement)11 UUID (java.util.UUID)7 PreparedStatement (com.datastax.driver.core.PreparedStatement)5 Session (com.datastax.driver.core.Session)4 Test (org.junit.Test)4 ResultSet (com.datastax.driver.core.ResultSet)3 StringAssertions.validUUID (tech.sirwellington.alchemy.arguments.assertions.StringAssertions.validUUID)3 BoundStatement (com.datastax.driver.core.BoundStatement)2 StringAssertions.nonEmptyString (tech.sirwellington.alchemy.arguments.assertions.StringAssertions.nonEmptyString)2 Type (com.datastax.driver.core.BatchStatement.Type)1 Cluster (com.datastax.driver.core.Cluster)1 Builder (com.datastax.driver.core.Cluster.Builder)1 ConsistencyLevel (com.datastax.driver.core.ConsistencyLevel)1 HostDistance (com.datastax.driver.core.HostDistance)1 JdkSSLOptions (com.datastax.driver.core.JdkSSLOptions)1 KeyspaceMetadata (com.datastax.driver.core.KeyspaceMetadata)1 PoolingOptions (com.datastax.driver.core.PoolingOptions)1 ProtocolVersion (com.datastax.driver.core.ProtocolVersion)1 SimpleStatement (com.datastax.driver.core.SimpleStatement)1