Search in sources :

Example 6 with BatchStatement

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

the class CassandraApplicationRepository method createDeleteStatementFor.

private Statement createDeleteStatementFor(Application app) {
    BatchStatement batch = new BatchStatement();
    UUID appId = UUID.fromString(app.applicationId);
    Statement deleteFromMainTable = QueryBuilder.delete().all().from(TABLE_NAME).where(eq(APP_ID, appId));
    batch.add(deleteFromMainTable);
    Statement deleteFromRecentsTable = QueryBuilder.delete().all().from(TABLE_NAME_RECENTLY_CREATED).where(eq(APP_ID, appId));
    batch.add(deleteFromRecentsTable);
    return batch;
}
Also used : BatchStatement(com.datastax.driver.core.BatchStatement) Statement(com.datastax.driver.core.Statement) BatchStatement(com.datastax.driver.core.BatchStatement) UUID(java.util.UUID)

Example 7 with BatchStatement

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

the class BatchMetricsTest method executeBatch.

private void executeBatch(boolean isLogged, int distinctPartitions, int statementsPerPartition) {
    BatchStatement.Type batchType;
    if (isLogged) {
        batchType = BatchStatement.Type.LOGGED;
    } else {
        batchType = BatchStatement.Type.UNLOGGED;
    }
    BatchStatement batch = new BatchStatement(batchType);
    for (int i = 0; i < distinctPartitions; i++) {
        for (int j = 0; j < statementsPerPartition; j++) {
            batch.add(ps.bind(i, "aaaaaaaa"));
        }
    }
    session.execute(batch);
}
Also used : BatchStatement(com.datastax.driver.core.BatchStatement)

Example 8 with BatchStatement

use of com.datastax.driver.core.BatchStatement in project janusgraph by JanusGraph.

the class CQLStoreManager method mutateManyLogged.

// Use a single logged batch
private void mutateManyLogged(final Map<String, Map<StaticBuffer, KCVMutation>> mutations, final StoreTransaction txh) throws BackendException {
    final MaskedTimestamp commitTime = new MaskedTimestamp(txh);
    final BatchStatement batchStatement = new BatchStatement(Type.LOGGED);
    batchStatement.setConsistencyLevel(getTransaction(txh).getWriteConsistencyLevel());
    batchStatement.addAll(Iterator.ofAll(mutations.entrySet()).flatMap(tableNameAndMutations -> {
        final String tableName = tableNameAndMutations.getKey();
        final Map<StaticBuffer, KCVMutation> tableMutations = tableNameAndMutations.getValue();
        final CQLKeyColumnValueStore columnValueStore = Option.of(this.openStores.get(tableName)).getOrElseThrow(() -> new IllegalStateException("Store cannot be found: " + tableName));
        return Iterator.ofAll(tableMutations.entrySet()).flatMap(keyAndMutations -> {
            final StaticBuffer key = keyAndMutations.getKey();
            final KCVMutation keyMutations = keyAndMutations.getValue();
            final Iterator<Statement> deletions = Iterator.of(commitTime.getDeletionTime(this.times)).flatMap(deleteTime -> Iterator.ofAll(keyMutations.getDeletions()).map(deletion -> columnValueStore.deleteColumn(key, deletion, deleteTime)));
            final Iterator<Statement> additions = Iterator.of(commitTime.getAdditionTime(this.times)).flatMap(addTime -> Iterator.ofAll(keyMutations.getAdditions()).map(addition -> columnValueStore.insertColumn(key, addition, addTime)));
            return Iterator.concat(deletions, additions);
        });
    }));
    final Future<ResultSet> result = Future.fromJavaFuture(this.executorService, this.session.executeAsync(batchStatement));
    result.await();
    if (result.isFailure()) {
        throw EXCEPTION_MAPPER.apply(result.getCause().get());
    }
    sleepAfterWrite(txh, commitTime);
}
Also used : Match(io.vavr.API.Match) PoolingOptions(com.datastax.driver.core.PoolingOptions) GRAPH_NAME(org.janusgraph.graphdb.configuration.GraphDatabaseConfiguration.GRAPH_NAME) SSLContext(javax.net.ssl.SSLContext) REMOTE_MAX_REQUESTS_PER_CONNECTION(org.janusgraph.diskstorage.cql.CQLConfigOptions.REMOTE_MAX_REQUESTS_PER_CONNECTION) TrustManager(javax.net.ssl.TrustManager) KeyStoreException(java.security.KeyStoreException) REMOTE_CORE_CONNECTIONS_PER_HOST(org.janusgraph.diskstorage.cql.CQLConfigOptions.REMOTE_CORE_CONNECTIONS_PER_HOST) StandardStoreFeatures(org.janusgraph.diskstorage.keycolumnvalue.StandardStoreFeatures) REMOTE_MAX_CONNECTIONS_PER_HOST(org.janusgraph.diskstorage.cql.CQLConfigOptions.REMOTE_MAX_CONNECTIONS_PER_HOST) Option(io.vavr.control.Option) Map(java.util.Map) Session(com.datastax.driver.core.Session) TokenAwarePolicy(com.datastax.driver.core.policies.TokenAwarePolicy) StaticBuffer(org.janusgraph.diskstorage.StaticBuffer) SSL_TRUSTSTORE_LOCATION(org.janusgraph.diskstorage.cql.CQLConfigOptions.SSL_TRUSTSTORE_LOCATION) Iterator(io.vavr.collection.Iterator) SchemaBuilder.dropKeyspace(com.datastax.driver.core.schemabuilder.SchemaBuilder.dropKeyspace) BatchStatement(com.datastax.driver.core.BatchStatement) LOCAL_MAX_CONNECTIONS_PER_HOST(org.janusgraph.diskstorage.cql.CQLConfigOptions.LOCAL_MAX_CONNECTIONS_PER_HOST) KEYSPACE(org.janusgraph.diskstorage.cql.CQLConfigOptions.KEYSPACE) KCVMutation(org.janusgraph.diskstorage.keycolumnvalue.KCVMutation) NetworkUtil(org.janusgraph.util.system.NetworkUtil) Tuple(io.vavr.Tuple) TrustManagerFactory(javax.net.ssl.TrustManagerFactory) READ_CONSISTENCY(org.janusgraph.diskstorage.cql.CQLConfigOptions.READ_CONSISTENCY) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Array(io.vavr.collection.Array) Case(io.vavr.API.Case) KeyStore(java.security.KeyStore) KeyManagementException(java.security.KeyManagementException) InetSocketAddress(java.net.InetSocketAddress) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) StoreFeatures(org.janusgraph.diskstorage.keycolumnvalue.StoreFeatures) GraphDatabaseConfiguration.buildGraphConfiguration(org.janusgraph.graphdb.configuration.GraphDatabaseConfiguration.buildGraphConfiguration) REPLICATION_FACTOR(org.janusgraph.diskstorage.cql.CQLConfigOptions.REPLICATION_FACTOR) KeyColumnValueStore(org.janusgraph.diskstorage.keycolumnvalue.KeyColumnValueStore) Builder(com.datastax.driver.core.Cluster.Builder) ProtocolVersion(com.datastax.driver.core.ProtocolVersion) List(java.util.List) METRICS_SYSTEM_PREFIX_DEFAULT(org.janusgraph.graphdb.configuration.GraphDatabaseConfiguration.METRICS_SYSTEM_PREFIX_DEFAULT) DROP_ON_CLEAR(org.janusgraph.graphdb.configuration.GraphDatabaseConfiguration.DROP_ON_CLEAR) Cluster(com.datastax.driver.core.Cluster) ATOMIC_BATCH_MUTATE(org.janusgraph.diskstorage.cql.CQLConfigOptions.ATOMIC_BATCH_MUTATE) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) HostDistance(com.datastax.driver.core.HostDistance) SSL_TRUSTSTORE_PASSWORD(org.janusgraph.diskstorage.cql.CQLConfigOptions.SSL_TRUSTSTORE_PASSWORD) AUTH_PASSWORD(org.janusgraph.graphdb.configuration.GraphDatabaseConfiguration.AUTH_PASSWORD) Statement(com.datastax.driver.core.Statement) EXCEPTION_MAPPER(org.janusgraph.diskstorage.cql.CQLKeyColumnValueStore.EXCEPTION_MAPPER) KeyRange(org.janusgraph.diskstorage.keycolumnvalue.KeyRange) ThreadFactoryBuilder(com.google.common.util.concurrent.ThreadFactoryBuilder) CLUSTER_NAME(org.janusgraph.diskstorage.cql.CQLConfigOptions.CLUSTER_NAME) DistributedStoreManager(org.janusgraph.diskstorage.common.DistributedStoreManager) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) BATCH_STATEMENT_SIZE(org.janusgraph.diskstorage.cql.CQLConfigOptions.BATCH_STATEMENT_SIZE) METRICS_PREFIX(org.janusgraph.graphdb.configuration.GraphDatabaseConfiguration.METRICS_PREFIX) LOCAL_CORE_CONNECTIONS_PER_HOST(org.janusgraph.diskstorage.cql.CQLConfigOptions.LOCAL_CORE_CONNECTIONS_PER_HOST) REPLICATION_OPTIONS(org.janusgraph.diskstorage.cql.CQLConfigOptions.REPLICATION_OPTIONS) ONLY_USE_LOCAL_CONSISTENCY_FOR_SYSTEM_OPERATIONS(org.janusgraph.diskstorage.cql.CQLConfigOptions.ONLY_USE_LOCAL_CONSISTENCY_FOR_SYSTEM_OPERATIONS) ResultSet(com.datastax.driver.core.ResultSet) CQLTransaction.getTransaction(org.janusgraph.diskstorage.cql.CQLTransaction.getTransaction) Type(com.datastax.driver.core.BatchStatement.Type) WRITE_CONSISTENCY(org.janusgraph.diskstorage.cql.CQLConfigOptions.WRITE_CONSISTENCY) Future(io.vavr.concurrent.Future) SchemaBuilder.createKeyspace(com.datastax.driver.core.schemabuilder.SchemaBuilder.createKeyspace) AUTH_USERNAME(org.janusgraph.graphdb.configuration.GraphDatabaseConfiguration.AUTH_USERNAME) Container(org.janusgraph.diskstorage.StoreMetaData.Container) LOCAL_MAX_REQUESTS_PER_CONNECTION(org.janusgraph.diskstorage.cql.CQLConfigOptions.LOCAL_MAX_REQUESTS_PER_CONNECTION) KeyColumnValueStoreManager(org.janusgraph.diskstorage.keycolumnvalue.KeyColumnValueStoreManager) StoreTransaction(org.janusgraph.diskstorage.keycolumnvalue.StoreTransaction) ExecutorService(java.util.concurrent.ExecutorService) API.$(io.vavr.API.$) SSL_ENABLED(org.janusgraph.diskstorage.cql.CQLConfigOptions.SSL_ENABLED) BackendException(org.janusgraph.diskstorage.BackendException) Configuration(org.janusgraph.diskstorage.configuration.Configuration) HashMap(io.vavr.collection.HashMap) LOCAL_DATACENTER(org.janusgraph.diskstorage.cql.CQLConfigOptions.LOCAL_DATACENTER) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) CertificateException(java.security.cert.CertificateException) REPLICATION_STRATEGY(org.janusgraph.diskstorage.cql.CQLConfigOptions.REPLICATION_STRATEGY) PROTOCOL_VERSION(org.janusgraph.diskstorage.cql.CQLConfigOptions.PROTOCOL_VERSION) TimeUnit(java.util.concurrent.TimeUnit) JdkSSLOptions(com.datastax.driver.core.JdkSSLOptions) KeyspaceMetadata(com.datastax.driver.core.KeyspaceMetadata) BaseTransactionConfig(org.janusgraph.diskstorage.BaseTransactionConfig) QueryBuilder.truncate(com.datastax.driver.core.querybuilder.QueryBuilder.truncate) Seq(io.vavr.collection.Seq) PermanentBackendException(org.janusgraph.diskstorage.PermanentBackendException) DCAwareRoundRobinPolicy(com.datastax.driver.core.policies.DCAwareRoundRobinPolicy) BatchStatement(com.datastax.driver.core.BatchStatement) Iterator(io.vavr.collection.Iterator) ResultSet(com.datastax.driver.core.ResultSet) StaticBuffer(org.janusgraph.diskstorage.StaticBuffer) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(io.vavr.collection.HashMap) KCVMutation(org.janusgraph.diskstorage.keycolumnvalue.KCVMutation)

Example 9 with BatchStatement

use of com.datastax.driver.core.BatchStatement in project atlasdb by palantir.

the class CqlKeyValueService method getPutPartitionResultSetFuture.

protected ResultSetFuture getPutPartitionResultSetFuture(TableReference tableRef, List<Entry<Cell, Value>> partition, TransactionType transactionType, int ttl) {
    PreparedStatement preparedStatement = getPreparedStatement(tableRef, getPutQueryForPossibleTransaction(tableRef, transactionType, ttl), session);
    preparedStatement.setConsistencyLevel(writeConsistency);
    // Be mindful when using the atomicity semantics of UNLOGGED batch statements.
    // This usage should be okay, as the KVS.multiPut explicitly does not guarantee
    // atomicity across cells (nor batch isolation, which we also cannot provide)
    BatchStatement batchStatement = new BatchStatement(BatchStatement.Type.UNLOGGED);
    if (shouldTraceQuery(tableRef)) {
        batchStatement.enableTracing();
    }
    for (Entry<Cell, Value> e : partition) {
        BoundStatement boundStatement = preparedStatement.bind();
        boundStatement.setBytes(fieldNameProvider.row(), ByteBuffer.wrap(e.getKey().getRowName()));
        boundStatement.setBytes(fieldNameProvider.column(), ByteBuffer.wrap(e.getKey().getColumnName()));
        boundStatement.setLong(fieldNameProvider.timestamp(), ~e.getValue().getTimestamp());
        boundStatement.setBytes(fieldNameProvider.value(), ByteBuffer.wrap(e.getValue().getContents()));
        if (partition.size() > 1) {
            batchStatement.add(boundStatement);
        } else {
            return session.executeAsync(boundStatement);
        }
    }
    return session.executeAsync(batchStatement);
}
Also used : BatchStatement(com.datastax.driver.core.BatchStatement) Value(com.palantir.atlasdb.keyvalue.api.Value) PreparedStatement(com.datastax.driver.core.PreparedStatement) Cell(com.palantir.atlasdb.keyvalue.api.Cell) BoundStatement(com.datastax.driver.core.BoundStatement)

Example 10 with BatchStatement

use of com.datastax.driver.core.BatchStatement in project java-driver by datastax.

the class DowngradingRetry method write.

/**
 * Inserts data, retrying if necessary with a downgraded CL.
 *
 * @param cl         the consistency level to apply.
 * @param retryCount the current retry count.
 * @throws DriverException if the current consistency level cannot be downgraded.
 */
private void write(ConsistencyLevel cl, int retryCount) {
    System.out.printf("Writing at %s (retry count: %d)%n", cl, retryCount);
    BatchStatement batch = new BatchStatement(UNLOGGED);
    batch.add(new SimpleStatement("INSERT INTO downgrading.sensor_data " + "(sensor_id, date, timestamp, value) " + "VALUES (" + "756716f7-2e54-4715-9f00-91dcbea6cf50," + "'2018-02-26'," + "'2018-02-26T13:53:46.345+01:00'," + "2.34)"));
    batch.add(new SimpleStatement("INSERT INTO downgrading.sensor_data " + "(sensor_id, date, timestamp, value) " + "VALUES (" + "756716f7-2e54-4715-9f00-91dcbea6cf50," + "'2018-02-26'," + "'2018-02-26T13:54:27.488+01:00'," + "2.47)"));
    batch.add(new SimpleStatement("INSERT INTO downgrading.sensor_data " + "(sensor_id, date, timestamp, value) " + "VALUES (" + "756716f7-2e54-4715-9f00-91dcbea6cf50," + "'2018-02-26'," + "'2018-02-26T13:56:33.739+01:00'," + "2.52)"));
    batch.setConsistencyLevel(cl);
    try {
        session.execute(batch);
        System.out.println("Write succeeded at " + cl);
    } catch (DriverException e) {
        if (retryCount == maxRetries) {
            throw e;
        }
        e = unwrapNoHostAvailableException(e);
        System.out.println("Write failed: " + e);
        if (e instanceof UnavailableException) {
            // With an UnavailableException, we know that the write wasn't even attempted.
            // Downgrade to the number of replicas reported alive and retry.
            int aliveReplicas = ((UnavailableException) e).getAliveReplicas();
            ConsistencyLevel downgraded = downgrade(cl, aliveReplicas, e);
            write(downgraded, retryCount + 1);
        } else if (e instanceof WriteTimeoutException) {
            WriteType writeType = ((WriteTimeoutException) e).getWriteType();
            int acknowledgements = ((WriteTimeoutException) e).getReceivedAcknowledgements();
            switch(writeType) {
                case SIMPLE:
                case BATCH:
                    // a retry would ever succeed.
                    if (acknowledgements == 0) {
                        throw e;
                    }
                    break;
                case UNLOGGED_BATCH:
                    // For unlogged batches, the write might have been persisted only partially,
                    // so we can't simply ignore the exception: instead, we need to retry with
                    // consistency level equal to the number of acknowledged writes.
                    ConsistencyLevel downgraded = downgrade(cl, acknowledgements, e);
                    write(downgraded, retryCount + 1);
                    break;
                case BATCH_LOG:
                    // Rare edge case: the peers that were chosen by the coordinator
                    // to receive the distributed batch log failed to respond.
                    // Simply retry with same consistency level.
                    write(cl, retryCount + 1);
                    break;
                default:
                    // Other write types are uncommon and should not be retried.
                    throw e;
            }
        } else {
            // Unexpected error: just retry with same consistency level
            // and hope to talk to a healthier coordinator.
            write(cl, retryCount + 1);
        }
    }
}
Also used : ConsistencyLevel(com.datastax.driver.core.ConsistencyLevel) WriteTimeoutException(com.datastax.driver.core.exceptions.WriteTimeoutException) SimpleStatement(com.datastax.driver.core.SimpleStatement) WriteType(com.datastax.driver.core.WriteType) BatchStatement(com.datastax.driver.core.BatchStatement) UnavailableException(com.datastax.driver.core.exceptions.UnavailableException) DriverException(com.datastax.driver.core.exceptions.DriverException)

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