use of com.datastax.driver.core.BoundStatement in project newts by OpenNMS.
the class CassandraSearcher method searchForIds.
/**
* Returns the set of resource ids that match the given
* term query.
*/
private Set<String> searchForIds(Context context, TermQuery query, ConsistencyLevel readConsistency) {
Set<String> ids = Sets.newTreeSet();
BoundStatement bindStatement = m_searchStatement.bind();
bindStatement.setString(Schema.C_TERMS_CONTEXT, context.getId());
bindStatement.setString(Schema.C_TERMS_FIELD, query.getTerm().getField(Constants.DEFAULT_TERM_FIELD));
bindStatement.setString(Schema.C_TERMS_VALUE, query.getTerm().getValue());
bindStatement.setConsistencyLevel(readConsistency);
for (Row row : m_session.execute(bindStatement)) {
ids.add(row.getString(Constants.Schema.C_TERMS_RESOURCE));
}
return ids;
}
use of com.datastax.driver.core.BoundStatement in project newts by OpenNMS.
the class CassandraSearcher method fetchMetricNames.
private ResultSetFuture fetchMetricNames(Context context, String resourceId, ConsistencyLevel readConsistency) {
BoundStatement bindStatement = m_selectMetricNamesStatement.bind();
bindStatement.setString(Schema.C_METRICS_CONTEXT, context.getId());
bindStatement.setString(Schema.C_METRICS_RESOURCE, resourceId);
bindStatement.setConsistencyLevel(readConsistency);
return m_session.executeAsync(bindStatement);
}
use of com.datastax.driver.core.BoundStatement in project newts by OpenNMS.
the class CassandraIndexerTest method insertStatementsAreDeduplicatedWhenIndexingManySamples.
@Test
public void insertStatementsAreDeduplicatedWhenIndexingManySamples() {
CassandraSession session = mock(CassandraSession.class);
ArgumentCaptor<Statement> statementCaptor = ArgumentCaptor.forClass(Statement.class);
when(session.executeAsync(statementCaptor.capture())).thenReturn(mock(ResultSetFuture.class));
PreparedStatement statement = mock(PreparedStatement.class);
BoundStatement boundStatement = mock(BoundStatement.class);
when(session.prepare(any(RegularStatement.class))).thenReturn(statement);
when(statement.bind()).thenReturn(boundStatement);
when(boundStatement.setString(any(String.class), any(String.class))).thenReturn(boundStatement);
CassandraIndexingOptions options = new CassandraIndexingOptions.Builder().withHierarchicalIndexing(true).withMaxBatchSize(1).build();
MetricRegistry registry = new MetricRegistry();
GuavaResourceMetadataCache cache = new GuavaResourceMetadataCache(2048, registry);
CassandraIndexer indexer = new CassandraIndexer(session, 0, cache, registry, options, new EscapableResourceIdSplitter(), new ContextConfigurations());
Resource r = new Resource("snmp:1589:vmware5Cpu:2:vmware5Cpu");
List<Sample> samples = Lists.newArrayList();
samples.add(new Sample(Timestamp.now(), r, "CpuCostopSum", MetricType.GAUGE, new Gauge(0)));
samples.add(new Sample(Timestamp.now(), r, "CpuIdleSum", MetricType.GAUGE, new Gauge(19299.0)));
samples.add(new Sample(Timestamp.now(), r, "CpuMaxLdSum", MetricType.GAUGE, new Gauge(0)));
samples.add(new Sample(Timestamp.now(), r, "CpuOverlapSum", MetricType.GAUGE, new Gauge(5.0)));
samples.add(new Sample(Timestamp.now(), r, "CpuRdySum", MetricType.GAUGE, new Gauge(41.0)));
samples.add(new Sample(Timestamp.now(), r, "CpuRunSum", MetricType.GAUGE, new Gauge(619.0)));
samples.add(new Sample(Timestamp.now(), r, "CpuSpwaitSum", MetricType.GAUGE, new Gauge(0)));
samples.add(new Sample(Timestamp.now(), r, "CpuSystemSum", MetricType.GAUGE, new Gauge(0)));
samples.add(new Sample(Timestamp.now(), r, "CpuUsagemhzAvg", MetricType.GAUGE, new Gauge(32.0)));
samples.add(new Sample(Timestamp.now(), r, "CpuUsedSum", MetricType.GAUGE, new Gauge(299.0)));
samples.add(new Sample(Timestamp.now(), r, "CpuWaitSum", MetricType.GAUGE, new Gauge(19343)));
// Index the collection of samples
indexer.update(samples);
// Verify the number of exectuteAsync calls
verify(session, times(20)).executeAsync(any(Statement.class));
}
use of com.datastax.driver.core.BoundStatement in project newts by OpenNMS.
the class CassandraSampleRepository method delete.
@Override
public void delete(Context context, Resource resource) {
/**
* Check for ttl value > 0
*/
if (m_ttl > 0) {
/**
* Delete exactly from (now - ttl) till now
*/
final Timestamp start = Timestamp.now().minus(m_ttl, TimeUnit.SECONDS);
final Timestamp end = Timestamp.now();
final Duration resourceShard = m_contextConfigurations.getResourceShard(context);
final List<Future<ResultSet>> futures = Lists.newArrayList();
for (Timestamp partition : new IntervalGenerator(start.stepFloor(resourceShard), end.stepFloor(resourceShard), resourceShard)) {
BoundStatement bindStatement = m_deleteStatement.bind();
bindStatement.setString(SchemaConstants.F_CONTEXT, context.getId());
bindStatement.setInt(SchemaConstants.F_PARTITION, (int) partition.asSeconds());
bindStatement.setString(SchemaConstants.F_RESOURCE, resource.getId());
futures.add(m_session.executeAsync(bindStatement));
}
for (final Future<ResultSet> future : futures) {
try {
future.get();
} catch (final InterruptedException | ExecutionException e) {
throw Throwables.propagate(e);
}
}
} else {
// Choose (now - one year) till now...
Timestamp end = Timestamp.now();
Timestamp start = end.minus(DELETION_INTERVAL, TimeUnit.DAYS);
// ... and check whether samples exist for this period of time.
while (cassandraSelect(context, resource, start, end).hasNext()) {
// Now delete the samples...
final Duration resourceShard = m_contextConfigurations.getResourceShard(context);
final List<Future<ResultSet>> futures = Lists.newArrayList();
for (Timestamp partition : new IntervalGenerator(start.stepFloor(resourceShard), end.stepFloor(resourceShard), resourceShard)) {
BoundStatement bindStatement = m_deleteStatement.bind();
bindStatement.setString(SchemaConstants.F_CONTEXT, context.getId());
bindStatement.setInt(SchemaConstants.F_PARTITION, (int) partition.asSeconds());
bindStatement.setString(SchemaConstants.F_RESOURCE, resource.getId());
futures.add(m_session.executeAsync(bindStatement));
}
for (final Future<ResultSet> future : futures) {
try {
future.get();
} catch (final InterruptedException | ExecutionException e) {
throw Throwables.propagate(e);
}
}
// ...set end to start and start to (end - one year)
end = start;
start = end.minus(DELETION_INTERVAL, TimeUnit.DAYS);
// and start over again until no more samples are found
}
}
}
use of com.datastax.driver.core.BoundStatement in project zipkin by openzipkin.
the class CassandraSpanStore method getSpanNames.
@Override
public ListenableFuture<List<String>> getSpanNames(String serviceName) {
if (serviceName == null || serviceName.isEmpty())
return EMPTY_LIST;
serviceName = checkNotNull(serviceName, "serviceName").toLowerCase();
int bucket = 0;
try {
BoundStatement bound = CassandraUtil.bindWithName(selectSpanNames, "select-span-names").setString("service_name", serviceName).setInt("bucket", bucket).setInt("limit_", 1000);
return transform(session.executeAsync(bound), new Function<ResultSet, List<String>>() {
@Override
public List<String> apply(ResultSet input) {
Set<String> spanNames = new HashSet<>();
for (Row row : input) {
spanNames.add(row.getString("span_name"));
}
return Ordering.natural().sortedCopy(spanNames);
}
});
} catch (RuntimeException ex) {
return immediateFailedFuture(ex);
}
}
Aggregations