use of org.apache.ratis.protocol.RaftClientReply in project alluxio by Alluxio.
the class SnapshotReplicationManager method requestInfo.
private void requestInfo() {
Preconditions.checkState(mDownloadState.get() == DownloadState.REQUEST_INFO);
try {
SingleFileSnapshotInfo latestSnapshot = mStorage.getLatestSnapshot();
SnapshotMetadata snapshotMetadata = latestSnapshot == null ? null : SnapshotMetadata.newBuilder().setSnapshotTerm(latestSnapshot.getTerm()).setSnapshotIndex(latestSnapshot.getIndex()).build();
// build SnapshotInfoRequests
Map<RaftPeerId, CompletableFuture<RaftClientReply>> jobs = mJournalSystem.getQuorumServerInfoList().stream().filter(server -> server.getServerState() == QuorumServerState.AVAILABLE).map(server -> RaftJournalUtils.getPeerId(server.getServerAddress().getHost(), server.getServerAddress().getRpcPort())).filter(peerId -> !peerId.equals(mJournalSystem.getLocalPeerId())).collect(Collectors.toMap(Function.identity(), peerId -> mJournalSystem.sendMessageAsync(peerId, toMessage(JournalQueryRequest.newBuilder().setSnapshotInfoRequest(GetSnapshotInfoRequest.getDefaultInstance()).build()))));
// query all secondary masters for information about their latest snapshot
for (Map.Entry<RaftPeerId, CompletableFuture<RaftClientReply>> job : jobs.entrySet()) {
RaftPeerId peerId = job.getKey();
try {
RaftClientReply reply = job.getValue().get();
if (reply.getException() != null) {
throw reply.getException();
}
JournalQueryResponse response = JournalQueryResponse.parseFrom(reply.getMessage().getContent().asReadOnlyByteBuffer());
if (!response.hasSnapshotInfoResponse()) {
throw new IOException("Invalid response for GetSnapshotInfoRequest " + response);
}
LOG.debug("Received snapshot info from follower {} - {}", peerId, response);
SnapshotMetadata latest = response.getSnapshotInfoResponse().getLatest();
if (snapshotMetadata == null || (latest.getSnapshotTerm() >= snapshotMetadata.getSnapshotTerm()) && latest.getSnapshotIndex() > snapshotMetadata.getSnapshotIndex()) {
mSnapshotCandidates.add(new Pair<>(latest, peerId));
}
} catch (Exception e) {
LOG.warn("Error while requesting snapshot info from {}: {}", peerId, e.toString());
}
}
} catch (Exception e) {
LogUtils.warnWithException(LOG, "Failed to request snapshot info from followers", e);
}
}
use of org.apache.ratis.protocol.RaftClientReply in project alluxio by Alluxio.
the class SnapshotReplicationManager method requestData.
private boolean requestData() {
Preconditions.checkState(mDownloadState.get() == DownloadState.REQUEST_DATA);
// request snapshots from the most recent to least recent
while (!mSnapshotCandidates.isEmpty()) {
Pair<SnapshotMetadata, RaftPeerId> candidate = mSnapshotCandidates.poll();
SnapshotMetadata metadata = candidate.getFirst();
RaftPeerId peerId = candidate.getSecond();
LOG.info("Request data from follower {} for snapshot (t: {}, i: {})", peerId, metadata.getSnapshotTerm(), metadata.getSnapshotIndex());
try {
RaftClientReply reply = mJournalSystem.sendMessageAsync(peerId, toMessage(JournalQueryRequest.newBuilder().setSnapshotRequest(GetSnapshotRequest.getDefaultInstance()).build())).get();
if (reply.getException() != null) {
throw reply.getException();
}
return true;
} catch (Exception e) {
LOG.warn("Failed to request snapshot data from {}: {}", peerId, e);
}
}
// return failure if there are no more candidates to ask snapshot from
return false;
}
use of org.apache.ratis.protocol.RaftClientReply in project incubator-ratis by apache.
the class UnorderedAsync method sendRequestWithRetry.
static void sendRequestWithRetry(PendingClientRequest pending, RaftClientImpl client) {
final CompletableFuture<RaftClientReply> f = pending.getReplyFuture();
if (f.isDone()) {
return;
}
final RaftClientRequest request = pending.newRequest();
final int attemptCount = pending.getAttemptCount();
final ClientId clientId = client.getId();
LOG.debug("{}: attempt #{} send~ {}", clientId, attemptCount, request);
client.getClientRpc().sendRequestAsyncUnordered(request).whenCompleteAsync((reply, e) -> {
try {
LOG.debug("{}: attempt #{} receive~ {}", clientId, attemptCount, reply);
final RaftException replyException = reply != null ? reply.getException() : null;
reply = client.handleLeaderException(request, reply);
if (reply != null) {
client.handleReply(request, reply);
f.complete(reply);
return;
}
final Throwable cause = replyException != null ? replyException : e;
pending.incrementExceptionCount(cause);
final ClientRetryEvent event = new ClientRetryEvent(request, cause, pending);
RetryPolicy retryPolicy = client.getRetryPolicy();
final RetryPolicy.Action action = retryPolicy.handleAttemptFailure(event);
TimeDuration sleepTime = client.getEffectiveSleepTime(cause, action.getSleepTime());
if (!action.shouldRetry()) {
f.completeExceptionally(client.noMoreRetries(event));
return;
}
if (e != null) {
if (LOG.isTraceEnabled()) {
LOG.trace(clientId + ": attempt #" + attemptCount + " failed~ " + request, e);
} else {
LOG.debug("{}: attempt #{} failed {} with {}", clientId, attemptCount, request, e);
}
e = JavaUtils.unwrapCompletionException(e);
if (e instanceof IOException) {
if (e instanceof NotLeaderException) {
client.handleNotLeaderException(request, (NotLeaderException) e, null);
} else if (e instanceof GroupMismatchException) {
f.completeExceptionally(e);
return;
} else {
client.handleIOException(request, (IOException) e);
}
} else {
if (!client.getClientRpc().handleException(request.getServerId(), e, false)) {
f.completeExceptionally(e);
return;
}
}
}
LOG.debug("schedule retry for attempt #{}, policy={}, request={}", attemptCount, retryPolicy, request);
client.getScheduler().onTimeout(sleepTime, () -> sendRequestWithRetry(pending, client), LOG, () -> clientId + ": Failed~ to retry " + request);
} catch (Exception ex) {
LOG.error(clientId + ": Failed " + request, ex);
f.completeExceptionally(ex);
}
});
}
use of org.apache.ratis.protocol.RaftClientReply in project incubator-ratis by apache.
the class OrderedAsync method sendRequest.
private CompletableFuture<RaftClientReply> sendRequest(PendingOrderedRequest pending) {
final RetryPolicy retryPolicy = client.getRetryPolicy();
final CompletableFuture<RaftClientReply> f;
final RaftClientRequest request;
if (getSlidingWindow((RaftPeerId) null).isFirst(pending.getSeqNum())) {
pending.setFirstRequest();
}
request = pending.newRequest();
LOG.debug("{}: send* {}", client.getId(), request);
f = client.getClientRpc().sendRequestAsync(request);
return f.thenApply(reply -> {
LOG.debug("{}: receive* {}", client.getId(), reply);
getSlidingWindow(request).receiveReply(request.getSlidingWindowEntry().getSeqNum(), reply, this::sendRequestWithRetry);
return reply;
}).exceptionally(e -> {
if (LOG.isTraceEnabled()) {
LOG.trace(client.getId() + ": Failed* " + request, e);
} else {
LOG.debug("{}: Failed* {} with {}", client.getId(), request, e);
}
e = JavaUtils.unwrapCompletionException(e);
if (e instanceof IOException && !(e instanceof GroupMismatchException)) {
pending.incrementExceptionCount(e);
final ClientRetryEvent event = new ClientRetryEvent(request, e, pending);
if (!retryPolicy.handleAttemptFailure(event).shouldRetry()) {
handleAsyncRetryFailure(event);
} else {
if (e instanceof NotLeaderException) {
NotLeaderException nle = (NotLeaderException) e;
client.handleNotLeaderException(request, nle, this::resetSlidingWindow);
} else {
client.handleIOException(request, (IOException) e, null, this::resetSlidingWindow);
}
}
throw new CompletionException(e);
}
failAllAsyncRequests(request, e);
return null;
});
}
use of org.apache.ratis.protocol.RaftClientReply in project incubator-ratis by apache.
the class OrderedAsync method sendRequestWithRetry.
private void sendRequestWithRetry(PendingOrderedRequest pending) {
final CompletableFuture<RaftClientReply> f = pending.getReplyFuture();
if (f.isDone()) {
return;
}
final RaftClientRequest request = pending.newRequestImpl();
if (request == null) {
// already done
LOG.debug("{} newRequestImpl returns null", pending);
return;
}
final RetryPolicy retryPolicy = client.getRetryPolicy();
sendRequest(pending).thenAccept(reply -> {
if (f.isDone()) {
return;
}
if (reply == null) {
scheduleWithTimeout(pending, request, retryPolicy, null);
} else {
client.handleReply(request, reply);
f.complete(reply);
}
}).exceptionally(e -> {
if (e instanceof CompletionException) {
e = JavaUtils.unwrapCompletionException(e);
scheduleWithTimeout(pending, request, retryPolicy, e);
return null;
}
f.completeExceptionally(e);
return null;
});
}
Aggregations