Search in sources :

Example 6 with Timestamp

use of org.apache.ratis.util.Timestamp in project incubator-ratis by apache.

the class FollowerState method updateLastRpcTime.

void updateLastRpcTime(boolean inLogSync) {
    lastRpcTime = new Timestamp();
    LOG.trace("{} update last rpc time to {} {}", server.getId(), lastRpcTime, inLogSync);
    this.inLogSync = inLogSync;
}
Also used : Timestamp(org.apache.ratis.util.Timestamp)

Example 7 with Timestamp

use of org.apache.ratis.util.Timestamp in project incubator-ratis by apache.

the class LeaderElection method waitForResults.

private ResultAndTerm waitForResults(final long electionTerm, final int submitted) throws InterruptedException {
    final Timestamp timeout = new Timestamp().addTimeMs(server.getRandomTimeoutMs());
    final List<RequestVoteReplyProto> responses = new ArrayList<>();
    final List<Exception> exceptions = new ArrayList<>();
    int waitForNum = submitted;
    Collection<RaftPeerId> votedPeers = new ArrayList<>();
    while (waitForNum > 0 && running && server.isCandidate()) {
        final long waitTime = -timeout.elapsedTimeMs();
        if (waitTime <= 0) {
            return logAndReturn(Result.TIMEOUT, responses, exceptions, -1);
        }
        try {
            final Future<RequestVoteReplyProto> future = service.poll(waitTime, TimeUnit.MILLISECONDS);
            if (future == null) {
                // poll timeout, continue to return Result.TIMEOUT
                continue;
            }
            final RequestVoteReplyProto r = future.get();
            responses.add(r);
            if (r.getShouldShutdown()) {
                return logAndReturn(Result.SHUTDOWN, responses, exceptions, -1);
            }
            if (r.getTerm() > electionTerm) {
                return logAndReturn(Result.DISCOVERED_A_NEW_TERM, responses, exceptions, r.getTerm());
            }
            if (r.getServerReply().getSuccess()) {
                votedPeers.add(RaftPeerId.valueOf(r.getServerReply().getReplyId()));
                if (conf.hasMajority(votedPeers, server.getId())) {
                    return logAndReturn(Result.PASSED, responses, exceptions, -1);
                }
            }
        } catch (ExecutionException e) {
            LOG.info("{} got exception when requesting votes: {}", server.getId(), e);
            LOG.trace("TRACE", e);
            exceptions.add(e);
        }
        waitForNum--;
    }
    // received all the responses
    return logAndReturn(Result.REJECTED, responses, exceptions, -1);
}
Also used : ArrayList(java.util.ArrayList) RequestVoteReplyProto(org.apache.ratis.shaded.proto.RaftProtos.RequestVoteReplyProto) RaftPeerId(org.apache.ratis.protocol.RaftPeerId) Timestamp(org.apache.ratis.util.Timestamp) IOException(java.io.IOException)

Example 8 with Timestamp

use of org.apache.ratis.util.Timestamp in project incubator-ratis by apache.

the class ServerState method setLeader.

void setLeader(RaftPeerId newLeaderId, Object op) {
    if (!Objects.equals(leaderId, newLeaderId)) {
        String suffix;
        if (newLeaderId == null) {
            // reset the time stamp when a null leader is assigned
            lastNoLeaderTime = Timestamp.currentTime();
            suffix = "";
        } else {
            Timestamp previous = lastNoLeaderTime;
            lastNoLeaderTime = null;
            suffix = ", leader elected after " + previous.elapsedTimeMs() + "ms";
            server.getStateMachine().event().notifyLeaderChanged(getMemberId(), newLeaderId);
        }
        LOG.info("{}: change Leader from {} to {} at term {} for {}{}", getMemberId(), leaderId, newLeaderId, getCurrentTerm(), op, suffix);
        leaderId = newLeaderId;
        if (leaderId != null) {
            server.finishTransferLeadership();
        }
    }
}
Also used : Timestamp(org.apache.ratis.util.Timestamp)

Example 9 with Timestamp

use of org.apache.ratis.util.Timestamp in project incubator-ratis by apache.

the class FollowerInfoImpl method updateLastRpcSendTime.

@Override
public void updateLastRpcSendTime(boolean isHeartbeat) {
    final Timestamp currentTime = Timestamp.currentTime();
    lastRpcSendTime.set(currentTime);
    if (isHeartbeat) {
        lastHeartbeatSendTime.set(currentTime);
    }
}
Also used : Timestamp(org.apache.ratis.util.Timestamp)

Example 10 with Timestamp

use of org.apache.ratis.util.Timestamp in project incubator-ratis by apache.

the class DataStreamClusterTests method runTestWriteFile.

void runTestWriteFile(CLUSTER cluster, int i, CheckedConsumer<DataStreamOutputImpl, Exception> testCase) throws Exception {
    final RaftClientRequest request;
    final CompletableFuture<RaftClientReply> reply;
    final RaftPeer primaryServer = CollectionUtils.random(cluster.getGroup().getPeers());
    try (RaftClient client = cluster.createClient(primaryServer)) {
        try (final DataStreamOutputImpl out = (DataStreamOutputImpl) client.getDataStreamApi().stream(null, getRoutingTable(cluster.getGroup().getPeers(), primaryServer))) {
            request = out.getHeader();
            reply = out.getRaftClientReplyFuture();
            final Timestamp start = Timestamp.currentTime();
            testCase.accept(out);
            LOG.info("{}: {} elapsed {}ms", i, testCase, start.elapsedTimeMs());
        }
    }
    watchOrSleep(cluster, reply.join().getLogIndex());
    assertLogEntry(cluster, request);
}
Also used : RaftClientRequest(org.apache.ratis.protocol.RaftClientRequest) RaftClientReply(org.apache.ratis.protocol.RaftClientReply) DataStreamOutputImpl(org.apache.ratis.client.impl.DataStreamClientImpl.DataStreamOutputImpl) RaftPeer(org.apache.ratis.protocol.RaftPeer) Timestamp(org.apache.ratis.util.Timestamp) RaftClient(org.apache.ratis.client.RaftClient)

Aggregations

Timestamp (org.apache.ratis.util.Timestamp)11 RaftPeerId (org.apache.ratis.protocol.RaftPeerId)5 IOException (java.io.IOException)4 RaftClientReply (org.apache.ratis.protocol.RaftClientReply)4 TimeDuration (org.apache.ratis.util.TimeDuration)4 ArrayList (java.util.ArrayList)3 RaftClient (org.apache.ratis.client.RaftClient)3 RaftClientRequest (org.apache.ratis.protocol.RaftClientRequest)3 HashMap (java.util.HashMap)2 List (java.util.List)2 Optional (java.util.Optional)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 TimeUnit (java.util.concurrent.TimeUnit)2 RaftProperties (org.apache.ratis.conf.RaftProperties)2 RaftPeer (org.apache.ratis.protocol.RaftPeer)2 Timer (com.codahale.metrics.Timer)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 Iterator (java.util.Iterator)1