Search in sources :

Example 1 with KeyValuePersistenceSettings

use of org.apache.ignite.cache.store.cassandra.persistence.KeyValuePersistenceSettings 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 KeyValuePersistenceSettings

use of org.apache.ignite.cache.store.cassandra.persistence.KeyValuePersistenceSettings in project ignite by apache.

the class LoadTestsCassandraArtifactsCreator method main.

/**
 * Recreates Cassandra artifacts required for load tests
 * @param args not used
 */
public static void main(String[] args) {
    try {
        System.out.println("[INFO] Recreating Cassandra artifacts (keyspace, table, indexes) for load tests");
        KeyValuePersistenceSettings perSettings = new KeyValuePersistenceSettings(TestsHelper.getLoadTestsPersistenceSettings());
        System.out.println("[INFO] Dropping test keyspace: " + perSettings.getKeyspace());
        try {
            CassandraHelper.dropTestKeyspaces();
        } catch (Throwable e) {
            throw new RuntimeException("Failed to drop test keyspace: " + perSettings.getKeyspace(), e);
        }
        System.out.println("[INFO] Test keyspace '" + perSettings.getKeyspace() + "' was successfully dropped");
        System.out.println("[INFO] Creating test keyspace: " + perSettings.getKeyspace());
        try {
            CassandraHelper.executeWithAdminCredentials(perSettings.getKeyspaceDDLStatement());
        } catch (Throwable e) {
            throw new RuntimeException("Failed to create test keyspace: " + perSettings.getKeyspace(), e);
        }
        System.out.println("[INFO] Test keyspace '" + perSettings.getKeyspace() + "' was successfully created");
        System.out.println("[INFO] Creating test table: " + perSettings.getTable());
        try {
            CassandraHelper.executeWithAdminCredentials(perSettings.getTableDDLStatement(perSettings.getTable()));
        } catch (Throwable e) {
            throw new RuntimeException("Failed to create test table: " + perSettings.getTable(), e);
        }
        System.out.println("[INFO] Test table '" + perSettings.getTable() + "' was successfully created");
        List<String> statements = perSettings.getIndexDDLStatements(perSettings.getTable());
        if (statements == null)
            statements = new LinkedList<>();
        for (String statement : statements) {
            System.out.println("[INFO] Creating test table index:");
            System.out.println(statement);
            try {
                CassandraHelper.executeWithAdminCredentials(statement);
            } catch (Throwable e) {
                throw new RuntimeException("Failed to create test table index", e);
            }
            System.out.println("[INFO] Test table index was successfully created");
        }
        System.out.println("[INFO] All required Cassandra artifacts were successfully recreated");
    } catch (Throwable e) {
        System.out.println("[ERROR] Failed to recreate Cassandra artifacts");
        e.printStackTrace(System.out);
        if (e instanceof RuntimeException)
            throw (RuntimeException) e;
        else
            throw new RuntimeException(e);
    } finally {
        CassandraHelper.releaseCassandraResources();
    }
}
Also used : KeyValuePersistenceSettings(org.apache.ignite.cache.store.cassandra.persistence.KeyValuePersistenceSettings) LinkedList(java.util.LinkedList)

Example 3 with KeyValuePersistenceSettings

use of org.apache.ignite.cache.store.cassandra.persistence.KeyValuePersistenceSettings in project ignite by apache.

the class CacheStoreHelper method createCacheStore.

/**
 */
public static CacheStore createCacheStore(String cacheName, Resource persistenceSettings, DataSource conn, CacheStoreSession session, Logger log) {
    CassandraCacheStore<Integer, Integer> cacheStore = new CassandraCacheStore<>(conn, new KeyValuePersistenceSettings(persistenceSettings), Runtime.getRuntime().availableProcessors());
    try {
        Field sesField = CassandraCacheStore.class.getDeclaredField("storeSes");
        Field logField = CassandraCacheStore.class.getDeclaredField("log");
        sesField.setAccessible(true);
        logField.setAccessible(true);
        sesField.set(cacheStore, session != null ? session : new TestCacheSession(cacheName));
        logField.set(cacheStore, new Log4JLogger(log));
    } catch (Throwable e) {
        throw new RuntimeException("Failed to initialize test Ignite cache store", e);
    }
    return cacheStore;
}
Also used : Field(java.lang.reflect.Field) KeyValuePersistenceSettings(org.apache.ignite.cache.store.cassandra.persistence.KeyValuePersistenceSettings) Log4JLogger(org.apache.ignite.logger.log4j.Log4JLogger) CassandraCacheStore(org.apache.ignite.cache.store.cassandra.CassandraCacheStore)

Example 4 with KeyValuePersistenceSettings

use of org.apache.ignite.cache.store.cassandra.persistence.KeyValuePersistenceSettings in project ignite by apache.

the class DDLGenerator method main.

/**
 * DDLGenerator entry point.
 *
 * @param args Arguments for DDLGenerator.
 */
public static void main(String[] args) {
    if (args == null || args.length == 0)
        return;
    boolean success = true;
    for (String arg : args) {
        File file = new File(arg);
        if (!file.isFile()) {
            success = false;
            System.out.println("-------------------------------------------------------------");
            System.out.println("Incorrect file specified: " + arg);
            System.out.println("-------------------------------------------------------------");
            continue;
        }
        try {
            KeyValuePersistenceSettings settings = new KeyValuePersistenceSettings(file);
            String table = settings.getTable() != null ? settings.getTable() : "my_table";
            System.out.println("-------------------------------------------------------------");
            System.out.println("DDL for keyspace/table from file: " + arg);
            System.out.println("-------------------------------------------------------------");
            System.out.println();
            System.out.println(settings.getKeyspaceDDLStatement());
            System.out.println();
            System.out.println(settings.getTableDDLStatement(table));
            System.out.println();
            List<String> statements = settings.getIndexDDLStatements(table);
            if (statements != null && !statements.isEmpty()) {
                for (String st : statements) {
                    System.out.println(st);
                    System.out.println();
                }
            }
        } catch (Throwable e) {
            success = false;
            System.out.println("-------------------------------------------------------------");
            System.out.println("Invalid file specified: " + arg);
            System.out.println("-------------------------------------------------------------");
            e.printStackTrace();
        }
    }
    if (!success)
        throw new RuntimeException("Failed to process some of the specified files");
}
Also used : KeyValuePersistenceSettings(org.apache.ignite.cache.store.cassandra.persistence.KeyValuePersistenceSettings) File(java.io.File)

Aggregations

KeyValuePersistenceSettings (org.apache.ignite.cache.store.cassandra.persistence.KeyValuePersistenceSettings)4 BatchStatement (com.datastax.driver.core.BatchStatement)1 PreparedStatement (com.datastax.driver.core.PreparedStatement)1 File (java.io.File)1 Field (java.lang.reflect.Field)1 HashMap (java.util.HashMap)1 LinkedList (java.util.LinkedList)1 IgniteException (org.apache.ignite.IgniteException)1 CassandraCacheStore (org.apache.ignite.cache.store.cassandra.CassandraCacheStore)1 RandomSleeper (org.apache.ignite.cache.store.cassandra.common.RandomSleeper)1 Mutation (org.apache.ignite.cache.store.cassandra.session.transaction.Mutation)1 Log4JLogger (org.apache.ignite.logger.log4j.Log4JLogger)1