List of usage examples for com.google.common.util.concurrent Futures allAsList
@Beta @CheckReturnValue public static <V> ListenableFuture<List<V>> allAsList( Iterable<? extends ListenableFuture<? extends V>> futures)
From source file:c5db.replication.ReplicatorInstance.java
@FiberOnly private void doAppendMessage(final Request<RpcWireRequest, RpcReply> request) { final AppendEntries appendMessage = request.getRequest().getAppendMessage(); // 1. return if term < currentTerm (sec 5.1) if (appendMessage.getTerm() < currentTerm) { appendReply(request, false);// w ww .j av a 2 s . c o m return; } // 2. if term > currentTerm, set it (sec 5.1) if (appendMessage.getTerm() > currentTerm) { setCurrentTerm(appendMessage.getTerm()); } // 3. Step down if we are a leader or a candidate (sec 5.2, 5.5) if (myState != State.FOLLOWER) { becomeFollower(); } // 4. reset election timeout lastRPC = clock.currentTimeMillis(); long theLeader = appendMessage.getLeaderId(); if (whosLeader != theLeader) { updateFollowersKnowledgeOfCurrentLeader(theLeader); } // 5. return failure if log doesn't contain an entry at // prevLogIndex whose term matches prevLogTerm (sec 5.3) // if msgPrevLogIndex == 0 -> special case of starting the log! long msgPrevLogIndex = appendMessage.getPrevLogIndex(); long msgPrevLogTerm = appendMessage.getPrevLogTerm(); if (msgPrevLogIndex != 0 && log.getLogTerm(msgPrevLogIndex) != msgPrevLogTerm) { AppendEntriesReply m = new AppendEntriesReply(currentTerm, false, log.getLastIndex() + 1); RpcReply reply = new RpcReply(m); request.reply(reply); return; } if (appendMessage.getEntriesList().isEmpty()) { appendReply(request, true); long newCommitIndex = Math.min(appendMessage.getCommitIndex(), log.getLastIndex()); setLastCommittedIndex(newCommitIndex); return; } // 6. if existing entries conflict with new entries, delete all // existing entries starting with first conflicting entry (sec 5.3) // nb: The process in which we fix the local log may involve several async log operations, so that is entirely // hidden up in these futures. Note that the process can fail, so we handle that as well. List<ListenableFuture<Boolean>> logOperationFutures = reconcileAppendMessageWithLocalLog(appendMessage); ListenableFuture<List<Boolean>> bundledLogFuture = Futures.allAsList(logOperationFutures); // wait for the log to commit before returning message. But do so async. C5Futures.addCallback(bundledLogFuture, (resultList) -> { appendReply(request, true); // 8. Signal the client of the Replicator that it can apply newly committed entries to state machine long newCommitIndex = Math.min(appendMessage.getCommitIndex(), log.getLastIndex()); setLastCommittedIndex(newCommitIndex); }, (Throwable t) -> { // TODO A log commit failure is probably a fatal error. Quit the instance? // TODO better error reporting. A log commit failure will be a serious issue. logger.error("failure reconciling received entries with the local log", t); appendReply(request, false); }, fiber); }
From source file:diskCacheV111.srm.SrmHandler.java
private Object dispatch(SrmReleaseFilesRequest request, Function<Object, SrmRequest> toMessage) throws InterruptedException, ExecutionException, SRMInternalErrorException { if (request.getRequestToken() == null) { // TODO: We could do the unpin calls here to avoid that each backend does that repeatedly List<ListenableFuture<SrmResponse>> futures = backends.getCurrentData().stream().map(this::toCellPath) .map(path -> srmManagerStub.send(path, toMessage.apply(request), SrmResponse.class)) .collect(toList());// ww w .j a v a 2 s. c o m return mapReleaseFilesResponse(request, Futures.allAsList(futures).get()); } else { return dispatch((Object) request, toMessage); } }
From source file:io.druid.indexing.common.task.AppenderatorDriverRealtimeIndexTask.java
private void waitForSegmentPublishAndHandoff(long timeout) throws InterruptedException, ExecutionException, TimeoutException { if (!pendingHandoffs.isEmpty()) { ListenableFuture<?> allHandoffs = Futures.allAsList(pendingHandoffs); log.info("Waiting for handoffs"); if (timeout > 0) { allHandoffs.get(timeout, TimeUnit.MILLISECONDS); } else {/*from w w w.j a v a 2 s .c om*/ allHandoffs.get(); } } }
From source file:com.spotify.futures.FuturesExtra.java
private static <Z> ListenableFuture<Z> transform(final List<? extends ListenableFuture<?>> inputs, final Function<List<Object>, Z> function) { return Futures.transform(Futures.allAsList(inputs), function); }
From source file:org.apache.brooklyn.core.mgmt.persist.BrooklynMementoPersisterToObjectStore.java
protected void visitMemento(final String phase, final BrooklynMementoRawData rawData, final Visitor visitor, final RebindExceptionHandler exceptionHandler) { List<ListenableFuture<?>> futures = Lists.newArrayList(); class VisitorWrapper implements Runnable { private final BrooklynObjectType type; private final Map.Entry<String, String> objectIdAndData; public VisitorWrapper(BrooklynObjectType type, Map.Entry<String, String> objectIdAndData) { this.type = type; this.objectIdAndData = objectIdAndData; }/* ww w. j a v a2s . c o m*/ public void run() { try { visitor.visit(type, objectIdAndData.getKey(), objectIdAndData.getValue()); } catch (Exception e) { Exceptions.propagateIfFatal(e); exceptionHandler.onLoadMementoFailed(type, "memento " + objectIdAndData.getKey() + " " + phase + " error", e); } } } for (BrooklynObjectType type : BrooklynPersistenceUtils.STANDARD_BROOKLYN_OBJECT_TYPE_PERSISTENCE_ORDER) { for (final Map.Entry<String, String> entry : rawData.getObjectsOfType(type).entrySet()) { futures.add(executor.submit(new VisitorWrapper(type, entry))); } } try { // Wait for all, failing fast if any exceptions. Futures.allAsList(futures).get(); } catch (Exception e) { Exceptions.propagateIfFatal(e); List<Exception> exceptions = Lists.newArrayList(); for (ListenableFuture<?> future : futures) { if (future.isDone()) { try { future.get(); } catch (InterruptedException e2) { throw Exceptions.propagate(e2); } catch (ExecutionException e2) { LOG.warn("Problem loading memento (" + phase + "): " + e2, e2); exceptions.add(e2); } future.cancel(true); } } if (exceptions.isEmpty()) { throw Exceptions.propagate(e); } else { // Normally there should be at lesat one failure; otherwise all.get() would not have failed. throw new CompoundRuntimeException("Problem loading mementos (" + phase + ")", exceptions); } } }
From source file:org.apache.druid.server.lookup.cache.LookupCoordinatorManager.java
@VisibleForTesting void lookupManagementLoop() { // Sanity check for if we are shutting down if (Thread.currentThread().isInterrupted() || !lifecycleLock.awaitStarted(15, TimeUnit.SECONDS)) { LOG.info("Not updating lookups because process was interrupted or not finished starting yet."); return;//from w w w .j a v a 2 s . com } final Map<String, Map<String, LookupExtractorFactoryMapContainer>> allLookupTiers = lookupMapConfigRef .get(); if (allLookupTiers == null) { LOG.info("Not updating lookups because no data exists"); return; } LOG.debug("Starting lookup sync for on all nodes."); try { List<ListenableFuture<Map.Entry>> futures = new ArrayList<>(); for (Map.Entry<String, Map<String, LookupExtractorFactoryMapContainer>> tierEntry : allLookupTiers .entrySet()) { LOG.debug("Starting lookup mgmt for tier [%s].", tierEntry.getKey()); final Map<String, LookupExtractorFactoryMapContainer> tierLookups = tierEntry.getValue(); for (final HostAndPortWithScheme node : lookupNodeDiscovery.getNodesInTier(tierEntry.getKey())) { LOG.debug("Starting lookup mgmt for tier [%s] and host [%s:%s:%s].", tierEntry.getKey(), node.getScheme(), node.getHostText(), node.getPort()); futures.add(executorService.submit(() -> { try { return new AbstractMap.SimpleImmutableEntry<>(node.getHostAndPort(), doLookupManagementOnNode(node, tierLookups)); } catch (InterruptedException ex) { LOG.warn(ex, "lookup management on node [%s:%s:%s] interrupted.", node.getScheme(), node.getHostText(), node.getPort()); return null; } catch (Exception ex) { LOG.makeAlert(ex, "Failed to finish lookup management on node [%s:%s:%s]", node.getScheme(), node.getHostText(), node.getPort()).emit(); return null; } })); } } final ListenableFuture<List<Map.Entry>> allFuture = Futures.allAsList(futures); try { ImmutableMap.Builder<HostAndPort, LookupsState<LookupExtractorFactoryMapContainer>> stateBuilder = ImmutableMap .builder(); allFuture.get(lookupCoordinatorManagerConfig.getAllHostTimeout().getMillis(), TimeUnit.MILLISECONDS) .stream().filter(Objects::nonNull).forEach(stateBuilder::put); knownOldState.set(stateBuilder.build()); } catch (InterruptedException ex) { allFuture.cancel(true); Thread.currentThread().interrupt(); throw ex; } catch (Exception ex) { allFuture.cancel(true); throw ex; } } catch (Exception ex) { LOG.makeAlert(ex, "Failed to finish lookup management loop.").emit(); } LOG.debug("Finished lookup sync for on all nodes."); }
From source file:com.spotify.futures.FuturesExtra.java
private static <Z> ListenableFuture<Z> transform(final List<? extends ListenableFuture<?>> inputs, final AsyncFunction<List<Object>, Z> function) { return Futures.transform(Futures.allAsList(inputs), function); }
From source file:com.spotify.futures.FuturesExtra.java
/** * <p>Transform a list of futures to a future that returns a joined result of them all. * The result can be used to get the joined results and ensures no future that were not part of * the join is accessed.</p>/*from w w w . j av a 2s. c o m*/ * @see #join(ListenableFuture...) */ public static ListenableFuture<JoinedResults> join(List<? extends ListenableFuture<?>> inputs) { return Futures.transform(Futures.allAsList(inputs), new JoinedResults.Transform(inputs)); }
From source file:org.hawkular.alerts.engine.impl.CassActionsServiceImpl.java
private boolean filterByActionPlugin(String tenantId, Set<ActionHistoryPK> actionPks, ActionsCriteria criteria) throws Exception { boolean filterByActionPlugin = false; if (criteria.getActionPlugin() != null || (criteria.getActionPlugins() != null && !criteria.getActionPlugins().isEmpty())) { filterByActionPlugin = true;/* w w w . j a v a 2s.com*/ PreparedStatement selectActionHistoryActionPlugin = CassStatement.get(session, CassStatement.SELECT_ACTION_HISTORY_ACTION_PLUGIN); List<ResultSetFuture> futures = new ArrayList<>(); if (criteria.getActionPlugin() != null) { futures.add(session .executeAsync(selectActionHistoryActionPlugin.bind(tenantId, criteria.getActionPlugin()))); } if (criteria.getActionPlugins() != null && !criteria.getActionPlugins().isEmpty()) { for (String actionPlugin : criteria.getActionPlugins()) { futures.add(session.executeAsync(selectActionHistoryActionPlugin.bind(tenantId, actionPlugin))); } } List<ResultSet> rsActionHistory = Futures.allAsList(futures).get(); rsActionHistory.stream().forEach(r -> { for (Row row : r) { ActionHistoryPK actionHistoryPK = new ActionHistoryPK(); actionHistoryPK.tenantId = tenantId; actionHistoryPK.actionPlugin = row.getString("actionPlugin"); actionHistoryPK.actionId = row.getString("actionId"); actionHistoryPK.alertId = row.getString("alertId"); actionHistoryPK.ctime = row.getLong("ctime"); actionPks.add(actionHistoryPK); } }); } return filterByActionPlugin; }
From source file:org.opendaylight.openflowplugin.applications.frsync.impl.strategy.SyncPlanPushStrategyIncrementalImpl.java
ListenableFuture<RpcResult<Void>> addMissingMeters(final NodeId nodeId, final InstanceIdentifier<FlowCapableNode> nodeIdent, final ItemSyncBox<Meter> syncBox, final SyncCrudCounters counters) { if (syncBox.isEmpty()) { LOG.trace("no meters configured for node: {} -> SKIPPING", nodeId.getValue()); return RpcResultBuilder.<Void>success().buildFuture(); }/* ww w . j a v a2 s . c o m*/ final CrudCounts meterCrudCounts = counters.getMeterCrudCounts(); final List<ListenableFuture<RpcResult<AddMeterOutput>>> allResults = new ArrayList<>(); final List<ListenableFuture<RpcResult<UpdateMeterOutput>>> allUpdateResults = new ArrayList<>(); for (Meter meter : syncBox.getItemsToPush()) { final KeyedInstanceIdentifier<Meter, MeterKey> meterIdent = nodeIdent.child(Meter.class, meter.getKey()); LOG.debug("adding meter {} - absent on device {}", meter.getMeterId(), nodeId); allResults.add(JdkFutureAdapters.listenInPoolThread(meterForwarder.add(meterIdent, meter, nodeIdent))); meterCrudCounts.incAdded(); } for (ItemSyncBox.ItemUpdateTuple<Meter> meterTuple : syncBox.getItemsToUpdate()) { final Meter existingMeter = meterTuple.getOriginal(); final Meter updated = meterTuple.getUpdated(); final KeyedInstanceIdentifier<Meter, MeterKey> meterIdent = nodeIdent.child(Meter.class, updated.getKey()); LOG.trace("meter {} - needs update on device {}", updated.getMeterId(), nodeId); allUpdateResults.add(JdkFutureAdapters .listenInPoolThread(meterForwarder.update(meterIdent, existingMeter, updated, nodeIdent))); meterCrudCounts.incUpdated(); } final ListenableFuture<RpcResult<Void>> singleVoidAddResult = Futures.transform( Futures.allAsList(allResults), ReconcileUtil.<AddMeterOutput>createRpcResultCondenser("meter add")); final ListenableFuture<RpcResult<Void>> singleVoidUpdateResult = Futures.transform( Futures.allAsList(allUpdateResults), ReconcileUtil.<UpdateMeterOutput>createRpcResultCondenser("meter update")); return Futures.transform(Futures.allAsList(singleVoidUpdateResult, singleVoidAddResult), ReconcileUtil.<Void>createRpcResultCondenser("meter add/update")); }