use of com.google.common.util.concurrent.ListenableFuture in project weave by continuuity.
the class SimpleKafkaClient method preparePublish.
@Override
public PreparePublish preparePublish(final String topic, final Compression compression) {
final Map<Integer, MessageSetEncoder> encoders = Maps.newHashMap();
return new PreparePublish() {
@Override
public PreparePublish add(byte[] payload, Object partitionKey) {
return add(ByteBuffer.wrap(payload), partitionKey);
}
@Override
public PreparePublish add(ByteBuffer payload, Object partitionKey) {
// TODO: Partition
int partition = 0;
MessageSetEncoder encoder = encoders.get(partition);
if (encoder == null) {
encoder = getEncoder(compression);
encoders.put(partition, encoder);
}
encoder.add(ChannelBuffers.wrappedBuffer(payload));
return this;
}
@Override
public ListenableFuture<?> publish() {
List<ListenableFuture<?>> futures = Lists.newArrayListWithCapacity(encoders.size());
for (Map.Entry<Integer, MessageSetEncoder> entry : encoders.entrySet()) {
futures.add(doPublish(topic, entry.getKey(), entry.getValue().finish()));
}
encoders.clear();
return Futures.allAsList(futures);
}
private ListenableFuture<?> doPublish(String topic, int partition, ChannelBuffer messageSet) {
final KafkaRequest request = KafkaRequest.createProduce(topic, partition, messageSet);
final SettableFuture<?> result = SettableFuture.create();
final ConnectionPool.ConnectResult connection = connectionPool.connect(getTopicBroker(topic, partition).getAddress());
connection.getChannelFuture().addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture future) throws Exception {
try {
future.getChannel().write(request).addListener(getPublishChannelFutureListener(result, null, connection));
} catch (Exception e) {
result.setException(e);
}
}
});
return result;
}
};
}
use of com.google.common.util.concurrent.ListenableFuture in project weave by continuuity.
the class ZKServiceDecorator method doStart.
@Override
protected void doStart() {
callbackExecutor = Executors.newSingleThreadExecutor(Threads.createDaemonThreadFactory("message-callback"));
Futures.addCallback(createLiveNode(), new FutureCallback<String>() {
@Override
public void onSuccess(String result) {
// Create nodes for states and messaging
StateNode stateNode = new StateNode(ServiceController.State.STARTING);
final ListenableFuture<List<String>> createFuture = Futures.allAsList(ZKOperations.ignoreError(zkClient.create(getZKPath("messages"), null, CreateMode.PERSISTENT), KeeperException.NodeExistsException.class, null), zkClient.create(getZKPath("state"), encodeStateNode(stateNode), CreateMode.PERSISTENT));
createFuture.addListener(new Runnable() {
@Override
public void run() {
try {
createFuture.get();
// Starts the decorated service
decoratedService.addListener(createListener(), Threads.SAME_THREAD_EXECUTOR);
decoratedService.start();
} catch (Exception e) {
notifyFailed(e);
}
}
}, Threads.SAME_THREAD_EXECUTOR);
}
@Override
public void onFailure(Throwable t) {
notifyFailed(t);
}
});
}
use of com.google.common.util.concurrent.ListenableFuture in project druid by druid-io.
the class HdfsClasspathSetupTest method testConcurrentUpload.
@Test
public void testConcurrentUpload() throws IOException, InterruptedException, ExecutionException, TimeoutException {
final int concurrency = 10;
ListeningExecutorService pool = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(concurrency));
// barrier ensures that all jobs try to add files to classpath at same time.
final CyclicBarrier barrier = new CyclicBarrier(concurrency);
final DistributedFileSystem fs = miniCluster.getFileSystem();
final Path expectedJarPath = new Path(finalClasspath, dummyJarFile.getName());
List<ListenableFuture<Boolean>> futures = new ArrayList<>();
for (int i = 0; i < concurrency; i++) {
futures.add(pool.submit(new Callable() {
@Override
public Boolean call() throws Exception {
int id = barrier.await();
Job job = Job.getInstance(conf, "test-job-" + id);
Path intermediatePathForJob = new Path(intermediatePath, "job-" + id);
JobHelper.addJarToClassPath(dummyJarFile, finalClasspath, intermediatePathForJob, fs, job);
// check file gets uploaded to final HDFS path
Assert.assertTrue(fs.exists(expectedJarPath));
// check that the intermediate file is not present
Assert.assertFalse(fs.exists(new Path(intermediatePathForJob, dummyJarFile.getName())));
// check file gets added to the classpath
Assert.assertEquals(expectedJarPath.toString(), job.getConfiguration().get(MRJobConfig.CLASSPATH_FILES));
return true;
}
}));
}
Futures.allAsList(futures).get(30, TimeUnit.SECONDS);
pool.shutdownNow();
}
use of com.google.common.util.concurrent.ListenableFuture in project zipkin by openzipkin.
the class ElasticsearchSpanStore method getTraces.
@Override
public ListenableFuture<List<List<Span>>> getTraces(final QueryRequest request) {
long endMillis = request.endTs;
long beginMillis = endMillis - request.lookback;
BoolQueryBuilder filter = boolQuery().must(rangeQuery("timestamp_millis").gte(beginMillis).lte(endMillis));
if (request.serviceName != null) {
filter.must(boolQuery().should(nestedQuery("annotations", termQuery("annotations.endpoint.serviceName", request.serviceName))).should(nestedQuery("binaryAnnotations", termQuery("binaryAnnotations.endpoint.serviceName", request.serviceName))));
}
if (request.spanName != null) {
filter.must(termQuery("name", request.spanName));
}
for (String annotation : request.annotations) {
BoolQueryBuilder annotationQuery = boolQuery().must(termQuery("annotations.value", annotation));
if (request.serviceName != null) {
annotationQuery.must(termQuery("annotations.endpoint.serviceName", request.serviceName));
}
filter.must(nestedQuery("annotations", annotationQuery));
}
for (Map.Entry<String, String> kv : request.binaryAnnotations.entrySet()) {
// In our index template, we make sure the binaryAnnotation value is indexed as string,
// meaning non-string values won't even be indexed at all. This means that we can only
// match string values here, which happens to be exactly what we want.
BoolQueryBuilder binaryAnnotationQuery = boolQuery().must(termQuery("binaryAnnotations.key", kv.getKey())).must(termQuery("binaryAnnotations.value", kv.getValue()));
if (request.serviceName != null) {
binaryAnnotationQuery.must(termQuery("binaryAnnotations.endpoint.serviceName", request.serviceName));
}
filter.must(nestedQuery("binaryAnnotations", binaryAnnotationQuery));
}
if (request.minDuration != null) {
RangeQueryBuilder durationQuery = rangeQuery("duration").gte(request.minDuration);
if (request.maxDuration != null) {
durationQuery.lte(request.maxDuration);
}
filter.must(durationQuery);
}
Set<String> strings = indexNameFormatter.indexNamePatternsForRange(beginMillis, endMillis);
final String[] indices = strings.toArray(new String[0]);
// We need to filter to traces that contain at least one span that matches the request,
// but the zipkin API is supposed to order traces by first span, regardless of if it was
// filtered or not. This is not possible without either multiple, heavyweight queries
// or complex multiple indexing, defeating much of the elegance of using elasticsearch for this.
// So we fudge and order on the first span among the filtered spans - in practice, there should
// be no significant difference in user experience since span start times are usually very
// close to each other in human time.
ListenableFuture<List<String>> traceIds = client.collectBucketKeys(indices, boolQuery().must(matchAllQuery()).filter(filter), AggregationBuilders.terms("traceId_agg").field("traceId").subAggregation(AggregationBuilders.min("timestamps_agg").field("timestamp_millis")).order(Order.aggregation("timestamps_agg", false)).size(request.limit));
return transform(traceIds, new AsyncFunction<List<String>, List<List<Span>>>() {
@Override
public ListenableFuture<List<List<Span>>> apply(List<String> input) {
return getTracesByIds(input, indices, request);
}
});
}
use of com.google.common.util.concurrent.ListenableFuture in project chassis by Kixeye.
the class ActionInvokingWebSocket method onWebSocketBinary.
public void onWebSocketBinary(byte[] payload, int offset, int length) {
try {
// don't accept empty frames
if (payload == null || length < 1) {
throw new WebSocketServiceException(new ServiceError("EMPTY_ENVELOPE", "Empty envelope!"), "UNKNOWN", null);
}
// check if we need to do psk encryption
byte[] processedPayload = pskFrameProcessor.processIncoming(payload, offset, length);
if (processedPayload != payload) {
payload = processedPayload;
offset = 0;
length = payload.length;
}
// get the envelope
final WebSocketEnvelope envelope = new WebSocketEnvelope(serDe.deserialize(payload, offset, length, Envelope.class));
// gets all the actions
Collection<WebSocketAction> actions = mappingRegistry.getActionMethods(envelope.getAction());
final AtomicInteger invokedActions = new AtomicInteger(0);
// invokes them
for (final WebSocketAction action : actions) {
// get and validate type ID
Class<?> messageClass = null;
if (StringUtils.isNotBlank(envelope.getTypeId())) {
messageClass = messageRegistry.getClassByTypeId(envelope.getTypeId());
}
// validate if action has a payload class that it needs
if (action.getPayloadClass() != null && messageClass == null) {
throw new WebSocketServiceException(new ServiceError("INVALID_TYPE_ID", "Unknown type ID!"), envelope.getAction(), envelope.getTransactionId());
}
// invoke this action if allowed
if (action.canInvoke(webSocketSession, messageClass)) {
invokedActions.incrementAndGet();
final Object handler = handlerCache.get(action.getHandlerClass().getName());
final Class<?> finalMessageClass = messageClass;
ListenableFuture<DeferredResult<?>> invocation = serviceExecutor.submit(new Callable<DeferredResult<?>>() {
@Override
public DeferredResult<?> call() throws Exception {
// then invoke
return action.invoke(handler, new RawWebSocketMessage<>(envelope.getPayload(), finalMessageClass, messageValidator, serDe), envelope, webSocketSession);
}
});
Futures.addCallback(invocation, new FutureCallback<DeferredResult<?>>() {
public void onSuccess(DeferredResult<?> result) {
if (result != null) {
result.setResultHandler(new DeferredResultHandler() {
@Override
public void handleResult(Object result) {
if (result instanceof Exception) {
onFailure((Exception) result);
return;
}
sendResponse(result);
}
});
}
}
public void onFailure(Throwable t) {
if (t instanceof InvocationTargetException) {
t = ((InvocationTargetException) t).getTargetException();
}
ServiceError error = ExceptionServiceErrorMapper.mapException(t);
if (error != null && !ExceptionServiceErrorMapper.VALIDATION_ERROR_CODE.equals(error.code)) {
logger.error("Unexpected exception throw while executing action [{}]", envelope.getAction(), t);
}
sendResponse(error);
}
public Future<Void> sendResponse(Object response) {
try {
return sendMessage(envelope.getAction(), envelope.getTransactionId(), response);
} catch (IOException | GeneralSecurityException e) {
logger.error("Unable to send message to channel", e);
return Futures.immediateFuture(null);
}
}
}, responseExecutor);
}
}
// make sure we actually invoked something
if (invokedActions.get() < 1) {
throw new WebSocketServiceException(new ServiceError("INVALID_ACTION_MAPPING", "No actions invoked."), envelope.getAction(), envelope.getTransactionId());
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
Aggregations