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;
}
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();
}
}
Aggregations