2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.controller.cluster.raft;
10 import java.util.ArrayList;
11 import java.util.Collections;
12 import java.util.List;
15 * Abstract class handling the mapping of
16 * logical LogEntry Index and the physical list index.
18 public abstract class AbstractReplicatedLogImpl implements ReplicatedLog {
20 // We define this as ArrayList so we can use ensureCapacity.
21 protected ArrayList<ReplicatedLogEntry> journal;
23 private long snapshotIndex = -1;
24 private long snapshotTerm = -1;
26 // to be used for rollback during save snapshot failure
27 private ArrayList<ReplicatedLogEntry> snapshottedJournal;
28 private long previousSnapshotIndex = -1;
29 private long previousSnapshotTerm = -1;
30 protected int dataSize = 0;
32 public AbstractReplicatedLogImpl(long snapshotIndex,
33 long snapshotTerm, List<ReplicatedLogEntry> unAppliedEntries) {
34 this.snapshotIndex = snapshotIndex;
35 this.snapshotTerm = snapshotTerm;
36 this.journal = new ArrayList<>(unAppliedEntries);
39 public AbstractReplicatedLogImpl() {
40 this(-1L, -1L, Collections.<ReplicatedLogEntry>emptyList());
43 protected int adjustedIndex(long logEntryIndex) {
44 if(snapshotIndex < 0){
45 return (int) logEntryIndex;
47 return (int) (logEntryIndex - (snapshotIndex + 1));
51 public ReplicatedLogEntry get(long logEntryIndex) {
52 int adjustedIndex = adjustedIndex(logEntryIndex);
54 if (adjustedIndex < 0 || adjustedIndex >= journal.size()) {
55 // physical index should be less than list size and >= 0
59 return journal.get(adjustedIndex);
63 public ReplicatedLogEntry last() {
64 if (journal.isEmpty()) {
67 // get the last entry directly from the physical index
68 return journal.get(journal.size() - 1);
72 public long lastIndex() {
73 if (journal.isEmpty()) {
74 // it can happen that after snapshot, all the entries of the
75 // journal are trimmed till lastApplied, so lastIndex = snapshotIndex
78 return last().getIndex();
82 public long lastTerm() {
83 if (journal.isEmpty()) {
84 // it can happen that after snapshot, all the entries of the
85 // journal are trimmed till lastApplied, so lastTerm = snapshotTerm
88 return last().getTerm();
92 public void removeFrom(long logEntryIndex) {
93 int adjustedIndex = adjustedIndex(logEntryIndex);
94 if (adjustedIndex < 0 || adjustedIndex >= journal.size()) {
95 // physical index should be less than list size and >= 0
98 journal.subList(adjustedIndex , journal.size()).clear();
102 public void append(ReplicatedLogEntry replicatedLogEntry) {
103 journal.add(replicatedLogEntry);
107 public void increaseJournalLogCapacity(int amount) {
108 journal.ensureCapacity(journal.size() + amount);
112 public List<ReplicatedLogEntry> getFrom(long logEntryIndex) {
113 return getFrom(logEntryIndex, journal.size());
117 public List<ReplicatedLogEntry> getFrom(long logEntryIndex, int max) {
118 int adjustedIndex = adjustedIndex(logEntryIndex);
119 int size = journal.size();
120 List<ReplicatedLogEntry> entries = new ArrayList<>(100);
121 if (adjustedIndex >= 0 && adjustedIndex < size) {
122 // physical index should be less than list size and >= 0
123 int maxIndex = adjustedIndex + max;
127 entries.addAll(journal.subList(adjustedIndex, maxIndex));
135 return journal.size();
139 public boolean isPresent(long logEntryIndex) {
140 if (logEntryIndex > lastIndex()) {
141 // if the request logical index is less than the last present in the list
144 int adjustedIndex = adjustedIndex(logEntryIndex);
145 return (adjustedIndex >= 0);
149 public boolean isInSnapshot(long logEntryIndex) {
150 return logEntryIndex <= snapshotIndex && snapshotIndex != -1;
154 public long getSnapshotIndex() {
155 return snapshotIndex;
159 public long getSnapshotTerm() {
164 public abstract void appendAndPersist(ReplicatedLogEntry replicatedLogEntry);
167 public abstract void removeFromAndPersist(long index);
170 public void setSnapshotIndex(long snapshotIndex) {
171 this.snapshotIndex = snapshotIndex;
175 public void setSnapshotTerm(long snapshotTerm) {
176 this.snapshotTerm = snapshotTerm;
180 public void clear(int startIndex, int endIndex) {
181 journal.subList(startIndex, endIndex).clear();
185 public void snapshotPreCommit(long snapshotCapturedIndex, long snapshotCapturedTerm) {
186 snapshottedJournal = new ArrayList<>(journal.size());
188 snapshottedJournal.addAll(journal.subList(0, (int)(snapshotCapturedIndex - snapshotIndex)));
189 clear(0, (int) (snapshotCapturedIndex - snapshotIndex));
191 previousSnapshotIndex = snapshotIndex;
192 setSnapshotIndex(snapshotCapturedIndex);
194 previousSnapshotTerm = snapshotTerm;
195 setSnapshotTerm(snapshotCapturedTerm);
199 public void snapshotCommit() {
200 snapshottedJournal = null;
201 previousSnapshotIndex = -1;
202 previousSnapshotTerm = -1;
207 public void snapshotRollback() {
208 snapshottedJournal.addAll(journal);
209 journal = snapshottedJournal;
210 snapshottedJournal = null;
212 snapshotIndex = previousSnapshotIndex;
213 previousSnapshotIndex = -1;
215 snapshotTerm = previousSnapshotTerm;
216 previousSnapshotTerm = -1;