List of usage examples for com.google.common.util.concurrent SettableFuture get
@Override public V get() throws InterruptedException, ExecutionException
From source file:com.twitter.distributedlog.auditor.DLAuditor.java
/** * Find leak ledgers phase 1: collect ledgers set. *//* ww w . j a v a 2s . com*/ private Set<Long> collectLedgersFromBK(BookKeeperClient bkc, final ExecutorService executorService) throws IOException { LedgerManager lm = BookKeeperAccessor.getLedgerManager(bkc.get()); final Set<Long> ledgers = new HashSet<Long>(); final SettableFuture<Void> doneFuture = SettableFuture.create(); BookkeeperInternalCallbacks.Processor<Long> collector = new BookkeeperInternalCallbacks.Processor<Long>() { @Override public void process(Long lid, final AsyncCallback.VoidCallback cb) { synchronized (ledgers) { ledgers.add(lid); if (0 == ledgers.size() % 1000) { logger.info("Collected {} ledgers", ledgers.size()); } } executorService.submit(new Runnable() { @Override public void run() { cb.processResult(BKException.Code.OK, null, null); } }); } }; AsyncCallback.VoidCallback finalCb = new AsyncCallback.VoidCallback() { @Override public void processResult(int rc, String path, Object ctx) { if (BKException.Code.OK == rc) { doneFuture.set(null); } else { doneFuture.setException(BKException.create(rc)); } } }; lm.asyncProcessLedgers(collector, finalCb, null, BKException.Code.OK, BKException.Code.ZKException); try { doneFuture.get(); logger.info("Collected total {} ledgers", ledgers.size()); } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw new DLInterruptedException("Interrupted on collecting ledgers : ", e); } catch (ExecutionException e) { if (e.getCause() instanceof IOException) { throw (IOException) (e.getCause()); } else { throw new IOException("Failed to collect ledgers : ", e.getCause()); } } return ledgers; }
From source file:co.cask.cdap.internal.app.runtime.workflow.DefaultProgramWorkflowRunner.java
/** * Adds a listener to the {@link ProgramController} and blocks for completion. * * @param closeable a {@link Closeable} to call when the program execution completed * @param controller the {@link ProgramController} for the program * @throws Exception if the execution failed *///from w ww. j a va 2s . co m private void blockForCompletion(final Closeable closeable, final ProgramController controller) throws Exception { // Execute the program. final SettableFuture<Void> completion = SettableFuture.create(); controller.addListener(new AbstractListener() { @Override public void init(ProgramController.State currentState, @Nullable Throwable cause) { switch (currentState) { case COMPLETED: completed(); break; case KILLED: killed(); break; case ERROR: error(cause); break; } } @Override public void completed() { Closeables.closeQuietly(closeable); nodeStates.put(nodeId, new WorkflowNodeState(nodeId, NodeStatus.COMPLETED, controller.getRunId().getId(), null)); completion.set(null); } @Override public void killed() { Closeables.closeQuietly(closeable); nodeStates.put(nodeId, new WorkflowNodeState(nodeId, NodeStatus.KILLED, controller.getRunId().getId(), null)); completion.set(null); } @Override public void error(Throwable cause) { Closeables.closeQuietly(closeable); nodeStates.put(nodeId, new WorkflowNodeState(nodeId, NodeStatus.FAILED, controller.getRunId().getId(), cause)); completion.setException(cause); } }, Threads.SAME_THREAD_EXECUTOR); // Block for completion. try { completion.get(); } catch (ExecutionException e) { Throwable cause = e.getCause(); if (cause instanceof Exception) { throw (Exception) cause; } throw Throwables.propagate(cause); } catch (InterruptedException e) { try { Futures.getUnchecked(controller.stop()); } catch (Throwable t) { // no-op } // reset the interrupt Thread.currentThread().interrupt(); } }
From source file:io.crate.metadata.doc.DocTableInfo.java
@Override public Routing getRouting(final WhereClause whereClause, @Nullable final String preference) { Routing routing = getRouting(clusterService.state(), whereClause, preference, new ArrayList<ShardId>(0)); if (routing != null) return routing; ClusterStateObserver observer = new ClusterStateObserver(clusterService, routingFetchTimeout, logger); final SettableFuture<Routing> routingSettableFuture = SettableFuture.create(); observer.waitForNextChange(new FetchRoutingListener(routingSettableFuture, whereClause, preference), new ClusterStateObserver.ChangePredicate() { @Override/*from w ww .j av a 2 s. co m*/ public boolean apply(ClusterState previousState, ClusterState.ClusterStateStatus previousStatus, ClusterState newState, ClusterState.ClusterStateStatus newStatus) { return validate(newState); } @Override public boolean apply(ClusterChangedEvent changedEvent) { return validate(changedEvent.state()); } private boolean validate(ClusterState state) { final Map<String, Map<String, List<Integer>>> locations = new TreeMap<>(); GroupShardsIterator shardIterators; try { shardIterators = getShardIterators(whereClause, preference, state); } catch (IndexNotFoundException e) { return true; } final List<ShardId> missingShards = new ArrayList<>(0); fillLocationsFromShardIterators(locations, shardIterators, missingShards); return missingShards.isEmpty(); } }); try { return routingSettableFuture.get(); } catch (ExecutionException e) { throw Throwables.propagate(e.getCause()); } catch (Exception e) { throw Throwables.propagate(e); } }
From source file:de.btobastian.javacord.ImplDiscordAPI.java
@Override public Future<Server> createServer(final String name, final Region region, final BufferedImage icon, FutureCallback<Server> callback) { ListenableFuture<Server> future = getThreadPool().getListeningExecutorService() .submit(new Callable<Server>() { @Override//from www.j a va 2 s . c o m public Server call() throws Exception { logger.debug("Trying to create server (name: {}, region: {}, icon: {}", name, region == null ? "null" : region.getKey(), icon != null); if (name == null || name.length() < 2 || name.length() > 100) { throw new IllegalArgumentException("Name must be 2-100 characters long!"); } JSONObject params = new JSONObject(); if (icon != null) { if (icon.getHeight() != 128 || icon.getWidth() != 128) { throw new IllegalArgumentException("Icon must be 128*128px!"); } ByteArrayOutputStream os = new ByteArrayOutputStream(); ImageIO.write(icon, "jpg", os); params.put("icon", "data:image/jpg;base64," + BaseEncoding.base64().encode(os.toByteArray())); } params.put("name", name); params.put("region", region == null ? Region.US_WEST.getKey() : region.getKey()); final SettableFuture<Server> settableFuture; synchronized (listenerLock) { HttpResponse<JsonNode> response = Unirest.post("https://discordapp.com/api/guilds") .header("authorization", token).header("Content-Type", "application/json") .body(params.toString()).asJson(); checkResponse(response); String guildId = response.getBody().getObject().getString("id"); logger.info( "Created server and waiting for listener to be called " + "(name: {}, region: {}, icon: {}, server id: {})", name, region == null ? "null" : region.getKey(), icon != null, guildId); settableFuture = SettableFuture.create(); waitingForListener.put(guildId, settableFuture); } return settableFuture.get(); } }); if (callback != null) { Futures.addCallback(future, callback); } return future; }
From source file:de.btobastian.javacord.ImplDiscordAPI.java
@Override public Future<Server> acceptInvite(final String inviteCode, FutureCallback<Server> callback) { if (getYourself().isBot()) { throw new NotSupportedForBotsException(); }/*from w w w . j a v a 2 s. co m*/ ListenableFuture<Server> future = getThreadPool().getListeningExecutorService() .submit(new Callable<Server>() { @Override public Server call() throws Exception { logger.debug("Trying to accept invite (code: {})", inviteCode); final SettableFuture<Server> settableFuture; synchronized (listenerLock) { HttpResponse<JsonNode> response = Unirest .post("https://discordapp.com/api/invite/" + inviteCode) .header("authorization", token).asJson(); checkResponse(response); String guildId = response.getBody().getObject().getJSONObject("guild").getString("id"); if (getServerById(guildId) != null) { throw new IllegalStateException("Already member of this server!"); } logger.info( "Accepted invite and waiting for listener to be called (code: {}, server id: {})", inviteCode, guildId); settableFuture = SettableFuture.create(); waitingForListener.put(guildId, settableFuture); } return settableFuture.get(); } }); if (callback != null) { Futures.addCallback(future, callback); } return future; }
From source file:co.paralleluniverse.jersey.connector.AsyncHttpConnector.java
/** * Synchronously process client request into a response. * * The method is used by Jersey client runtime to synchronously send a request * and receive a response.//from w ww . java 2 s .co m * * @param request Jersey client request to be sent. * @return Jersey client response received for the client request. * @throws javax.ws.rs.ProcessingException in case of any invocation failure. */ @Override public ClientResponse apply(final ClientRequest request) throws ProcessingException { final SettableFuture<ClientResponse> responseFuture = SettableFuture.create(); final ByteBufferInputStream entityStream = new ByteBufferInputStream(); final AtomicBoolean futureSet = new AtomicBoolean(false); try { client.executeRequest(translateRequest(request), new AsyncHandler<Void>() { private volatile HttpResponseStatus status = null; @Override public STATE onStatusReceived(final HttpResponseStatus responseStatus) throws Exception { status = responseStatus; return STATE.CONTINUE; } @Override public STATE onHeadersReceived(HttpResponseHeaders headers) throws Exception { if (!futureSet.compareAndSet(false, true)) { return STATE.ABORT; } responseFuture.set(translateResponse(request, this.status, headers, entityStream)); return STATE.CONTINUE; } @Override public STATE onBodyPartReceived(HttpResponseBodyPart bodyPart) throws Exception { entityStream.put(bodyPart.getBodyByteBuffer()); return STATE.CONTINUE; } @Override public Void onCompleted() throws Exception { entityStream.closeQueue(); return null; } @Override public void onThrowable(Throwable t) { entityStream.closeQueue(t); if (futureSet.compareAndSet(false, true)) { t = t instanceof IOException ? new ProcessingException(t.getMessage(), t) : t; responseFuture.setException(t); } } }); return responseFuture.get(); } catch (IOException ex) { throw new ProcessingException(ex.getMessage(), ex.getCause()); } catch (ExecutionException ex) { Throwable e = ex.getCause() == null ? ex : ex.getCause(); throw new ProcessingException(e.getMessage(), e); } catch (InterruptedException ex) { throw new ProcessingException(ex.getMessage(), ex); } }
From source file:org.apache.tajo.engine.planner.physical.ExternalSortExec.java
private Scanner externalMergeAndSort(List<Chunk> chunks) throws Exception { int level = 0; final List<Chunk> inputFiles = new ArrayList<>(chunks); final List<Chunk> outputFiles = new ArrayList<>(); int remainRun = inputFiles.size(); int chunksSize = chunks.size(); long mergeStart = System.currentTimeMillis(); // continue until the remain runs are larger than defaultFanout while (remainRun > defaultFanout) { // reset outChunkId int remainInputRuns = inputFiles.size(); int outChunkId = 0; int outputFileNum = 0; List<Future<Chunk>> futures = new ArrayList<>(); // the number of files being merged in threads. List<Integer> numberOfMergingFiles = new ArrayList<>(); for (int startIdx = 0; startIdx < inputFiles.size();) { // calculate proper fanout int fanout = calculateFanout(remainInputRuns, inputFiles.size(), outputFileNum, startIdx); // how many files are merged in ith thread? numberOfMergingFiles.add(fanout); // launch a merger runner if (allocatedCoreNum > 1) { futures.add(executorService.submit( new KWayMergerCaller(level, outChunkId++, inputFiles, startIdx, fanout, false))); } else { final SettableFuture<Chunk> future = SettableFuture.create(); future.set(//from w ww.j av a 2 s . co m new KWayMergerCaller(level, outChunkId++, inputFiles, startIdx, fanout, false).call()); futures.add(future); } outputFileNum++; startIdx += fanout; remainInputRuns = inputFiles.size() - startIdx; // If unbalanced merge is available, it finishes the merge phase earlier. if (checkIfCanBeUnbalancedMerged(remainInputRuns, outputFileNum)) { info(LOG, "Unbalanced merge possibility detected: number of remain input (" + remainInputRuns + ") and output files (" + outputFileNum + ") <= " + defaultFanout); List<Chunk> switched = new ArrayList<>(); // switch the remain inputs to the next outputs for (int j = startIdx; j < inputFiles.size(); j++) { switched.add(inputFiles.get(j)); } inputFiles.removeAll(switched); outputFiles.addAll(switched); break; } } // wait for all sort runners int finishedMerger = 0; int index = 0; for (Future<Chunk> future : futures) { outputFiles.add(future.get()); // Getting the number of merged files finishedMerger += numberOfMergingFiles.get(index++); // progress = (# number of merged files / total number of files) * 0.5; progress = ((float) finishedMerger / (float) chunksSize) * 0.5f; } /* * delete merged intermediate files * * There may be 4 different types of file fragments in the list inputFiles * + A: a fragment created from fetched data from a remote host. By default, this fragment represents * a whole physical file (i.e., startOffset == 0 and length == length of physical file) * + B1: a fragment created from a local file (pseudo-fetched data from local host) in which the fragment * represents the whole physical file (i.e., startOffset == 0 AND length == length of physical file) * + B2: a fragment created from a local file (pseudo-fetched data from local host) in which the fragment * represents only a part of the physical file (i.e., startOffset > 0 OR length != length of physical file) * + C: a fragment created from merging some fragments of the above types. When this fragment is created, * its startOffset is set to 0 and its length is set to the length of the physical file, automatically * * Fragments of types A, B1, and B2 are inputs of ExternalSortExec. Among them, only B2-type fragments will * possibly be used by another task in the future. Thus, ideally, all fragments of types A, B1, and C can be * deleted at this point. However, for the ease of future code maintenance, we delete only type-C fragments here */ int numDeletedFiles = 0; for (Chunk chunk : inputFiles) { if (chunk.isMemory()) { if (LOG.isDebugEnabled()) { debug(LOG, "Remove intermediate memory tuples: " + chunk.getMemoryTuples().usedMem()); } chunk.getMemoryTuples().release(); } else if (chunk.getFragment().getInputSourceId().contains(INTERMEDIATE_FILE_PREFIX)) { localFS.delete(chunk.getFragment().getPath(), true); numDeletedFiles++; if (LOG.isDebugEnabled()) { debug(LOG, "Delete merged intermediate file: " + chunk.getFragment()); } } } if (LOG.isDebugEnabled()) { debug(LOG, numDeletedFiles + " merged intermediate files deleted"); } // switch input files to output files, and then clear outputFiles inputFiles.clear(); inputFiles.addAll(outputFiles); remainRun = inputFiles.size(); outputFiles.clear(); level++; } long mergeEnd = System.currentTimeMillis(); info(LOG, "Total merge time: " + (mergeEnd - mergeStart) + " msec"); // final result finalOutputFiles = inputFiles; result = createFinalMerger(inputFiles); return result; }
From source file:org.apache.hadoop.hive.ql.exec.tez.WorkloadManager.java
@Override public TezSessionState reopen(TezSessionState session) throws Exception { WmTezSession wmTezSession = ensureOwnedSession(session); HiveConf sessionConf = wmTezSession.getConf(); if (sessionConf == null) { // TODO: can this ever happen? LOG.warn("Session configuration is null for " + wmTezSession); sessionConf = new HiveConf(conf, WorkloadManager.class); }/* w w w . j a va2 s . c o m*/ SettableFuture<WmTezSession> future = SettableFuture.create(); currentLock.lock(); try { if (current.toReopen.containsKey(wmTezSession)) { throw new AssertionError("The session is being reopened more than once " + session); } current.toReopen.put(wmTezSession, future); notifyWmThreadUnderLock(); } finally { currentLock.unlock(); } return future.get(); }
From source file:org.apache.hadoop.hive.ql.exec.tez.WorkloadManager.java
public WmTezSession getSession(TezSessionState session, MappingInput input, HiveConf conf, final WmContext wmContext) throws Exception { WmEvent wmEvent = new WmEvent(WmEvent.EventType.GET); // Note: not actually used for pool sessions; verify some things like doAs are not set. validateConfig(conf);// w ww . j a v a 2 s. c om String queryId = HiveConf.getVar(conf, HiveConf.ConfVars.HIVEQUERYID); SettableFuture<WmTezSession> future = SettableFuture.create(); WmTezSession wmSession = checkSessionForReuse(session); GetRequest req = new GetRequest(input, queryId, future, wmSession, getRequestVersion.incrementAndGet(), wmContext); currentLock.lock(); try { current.getRequests.add(req); if (req.sessionToReuse != null) { // Note: we assume reuse is only possible for the same user and config. current.toReuse.put(wmSession, req); } notifyWmThreadUnderLock(); } finally { currentLock.unlock(); } try { WmTezSession sessionState = future.get(); wmEvent.endEvent(sessionState); return sessionState; } catch (ExecutionException ex) { Throwable realEx = ex.getCause(); throw realEx instanceof Exception ? (Exception) realEx : ex; } }
From source file:org.apache.twill.internal.kafka.client.ZKBrokerService.java
@Override public synchronized Iterable<BrokerInfo> getBrokers() { Preconditions.checkState(isRunning(), "BrokerService is not running."); if (brokerList != null) { return brokerList.get(); }// w w w . ja va 2s. c o m final SettableFuture<?> readerFuture = SettableFuture.create(); final AtomicReference<Iterable<BrokerInfo>> brokers = new AtomicReference<Iterable<BrokerInfo>>( ImmutableList.<BrokerInfo>of()); actOnExists(BROKER_IDS_PATH, new Runnable() { @Override public void run() { // Callback for fetching children list. This callback should be executed in the executorService. final FutureCallback<NodeChildren> childrenCallback = new FutureCallback<NodeChildren>() { @Override public void onSuccess(NodeChildren result) { try { // For each children node, get the BrokerInfo from the brokerInfo cache. brokers.set(ImmutableList.copyOf(Iterables.transform(brokerInfos .getAll(Iterables.transform(result.getChildren(), BROKER_ID_TRANSFORMER)) .values(), Suppliers.<BrokerInfo>supplierFunction()))); readerFuture.set(null); for (ListenerExecutor listener : listeners) { listener.changed(ZKBrokerService.this); } } catch (ExecutionException e) { readerFuture.setException(e.getCause()); } } @Override public void onFailure(Throwable t) { readerFuture.setException(t); } }; // Fetch list of broker ids Futures.addCallback(zkClient.getChildren(BROKER_IDS_PATH, new Watcher() { @Override public void process(WatchedEvent event) { if (!isRunning()) { return; } if (event.getType() == Event.EventType.NodeChildrenChanged) { Futures.addCallback(zkClient.getChildren(BROKER_IDS_PATH, this), childrenCallback, executorService); } } }), childrenCallback, executorService); } }, readerFuture, FAILURE_RETRY_SECONDS, TimeUnit.SECONDS); brokerList = createSupplier(brokers); try { readerFuture.get(); } catch (Exception e) { throw Throwables.propagate(e); } return brokerList.get(); }