Search in sources :

Example 1 with BoundStatement

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;
}
Also used : Row(com.datastax.driver.core.Row) BoundStatement(com.datastax.driver.core.BoundStatement)

Example 2 with BoundStatement

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);
}
Also used : BoundStatement(com.datastax.driver.core.BoundStatement)

Example 3 with BoundStatement

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));
}
Also used : ResultSetFuture(com.datastax.driver.core.ResultSetFuture) RegularStatement(com.datastax.driver.core.RegularStatement) PreparedStatement(com.datastax.driver.core.PreparedStatement) BoundStatement(com.datastax.driver.core.BoundStatement) Statement(com.datastax.driver.core.Statement) Sample(org.opennms.newts.api.Sample) MetricRegistry(com.codahale.metrics.MetricRegistry) Resource(org.opennms.newts.api.Resource) CassandraSession(org.opennms.newts.cassandra.CassandraSession) PreparedStatement(com.datastax.driver.core.PreparedStatement) RegularStatement(com.datastax.driver.core.RegularStatement) Gauge(org.opennms.newts.api.Gauge) ContextConfigurations(org.opennms.newts.cassandra.ContextConfigurations) BoundStatement(com.datastax.driver.core.BoundStatement) Test(org.junit.Test)

Example 4 with BoundStatement

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
        }
    }
}
Also used : ResultSet(com.datastax.driver.core.ResultSet) Future(java.util.concurrent.Future) Duration(org.opennms.newts.api.Duration) IntervalGenerator(org.opennms.newts.aggregate.IntervalGenerator) ExecutionException(java.util.concurrent.ExecutionException) Timestamp(org.opennms.newts.api.Timestamp) BoundStatement(com.datastax.driver.core.BoundStatement)

Example 5 with BoundStatement

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);
    }
}
Also used : ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) ContiguousSet(com.google.common.collect.ContiguousSet) HashSet(java.util.HashSet) ResultSet(com.datastax.driver.core.ResultSet) ResultSet(com.datastax.driver.core.ResultSet) Futures.allAsList(com.google.common.util.concurrent.Futures.allAsList) List(java.util.List) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) Row(com.datastax.driver.core.Row) BoundStatement(com.datastax.driver.core.BoundStatement)

Aggregations

BoundStatement (com.datastax.driver.core.BoundStatement)39 ResultSet (com.datastax.driver.core.ResultSet)7 ResultSetFuture (com.datastax.driver.core.ResultSetFuture)5 Row (com.datastax.driver.core.Row)5 Test (org.junit.Test)5 PreparedStatement (com.datastax.driver.core.PreparedStatement)4 ArrayList (java.util.ArrayList)4 RegularStatement (com.datastax.driver.core.RegularStatement)3 Session (com.datastax.driver.core.Session)3 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)3 List (java.util.List)3 Map (java.util.Map)3 MetricRegistry (com.codahale.metrics.MetricRegistry)2 Statement (com.datastax.driver.core.Statement)2 ImmutableList (com.google.common.collect.ImmutableList)2 ImmutableSet (com.google.common.collect.ImmutableSet)2 Futures.allAsList (com.google.common.util.concurrent.Futures.allAsList)2 LinkedHashMap (java.util.LinkedHashMap)2 UUID (java.util.UUID)2 ExecutionException (java.util.concurrent.ExecutionException)2