use of com.google.common.util.concurrent.FutureCallback in project presto by prestodb.
the class ContinuousTaskStatusFetcher method scheduleNextRequest.
private synchronized void scheduleNextRequest() {
// stopped or done?
TaskStatus taskStatus = getTaskStatus();
if (!running || taskStatus.getState().isDone()) {
return;
}
// outstanding request?
if (future != null && !future.isDone()) {
// this should never happen
log.error("Can not reschedule update because an update is already running");
return;
}
// if throttled due to error, asynchronously wait for timeout and try again
ListenableFuture<?> errorRateLimit = errorTracker.acquireRequestPermit();
if (!errorRateLimit.isDone()) {
errorRateLimit.addListener(this::scheduleNextRequest, executor);
return;
}
Request.Builder requestBuilder;
ResponseHandler responseHandler;
if (thriftTransportEnabled) {
requestBuilder = ThriftRequestUtils.prepareThriftGet(thriftProtocol);
responseHandler = new ThriftResponseHandler(unwrapThriftCodec(taskStatusCodec));
} else if (binaryTransportEnabled) {
requestBuilder = getBinaryTransportBuilder(prepareGet());
responseHandler = createFullSmileResponseHandler((SmileCodec<TaskStatus>) taskStatusCodec);
} else {
requestBuilder = getJsonTransportBuilder(prepareGet());
responseHandler = createAdaptingJsonResponseHandler((JsonCodec<TaskStatus>) taskStatusCodec);
}
Request request = requestBuilder.setUri(uriBuilderFrom(taskStatus.getSelf()).appendPath("status").build()).setHeader(PRESTO_CURRENT_STATE, taskStatus.getState().toString()).setHeader(PRESTO_MAX_WAIT, refreshMaxWait.toString()).build();
errorTracker.startRequest();
future = httpClient.executeAsync(request, responseHandler);
currentRequestStartNanos.set(System.nanoTime());
FutureCallback callback;
if (thriftTransportEnabled) {
callback = new ThriftHttpResponseHandler(this, request.getUri(), stats.getHttpResponseStats(), REMOTE_TASK_ERROR);
} else {
callback = new SimpleHttpResponseHandler<>(this, request.getUri(), stats.getHttpResponseStats(), REMOTE_TASK_ERROR);
}
Futures.addCallback(future, callback, executor);
}
use of com.google.common.util.concurrent.FutureCallback in project cassandra by apache.
the class SecondaryIndexManager method buildIndexesBlocking.
/**
* Performs a blocking (re)indexing/recovery of the specified SSTables for the specified indexes.
*
* If the index doesn't support ALL {@link Index.LoadType} it performs a recovery {@link Index#getRecoveryTaskSupport()}
* instead of a build {@link Index#getBuildTaskSupport()}
*
* @param sstables the SSTables to be (re)indexed
* @param indexes the indexes to be (re)built for the specifed SSTables
* @param isFullRebuild True if this method is invoked as a full index rebuild, false otherwise
*/
@SuppressWarnings({ "unchecked" })
private void buildIndexesBlocking(Collection<SSTableReader> sstables, Set<Index> indexes, boolean isFullRebuild) {
if (indexes.isEmpty())
return;
// Mark all indexes as building: this step must happen first, because if any index can't be marked, the whole
// process needs to abort
markIndexesBuilding(indexes, isFullRebuild, false);
// Build indexes in a try/catch, so that any index not marked as either built or failed will be marked as failed:
final Set<Index> builtIndexes = Sets.newConcurrentHashSet();
final Set<Index> unbuiltIndexes = Sets.newConcurrentHashSet();
// Any exception thrown during index building that could be suppressed by the finally block
Exception accumulatedFail = null;
try {
logger.info("Submitting index {} of {} for data in {}", isFullRebuild ? "recovery" : "build", indexes.stream().map(i -> i.getIndexMetadata().name).collect(Collectors.joining(",")), sstables.stream().map(SSTableReader::toString).collect(Collectors.joining(",")));
// Group all building tasks
Map<Index.IndexBuildingSupport, Set<Index>> byType = new HashMap<>();
for (Index index : indexes) {
IndexBuildingSupport buildOrRecoveryTask = isFullRebuild ? index.getBuildTaskSupport() : index.getRecoveryTaskSupport();
Set<Index> stored = byType.computeIfAbsent(buildOrRecoveryTask, i -> new HashSet<>());
stored.add(index);
}
// Schedule all index building tasks with a callback to mark them as built or failed
List<Future<?>> futures = new ArrayList<>(byType.size());
byType.forEach((buildingSupport, groupedIndexes) -> {
SecondaryIndexBuilder builder = buildingSupport.getIndexBuildTask(baseCfs, groupedIndexes, sstables);
final AsyncPromise<Object> build = new AsyncPromise<>();
CompactionManager.instance.submitIndexBuild(builder).addCallback(new FutureCallback() {
@Override
public void onFailure(Throwable t) {
logAndMarkIndexesFailed(groupedIndexes, t, false);
unbuiltIndexes.addAll(groupedIndexes);
build.tryFailure(t);
}
@Override
public void onSuccess(Object o) {
groupedIndexes.forEach(i -> markIndexBuilt(i, isFullRebuild));
logger.info("Index build of {} completed", getIndexNames(groupedIndexes));
builtIndexes.addAll(groupedIndexes);
build.trySuccess(o);
}
});
futures.add(build);
});
// Finally wait for the index builds to finish and flush the indexes that built successfully
FBUtilities.waitOnFutures(futures);
} catch (Exception e) {
accumulatedFail = e;
throw e;
} finally {
try {
// Fail any indexes that couldn't be marked
Set<Index> failedIndexes = Sets.difference(indexes, Sets.union(builtIndexes, unbuiltIndexes));
if (!failedIndexes.isEmpty()) {
logAndMarkIndexesFailed(failedIndexes, accumulatedFail, false);
}
// Flush all built indexes with an aynchronous callback to log the success or failure of the flush
flushIndexesBlocking(builtIndexes, new FutureCallback() {
String indexNames = StringUtils.join(builtIndexes.stream().map(i -> i.getIndexMetadata().name).collect(Collectors.toList()), ',');
@Override
public void onFailure(Throwable ignored) {
logger.info("Index flush of {} failed", indexNames);
}
@Override
public void onSuccess(Object ignored) {
logger.info("Index flush of {} completed", indexNames);
}
});
} catch (Exception e) {
if (accumulatedFail != null) {
accumulatedFail.addSuppressed(e);
} else {
throw e;
}
}
}
}
use of com.google.common.util.concurrent.FutureCallback in project flink by apache.
the class FlinkKinesisProducer method open.
// --------------------------- Lifecycle methods ---------------------------
@Override
public void open(Configuration parameters) throws Exception {
super.open(parameters);
schema.open(RuntimeContextInitializationContextAdapters.serializationAdapter(getRuntimeContext(), metricGroup -> metricGroup.addGroup("user")));
// check and pass the configuration properties
KinesisProducerConfiguration producerConfig = KinesisConfigUtil.getValidatedProducerConfiguration(configProps);
producer = getKinesisProducer(producerConfig);
final MetricGroup kinesisMectricGroup = getRuntimeContext().getMetricGroup().addGroup(KINESIS_PRODUCER_METRIC_GROUP);
this.backpressureCycles = kinesisMectricGroup.counter(METRIC_BACKPRESSURE_CYCLES);
kinesisMectricGroup.gauge(METRIC_OUTSTANDING_RECORDS_COUNT, producer::getOutstandingRecordsCount);
backpressureLatch = new TimeoutLatch();
callback = new FutureCallback<UserRecordResult>() {
@Override
public void onSuccess(UserRecordResult result) {
backpressureLatch.trigger();
if (!result.isSuccessful()) {
if (failOnError) {
// only remember the first thrown exception
if (thrownException == null) {
thrownException = new RuntimeException("Record was not sent successful");
}
} else {
LOG.warn("Record was not sent successful");
}
}
}
@Override
public void onFailure(Throwable t) {
backpressureLatch.trigger();
if (failOnError) {
thrownException = t;
} else {
LOG.warn("An exception occurred while processing a record", t);
}
}
};
if (this.customPartitioner != null) {
this.customPartitioner.initialize(getRuntimeContext().getIndexOfThisSubtask(), getRuntimeContext().getNumberOfParallelSubtasks());
}
final RuntimeContext ctx = getRuntimeContext();
ctx.registerUserCodeClassLoaderReleaseHookIfAbsent(KINESIS_PRODUCER_RELEASE_HOOK_NAME, () -> this.runClassLoaderReleaseHook(ctx.getUserCodeClassLoader()));
LOG.info("Started Kinesis producer instance for region '{}'", producerConfig.getRegion());
}
use of com.google.common.util.concurrent.FutureCallback in project genius by opendaylight.
the class AlivenessMonitor method monitorStart.
@Override
public Future<RpcResult<MonitorStartOutput>> monitorStart(MonitorStartInput input) {
RpcResultBuilder<MonitorStartOutput> rpcResultBuilder;
final Config in = input.getConfig();
Long profileId = in.getProfileId();
LOG.debug("Monitor Start invoked with Config: {}, Profile Id: {}", in, profileId);
try {
if (in.getMode() != MonitoringMode.OneOne) {
throw new UnsupportedConfigException("Unsupported Monitoring mode. Currently one-one mode is supported");
}
Optional<MonitorProfile> optProfile = SingleTransactionDataBroker.syncReadOptionalAndTreatReadFailedExceptionAsAbsentOptional(dataBroker, LogicalDatastoreType.OPERATIONAL, getMonitorProfileId(profileId));
final MonitorProfile profile;
if (!optProfile.isPresent()) {
String errMsg = String.format("No monitoring profile associated with Id: %d", profileId);
LOG.error("Monitor start failed. {}", errMsg);
throw new RuntimeException(errMsg);
} else {
profile = optProfile.get();
}
final EtherTypes ethType = profile.getProtocolType();
String interfaceName = null;
EndpointType srcEndpointType = in.getSource().getEndpointType();
if (srcEndpointType instanceof Interface) {
Interface endPoint = (Interface) srcEndpointType;
interfaceName = endPoint.getInterfaceName();
} else {
throw new UnsupportedConfigException("Unsupported source Endpoint type. Only Interface Endpoint currently supported for monitoring");
}
if (Strings.isNullOrEmpty(interfaceName)) {
throw new RuntimeException("Interface Name not defined in the source Endpoint");
}
// Initially the support is for one monitoring per interface.
// Revisit the retrieving monitor id logic when the multiple
// monitoring for same interface is needed.
EndpointType destEndpointType = null;
if (in.getDestination() != null) {
destEndpointType = in.getDestination().getEndpointType();
}
String idKey = getUniqueKey(interfaceName, ethType.toString(), srcEndpointType, destEndpointType);
final long monitorId = getUniqueId(idKey);
Optional<MonitoringInfo> optKey = SingleTransactionDataBroker.syncReadOptionalAndTreatReadFailedExceptionAsAbsentOptional(dataBroker, LogicalDatastoreType.OPERATIONAL, getMonitoringInfoId(monitorId));
final AlivenessProtocolHandler<?> handler;
if (optKey.isPresent()) {
String message = String.format("Monitoring for the interface %s with this configuration " + "is already registered.", interfaceName);
LOG.warn("Monitoring for the interface {} with this configuration is already registered.", interfaceName);
MonitorStartOutput output = new MonitorStartOutputBuilder().setMonitorId(monitorId).build();
rpcResultBuilder = RpcResultBuilder.success(output).withWarning(ErrorType.APPLICATION, "config-exists", message);
return Futures.immediateFuture(rpcResultBuilder.build());
} else {
// Construct the monitor key
final MonitoringInfo monitoringInfo = new MonitoringInfoBuilder().setId(monitorId).setMode(in.getMode()).setProfileId(profileId).setDestination(in.getDestination()).setSource(in.getSource()).build();
// Construct the initial monitor state
handler = alivenessProtocolHandlerRegistry.get(ethType);
final String monitoringKey = handler.getUniqueMonitoringKey(monitoringInfo);
MonitoringStateBuilder monitoringStateBuilder = new MonitoringStateBuilder().setMonitorKey(monitoringKey).setMonitorId(monitorId).setState(LivenessState.Unknown).setStatus(MonitorStatus.Started);
if (ethType != EtherTypes.Bfd) {
monitoringStateBuilder.setRequestCount(INITIAL_COUNT).setResponsePendingCount(INITIAL_COUNT);
}
MonitoringState monitoringState = monitoringStateBuilder.build();
Futures.addCallback(txRunner.callWithNewWriteOnlyTransactionAndSubmit(tx -> {
tx.put(LogicalDatastoreType.OPERATIONAL, getMonitoringInfoId(monitorId), monitoringInfo, CREATE_MISSING_PARENT);
LOG.debug("adding oper monitoring info {}", monitoringInfo);
tx.put(LogicalDatastoreType.OPERATIONAL, getMonitorStateId(monitoringKey), monitoringState, CREATE_MISSING_PARENT);
LOG.debug("adding oper monitoring state {}", monitoringState);
MonitoridKeyEntry mapEntry = new MonitoridKeyEntryBuilder().setMonitorId(monitorId).setMonitorKey(monitoringKey).build();
tx.put(LogicalDatastoreType.OPERATIONAL, getMonitorMapId(monitorId), mapEntry, CREATE_MISSING_PARENT);
LOG.debug("adding oper map entry {}", mapEntry);
}), new FutureCallback<Void>() {
@Override
public void onFailure(Throwable error) {
String errorMsg = String.format("Adding Monitoring info: %s in Datastore failed", monitoringInfo);
LOG.warn("Adding Monitoring info: {} in Datastore failed", monitoringInfo, error);
throw new RuntimeException(errorMsg, error);
}
@Override
public void onSuccess(Void noarg) {
lockMap.put(monitoringKey, new Semaphore(1, true));
if (ethType == EtherTypes.Bfd) {
handler.startMonitoringTask(monitoringInfo);
return;
}
// Schedule task
LOG.debug("Scheduling monitor task for config: {}", in);
scheduleMonitoringTask(monitoringInfo, profile.getMonitorInterval());
}
}, callbackExecutorService);
}
associateMonitorIdWithInterface(monitorId, interfaceName);
MonitorStartOutput output = new MonitorStartOutputBuilder().setMonitorId(monitorId).build();
rpcResultBuilder = RpcResultBuilder.success(output);
} catch (UnsupportedConfigException e) {
LOG.error("Start Monitoring Failed. ", e);
rpcResultBuilder = RpcResultBuilder.<MonitorStartOutput>failed().withError(ErrorType.APPLICATION, e.getMessage(), e);
}
return Futures.immediateFuture(rpcResultBuilder.build());
}
use of com.google.common.util.concurrent.FutureCallback in project genius by opendaylight.
the class AlivenessMonitor method resumeMonitoring.
private void resumeMonitoring(final long monitorId) {
final ReadOnlyTransaction tx = dataBroker.newReadOnlyTransaction();
ListenableFuture<Optional<MonitoringInfo>> readInfoResult = tx.read(LogicalDatastoreType.OPERATIONAL, getMonitoringInfoId(monitorId));
Futures.addCallback(readInfoResult, new FutureCallback<Optional<MonitoringInfo>>() {
@Override
public void onFailure(Throwable error) {
String msg = String.format("Unable to read monitoring info associated with monitor id %d", monitorId);
LOG.error("Monitor resume Failed. {}", msg, error);
tx.close();
}
@Override
public void onSuccess(@Nonnull Optional<MonitoringInfo> optInfo) {
if (optInfo.isPresent()) {
final MonitoringInfo info = optInfo.get();
ListenableFuture<Optional<MonitorProfile>> readProfile = tx.read(LogicalDatastoreType.OPERATIONAL, getMonitorProfileId(info.getProfileId()));
Futures.addCallback(readProfile, new FutureCallback<Optional<MonitorProfile>>() {
@Override
public void onFailure(Throwable error) {
String msg = String.format("Unable to read Monitoring profile associated with id %d", info.getProfileId());
LOG.warn("Monitor resume Failed. {}", msg, error);
tx.close();
}
@Override
public void onSuccess(@Nonnull Optional<MonitorProfile> optProfile) {
tx.close();
if (optProfile.isPresent()) {
updateMonitorStatusTo(monitorId, MonitorStatus.Started, currentStatus -> currentStatus != MonitorStatus.Started);
MonitorProfile profile = optProfile.get();
LOG.debug("Monitor Resume - Scheduling monitoring task for Id: {}", monitorId);
scheduleMonitoringTask(info, profile.getMonitorInterval());
} else {
String msg = String.format("Monitoring profile associated with id %d is not present", info.getProfileId());
LOG.warn("Monitor resume Failed. {}", msg);
}
}
}, MoreExecutors.directExecutor());
} else {
tx.close();
String msg = String.format("Monitoring info associated with id %d is not present", monitorId);
LOG.warn("Monitor resume Failed. {}", msg);
}
}
}, MoreExecutors.directExecutor());
}
Aggregations