+
+ return -1;
+ }
+
+ /**
+ * @return the log entry term for the given index or -1 if not found
+ */
+ protected long getLogEntryTerm(long index){
+ if(index == context.getReplicatedLog().getSnapshotIndex()){
+ return context.getReplicatedLog().getSnapshotTerm();
+ }
+
+ ReplicatedLogEntry entry = context.getReplicatedLog().get(index);
+ if(entry != null){
+ return entry.getTerm();
+ }
+
+ return -1;
+ }
+
+ /**
+ * Apply the provided index to the state machine
+ *
+ * @param index a log index that is known to be committed
+ */
+ protected void applyLogToStateMachine(final long index) {
+ long newLastApplied = context.getLastApplied();
+ // Now maybe we apply to the state machine
+ for (long i = context.getLastApplied() + 1; i < index + 1; i++) {
+
+ ReplicatedLogEntry replicatedLogEntry = context.getReplicatedLog().get(i);
+ if (replicatedLogEntry != null) {
+ // Send a local message to the local RaftActor (it's derived class to be
+ // specific to apply the log to it's index)
+
+ final ApplyState msg;
+ final ClientRequestTracker tracker = removeClientRequestTracker(i);
+ if (tracker != null) {
+ msg = new ApplyState(tracker.getClientActor(), tracker.getIdentifier(), replicatedLogEntry);
+ } else {
+ msg = new ApplyState(null, null, replicatedLogEntry);
+ }
+
+ actor().tell(msg, actor());
+ newLastApplied = i;
+ } else {
+ //if one index is not present in the log, no point in looping
+ // around as the rest wont be present either
+ LOG.warn("{}: Missing index {} from log. Cannot apply state. Ignoring {} to {}",
+ logName(), i, i, index);
+ break;