Search in sources :

Example 1 with Mutation

use of org.apache.ignite.cache.store.cassandra.session.transaction.Mutation 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 Mutation

use of org.apache.ignite.cache.store.cassandra.session.transaction.Mutation in project ignite by apache.

the class CassandraCacheStore method sessionEnd.

/** {@inheritDoc} */
@Override
public void sessionEnd(boolean commit) throws CacheWriterException {
    if (!storeSes.isWithinTransaction())
        return;
    List<Mutation> mutations = mutations();
    if (mutations == null || mutations.isEmpty())
        return;
    CassandraSession ses = getCassandraSession();
    try {
        ses.execute(mutations);
    } finally {
        mutations.clear();
        U.closeQuiet(ses);
    }
}
Also used : CassandraSession(org.apache.ignite.cache.store.cassandra.session.CassandraSession) DeleteMutation(org.apache.ignite.cache.store.cassandra.session.transaction.DeleteMutation) Mutation(org.apache.ignite.cache.store.cassandra.session.transaction.Mutation) WriteMutation(org.apache.ignite.cache.store.cassandra.session.transaction.WriteMutation)

Aggregations

Mutation (org.apache.ignite.cache.store.cassandra.session.transaction.Mutation)2 BatchStatement (com.datastax.driver.core.BatchStatement)1 PreparedStatement (com.datastax.driver.core.PreparedStatement)1 HashMap (java.util.HashMap)1 IgniteException (org.apache.ignite.IgniteException)1 RandomSleeper (org.apache.ignite.cache.store.cassandra.common.RandomSleeper)1 KeyValuePersistenceSettings (org.apache.ignite.cache.store.cassandra.persistence.KeyValuePersistenceSettings)1 CassandraSession (org.apache.ignite.cache.store.cassandra.session.CassandraSession)1 DeleteMutation (org.apache.ignite.cache.store.cassandra.session.transaction.DeleteMutation)1 WriteMutation (org.apache.ignite.cache.store.cassandra.session.transaction.WriteMutation)1