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;
}
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);
}
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();
}
}
}
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);
}
}
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);
}
Aggregations