if (message instanceof ApplyState){
ApplyState applyState = (ApplyState) message;
- LOG.debug("Applying state for log index {}",
- applyState.getReplicatedLogEntry().getIndex());
+ LOG.debug("Applying state for log index {} data {}",
+ applyState.getReplicatedLogEntry().getIndex(),
+ applyState.getReplicatedLogEntry().getData());
applyState(applyState.getClientActor(), applyState.getIdentifier(),
applyState.getReplicatedLogEntry().getData());
*/
protected void persistData(ActorRef clientActor, String identifier,
Object data) {
- LOG.debug("Persist data " + identifier);
+
ReplicatedLogEntry replicatedLogEntry = new ReplicatedLogImplEntry(
context.getReplicatedLog().lastIndex() + 1,
context.getTermInformation().getCurrentTerm(), data);
+ LOG.debug("Persist data {}", replicatedLogEntry);
+
replicatedLog
.appendAndPersist(clientActor, identifier, replicatedLogEntry);
}
final String identifier,
final ReplicatedLogEntry replicatedLogEntry) {
context.getLogger().debug(
- "Append log entry and persist " + replicatedLogEntry.getIndex());
+ "Append log entry and persist " + replicatedLogEntry);
// FIXME : By adding the replicated log entry to the in-memory journal we are not truly ensuring durability of the logs
journal.add(replicatedLogEntry);
}
@Override public long size() {
- return journal.size() + snapshotIndex;
+ return journal.size() + snapshotIndex + 1;
}
@Override public boolean isPresent(long index) {
@Override public long getIndex() {
return index;
}
+
+ @Override public String toString() {
+ return "Entry{" +
+ "index=" + index +
+ ", term=" + term +
+ '}';
+ }
}
/**
* The behavior of a RaftActor in the Follower state
- *
+ * <p/>
* <ul>
* <li> Respond to RPCs from candidates and leaders
* <li> If election timeout elapses without receiving AppendEntries
* RPC from current leader or granting vote to candidate:
* convert to candidate
* </ul>
- *
*/
public class Follower extends AbstractRaftActorBehavior {
public Follower(RaftActorContext context) {
.get(appendEntries.getPrevLogIndex());
- if (lastIndex() > -1 && previousEntry != null
- && previousEntry.getTerm() != appendEntries
- .getPrevLogTerm()) {
+ boolean noMatchingTerms = true;
+
+ if (lastIndex() == -1
+ && appendEntries.getPrevLogIndex() != -1) {
+
+ context.getLogger().debug(
+ "The followers log is empty and the senders prevLogIndex is {}",
+ appendEntries.getPrevLogIndex());
+
+ } else if (lastIndex() > -1
+ && appendEntries.getPrevLogIndex() != -1
+ && previousEntry == null) {
+
+ context.getLogger().debug(
+ "The log is not empty but the prevLogIndex {} was not found in it",
+ appendEntries.getPrevLogIndex());
+
+ } else if (lastIndex() > -1
+ && previousEntry != null
+ && previousEntry.getTerm()!= appendEntries.getPrevLogTerm()) {
context.getLogger().debug(
- "Cannot append entries because previous entry term "
- + previousEntry.getTerm()
- + " is not equal to append entries prevLogTerm "
- + appendEntries.getPrevLogTerm());
+ "Cannot append entries because previous entry term {} is not equal to append entries prevLogTerm {}"
+ , previousEntry.getTerm()
+ , appendEntries.getPrevLogTerm());
+ } else {
+ noMatchingTerms = false;
+ }
+ if (noMatchingTerms) {
sender.tell(
new AppendEntriesReply(context.getId(), currentTerm(), false,
lastIndex(), lastTerm()), actor()
&& appendEntries.getEntries().size() > 0) {
context.getLogger().debug(
"Number of entries to be appended = " + appendEntries
- .getEntries().size());
+ .getEntries().size()
+ );
// 3. If an existing entry conflicts with a new one (same index
// but different terms), delete the existing entry and all that
.getTerm()) {
context.getLogger().debug(
"Removing entries from log starting at "
- + matchEntry.getIndex());
+ + matchEntry.getIndex()
+ );
context.getReplicatedLog()
.removeFrom(matchEntry.getIndex());
break;
context.getLogger().debug(
"After cleanup entries to be added from = " + (addEntriesFrom
- + lastIndex()));
+ + lastIndex())
+ );
// 4. Append any new entries not already in the log
for (int i = addEntriesFrom;
i < appendEntries.getEntries().size(); i++) {
context.getLogger().debug(
- "Append entry to log " + appendEntries.getEntries().get(i)
- .toString());
+ "Append entry to log " + appendEntries.getEntries().get(i).getData()
+ .toString()
+ );
context.getReplicatedLog()
.appendAndPersist(appendEntries.getEntries().get(i));
}
}
@Override public RaftState handleMessage(ActorRef sender, Object message) {
- if(message instanceof ElectionTimeout){
+ if (message instanceof ElectionTimeout) {
return RaftState.Candidate;
- } else if(message instanceof InstallSnapshot){
+ } else if (message instanceof InstallSnapshot) {
InstallSnapshot snapshot = (InstallSnapshot) message;
actor().tell(new ApplySnapshot(snapshot), actor());
}