/*
* Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
+ * Copyright (c) 2015 Brocade Communications Systems, Inc. and others. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v1.0 which accompanies this distribution,
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Objects;
import com.google.common.base.Optional;
-import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import java.io.Serializable;
import java.util.Collection;
+import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
private final BehaviorStateHolder reusableBehaviorStateHolder = new BehaviorStateHolder();
- public RaftActor(String id, Map<String, String> peerAddresses) {
- this(id, peerAddresses, Optional.<ConfigParams>absent());
- }
-
public RaftActor(String id, Map<String, String> peerAddresses,
- Optional<ConfigParams> configParams) {
+ Optional<ConfigParams> configParams, short payloadVersion) {
context = new RaftActorContextImpl(this.getSelf(),
this.getContext(), id, new ElectionTermImpl(delegatingPersistenceProvider, id, LOG),
-1, -1, peerAddresses,
- (configParams.isPresent() ? configParams.get(): new DefaultConfigParamsImpl()), LOG);
+ (configParams.isPresent() ? configParams.get(): new DefaultConfigParamsImpl()),
+ delegatingPersistenceProvider, LOG);
- context.setReplicatedLog(ReplicatedLogImpl.newInstance(context, delegatingPersistenceProvider, currentBehavior));
+ context.setPayloadVersion(payloadVersion);
+ context.setReplicatedLog(ReplicatedLogImpl.newInstance(context, currentBehavior));
}
@Override
context.getConfigParams().getJournalRecoveryLogBatchSize());
super.preStart();
+
+ snapshotSupport = newRaftActorSnapshotMessageSupport();
}
@Override
@Override
public void handleRecover(Object message) {
if(raftRecovery == null) {
- raftRecovery = new RaftActorRecoverySupport(delegatingPersistenceProvider, context, currentBehavior,
- getRaftActorRecoveryCohort());
+ raftRecovery = newRaftActorRecoverySupport();
}
boolean recoveryComplete = raftRecovery.handleRecoveryMessage(message);
}
}
+ protected RaftActorRecoverySupport newRaftActorRecoverySupport() {
+ return new RaftActorRecoverySupport(context, currentBehavior, getRaftActorRecoveryCohort());
+ }
+
protected void initializeBehavior(){
changeCurrentBehavior(new Follower(context));
}
@Override
public void handleCommand(Object message) {
- if(snapshotSupport == null) {
- snapshotSupport = new RaftActorSnapshotMessageSupport(delegatingPersistenceProvider, context,
- currentBehavior, getRaftActorSnapshotCohort(), self());
- }
-
- boolean handled = snapshotSupport.handleSnapshotMessage(message);
- if(handled) {
- return;
- }
-
if (message instanceof ApplyState){
ApplyState applyState = (ApplyState) message;
applyState(applyState.getClientActor(), applyState.getIdentifier(),
applyState.getReplicatedLogEntry().getData());
+ if (!hasFollowers()) {
+ // for single node, the capture should happen after the apply state
+ // as we delete messages from the persistent journal which have made it to the snapshot
+ // capturing the snapshot before applying makes the persistent journal and snapshot out of sync
+ // and recovery shows data missing
+ context.getReplicatedLog().captureSnapshotIfReady(applyState.getReplicatedLogEntry());
+
+ context.getSnapshotManager().trimLog(context.getLastApplied(), currentBehavior);
+ }
+
} else if (message instanceof ApplyJournalEntries){
ApplyJournalEntries applyEntries = (ApplyJournalEntries) message;
if(LOG.isDebugEnabled()) {
);
} else if(message instanceof GetOnDemandRaftState) {
onGetOnDemandRaftStats();
- } else {
+ } else if(!snapshotSupport.handleSnapshotMessage(message)) {
reusableBehaviorStateHolder.init(getCurrentBehavior());
setCurrentBehavior(currentBehavior.handleMessage(getSender(), message));
}
}
+ protected RaftActorSnapshotMessageSupport newRaftActorSnapshotMessageSupport() {
+ return new RaftActorSnapshotMessageSupport(context, currentBehavior,
+ getRaftActorSnapshotCohort());
+ }
+
private void onGetOnDemandRaftStats() {
// Debugging message to retrieve raft stats.
.snapshotIndex(replicatedLog().getSnapshotIndex())
.snapshotTerm(replicatedLog().getSnapshotTerm())
.votedFor(context.getTermInformation().getVotedFor())
- .peerAddresses(ImmutableMap.copyOf(context.getPeerAddresses()));
+ .peerAddresses(new HashMap<>(context.getPeerAddresses()));
ReplicatedLogEntry lastLogEntry = getLastLogEntry();
if (lastLogEntry != null) {
// 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(oldBehaviorLeaderId, currentBehavior.getLeaderId()) ||
+ oldBehaviorState.getLeaderPayloadVersion() != currentBehavior.getLeaderPayloadVersion()) {
if(roleChangeNotifier.isPresent()) {
- roleChangeNotifier.get().tell(new LeaderStateChanged(getId(), currentBehavior.getLeaderId()), getSelf());
+ roleChangeNotifier.get().tell(newLeaderStateChanged(getId(), currentBehavior.getLeaderId(),
+ currentBehavior.getLeaderPayloadVersion()), getSelf());
}
onLeaderChanged(oldBehaviorLeaderId, currentBehavior.getLeaderId());
}
}
+ protected LeaderStateChanged newLeaderStateChanged(String memberId, String leaderId, short leaderPayloadVersion) {
+ return new LeaderStateChanged(memberId, leaderId, leaderPayloadVersion);
+ }
+
+ @Override
+ public long snapshotSequenceNr() {
+ // When we do a snapshot capture, we also capture and save the sequence-number of the persistent journal,
+ // so that we can delete the persistent journal based on the saved sequence-number
+ // However , when akka replays the journal during recovery, it replays it from the sequence number when the snapshot
+ // was saved and not the number we saved.
+ // We would want to override it , by asking akka to use the last-sequence number known to us.
+ return context.getSnapshotManager().getLastSequenceNumber();
+ }
+
/**
* When a derived RaftActor needs to persist something it must call
* persistData.
replicatedLog().appendAndPersist(replicatedLogEntry, new Procedure<ReplicatedLogEntry>() {
@Override
public void apply(ReplicatedLogEntry replicatedLogEntry) throws Exception {
- if(!hasFollowers()){
+ if (!hasFollowers()){
// Increment the Commit Index and the Last Applied values
raftContext.setCommitIndex(replicatedLogEntry.getIndex());
raftContext.setLastApplied(replicatedLogEntry.getIndex());
- // Apply the state immediately
- applyState(clientActor, identifier, data);
+ // Apply the state immediately.
+ self().tell(new ApplyState(clientActor, identifier, replicatedLogEntry), self());
// Send a ApplyJournalEntries message so that we write the fact that we applied
// the state to durable storage
self().tell(new ApplyJournalEntries(replicatedLogEntry.getIndex()), self());
- context.getSnapshotManager().trimLog(context.getLastApplied(), currentBehavior);
-
} else if (clientActor != null) {
+ context.getReplicatedLog().captureSnapshotIfReady(replicatedLogEntry);
+
// Send message for replication
currentBehavior.handleMessage(getSelf(),
new Replicate(clientActor, identifier, replicatedLogEntry));
return getRaftActorContext().hasFollowers();
}
+ /**
+ * @deprecated Deprecated in favor of {@link org.opendaylight.controller.cluster.raft.base.messages.DeleteEntries}
+ * whose type for fromIndex is long instead of int. This class was kept for backwards
+ * compatibility with Helium.
+ */
+ // Suppressing this warning as we can't set serialVersionUID to maintain backwards compatibility.
+ @SuppressWarnings("serial")
+ @Deprecated
static class DeleteEntries implements Serializable {
- private static final long serialVersionUID = 1L;
private final int fromIndex;
public DeleteEntries(int fromIndex) {
}
}
+ /**
+ * @deprecated Deprecated in favor of non-inner class {@link org.opendaylight.controller.cluster.raft.base.messages.UpdateElectionTerm}
+ * which has serialVersionUID set. This class was kept for backwards compatibility with Helium.
+ */
+ // Suppressing this warning as we can't set serialVersionUID to maintain backwards compatibility.
+ @SuppressWarnings("serial")
+ @Deprecated
static class UpdateElectionTerm implements Serializable {
- private static final long serialVersionUID = 1L;
private final long currentTerm;
private final String votedFor;
private static class BehaviorStateHolder {
private RaftActorBehavior behavior;
private String leaderId;
+ private short leaderPayloadVersion;
void init(RaftActorBehavior behavior) {
this.behavior = behavior;
this.leaderId = behavior != null ? behavior.getLeaderId() : null;
+ this.leaderPayloadVersion = behavior != null ? behavior.getLeaderPayloadVersion() : -1;
}
RaftActorBehavior getBehavior() {
String getLeaderId() {
return leaderId;
}
+
+ short getLeaderPayloadVersion() {
+ return leaderPayloadVersion;
+ }
}
}