Search in sources :

Example 36 with ReplicatedLogEntry

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);
    }
}
Also used : SimpleReplicatedLogEntry(org.opendaylight.controller.cluster.raft.persisted.SimpleReplicatedLogEntry) ReplicatedLogEntry(org.opendaylight.controller.cluster.raft.ReplicatedLogEntry)

Example 37 with ReplicatedLogEntry

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());
    }
}
Also used : ReplicatedLogEntry(org.opendaylight.controller.cluster.raft.ReplicatedLogEntry) AppendEntriesReply(org.opendaylight.controller.cluster.raft.messages.AppendEntriesReply)

Example 38 with ReplicatedLogEntry

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();
    }
}
Also used : ReplicatedLogEntry(org.opendaylight.controller.cluster.raft.ReplicatedLogEntry) FollowerLogInformation(org.opendaylight.controller.cluster.raft.FollowerLogInformation) PeerInfo(org.opendaylight.controller.cluster.raft.PeerInfo)

Aggregations

ReplicatedLogEntry (org.opendaylight.controller.cluster.raft.ReplicatedLogEntry)38 SimpleReplicatedLogEntry (org.opendaylight.controller.cluster.raft.persisted.SimpleReplicatedLogEntry)31 Test (org.junit.Test)30 AppendEntries (org.opendaylight.controller.cluster.raft.messages.AppendEntries)30 MockRaftActorContext (org.opendaylight.controller.cluster.raft.MockRaftActorContext)24 AppendEntriesReply (org.opendaylight.controller.cluster.raft.messages.AppendEntriesReply)15 FollowerInitialSyncUpStatus (org.opendaylight.controller.cluster.raft.base.messages.FollowerInitialSyncUpStatus)9 ArrayList (java.util.ArrayList)8 DefaultConfigParamsImpl (org.opendaylight.controller.cluster.raft.DefaultConfigParamsImpl)7 ByteString (com.google.protobuf.ByteString)5 FollowerLogInformation (org.opendaylight.controller.cluster.raft.FollowerLogInformation)5 ApplyState (org.opendaylight.controller.cluster.raft.base.messages.ApplyState)5 InstallSnapshot (org.opendaylight.controller.cluster.raft.messages.InstallSnapshot)5 ApplySnapshot (org.opendaylight.controller.cluster.raft.base.messages.ApplySnapshot)4 Snapshot (org.opendaylight.controller.cluster.raft.persisted.Snapshot)4 FiniteDuration (scala.concurrent.duration.FiniteDuration)4 AtomicReference (java.util.concurrent.atomic.AtomicReference)3 MockRaftActor (org.opendaylight.controller.cluster.raft.MockRaftActor)3 Builder (org.opendaylight.controller.cluster.raft.MockRaftActor.Builder)3 ActorSelection (akka.actor.ActorSelection)2