Search in sources :

Example 26 with BoundStatement

use of com.datastax.driver.core.BoundStatement in project newts by OpenNMS.

the class CassandraIndexerStressITCase method canIndexManyResources.

@Test
public void canIndexManyResources() {
    final int numResources = 20000;
    final int numSamplesPerResource = 3;
    // Setup the indexer
    ResultSetFuture future = mock(ResultSetFuture.class);
    CassandraSession session = mock(CassandraSession.class);
    when(session.executeAsync(any(Statement.class))).thenReturn(future);
    PreparedStatement preparedStatement = mock(PreparedStatement.class);
    BoundStatement boundStatement = mock(BoundStatement.class);
    when(session.prepare(any(RegularStatement.class))).thenReturn(preparedStatement);
    when(preparedStatement.bind()).thenReturn(boundStatement);
    when(boundStatement.setString(any(String.class), any(String.class))).thenReturn(boundStatement);
    ContextConfigurations contexts = new ContextConfigurations();
    MetricRegistry metrics = new MetricRegistry();
    CassandraIndexingOptions options = new CassandraIndexingOptions.Builder().withHierarchicalIndexing(true).build();
    ResourceIdSplitter resourceIdSplitter = new EscapableResourceIdSplitter();
    GuavaResourceMetadataCache cache = new GuavaResourceMetadataCache(numResources * 2, metrics);
    CassandraIndexer indexer = new CassandraIndexer(session, 0, cache, metrics, options, resourceIdSplitter, contexts);
    // Generate the resources and sample sets
    Resource[] resources = new Resource[numResources];
    List<List<Sample>> sampleSets = Lists.newArrayListWithCapacity(numResources);
    System.out.println("Building sample sets...");
    for (int i = 0; i < numResources; i++) {
        resources[i] = new Resource(String.format("snmp:%d:eth0-x:ifHcInOctets", i));
        List<Sample> samples = Lists.newArrayListWithCapacity(numSamplesPerResource);
        for (int j = 0; j < numSamplesPerResource; j++) {
            samples.add(new Sample(Timestamp.now(), resources[i], "y" + j, MetricType.COUNTER, new Counter(i * j)));
        }
        sampleSets.add(samples);
    }
    ;
    System.out.println("Done building sample sets.");
    // Index the resources and associated samples several times over
    for (int k = 0; k < 3; k++) {
        System.out.println("Indexing samples sets...");
        long start = System.currentTimeMillis();
        for (List<Sample> sampleSet : sampleSets) {
            indexer.update(sampleSet);
        }
        long elapsed = System.currentTimeMillis() - start;
        System.out.println("Done indexing samples in : " + elapsed + " ms");
    }
}
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) Counter(org.opennms.newts.api.Counter) List(java.util.List) ContextConfigurations(org.opennms.newts.cassandra.ContextConfigurations) BoundStatement(com.datastax.driver.core.BoundStatement) Test(org.junit.Test)

Example 27 with BoundStatement

use of com.datastax.driver.core.BoundStatement in project newts by OpenNMS.

the class CassandraSampleRepository method cassandraSelect.

private Iterator<com.datastax.driver.core.Row> cassandraSelect(Context context, Resource resource, Timestamp start, Timestamp end) {
    List<Future<ResultSet>> futures = Lists.newArrayList();
    Duration resourceShard = m_contextConfigurations.getResourceShard(context);
    Timestamp lower = start.stepFloor(resourceShard);
    Timestamp upper = end.stepFloor(resourceShard);
    for (Timestamp partition : new IntervalGenerator(lower, upper, resourceShard)) {
        BoundStatement bindStatement = m_selectStatement.bind();
        bindStatement.setString(SchemaConstants.F_CONTEXT, context.getId());
        bindStatement.setInt(SchemaConstants.F_PARTITION, (int) partition.asSeconds());
        bindStatement.setString(SchemaConstants.F_RESOURCE, resource.getId());
        bindStatement.setTimestamp("start", start.asDate());
        bindStatement.setTimestamp("end", end.asDate());
        // Use the context specific consistency level
        bindStatement.setConsistencyLevel(m_contextConfigurations.getReadConsistency(context));
        futures.add(m_session.executeAsync(bindStatement));
    }
    return new ConcurrentResultWrapper(futures);
}
Also used : Future(java.util.concurrent.Future) Duration(org.opennms.newts.api.Duration) IntervalGenerator(org.opennms.newts.aggregate.IntervalGenerator) Timestamp(org.opennms.newts.api.Timestamp) BoundStatement(com.datastax.driver.core.BoundStatement)

Example 28 with BoundStatement

use of com.datastax.driver.core.BoundStatement in project newts by OpenNMS.

the class CassandraSearcher method fetchResourceAttributes.

private ResultSetFuture fetchResourceAttributes(Context context, String resourceId, ConsistencyLevel readConsistency) {
    BoundStatement bindStatement = m_selectAttributesStatement.bind();
    bindStatement.setString(Schema.C_ATTRS_CONTEXT, context.getId());
    bindStatement.setString(Schema.C_ATTRS_RESOURCE, resourceId);
    bindStatement.setConsistencyLevel(readConsistency);
    return m_session.executeAsync(bindStatement);
}
Also used : BoundStatement(com.datastax.driver.core.BoundStatement)

Example 29 with BoundStatement

use of com.datastax.driver.core.BoundStatement in project pinpoint by naver.

the class CassandraDatastaxITBase method test.

@Test
public void test() throws Exception {
    final int testId = 99;
    final String testValue = "testValue";
    PluginTestVerifier verifier = PluginTestVerifierHolder.getInstance();
    Session myKeyspaceSession = null;
    try {
        myKeyspaceSession = cluster.connect(TEST_KEYSPACE);
        // ===============================================
        // Insert Data (PreparedStatement, BoundStatement)
        PreparedStatement preparedStatement = myKeyspaceSession.prepare(CQL_INSERT);
        BoundStatement boundStatement = new BoundStatement(preparedStatement);
        boundStatement.bind(testId, testValue);
        myKeyspaceSession.execute(boundStatement);
        verifier.printCache();
        // Cluster#connect(String)
        Class<?> clusterClass = Class.forName("com.datastax.driver.core.Cluster");
        Method connect = clusterClass.getDeclaredMethod("connect", String.class);
        verifier.verifyTrace(event(CASSANDRA, connect, null, CASSANDRA_ADDRESS, TEST_KEYSPACE));
        // SessionManager#prepare(String) OR AbstractSession#prepare(String)
        Class<?> sessionClass;
        try {
            sessionClass = Class.forName("com.datastax.driver.core.AbstractSession");
        } catch (ClassNotFoundException e) {
            sessionClass = Class.forName("com.datastax.driver.core.SessionManager");
        }
        Method prepare = sessionClass.getDeclaredMethod("prepare", String.class);
        verifier.verifyTrace(event(CASSANDRA, prepare, null, CASSANDRA_ADDRESS, TEST_KEYSPACE, sql(CQL_INSERT, null)));
        // SessionManager#execute(Statement) OR AbstractSession#execute(Statement)
        Method execute = sessionClass.getDeclaredMethod("execute", Statement.class);
        verifier.verifyTrace(event(CASSANDRA_EXECUTE_QUERY, execute, null, CASSANDRA_ADDRESS, TEST_KEYSPACE, sql(CQL_INSERT, null)));
        // ====================
        // Select Data (String)
        final String cqlSelect = String.format("SELECT %s, %s FROM %s WHERE %s = %d", TEST_COL_ID, TEST_COL_VALUE, TEST_TABLE, TEST_COL_ID, testId);
        verifySelect(myKeyspaceSession.execute(cqlSelect), testId, testValue);
        verifier.printCache();
        // SessionManager#execute(String) OR AbstractSession#execute(String)
        execute = sessionClass.getDeclaredMethod("execute", String.class);
        String normalizedSelectSql = SQL_PARSER.normalizedSql(cqlSelect).getNormalizedSql();
        verifier.verifyTrace(event(CASSANDRA_EXECUTE_QUERY, execute, null, CASSANDRA_ADDRESS, TEST_KEYSPACE, sql(normalizedSelectSql, String.valueOf(testId))));
        // ====================
        // Delete Data (String)
        final String cqlDelete = String.format("DELETE FROM %s.%s WHERE %s = ?", TEST_KEYSPACE, TEST_TABLE, TEST_COL_ID);
        myKeyspaceSession.execute(cqlDelete, testId);
        verifier.printCache();
        // SessionManager#execute(String, Object[]) OR AbstractSession#execute(String, Object[])
        execute = sessionClass.getDeclaredMethod("execute", String.class, Object[].class);
        String normalizedDeleteSql = SQL_PARSER.normalizedSql(cqlDelete).getNormalizedSql();
        verifier.verifyTrace(event(CASSANDRA_EXECUTE_QUERY, execute, null, CASSANDRA_ADDRESS, TEST_KEYSPACE, sql(normalizedDeleteSql, null)));
    } finally {
        closeSession(myKeyspaceSession);
    }
}
Also used : PreparedStatement(com.datastax.driver.core.PreparedStatement) Method(java.lang.reflect.Method) PluginTestVerifier(com.navercorp.pinpoint.bootstrap.plugin.test.PluginTestVerifier) BoundStatement(com.datastax.driver.core.BoundStatement) Session(com.datastax.driver.core.Session) Test(org.junit.Test)

Example 30 with BoundStatement

use of com.datastax.driver.core.BoundStatement in project pinpoint by naver.

the class CassandraStatementExecuteQueryInterceptor method before.

@Override
public void before(Object target, Object[] args) {
    if (isDebug) {
        logger.beforeInterceptor(target, args);
    }
    Trace trace = traceContext.currentTraceObject();
    if (trace == null) {
        return;
    }
    SpanEventRecorder recorder = trace.traceBlockBegin();
    try {
        DatabaseInfo databaseInfo = (target instanceof DatabaseInfoAccessor) ? ((DatabaseInfoAccessor) target)._$PINPOINT$_getDatabaseInfo() : null;
        if (databaseInfo == null) {
            databaseInfo = UnKnownDatabaseInfo.INSTANCE;
        }
        recorder.recordServiceType(databaseInfo.getExecuteQueryType());
        recorder.recordEndPoint(databaseInfo.getMultipleHost());
        recorder.recordDestinationId(databaseInfo.getDatabaseId());
        String sql;
        if (args[0] instanceof BoundStatement) {
            sql = ((BoundStatement) args[0]).preparedStatement().getQueryString();
        } else if (args[0] instanceof RegularStatement) {
            sql = ((RegularStatement) args[0]).getQueryString();
        } else {
            // we have string
            sql = (String) args[0];
        }
        ParsingResult parsingResult = traceContext.parseSql(sql);
        if (parsingResult != null) {
            ((ParsingResultAccessor) target)._$PINPOINT$_setParsingResult(parsingResult);
        } else {
            if (logger.isErrorEnabled()) {
                logger.error("sqlParsing fail. parsingResult is null sql:{}", sql);
            }
        }
        Map<Integer, String> bindValue = ((BindValueAccessor) target)._$PINPOINT$_getBindValue();
        // Extracting bind variables from already-serialized is too risky
        if (bindValue != null && !bindValue.isEmpty()) {
            String bindString = toBindVariable(bindValue);
            recorder.recordSqlParsingResult(parsingResult, bindString);
        } else {
            recorder.recordSqlParsingResult(parsingResult);
        }
        recorder.recordApi(descriptor);
        clean(target);
    } catch (Exception e) {
        if (logger.isWarnEnabled()) {
            logger.warn(e.getMessage(), e);
        }
    }
}
Also used : UnKnownDatabaseInfo(com.navercorp.pinpoint.bootstrap.plugin.jdbc.UnKnownDatabaseInfo) DatabaseInfo(com.navercorp.pinpoint.bootstrap.context.DatabaseInfo) SpanEventRecorder(com.navercorp.pinpoint.bootstrap.context.SpanEventRecorder) DatabaseInfoAccessor(com.navercorp.pinpoint.bootstrap.plugin.jdbc.DatabaseInfoAccessor) BindValueAccessor(com.navercorp.pinpoint.bootstrap.plugin.jdbc.BindValueAccessor) ParsingResultAccessor(com.navercorp.pinpoint.bootstrap.plugin.jdbc.ParsingResultAccessor) RegularStatement(com.datastax.driver.core.RegularStatement) Trace(com.navercorp.pinpoint.bootstrap.context.Trace) ParsingResult(com.navercorp.pinpoint.bootstrap.context.ParsingResult) 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