*/
package org.opendaylight.controller.cluster.raft;
-import com.google.protobuf.ByteString;
-
import java.util.ArrayList;
import java.util.List;
*/
public abstract class AbstractReplicatedLogImpl implements ReplicatedLog {
- protected List<ReplicatedLogEntry> journal;
- protected ByteString snapshot;
+ // We define this as ArrayList so we can use ensureCapacity.
+ protected ArrayList<ReplicatedLogEntry> journal;
+
protected long snapshotIndex = -1;
protected long snapshotTerm = -1;
// to be used for rollback during save snapshot failure
- protected List<ReplicatedLogEntry> snapshottedJournal;
- protected ByteString previousSnapshot;
+ protected ArrayList<ReplicatedLogEntry> snapshottedJournal;
protected long previousSnapshotIndex = -1;
protected long previousSnapshotTerm = -1;
+ protected int dataSize = 0;
- public AbstractReplicatedLogImpl(ByteString state, long snapshotIndex,
+ public AbstractReplicatedLogImpl(long snapshotIndex,
long snapshotTerm, List<ReplicatedLogEntry> unAppliedEntries) {
- this.snapshot = state;
this.snapshotIndex = snapshotIndex;
this.snapshotTerm = snapshotTerm;
this.journal = new ArrayList<>(unAppliedEntries);
}
-
public AbstractReplicatedLogImpl() {
- this.snapshot = null;
this.journal = new ArrayList<>();
}
journal.add(replicatedLogEntry);
}
+ @Override
+ public void increaseJournalLogCapacity(int amount) {
+ journal.ensureCapacity(journal.size() + amount);
+ }
+
@Override
public List<ReplicatedLogEntry> getFrom(long logEntryIndex) {
return getFrom(logEntryIndex, journal.size());
return logEntryIndex <= snapshotIndex && snapshotIndex != -1;
}
- @Override
- public ByteString getSnapshot() {
- return snapshot;
- }
-
@Override
public long getSnapshotIndex() {
return snapshotIndex;
this.snapshotTerm = snapshotTerm;
}
- @Override
- public void setSnapshot(ByteString snapshot) {
- this.snapshot = snapshot;
- }
-
@Override
public void clear(int startIndex, int endIndex) {
journal.subList(startIndex, endIndex).clear();
}
@Override
- public void snapshotPreCommit(ByteString snapshot, long snapshotCapturedIndex, long snapshotCapturedTerm) {
+ public void snapshotPreCommit(long snapshotCapturedIndex, long snapshotCapturedTerm) {
snapshottedJournal = new ArrayList<>(journal.size());
snapshottedJournal.addAll(journal.subList(0, (int)(snapshotCapturedIndex - snapshotIndex)));
previousSnapshotTerm = snapshotTerm;
setSnapshotTerm(snapshotCapturedTerm);
-
- previousSnapshot = getSnapshot();
- setSnapshot(snapshot);
}
@Override
public void snapshotCommit() {
- snapshottedJournal.clear();
snapshottedJournal = null;
previousSnapshotIndex = -1;
previousSnapshotTerm = -1;
- previousSnapshot = null;
+ dataSize = 0;
}
@Override
public void snapshotRollback() {
snapshottedJournal.addAll(journal);
- journal.clear();
journal = snapshottedJournal;
snapshottedJournal = null;
snapshotTerm = previousSnapshotTerm;
previousSnapshotTerm = -1;
-
- snapshot = previousSnapshot;
- previousSnapshot = null;
-
}
}