Search in sources :

Example 11 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 12 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 13 with BoundStatement

use of com.datastax.driver.core.BoundStatement in project zipkin by openzipkin.

the class TracedSession method handleInvocation.

@Override
protected Object handleInvocation(Object proxy, Method method, Object[] args) throws Throwable {
    // Only join traces, don't start them. This prevents LocalCollector's thread from amplifying.
    if (brave.serverSpanThreadBinder().getCurrentServerSpan() != null && brave.serverSpanThreadBinder().getCurrentServerSpan().getSpan() != null && method.getName().equals("executeAsync") && args[0] instanceof BoundStatement) {
        BoundStatement statement = (BoundStatement) args[0];
        // via an internal class z.s.cassandra3.NamedBoundStatement, toString() is a nice name
        SpanId spanId = brave.clientTracer().startNewSpan(statement.toString());
        // o.a.c.tracing.Tracing.newSession must use the same format for the key zipkin
        if (version.compareTo(ProtocolVersion.V4) >= 0) {
            statement.enableTracing();
            statement.setOutgoingPayload(singletonMap("zipkin", ByteBuffer.wrap(spanId.bytes())));
        }
        // start the span and store it
        brave.clientTracer().setClientSent();
        brave.clientTracer().submitBinaryAnnotation("cql.query", statement.preparedStatement().getQueryString());
        cache.put(statement, brave.clientSpanThreadBinder().getCurrentClientSpan());
        // let go of the client span as it is only used for the RPC (will have no local children)
        brave.clientSpanThreadBinder().setCurrentSpan(null);
        return new BraveResultSetFuture(target.executeAsync(statement), brave);
    }
    try {
        return method.invoke(target, args);
    } catch (InvocationTargetException e) {
        if (e.getCause() instanceof RuntimeException)
            throw e.getCause();
        throw e;
    }
}
Also used : BoundStatement(com.datastax.driver.core.BoundStatement) InvocationTargetException(java.lang.reflect.InvocationTargetException) SpanId(com.github.kristofa.brave.SpanId)

Example 14 with BoundStatement

use of com.datastax.driver.core.BoundStatement in project cassandra-driver-mapping by valchkou.

the class MappingSession method updateValues.

/**
     * Update values with options.
     * 
     * @param id Primary Key
     * @param class Entity.class
     * @param propertyNames Array of properties to update
     * @param value array of values to update
     */
public void updateValues(Object id, Class<?> clazz, String[] propertyNames, Object[] values, WriteOptions options) {
    maybeSync(clazz);
    BoundStatement bs = MappingBuilder.prepareUpdateValues(id, clazz, propertyNames, values, options, keyspace, session);
    execute(bs);
}
Also used : BoundStatement(com.datastax.driver.core.BoundStatement)

Example 15 with BoundStatement

use of com.datastax.driver.core.BoundStatement in project cassandra-driver-mapping by valchkou.

the class MappingSession method get.

/**
     * Get Entity by Id(Primary Key)
     * 
     * @param class Entity.class
     * @param id primary key
     * @param options ReadOptions
     * @return Entity instance or null
     */
public <T> T get(Class<T> clazz, Object id, ReadOptions options) {
    maybeSync(clazz);
    BoundStatement bs = MappingBuilder.prepareSelect(clazz, id, options, keyspace, session);
    if (bs != null) {
        ResultSet rs = session.execute(bs);
        List<T> all = getFromResultSet(clazz, rs);
        if (all.size() > 0) {
            return all.get(0);
        }
    }
    return null;
}
Also used : ResultSet(com.datastax.driver.core.ResultSet) BoundStatement(com.datastax.driver.core.BoundStatement)

Aggregations

BoundStatement (com.datastax.driver.core.BoundStatement)48 ResultSet (com.datastax.driver.core.ResultSet)9 PreparedStatement (com.datastax.driver.core.PreparedStatement)6 ResultSetFuture (com.datastax.driver.core.ResultSetFuture)5 Row (com.datastax.driver.core.Row)5 Test (org.junit.Test)5 ArrayList (java.util.ArrayList)4 RegularStatement (com.datastax.driver.core.RegularStatement)3 Session (com.datastax.driver.core.Session)3 DriverException (com.datastax.driver.core.exceptions.DriverException)3 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)3 List (java.util.List)3 Map (java.util.Map)3 UUID (java.util.UUID)3 MetricRegistry (com.codahale.metrics.MetricRegistry)2 DataType (com.datastax.driver.core.DataType)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