Search in sources :

Example 6 with BoundStatement

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

the class CassandraSpanStore method getTraceIdsBySpanName.

ListenableFuture<Map<Long, Long>> getTraceIdsBySpanName(String serviceName, String spanName, long endTs, long lookback, int limit) {
    checkArgument(serviceName != null, "serviceName required on spanName query");
    checkArgument(spanName != null, "spanName required on spanName query");
    String serviceSpanName = serviceName + "." + spanName;
    long startTs = endTs - lookback;
    try {
        BoundStatement bound = CassandraUtil.bindWithName(selectTraceIdsBySpanName, "select-trace-ids-by-span-name").setString("service_span_name", serviceSpanName).setBytesUnsafe("start_ts", timestampCodec.serialize(startTs)).setBytesUnsafe("end_ts", timestampCodec.serialize(endTs)).setInt("limit_", limit);
        return transform(session.executeAsync(bound), traceIdToTimestamp);
    } catch (RuntimeException ex) {
        return immediateFailedFuture(ex);
    }
}
Also used : BoundStatement(com.datastax.driver.core.BoundStatement)

Example 7 with BoundStatement

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

the class CassandraSpanStore method getTraceIdsByServiceNames.

ListenableFuture<Map<TraceIdUDT, Long>> getTraceIdsByServiceNames(QueryRequest request) {
    long oldestData = indexTtl == 0 ? 0 : (System.currentTimeMillis() - indexTtl * 1000);
    long startTsMillis = Math.max((request.endTs - request.lookback), oldestData);
    long endTsMillis = Math.max(request.endTs, oldestData);
    try {
        Set<String> serviceNames;
        if (null != request.serviceName) {
            serviceNames = Collections.singleton(request.serviceName);
        } else {
            serviceNames = new LinkedHashSet<>(getServiceNames().get());
            if (serviceNames.isEmpty()) {
                return immediateFuture(Collections.<TraceIdUDT, Long>emptyMap());
            }
        }
        int startBucket = CassandraUtil.durationIndexBucket(startTsMillis * 1000);
        int endBucket = CassandraUtil.durationIndexBucket(endTsMillis * 1000);
        if (startBucket > endBucket) {
            throw new IllegalArgumentException("Start bucket (" + startBucket + ") > end bucket (" + endBucket + ")");
        }
        Set<Integer> buckets = ContiguousSet.create(Range.closed(startBucket, endBucket), integers());
        boolean withDuration = null != request.minDuration || null != request.maxDuration;
        List<ListenableFuture<Map<TraceIdUDT, Long>>> futures = new ArrayList<>();
        if (200 < serviceNames.size() * buckets.size()) {
            LOG.warn("read against " + TABLE_TRACE_BY_SERVICE_SPAN + " fanning out to " + serviceNames.size() * buckets.size() + " requests");
        //@xxx the fan-out of requests here can be improved
        }
        for (String serviceName : serviceNames) {
            for (Integer bucket : buckets) {
                BoundStatement bound = CassandraUtil.bindWithName(withDuration ? selectTraceIdsByServiceSpanNameAndDuration : selectTraceIdsByServiceSpanName, "select-trace-ids-by-service-name").setString("service_name", serviceName).setString("span_name", null != request.spanName ? request.spanName : "").setInt("bucket", bucket).setUUID("start_ts", UUIDs.startOf(startTsMillis)).setUUID("end_ts", UUIDs.endOf(endTsMillis)).setInt("limit_", request.limit);
                if (withDuration) {
                    bound = bound.setLong("start_duration", null != request.minDuration ? request.minDuration : 0).setLong("end_duration", null != request.maxDuration ? request.maxDuration : Long.MAX_VALUE);
                }
                bound.setFetchSize(Integer.MAX_VALUE);
                futures.add(transform(session.executeAsync(bound), traceIdToTimestamp));
            }
        }
        return transform(allAsList(futures), collapseTraceIdMaps);
    } catch (RuntimeException | InterruptedException | ExecutionException ex) {
        return immediateFailedFuture(ex);
    }
}
Also used : ArrayList(java.util.ArrayList) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) ExecutionException(java.util.concurrent.ExecutionException) BoundStatement(com.datastax.driver.core.BoundStatement) TraceIdUDT(zipkin.storage.cassandra3.Schema.TraceIdUDT)

Example 8 with BoundStatement

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

the class CassandraSpanStore method getSpansByTraceIds.

/**
   * Get the available trace information from the storage system. Spans in trace should be sorted by
   * the first annotation timestamp in that span. First event should be first in the spans list. <p>
   * The return list will contain only spans that have been found, thus the return list may not
   * match the provided list of ids.
   */
ListenableFuture<List<Span>> getSpansByTraceIds(Set<Long> traceIds, int limit) {
    checkNotNull(traceIds, "traceIds");
    if (traceIds.isEmpty()) {
        return immediateFuture(Collections.<Span>emptyList());
    }
    try {
        BoundStatement bound = CassandraUtil.bindWithName(selectTraces, "select-traces").setSet("trace_id", traceIds).setInt("limit_", limit);
        bound.setFetchSize(Integer.MAX_VALUE);
        return transform(session.executeAsync(bound), new Function<ResultSet, List<Span>>() {

            @Override
            public List<Span> apply(ResultSet input) {
                List<Span> result = new ArrayList<>(input.getAvailableWithoutFetching());
                for (Row row : input) {
                    result.add(Codec.THRIFT.readSpan(row.getBytes("span")));
                }
                return result;
            }
        });
    } catch (RuntimeException ex) {
        return immediateFailedFuture(ex);
    }
}
Also used : 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)

Example 9 with BoundStatement

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

the class CassandraSpanStore method getTraceIdsByServiceNames.

ListenableFuture<Map<Long, Long>> getTraceIdsByServiceNames(List<String> serviceNames, long endTs, long lookback, int limit) {
    if (serviceNames.isEmpty())
        return immediateFuture(Collections.<Long, Long>emptyMap());
    long startTs = endTs - lookback;
    try {
        // This guards use of "in" query to give people a little more time to move off Cassandra 2.1
        // Note that it will still fail when serviceNames.size() > 1
        BoundStatement bound = serviceNames.size() == 1 ? CassandraUtil.bindWithName(selectTraceIdsByServiceName, "select-trace-ids-by-service-name").setString("service_name", serviceNames.get(0)).setSet("bucket", buckets).setBytesUnsafe("start_ts", timestampCodec.serialize(startTs)).setBytesUnsafe("end_ts", timestampCodec.serialize(endTs)).setInt("limit_", limit) : CassandraUtil.bindWithName(selectTraceIdsByServiceNames, "select-trace-ids-by-service-names").setList("service_name", serviceNames).setSet("bucket", buckets).setBytesUnsafe("start_ts", timestampCodec.serialize(startTs)).setBytesUnsafe("end_ts", timestampCodec.serialize(endTs)).setInt("limit_", limit);
        bound.setFetchSize(Integer.MAX_VALUE);
        return transform(session.executeAsync(bound), traceIdToTimestamp);
    } catch (RuntimeException ex) {
        return immediateFailedFuture(ex);
    }
}
Also used : BoundStatement(com.datastax.driver.core.BoundStatement)

Example 10 with BoundStatement

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

the class CassandraSpanStore method getTraceIdsByAnnotation.

ListenableFuture<Map<Long, Long>> getTraceIdsByAnnotation(String annotationKey, long endTs, long lookback, int limit) {
    long startTs = endTs - lookback;
    try {
        BoundStatement bound = CassandraUtil.bindWithName(selectTraceIdsByAnnotation, "select-trace-ids-by-annotation").setBytes("annotation", CassandraUtil.toByteBuffer(annotationKey)).setSet("bucket", buckets).setBytesUnsafe("start_ts", timestampCodec.serialize(startTs)).setBytesUnsafe("end_ts", timestampCodec.serialize(endTs)).setInt("limit_", limit);
        bound.setFetchSize(Integer.MAX_VALUE);
        return transform(session.executeAsync(bound), new Function<ResultSet, Map<Long, Long>>() {

            @Override
            public Map<Long, Long> apply(ResultSet input) {
                Map<Long, Long> traceIdsToTimestamps = new LinkedHashMap<>();
                for (Row row : input) {
                    traceIdsToTimestamps.put(row.getLong("trace_id"), timestampCodec.deserialize(row, "ts"));
                }
                return traceIdsToTimestamps;
            }
        });
    } catch (CharacterCodingException | RuntimeException ex) {
        return immediateFailedFuture(ex);
    }
}
Also used : ResultSet(com.datastax.driver.core.ResultSet) Row(com.datastax.driver.core.Row) CharacterCodingException(java.nio.charset.CharacterCodingException) BoundStatement(com.datastax.driver.core.BoundStatement) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap)

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