Search in sources :

Example 1 with StatementGenerator

use of org.opennms.newts.cassandra.search.support.StatementGenerator in project newts by OpenNMS.

the class CassandraIndexer method toStatements.

private List<Statement> toStatements(Set<StatementGenerator> generators) {
    List<Statement> statementsToExecute = Lists.newArrayList();
    Map<String, List<Statement>> statementsByKey = Maps.newHashMap();
    for (StatementGenerator generator : generators) {
        Statement statement = generator.toStatement().setConsistencyLevel(m_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, m_options.getMaxBatchSize())) {
            statementsToExecute.add(unloggedBatch(partition.toArray(new RegularStatement[partition.size()])));
        }
    }
    return statementsToExecute;
}
Also used : RegularStatement(com.datastax.driver.core.RegularStatement) PreparedStatement(com.datastax.driver.core.PreparedStatement) BoundStatement(com.datastax.driver.core.BoundStatement) Statement(com.datastax.driver.core.Statement) List(java.util.List) StatementGenerator(org.opennms.newts.cassandra.search.support.StatementGenerator)

Example 2 with StatementGenerator

use of org.opennms.newts.cassandra.search.support.StatementGenerator 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)

Aggregations

BoundStatement (com.datastax.driver.core.BoundStatement)2 PreparedStatement (com.datastax.driver.core.PreparedStatement)2 RegularStatement (com.datastax.driver.core.RegularStatement)2 Statement (com.datastax.driver.core.Statement)2 StatementGenerator (org.opennms.newts.cassandra.search.support.StatementGenerator)2 Timer (com.codahale.metrics.Timer)1 ResultSetFuture (com.datastax.driver.core.ResultSetFuture)1 List (java.util.List)1 Map (java.util.Map)1 Context (org.opennms.newts.api.Context)1 Resource (org.opennms.newts.api.Resource)1 Sample (org.opennms.newts.api.Sample)1