Search in sources :

Example 16 with Statement

use of com.datastax.driver.core.Statement in project newts by OpenNMS.

the class CassandraIndexer method update.

@Override
public void update(Collection<Sample> samples) {
    Timer.Context ctx = m_updateTimer.time();
    Set<StatementGenerator> generators = Sets.newHashSet();
    Map<Context, Map<Resource, ResourceMetadata>> cacheQueue = Maps.newHashMap();
    for (Sample sample : samples) {
        maybeIndexResource(cacheQueue, generators, sample.getContext(), sample.getResource());
        maybeIndexResourceAttributes(cacheQueue, generators, sample.getContext(), sample.getResource());
        maybeAddMetricName(cacheQueue, generators, sample.getContext(), sample.getResource(), sample.getName());
    }
    try {
        if (!generators.isEmpty()) {
            synchronized (statementsInFlight) {
                generators.removeAll(statementsInFlight);
                statementsInFlight.addAll(generators);
            }
            m_inserts.mark(generators.size());
            // Asynchronously execute the statements
            List<ResultSetFuture> futures = Lists.newArrayList();
            for (Statement statementToExecute : toStatements(generators)) {
                futures.add(m_session.executeAsync(statementToExecute));
            }
            for (ResultSetFuture future : futures) {
                future.getUninterruptibly();
            }
        }
        // Order matters here; We want the cache updated only after a successful Cassandra write.
        for (Context context : cacheQueue.keySet()) {
            for (Map.Entry<Resource, ResourceMetadata> entry : cacheQueue.get(context).entrySet()) {
                m_cache.merge(context, entry.getKey(), entry.getValue());
            }
        }
    } finally {
        synchronized (statementsInFlight) {
            statementsInFlight.removeAll(generators);
        }
        ctx.stop();
    }
}
Also used : Context(org.opennms.newts.api.Context) ResultSetFuture(com.datastax.driver.core.ResultSetFuture) Sample(org.opennms.newts.api.Sample) RegularStatement(com.datastax.driver.core.RegularStatement) PreparedStatement(com.datastax.driver.core.PreparedStatement) BoundStatement(com.datastax.driver.core.BoundStatement) Statement(com.datastax.driver.core.Statement) Resource(org.opennms.newts.api.Resource) StatementGenerator(org.opennms.newts.cassandra.search.support.StatementGenerator) Timer(com.codahale.metrics.Timer) Map(java.util.Map)

Example 17 with Statement

use of com.datastax.driver.core.Statement in project newts by OpenNMS.

the class StatementUtils method getStatements.

public static List<Statement> getStatements(ContextConfigurations contextConfigurations, int maxBatchSize, Set<StatementGenerator> generators) {
    List<Statement> statementsToExecute = Lists.newArrayList();
    Map<String, List<Statement>> statementsByKey = Maps.newHashMap();
    for (StatementGenerator generator : generators) {
        Statement statement = generator.toStatement().setConsistencyLevel(contextConfigurations.getWriteConsistency(generator.getContext()));
        String key = generator.getKey();
        if (key == null) {
            // Don't try batching these
            statementsToExecute.add(statement);
            continue;
        }
        // Group these by key
        List<Statement> statementsForKey = statementsByKey.get(key);
        if (statementsForKey == null) {
            statementsForKey = Lists.newArrayList();
            statementsByKey.put(key, statementsForKey);
        }
        statementsForKey.add(statement);
    }
    // Consolidate the grouped statements into batches
    for (List<Statement> statementsForKey : statementsByKey.values()) {
        for (List<Statement> partition : Lists.partition(statementsForKey, maxBatchSize)) {
            statementsToExecute.add(unloggedBatch(partition.toArray(new RegularStatement[partition.size()])));
        }
    }
    return statementsToExecute;
}
Also used : RegularStatement(com.datastax.driver.core.RegularStatement) Statement(com.datastax.driver.core.Statement) List(java.util.List)

Example 18 with Statement

use of com.datastax.driver.core.Statement in project presto by prestodb.

the class CassandraTokenSplitManager method getSizeEstimates.

private List<SizeEstimate> getSizeEstimates(String keyspaceName, String tableName) {
    checkSizeEstimatesTableExist();
    Statement statement = select("range_start", "range_end", "mean_partition_size", "partitions_count").from(SYSTEM, SIZE_ESTIMATES).where(eq("keyspace_name", keyspaceName)).and(eq("table_name", tableName));
    ResultSet result = session.executeWithSession(session -> session.execute(statement));
    ImmutableList.Builder<SizeEstimate> estimates = ImmutableList.builder();
    for (Row row : result.all()) {
        SizeEstimate estimate = new SizeEstimate(row.getString("range_start"), row.getString("range_end"), row.getLong("mean_partition_size"), row.getLong("partitions_count"));
        estimates.add(estimate);
    }
    return estimates.build();
}
Also used : Statement(com.datastax.driver.core.Statement) ImmutableList(com.google.common.collect.ImmutableList) ResultSet(com.datastax.driver.core.ResultSet) Row(com.datastax.driver.core.Row)

Example 19 with Statement

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

the class CassandraSessionImpl method execute.

/** {@inheritDoc} */
@Override
public <V> V execute(ExecutionAssistant<V> assistant) {
    int attempt = 0;
    Throwable error = null;
    String errorMsg = "Failed to execute Cassandra CQL statement: " + assistant.getStatement();
    RandomSleeper sleeper = newSleeper();
    incrementSessionRefs();
    try {
        while (attempt < CQL_EXECUTION_ATTEMPTS_COUNT) {
            error = null;
            if (attempt != 0) {
                log.warning("Trying " + (attempt + 1) + " attempt to execute Cassandra CQL statement: " + assistant.getStatement());
            }
            try {
                PreparedStatement preparedSt = prepareStatement(assistant.getTable(), assistant.getStatement(), assistant.getPersistenceSettings(), assistant.tableExistenceRequired());
                if (preparedSt == null)
                    return null;
                Statement statement = tuneStatementExecutionOptions(assistant.bindStatement(preparedSt));
                ResultSet res = session().execute(statement);
                Row row = res == null || !res.iterator().hasNext() ? null : res.iterator().next();
                return row == null ? null : assistant.process(row);
            } catch (Throwable e) {
                error = e;
                if (CassandraHelper.isTableAbsenceError(e)) {
                    if (!assistant.tableExistenceRequired()) {
                        log.warning(errorMsg, e);
                        return null;
                    }
                    handleTableAbsenceError(assistant.getTable(), assistant.getPersistenceSettings());
                } else if (CassandraHelper.isHostsAvailabilityError(e))
                    handleHostsAvailabilityError(e, attempt, errorMsg);
                else if (CassandraHelper.isPreparedStatementClusterError(e))
                    handlePreparedStatementClusterError(e);
                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 : PreparedStatement(com.datastax.driver.core.PreparedStatement) BoundStatement(com.datastax.driver.core.BoundStatement) BatchStatement(com.datastax.driver.core.BatchStatement) Statement(com.datastax.driver.core.Statement) IgniteException(org.apache.ignite.IgniteException) ResultSet(com.datastax.driver.core.ResultSet) RandomSleeper(org.apache.ignite.cache.store.cassandra.common.RandomSleeper) PreparedStatement(com.datastax.driver.core.PreparedStatement) Row(com.datastax.driver.core.Row)

Aggregations

Statement (com.datastax.driver.core.Statement)19 BoundStatement (com.datastax.driver.core.BoundStatement)9 PreparedStatement (com.datastax.driver.core.PreparedStatement)9 ResultSet (com.datastax.driver.core.ResultSet)7 Row (com.datastax.driver.core.Row)7 BatchStatement (com.datastax.driver.core.BatchStatement)6 RegularStatement (com.datastax.driver.core.RegularStatement)6 SimpleStatement (com.datastax.driver.core.SimpleStatement)5 ArrayList (java.util.ArrayList)5 ResultSetFuture (com.datastax.driver.core.ResultSetFuture)4 List (java.util.List)4 DBException (com.yahoo.ycsb.DBException)3 IgniteException (org.apache.ignite.IgniteException)3 RandomSleeper (org.apache.ignite.cache.store.cassandra.common.RandomSleeper)3 ColumnDefinitions (com.datastax.driver.core.ColumnDefinitions)2 BuiltStatement (com.datastax.driver.core.querybuilder.BuiltStatement)2 Select (com.datastax.driver.core.querybuilder.Select)2 ByteArrayByteIterator (com.yahoo.ycsb.ByteArrayByteIterator)2 ByteBuffer (java.nio.ByteBuffer)2 FailedException (org.apache.storm.topology.FailedException)2