use of org.opendaylight.controller.cluster.raft.ReplicatedLogEntry in project controller by opendaylight.
the class AppendEntriesTest method verifyAppendEntries.
private static void verifyAppendEntries(AppendEntries expected, AppendEntries actual) {
assertEquals("getLeaderId", expected.getLeaderId(), actual.getLeaderId());
assertEquals("getTerm", expected.getTerm(), actual.getTerm());
assertEquals("getLeaderCommit", expected.getLeaderCommit(), actual.getLeaderCommit());
assertEquals("getPrevLogIndex", expected.getPrevLogIndex(), actual.getPrevLogIndex());
assertEquals("getPrevLogTerm", expected.getPrevLogTerm(), actual.getPrevLogTerm());
assertEquals("getReplicatedToAllIndex", expected.getReplicatedToAllIndex(), actual.getReplicatedToAllIndex());
assertEquals("getPayloadVersion", expected.getPayloadVersion(), actual.getPayloadVersion());
assertEquals("getEntries size", expected.getEntries().size(), actual.getEntries().size());
Iterator<ReplicatedLogEntry> iter = expected.getEntries().iterator();
for (ReplicatedLogEntry e : actual.getEntries()) {
verifyReplicatedLogEntry(iter.next(), e);
}
}
use of org.opendaylight.controller.cluster.raft.ReplicatedLogEntry in project controller by opendaylight.
the class DummyShard method handleAppendEntries.
protected void handleAppendEntries(AppendEntries req) throws InterruptedException {
LOG.info("{} - Received AppendEntries message : leader term = {}, index = {}, prevLogIndex = {}, size = {}", followerId, req.getTerm(), req.getLeaderCommit(), req.getPrevLogIndex(), req.getEntries().size());
if (appendEntriesWatch != null) {
long elapsed = appendEntriesWatch.elapsed(TimeUnit.SECONDS);
if (elapsed >= 5) {
LOG.error("More than 5 seconds since last append entry, elapsed Time = {} seconds" + ", leaderCommit = {}, prevLogIndex = {}, size = {}", elapsed, req.getLeaderCommit(), req.getPrevLogIndex(), req.getEntries().size());
}
appendEntriesWatch.reset().start();
} else {
appendEntriesWatch = Stopwatch.createStarted();
}
if (lastMessageIndex == req.getLeaderCommit() && req.getEntries().size() > 0 && lastMessageSize > 0) {
LOG.error("{} - Duplicate message with leaderCommit = {} prevLogIndex = {} received", followerId, req.getLeaderCommit(), req.getPrevLogIndex());
}
lastMessageIndex = req.getLeaderCommit();
lastMessageSize = req.getEntries().size();
long lastIndex = req.getLeaderCommit();
if (req.getEntries().size() > 0) {
for (ReplicatedLogEntry entry : req.getEntries()) {
lastIndex = entry.getIndex();
}
}
if (configuration.shouldCauseTrouble() && req.getEntries().size() > 0) {
boolean ignore = false;
if (configuration.shouldDropReplies()) {
ignore = Math.random() > 0.5;
}
long delay = (long) (Math.random() * configuration.getMaxDelayInMillis());
if (!ignore) {
LOG.info("{} - Randomizing delay : {}", followerId, delay);
Thread.sleep(delay);
sender().tell(new AppendEntriesReply(followerId, req.getTerm(), true, lastIndex, req.getTerm(), DataStoreVersions.CURRENT_VERSION), self());
}
} else {
sender().tell(new AppendEntriesReply(followerId, req.getTerm(), true, lastIndex, req.getTerm(), DataStoreVersions.CURRENT_VERSION), self());
}
}
use of org.opendaylight.controller.cluster.raft.ReplicatedLogEntry in project controller by opendaylight.
the class AbstractLeader method possiblyUpdateCommitIndex.
private void possiblyUpdateCommitIndex() {
// set commitIndex = N (§5.3, §5.4).
for (long index = context.getCommitIndex() + 1; ; index++) {
ReplicatedLogEntry replicatedLogEntry = context.getReplicatedLog().get(index);
if (replicatedLogEntry == null) {
log.trace("{}: ReplicatedLogEntry not found for index {} - snapshotIndex: {}, journal size: {}", logName(), index, context.getReplicatedLog().getSnapshotIndex(), context.getReplicatedLog().size());
break;
}
// Count our entry if it has been persisted.
int replicatedCount = replicatedLogEntry.isPersistencePending() ? 0 : 1;
if (replicatedCount == 0) {
// amongst the followers w/o the local persistence ack.
break;
}
log.trace("{}: checking Nth index {}", logName(), index);
for (FollowerLogInformation info : followerToLog.values()) {
final PeerInfo peerInfo = context.getPeerInfo(info.getId());
if (info.getMatchIndex() >= index && peerInfo != null && peerInfo.isVoting()) {
replicatedCount++;
} else if (log.isTraceEnabled()) {
log.trace("{}: Not counting follower {} - matchIndex: {}, {}", logName(), info.getId(), info.getMatchIndex(), peerInfo);
}
}
if (log.isTraceEnabled()) {
log.trace("{}: replicatedCount {}, minReplicationCount: {}", logName(), replicatedCount, minReplicationCount);
}
if (replicatedCount >= minReplicationCount) {
// counting replicas, then all prior entries are committed indirectly".
if (replicatedLogEntry.getTerm() == currentTerm()) {
log.trace("{}: Setting commit index to {}", logName(), index);
context.setCommitIndex(index);
} else {
log.debug("{}: Not updating commit index to {} - retrieved log entry with index {}, " + "term {} does not match the current term {}", logName(), index, replicatedLogEntry.getIndex(), replicatedLogEntry.getTerm(), currentTerm());
}
} else {
log.trace("{}: minReplicationCount not reached, actual {} - breaking", logName(), replicatedCount);
break;
}
}
// Apply the change to the state machine
if (context.getCommitIndex() > context.getLastApplied()) {
log.debug("{}: Applying to log - commitIndex: {}, lastAppliedIndex: {}", logName(), context.getCommitIndex(), context.getLastApplied());
applyLogToStateMachine(context.getCommitIndex());
}
if (!context.getSnapshotManager().isCapturing()) {
purgeInMemoryLog();
}
}
Aggregations