use of java.util.concurrent.CompletionException in project redisson by redisson.
the class RedissonKeys method deleteByPatternAsync.
@Override
public RFuture<Long> deleteByPatternAsync(String pattern) {
if (commandExecutor instanceof CommandBatchService || commandExecutor instanceof CommandReactiveBatchService || commandExecutor instanceof CommandRxBatchService) {
if (getConnectionManager().isClusterMode()) {
throw new IllegalStateException("This method doesn't work in batch for Redis cluster mode. For Redis cluster execute it as non-batch method");
}
return commandExecutor.evalWriteAsync((String) null, null, RedisCommands.EVAL_LONG, "local keys = redis.call('keys', ARGV[1]) " + "local n = 0 " + "for i=1, #keys,5000 do " + "n = n + redis.call('del', unpack(keys, i, math.min(i+4999, table.getn(keys)))) " + "end " + "return n;", Collections.emptyList(), pattern);
}
int batchSize = 500;
List<CompletableFuture<Long>> futures = new ArrayList<>();
for (MasterSlaveEntry entry : commandExecutor.getConnectionManager().getEntrySet()) {
commandExecutor.getConnectionManager().getExecutor().execute(() -> {
long count = 0;
try {
Iterator<String> keysIterator = createKeysIterator(entry, RedisCommands.SCAN, pattern, batchSize);
List<String> keys = new ArrayList<>();
while (keysIterator.hasNext()) {
String key = keysIterator.next();
keys.add(key);
if (keys.size() % batchSize == 0) {
count += delete(keys.toArray(new String[0]));
keys.clear();
}
}
if (!keys.isEmpty()) {
count += delete(keys.toArray(new String[0]));
keys.clear();
}
RFuture<Long> future = RedissonPromise.newSucceededFuture(count);
futures.add(future.toCompletableFuture());
} catch (Exception e) {
CompletableFuture<Long> future = new CompletableFuture<>();
future.completeExceptionally(e);
futures.add(future);
}
});
}
CompletableFuture<Void> future = CompletableFuture.allOf(futures.toArray(new CompletableFuture[0]));
CompletableFuture<Long> res = future.handle((r, e) -> {
long cc = futures.stream().filter(f -> f.isDone()).mapToLong(f -> f.getNow(0L)).sum();
if (e != null) {
if (cc > 0) {
RedisException ex = new RedisException(cc + " keys have been deleted. But one or more nodes has an error", e);
throw new CompletionException(ex);
} else {
throw new CompletionException(e);
}
}
return cc;
});
return new CompletableFutureWrapper<>(res);
}
use of java.util.concurrent.CompletionException in project alluxio by Alluxio.
the class RaftJournalSystem method sendMessageAsync.
/**
* Sends a message to a raft server asynchronously.
*
* @param server the raft peer id of the target server
* @param message the message to send
* @return a future to be completed with the client reply
*/
public synchronized CompletableFuture<RaftClientReply> sendMessageAsync(RaftPeerId server, Message message) {
RaftClient client = createClient();
RaftClientRequest request = RaftClientRequest.newBuilder().setClientId(mRawClientId).setServerId(server).setGroupId(RAFT_GROUP_ID).setCallId(nextCallId()).setMessage(message).setType(RaftClientRequest.staleReadRequestType(0)).setSlidingWindowEntry(null).build();
return client.getClientRpc().sendRequestAsync(request).whenComplete((reply, t) -> {
try {
client.close();
} catch (IOException e) {
throw new CompletionException(e);
}
});
}
use of java.util.concurrent.CompletionException in project alluxio by Alluxio.
the class SnapshotReplicationManager method installSnapshotFromLeader.
/**
* Downloads and installs a snapshot from the leader.
*
* @return a future with the term index of the installed snapshot
*/
public CompletableFuture<TermIndex> installSnapshotFromLeader() {
if (mJournalSystem.isLeader()) {
return RaftJournalUtils.completeExceptionally(new IllegalStateException("Abort snapshot installation after becoming a leader"));
}
if (!transitionState(DownloadState.IDLE, DownloadState.STREAM_DATA)) {
return RaftJournalUtils.completeExceptionally(new IllegalStateException("State is not IDLE when starting a snapshot installation"));
}
try (RaftJournalServiceClient client = getJournalServiceClient()) {
String address = String.valueOf(client.getAddress());
SnapshotDownloader<DownloadSnapshotPRequest, DownloadSnapshotPResponse> observer = SnapshotDownloader.forFollower(mStorage, address);
Timer.Context ctx = MetricsSystem.timer(MetricKey.MASTER_EMBEDDED_JOURNAL_SNAPSHOT_DOWNLOAD_TIMER.getName()).time();
client.downloadSnapshot(observer);
return observer.getFuture().thenApplyAsync((termIndex) -> {
ctx.close();
mDownloadedSnapshot = observer.getSnapshotToInstall();
transitionState(DownloadState.STREAM_DATA, DownloadState.DOWNLOADED);
long index = installDownloadedSnapshot();
if (index == RaftLog.INVALID_LOG_INDEX) {
throw new CompletionException(new RuntimeException(String.format("Failed to install the downloaded snapshot %s", termIndex)));
}
if (index != termIndex.getIndex()) {
throw new CompletionException(new IllegalStateException(String.format("Mismatched snapshot installed - downloaded %d, installed %d", termIndex.getIndex(), index)));
}
return termIndex;
}).whenComplete((termIndex, throwable) -> {
if (throwable != null) {
LOG.error("Unexpected exception downloading snapshot from leader {}.", address, throwable);
transitionState(DownloadState.STREAM_DATA, DownloadState.IDLE);
}
});
} catch (Exception e) {
transitionState(DownloadState.STREAM_DATA, DownloadState.IDLE);
return RaftJournalUtils.completeExceptionally(e);
}
}
use of java.util.concurrent.CompletionException in project flink by apache.
the class RestClusterClient method disposeSavepoint.
@Override
public CompletableFuture<Acknowledge> disposeSavepoint(String savepointPath) {
final SavepointDisposalRequest savepointDisposalRequest = new SavepointDisposalRequest(savepointPath);
final CompletableFuture<TriggerResponse> savepointDisposalTriggerFuture = sendRequest(SavepointDisposalTriggerHeaders.getInstance(), savepointDisposalRequest);
final CompletableFuture<AsynchronousOperationInfo> savepointDisposalFuture = savepointDisposalTriggerFuture.thenCompose((TriggerResponse triggerResponse) -> {
final TriggerId triggerId = triggerResponse.getTriggerId();
final SavepointDisposalStatusHeaders savepointDisposalStatusHeaders = SavepointDisposalStatusHeaders.getInstance();
final SavepointDisposalStatusMessageParameters savepointDisposalStatusMessageParameters = savepointDisposalStatusHeaders.getUnresolvedMessageParameters();
savepointDisposalStatusMessageParameters.triggerIdPathParameter.resolve(triggerId);
return pollResourceAsync(() -> sendRequest(savepointDisposalStatusHeaders, savepointDisposalStatusMessageParameters));
});
return savepointDisposalFuture.thenApply((AsynchronousOperationInfo asynchronousOperationInfo) -> {
if (asynchronousOperationInfo.getFailureCause() == null) {
return Acknowledge.get();
} else {
throw new CompletionException(asynchronousOperationInfo.getFailureCause());
}
});
}
use of java.util.concurrent.CompletionException in project flink by apache.
the class RestClusterClient method triggerSavepoint.
private CompletableFuture<String> triggerSavepoint(final JobID jobId, @Nullable final String savepointDirectory, final boolean cancelJob, final SavepointFormatType formatType) {
final SavepointTriggerHeaders savepointTriggerHeaders = SavepointTriggerHeaders.getInstance();
final SavepointTriggerMessageParameters savepointTriggerMessageParameters = savepointTriggerHeaders.getUnresolvedMessageParameters();
savepointTriggerMessageParameters.jobID.resolve(jobId);
final CompletableFuture<TriggerResponse> responseFuture = sendRequest(savepointTriggerHeaders, savepointTriggerMessageParameters, new SavepointTriggerRequestBody(savepointDirectory, cancelJob, formatType, null));
return responseFuture.thenCompose(savepointTriggerResponseBody -> {
final TriggerId savepointTriggerId = savepointTriggerResponseBody.getTriggerId();
return pollSavepointAsync(jobId, savepointTriggerId);
}).thenApply(savepointInfo -> {
if (savepointInfo.getFailureCause() != null) {
throw new CompletionException(savepointInfo.getFailureCause());
}
return savepointInfo.getLocation();
});
}
Aggregations