import akka.actor.ActorRef;
import akka.actor.ActorSelection;
import akka.japi.Procedure;
-import akka.persistence.SnapshotSelectionCriteria;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Objects;
import com.google.common.base.Optional;
*/
private final RaftActorContextImpl context;
- private final DelegatingPersistentDataProvider delegatingPersistenceProvider = new DelegatingPersistentDataProvider(null);
+ private final DelegatingPersistentDataProvider delegatingPersistenceProvider;
+
+ private final PersistentDataProvider persistentProvider;
private RaftActorRecoverySupport raftRecovery;
private final SwitchBehaviorSupplier reusableSwitchBehaviorSupplier = new SwitchBehaviorSupplier();
+ private RaftActorServerConfigurationSupport serverConfigurationSupport;
+
public RaftActor(String id, Map<String, String> peerAddresses,
Optional<ConfigParams> configParams, short payloadVersion) {
+ persistentProvider = new PersistentDataProvider(this);
+ delegatingPersistenceProvider = new RaftActorDelegatingPersistentDataProvider(null, persistentProvider);
+
context = new RaftActorContextImpl(this.getSelf(),
- this.getContext(), id, new ElectionTermImpl(delegatingPersistenceProvider, id, LOG),
+ this.getContext(), id, new ElectionTermImpl(persistentProvider, id, LOG),
-1, -1, peerAddresses,
(configParams.isPresent() ? configParams.get(): new DefaultConfigParamsImpl()),
delegatingPersistenceProvider, LOG);
super.preStart();
snapshotSupport = newRaftActorSnapshotMessageSupport();
+ serverConfigurationSupport = new RaftActorServerConfigurationSupport(getRaftActorContext());
}
@Override
raftRecovery = newRaftActorRecoverySupport();
}
- boolean recoveryComplete = raftRecovery.handleRecoveryMessage(message);
+ boolean recoveryComplete = raftRecovery.handleRecoveryMessage(message, persistentProvider);
if(recoveryComplete) {
- if(!persistence().isRecoveryApplicable()) {
- // Delete all the messages from the akka journal so that we do not end up with consistency issues
- // Note I am not using the dataPersistenceProvider and directly using the akka api here
- deleteMessages(lastSequenceNr());
-
- // Delete all the akka snapshots as they will not be needed
- deleteSnapshots(new SnapshotSelectionCriteria(scala.Long.MaxValue(), scala.Long.MaxValue()));
- }
-
onRecoveryComplete();
initializeBehavior();
raftRecovery = null;
+
+ if (context.getReplicatedLog().size() > 0) {
+ self().tell(new InitiateCaptureSnapshot(), self());
+ LOG.info("Snapshot capture initiated after recovery");
+ } else {
+ LOG.info("Snapshot capture NOT initiated after recovery, journal empty");
+ }
}
}
@Override
public void handleCommand(final Object message) {
- if (message instanceof ApplyState){
+ if(serverConfigurationSupport.handleMessage(message, this, getSender())) {
+ return;
+ } else if (message instanceof ApplyState){
ApplyState applyState = (ApplyState) message;
long elapsedTime = (System.nanoTime() - applyState.getStartTime());
private void onGetOnDemandRaftStats() {
// Debugging message to retrieve raft stats.
+ Map<String, String> peerAddresses = new HashMap<>();
+ for(String peerId: context.getPeerIds()) {
+ peerAddresses.put(peerId, context.getPeerAddress(peerId));
+ }
+
OnDemandRaftState.Builder builder = OnDemandRaftState.builder()
.commitIndex(context.getCommitIndex())
.currentTerm(context.getTermInformation().getCurrentTerm())
.snapshotIndex(replicatedLog().getSnapshotIndex())
.snapshotTerm(replicatedLog().getSnapshotTerm())
.votedFor(context.getTermInformation().getVotedFor())
- .peerAddresses(new HashMap<>(context.getPeerAddresses()));
+ .peerAddresses(peerAddresses);
ReplicatedLogEntry lastLogEntry = getLastLogEntry();
if (lastLogEntry != null) {
onStateChanged();
}
- String oldBehaviorLeaderId = oldBehavior == null ? null : oldBehaviorState.getLeaderId();
+ String lastValidLeaderId = oldBehavior == null ? null : oldBehaviorState.getLastValidLeaderId();
String oldBehaviorStateName = oldBehavior == null ? null : oldBehavior.state().name();
// it can happen that the state has not changed but the leader has changed.
Optional<ActorRef> roleChangeNotifier = getRoleChangeNotifier();
- if(!Objects.equal(oldBehaviorLeaderId, currentBehavior.getLeaderId()) ||
+ if(!Objects.equal(lastValidLeaderId, currentBehavior.getLeaderId()) ||
oldBehaviorState.getLeaderPayloadVersion() != currentBehavior.getLeaderPayloadVersion()) {
if(roleChangeNotifier.isPresent()) {
roleChangeNotifier.get().tell(newLeaderStateChanged(getId(), currentBehavior.getLeaderId(),
currentBehavior.getLeaderPayloadVersion()), getSelf());
}
- onLeaderChanged(oldBehaviorLeaderId, currentBehavior.getLeaderId());
+ onLeaderChanged(lastValidLeaderId, currentBehavior.getLeaderId());
}
if (roleChangeNotifier.isPresent() &&
replicatedLog().appendAndPersist(replicatedLogEntry, new Procedure<ReplicatedLogEntry>() {
@Override
- public void apply(ReplicatedLogEntry replicatedLogEntry) throws Exception {
+ public void apply(ReplicatedLogEntry replicatedLogEntry) {
if (!hasFollowers()){
// Increment the Commit Index and the Last Applied values
raftContext.setCommitIndex(replicatedLogEntry.getIndex());
}
protected void updateConfigParams(ConfigParams configParams) {
+
+ // obtain the RaftPolicy for oldConfigParams and the updated one.
+ String oldRaftPolicy = context.getConfigParams().
+ getCustomRaftPolicyImplementationClass();
+ String newRaftPolicy = configParams.
+ getCustomRaftPolicyImplementationClass();
+
+ LOG.debug ("RaftPolicy used with prev.config {}, RaftPolicy used with newConfig {}",
+ oldRaftPolicy, newRaftPolicy);
context.setConfigParams(configParams);
+ if (!Objects.equal(oldRaftPolicy, newRaftPolicy)) {
+ //RaftPolicy is modifed for the Actor. Re-initialize its current behaviour
+ initializeBehavior();
+ }
}
public final DataPersistenceProvider persistence() {
private static class BehaviorStateHolder {
private RaftActorBehavior behavior;
- private String leaderId;
+ private String lastValidLeaderId;
private short leaderPayloadVersion;
void init(RaftActorBehavior behavior) {
this.behavior = behavior;
- this.leaderId = behavior != null ? behavior.getLeaderId() : null;
this.leaderPayloadVersion = behavior != null ? behavior.getLeaderPayloadVersion() : -1;
+
+ String behaviorLeaderId = behavior != null ? behavior.getLeaderId() : null;
+ if(behaviorLeaderId != null) {
+ this.lastValidLeaderId = behaviorLeaderId;
+ }
}
RaftActorBehavior getBehavior() {
return behavior;
}
- String getLeaderId() {
- return leaderId;
+ String getLastValidLeaderId() {
+ return lastValidLeaderId;
}
short getLeaderPayloadVersion() {