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);
}
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);
}
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);
}
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);
}
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;
}
Aggregations