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);
}
}
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);
}
}
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);
}
}
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);
}
}
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);
}
}
Aggregations