Search in sources :

Example 1 with AppendEntriesRequestProto

use of org.apache.ratis.proto.RaftProtos.AppendEntriesRequestProto in project incubator-ratis by apache.

the class GrpcLogAppender method appendLog.

private void appendLog(boolean excludeLogEntries) throws IOException {
    final AppendEntriesRequestProto pending;
    final AppendEntriesRequest request;
    final StreamObserver<AppendEntriesRequestProto> s;
    try (AutoCloseableLock writeLock = lock.writeLock(caller, LOG::trace)) {
        // prepare and enqueue the append request. note changes on follower's
        // nextIndex and ops on pendingRequests should always be associated
        // together and protected by the lock
        pending = newAppendEntriesRequest(callId++, excludeLogEntries);
        if (pending == null) {
            return;
        }
        request = new AppendEntriesRequest(pending, getFollowerId(), grpcServerMetrics);
        pendingRequests.put(request);
        increaseNextIndex(pending);
        if (appendLogRequestObserver == null) {
            appendLogRequestObserver = getClient().appendEntries(new AppendLogResponseHandler());
        }
        s = appendLogRequestObserver;
    }
    if (isRunning()) {
        sendRequest(request, pending, s);
    }
}
Also used : AppendEntriesRequestProto(org.apache.ratis.proto.RaftProtos.AppendEntriesRequestProto)

Example 2 with AppendEntriesRequestProto

use of org.apache.ratis.proto.RaftProtos.AppendEntriesRequestProto in project incubator-ratis by apache.

the class LogAppenderDefault method sendAppendEntriesWithRetries.

/**
 * Send an appendEntries RPC; retry indefinitely.
 */
@SuppressFBWarnings("RCN_REDUNDANT_NULLCHECK_OF_NULL_VALUE")
private AppendEntriesReplyProto sendAppendEntriesWithRetries() throws InterruptedException, InterruptedIOException, RaftLogIOException {
    int retry = 0;
    AppendEntriesRequestProto request = null;
    while (isRunning()) {
        // keep retrying for IOException
        try {
            if (request == null || request.getEntriesCount() == 0) {
                request = newAppendEntriesRequest(CallId.getAndIncrement(), false);
            }
            if (request == null) {
                LOG.trace("{} no entries to send now, wait ...", this);
                return null;
            } else if (!isRunning()) {
                LOG.info("{} is stopped. Skip appendEntries.", this);
                return null;
            }
            getFollower().updateLastRpcSendTime(request.getEntriesCount() == 0);
            final AppendEntriesReplyProto r = getServerRpc().appendEntries(request);
            getFollower().updateLastRpcResponseTime();
            getLeaderState().onFollowerCommitIndex(getFollower(), r.getFollowerCommit());
            return r;
        } catch (InterruptedIOException | RaftLogIOException e) {
            throw e;
        } catch (IOException ioe) {
            // TODO should have more detailed retry policy here.
            if (retry++ % 10 == 0) {
                // to reduce the number of messages
                LOG.warn("{}: Failed to appendEntries (retry={}): {}", this, retry++, ioe);
            }
            handleException(ioe);
        }
        if (isRunning()) {
            getServer().properties().rpcSleepTime().sleep();
        }
    }
    return null;
}
Also used : InterruptedIOException(java.io.InterruptedIOException) RaftLogIOException(org.apache.ratis.server.raftlog.RaftLogIOException) IOException(java.io.IOException) RaftLogIOException(org.apache.ratis.server.raftlog.RaftLogIOException) InterruptedIOException(java.io.InterruptedIOException) AppendEntriesRequestProto(org.apache.ratis.proto.RaftProtos.AppendEntriesRequestProto) AppendEntriesReplyProto(org.apache.ratis.proto.RaftProtos.AppendEntriesReplyProto) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Aggregations

AppendEntriesRequestProto (org.apache.ratis.proto.RaftProtos.AppendEntriesRequestProto)2 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)1 IOException (java.io.IOException)1 InterruptedIOException (java.io.InterruptedIOException)1 AppendEntriesReplyProto (org.apache.ratis.proto.RaftProtos.AppendEntriesReplyProto)1 RaftLogIOException (org.apache.ratis.server.raftlog.RaftLogIOException)1