List of usage examples for com.google.common.util.concurrent SettableFuture isDone
@Override public boolean isDone()
From source file:com.vmware.photon.controller.common.thrift.ClientPoolImpl.java
@Override public synchronized ListenableFuture<C> acquire() { if (promises.size() < options.getMaxWaiters()) { SettableFuture<C> future = SettableFuture.create(); Promise<C> promise = new Promise<>(future); promises.add(promise);/* w w w . j a v a 2 s .co m*/ processPromises(); logger.info("options.getTimeoutMs() is {}", options.getTimeoutMs()); if (options.getTimeoutMs() > 0 && !future.isDone()) { setTimeout(promise); } logger.info("{} Promise processed {}", options.getServiceName(), promise); return future; } return Futures.immediateFailedFuture(new ClientPoolException("Too many waiters")); }
From source file:com.google.cloud.dataflow.sdk.util.state.WindmillStateReader.java
private <T> void consumeTagList(TagList list, StateTag stateTag) { @SuppressWarnings("unchecked") SettableFuture<Iterable<T>> future = (SettableFuture<Iterable<T>>) futures.get(stateTag); if (future == null) { throw new IllegalStateException("Missing future for " + stateTag); } else if (future.isDone()) { LOG.error("Future for {} is already done", stateTag); }/*from w w w . j a v a 2s . com*/ if (list.getValuesCount() == 0) { future.set(Collections.<T>emptyList()); return; } @SuppressWarnings("unchecked") Coder<T> elemCoder = (Coder<T>) coders.remove(stateTag); if (elemCoder == null) { throw new IllegalStateException("Missing element coder for " + stateTag); } List<T> valueList = new ArrayList<>(list.getValuesCount()); for (Windmill.Value value : list.getValuesList()) { if (value.hasData() && !value.getData().isEmpty()) { // Drop the first byte of the data; it's the zero byte we prependend to avoid writing // empty data. InputStream inputStream = value.getData().substring(1).newInput(); try { valueList.add(elemCoder.decode(inputStream, Coder.Context.OUTER)); } catch (IOException e) { throw new IllegalStateException("Unable to decode tag list using " + elemCoder, e); } } } future.set(Collections.unmodifiableList(valueList)); }
From source file:io.prestosql.operator.Driver.java
public ListenableFuture<?> process() { checkLockNotHeld("Can not process while holding the driver lock"); // if the driver is blocked we don't need to continue SettableFuture<?> blockedFuture = driverBlockedFuture.get(); if (!blockedFuture.isDone()) { return blockedFuture; }//w w w. j a va 2s . c om Optional<ListenableFuture<?>> result = tryWithLock(100, TimeUnit.MILLISECONDS, () -> { ListenableFuture<?> future = processInternal(createTimer()); return updateDriverBlockedFuture(future); }); return result.orElse(NOT_BLOCKED); }
From source file:io.prestosql.operator.Driver.java
public ListenableFuture<?> processFor(Duration duration) { checkLockNotHeld("Can not process for a duration while holding the driver lock"); requireNonNull(duration, "duration is null"); // if the driver is blocked we don't need to continue SettableFuture<?> blockedFuture = driverBlockedFuture.get(); if (!blockedFuture.isDone()) { return blockedFuture; }/*from w w w . jav a 2 s.c om*/ long maxRuntime = duration.roundTo(TimeUnit.NANOSECONDS); Optional<ListenableFuture<?>> result = tryWithLock(100, TimeUnit.MILLISECONDS, () -> { OperationTimer operationTimer = createTimer(); driverContext.startProcessTimer(); driverContext.getYieldSignal().setWithDelay(maxRuntime, driverContext.getYieldExecutor()); try { long start = System.nanoTime(); do { ListenableFuture<?> future = processInternal(operationTimer); if (!future.isDone()) { return updateDriverBlockedFuture(future); } } while (System.nanoTime() - start < maxRuntime && !isFinishedInternal()); } finally { driverContext.getYieldSignal().reset(); driverContext.recordProcessed(operationTimer); } return NOT_BLOCKED; }); return result.orElse(NOT_BLOCKED); }
From source file:com.facebook.presto.hive.util.AsyncWalker.java
private void doWalk(Path path, FileStatusCallback callback, AtomicLong taskCount, SettableFuture<Void> future) { try (SetThreadName ignored = new SetThreadName("HiveHdfsWalker")) { RemoteIterator<LocatedFileStatus> iterator = getLocatedFileStatusRemoteIterator(path); while (iterator.hasNext()) { LocatedFileStatus status = getLocatedFileStatus(iterator); // ignore hidden files. Hive ignores files starting with _ and . as well. String fileName = status.getPath().getName(); if (fileName.startsWith("_") || fileName.startsWith(".")) { continue; }/*w w w . j ava 2s . c o m*/ if (!isDirectory(status)) { callback.process(status, status.getBlockLocations()); } else if (recursive) { recursiveWalk(status.getPath(), callback, taskCount, future); } if (future.isDone()) { return; } } } catch (FileNotFoundException e) { future.setException(new FileNotFoundException("Partition location does not exist: " + path)); } catch (Throwable t) { future.setException(t); } finally { if (taskCount.decrementAndGet() == 0) { future.set(null); } } }
From source file:com.google.gapid.server.ChildProcess.java
protected void runProcess(final SettableFuture<T> result, final ProcessBuilder pb) { try {/*ww w. j a v a 2s .c o m*/ // This will throw IOException if the executable is not found. LOG.log(INFO, "Starting " + name + " as " + pb.command()); process = pb.start(); } catch (IOException e) { LOG.log(WARNING, "IO Error running process", e); result.setException(e); return; } int exitCode = -1; try (OutputHandler<T> stdout = createStdoutHandler(); OutputHandler<T> stderr = createStderrHandler()) { stdout.start(process.getInputStream(), result); stderr.start(process.getErrorStream(), result); exitCode = process.waitFor(); stderr.join(); stdout.join(); stderr.finish(result); stdout.finish(result); if (!result.isDone()) { result.setException(new Exception(name + " has exited")); } } catch (InterruptedException e) { LOG.log(INFO, "Killing " + name); result.setException(e); process.destroy(); } finally { onExit(exitCode); } }
From source file:com.kixeye.kixmpp.server.KixmppServer.java
/** * Starts the server.//from ww w.j av a 2 s .c o m * * @throws Exception */ public ListenableFuture<KixmppServer> start() throws Exception { checkAndSetState(State.STARTING, State.STOPPED); logger.info("Starting Kixmpp Server on [{}]...", bindAddress); // register all modules for (String moduleClassName : modulesToRegister) { installModule(moduleClassName); } final SettableFuture<KixmppServer> responseFuture = SettableFuture.create(); final GenericFutureListener<Future<? super Void>> channelFutureListener = new GenericFutureListener<Future<? super Void>>() { @Override public synchronized void operationComplete(Future<? super Void> future) throws Exception { if (webSocketChannelFuture.get() != null && webSocketChannelFuture.get().isDone()) { if (webSocketChannelFuture.get().isSuccess()) { logger.info("Kixmpp WebSocket Server listening on [{}]", webSocketAddress); webSocketChannel.set(webSocketChannelFuture.get().channel()); if (channelFuture.get() == null && !responseFuture.isDone()) { logger.info("Started Kixmpp Server"); state.set(State.STARTED); responseFuture.set(KixmppServer.this); } webSocketChannelFuture.set(null); } else { logger.error("Unable to start Kixmpp WebSocket Server on [{}]", webSocketAddress, future.cause()); if (channelFuture.get() == null && !responseFuture.isDone()) { state.set(State.STOPPED); responseFuture.setException(future.cause()); } webSocketChannelFuture.set(null); } } else if (channelFuture.get() != null && channelFuture.get().isDone()) { if (channelFuture.get().isSuccess()) { logger.info("Kixmpp Server listening on [{}]", bindAddress); channel.set(channelFuture.get().channel()); if (webSocketChannelFuture.get() == null && !responseFuture.isDone()) { logger.info("Started Kixmpp Server"); state.set(State.STARTED); responseFuture.set(KixmppServer.this); } channelFuture.set(null); } else { logger.error("Unable to start Kixmpp Server on [{}]", bindAddress, future.cause()); if (webSocketChannelFuture.get() == null && !responseFuture.isDone()) { state.set(State.STOPPED); responseFuture.setException(future.cause()); } channelFuture.set(null); } } } }; channelFuture.set(bootstrap.bind(bindAddress)); channelFuture.get().addListener(channelFutureListener); if (webSocketAddress != null && webSocketBootstrap != null) { webSocketChannelFuture.set(webSocketBootstrap.bind(webSocketAddress)); webSocketChannelFuture.get().addListener(channelFutureListener); } return responseFuture; }
From source file:org.apache.twill.internal.zookeeper.ReentrantDistributedLock.java
/** * Acquires a distributed lock through ZooKeeper. * * @param interruptible true if acquisition of lock can be interrupted * @param waitForLock true if wants to wait for the lock when not able to acquire it * @param timeout time to wait for the lock before giving up * @param unit unit for the timeout//from www. j a va2 s .c o m * @throws InterruptedException if {@code interruptible} is set to {@code true} and the current thread is interrupted * while acquiring the lock * @throws ExecutionException if there is failure while trying to acquire the lock */ private boolean acquire(boolean interruptible, final boolean waitForLock, long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException { Preconditions.checkState(lock.isHeldByCurrentThread(), "Not owner of local lock."); if (lock.getHoldCount() > 1) { // Already owner of the lock, simply return. return true; } // Use a Future to help deal with different variants of locking // (lock, lockInterruptibly, tryLock, tryLock with timeout) // When the completion future is completed successfully, it means the lock is acquired and the future contains // the ZK node path to the ephemeral node that is representing this lock. // If it is failed, it means there is exception while trying to acquire the lock // If it is cancelled, it means to abort the acquisition logic (due to timeout / interrupt). final SettableFuture<String> completion = SettableFuture.create(); // If the connection expired, fail the locking process if it is still in progress final Cancellable watcherCancellable = zkClient.addConnectionWatcher(new Watcher() { @Override public void process(WatchedEvent event) { if (event.getState() == Event.KeeperState.Expired) { completion.setException(new IllegalStateException("ZK session expired")); } } }); // Always remove the watcher on completion completion.addListener(new Runnable() { @Override public void run() { watcherCancellable.cancel(); } }, Threads.SAME_THREAD_EXECUTOR); // Step 1. Create a ephemeral sequential node final String guid = UUID.randomUUID().toString(); final String lockPath = String.format("%s/%s-", path, guid); OperationFuture<String> future = zkClient.create(lockPath, null, CreateMode.EPHEMERAL_SEQUENTIAL, true); Futures.addCallback(future, new FutureCallback<String>() { @Override public void onSuccess(final String lockNode) { // If lock failed due to whatever reason, delete the lock node. deleteNodeOnFailure(completion, lockNode); // If the lock is completed (mainly due to cancellation), simply abort the lock acquisition logic. if (completion.isDone()) { return; } // Step 2-5. Try to determine who is the lock owner and watch for ZK node changes if itself is not the owner. doAcquire(completion, waitForLock, guid, lockNode); } @Override public void onFailure(Throwable t) { if (t instanceof KeeperException.ConnectionLossException) { // Ignore connection exception in create. Going to handle it in next step. // See the ZK receipt for details about the possible failure situation that can cause this. doAcquire(completion, waitForLock, guid, null); } else { LOG.error("Exception raised when creating lock node at {}", lockPath, t); completion.setException(t); } } }); // Gets the result from the completion try { if (interruptible) { localLockNode.set(completion.get(timeout, unit)); } else { localLockNode.set(Uninterruptibles.getUninterruptibly(completion, timeout, unit)); } return true; } catch (InterruptedException e) { completion.cancel(true); throw e; } catch (TimeoutException e) { completion.cancel(true); throw e; } catch (CancellationException e) { // If the completion get cancelled, meaning the lock acquisition is aborted. return false; } }
From source file:org.apache.twill.internal.zookeeper.ReentrantDistributedLock.java
/** * Executes the lock acquisition process. This corresponds to step 2-5 of the distributed lock receipt. */// w ww.ja va 2 s . c o m private void doAcquire(final SettableFuture<String> completion, final boolean waitForLock, final String guid, @Nullable final String lockPath) { // Step 2. Get all children under the lock parent. Futures.addCallback(zkClient.getChildren(path), new FutureCallback<NodeChildren>() { @Override public void onSuccess(NodeChildren children) { // Find the lock node in case the creation step failed by matching the guid // See "Recoverable Errors and the GUID" in the ZooKeeper guide final String lockNode = lockPath == null ? findLockNode(children.getChildren(), guid) : lockPath; if (lockNode == null) { // If not able to find the lock node, fail the locking procedure. completion.setException(new IllegalStateException("Failed to acquire lock").fillInStackTrace()); return; } if (lockPath == null) { // If lock node was not determined in step 1 due to connection loss exception, need to add the // node deletion handler in here after the actual lockNode is determined. deleteNodeOnFailure(completion, lockNode); } // Find the node to watch, which is the one with the largest id that is smaller than currentId // If the current id is the smallest one, nodeToWatch will be null final String nodeToWatch = findNodeToWatch(children, lockNode, guid); // Step 3a. lockNode is the lowest, hence this become lock owner. if (nodeToWatch == null) { // Acquired lock completion.set(lockNode); } else if (!waitForLock) { // This is for the case of tryLock() without timeout. completion.cancel(true); } // If the lock acquisition is completed, due to whatever reason, we don't need to watch for any other nodes if (completion.isDone()) { return; } // Step 3b and 4. See if the the next lowest sequence ID exists. If it does, leave a watch // Use getData() instead of exists() to avoid leaking Watcher resources (if the node is gone, there will // be a watch left on the ZK server if exists() is used). OperationFuture<NodeData> getDataFuture = zkClient.getData(nodeToWatch, new Watcher() { @Override public void process(WatchedEvent event) { if (!completion.isDone()) { // If the watching node changed, go to step 2. doAcquire(completion, waitForLock, guid, lockNode); } } }); // Step 5. Depends on the exists call result, either go to step 2 if the nodeToWatch is gone or just // let the watcher to trigger step 2 when there is change to the nodeToWatch. Futures.addCallback(getDataFuture, new FutureCallback<NodeData>() { @Override public void onSuccess(NodeData nodeData) { // No-op } @Override public void onFailure(Throwable t) { // See if the failure is due to node not exists. If that's the case, go to step 2. if (t instanceof KeeperException.NoNodeException && !completion.isDone()) { doAcquire(completion, waitForLock, guid, lockNode); } else { // If failed due to something else, fail the lock acquisition. completion.setException(t); } } }); } @Override public void onFailure(Throwable t) { if (lockPath != null) { completion.setException(t); } else { doAcquire(completion, waitForLock, guid, null); } } }); }
From source file:io.druid.java.util.http.client.NettyHttpClient.java
@Override public <Intermediate, Final> ListenableFuture<Final> go(final Request request, final HttpResponseHandler<Intermediate, Final> handler, final Duration requestReadTimeout) { final HttpMethod method = request.getMethod(); final URL url = request.getUrl(); final Multimap<String, String> headers = request.getHeaders(); final String requestDesc = StringUtils.format("%s %s", method, url); if (log.isDebugEnabled()) { log.debug("[%s] starting", requestDesc); }/*from w w w . j a v a 2 s. c o m*/ // Block while acquiring a channel from the pool, then complete the request asynchronously. final Channel channel; final String hostKey = getPoolKey(url); final ResourceContainer<ChannelFuture> channelResourceContainer = pool.take(hostKey); final ChannelFuture channelFuture = channelResourceContainer.get().awaitUninterruptibly(); if (!channelFuture.isSuccess()) { channelResourceContainer.returnResource(); // Some other poor sap will have to deal with it... return Futures.immediateFailedFuture( new ChannelException("Faulty channel in resource pool", channelFuture.getCause())); } else { channel = channelFuture.getChannel(); } final String urlFile = Strings.nullToEmpty(url.getFile()); final HttpRequest httpRequest = new DefaultHttpRequest(HttpVersion.HTTP_1_1, method, urlFile.isEmpty() ? "/" : urlFile); if (!headers.containsKey(HttpHeaders.Names.HOST)) { httpRequest.headers().add(HttpHeaders.Names.HOST, getHost(url)); } // If Accept-Encoding is set in the Request, use that. Otherwise use the default from "compressionCodec". if (!headers.containsKey(HttpHeaders.Names.ACCEPT_ENCODING)) { httpRequest.headers().set(HttpHeaders.Names.ACCEPT_ENCODING, compressionCodec.getEncodingString()); } for (Map.Entry<String, Collection<String>> entry : headers.asMap().entrySet()) { String key = entry.getKey(); for (String obj : entry.getValue()) { httpRequest.headers().add(key, obj); } } if (request.hasContent()) { httpRequest.setContent(request.getContent()); } final long readTimeout = getReadTimeout(requestReadTimeout); final SettableFuture<Final> retVal = SettableFuture.create(); if (readTimeout > 0) { channel.getPipeline().addLast(READ_TIMEOUT_HANDLER_NAME, new ReadTimeoutHandler(timer, readTimeout, TimeUnit.MILLISECONDS)); } channel.getPipeline().addLast(LAST_HANDLER_NAME, new SimpleChannelUpstreamHandler() { private volatile ClientResponse<Intermediate> response = null; @Override public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) { if (log.isDebugEnabled()) { log.debug("[%s] messageReceived: %s", requestDesc, e.getMessage()); } try { Object msg = e.getMessage(); if (msg instanceof HttpResponse) { HttpResponse httpResponse = (HttpResponse) msg; if (log.isDebugEnabled()) { log.debug("[%s] Got response: %s", requestDesc, httpResponse.getStatus()); } response = handler.handleResponse(httpResponse); if (response.isFinished()) { retVal.set((Final) response.getObj()); } if (!httpResponse.isChunked()) { finishRequest(); } } else if (msg instanceof HttpChunk) { HttpChunk httpChunk = (HttpChunk) msg; if (log.isDebugEnabled()) { log.debug("[%s] Got chunk: %sB, last=%s", requestDesc, httpChunk.getContent().readableBytes(), httpChunk.isLast()); } if (httpChunk.isLast()) { finishRequest(); } else { response = handler.handleChunk(response, httpChunk); if (response.isFinished() && !retVal.isDone()) { retVal.set((Final) response.getObj()); } } } else { throw new IllegalStateException( StringUtils.format("Unknown message type[%s]", msg.getClass())); } } catch (Exception ex) { log.warn(ex, "[%s] Exception thrown while processing message, closing channel.", requestDesc); if (!retVal.isDone()) { retVal.set(null); } channel.close(); channelResourceContainer.returnResource(); throw ex; } } private void finishRequest() { ClientResponse<Final> finalResponse = handler.done(response); if (!finalResponse.isFinished()) { throw new IllegalStateException( StringUtils.format("[%s] Didn't get a completed ClientResponse Object from [%s]", requestDesc, handler.getClass())); } if (!retVal.isDone()) { retVal.set(finalResponse.getObj()); } removeHandlers(); channelResourceContainer.returnResource(); } @Override public void exceptionCaught(ChannelHandlerContext context, ExceptionEvent event) { if (log.isDebugEnabled()) { final Throwable cause = event.getCause(); if (cause == null) { log.debug("[%s] Caught exception", requestDesc); } else { log.debug(cause, "[%s] Caught exception", requestDesc); } } retVal.setException(event.getCause()); // response is non-null if we received initial chunk and then exception occurs if (response != null) { handler.exceptionCaught(response, event.getCause()); } removeHandlers(); try { channel.close(); } catch (Exception e) { // ignore } finally { channelResourceContainer.returnResource(); } context.sendUpstream(event); } @Override public void channelDisconnected(ChannelHandlerContext context, ChannelStateEvent event) { if (log.isDebugEnabled()) { log.debug("[%s] Channel disconnected", requestDesc); } // response is non-null if we received initial chunk and then exception occurs if (response != null) { handler.exceptionCaught(response, new ChannelException("Channel disconnected")); } channel.close(); channelResourceContainer.returnResource(); if (!retVal.isDone()) { log.warn("[%s] Channel disconnected before response complete", requestDesc); retVal.setException(new ChannelException("Channel disconnected")); } context.sendUpstream(event); } private void removeHandlers() { if (readTimeout > 0) { channel.getPipeline().remove(READ_TIMEOUT_HANDLER_NAME); } channel.getPipeline().remove(LAST_HANDLER_NAME); } }); channel.write(httpRequest).addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) { if (!future.isSuccess()) { channel.close(); channelResourceContainer.returnResource(); if (!retVal.isDone()) { retVal.setException(new ChannelException( StringUtils.format("[%s] Failed to write request to channel", requestDesc), future.getCause())); } } } }); return retVal; }