Fix intermittent PreLeaderScenarioTest failure
[controller.git] / opendaylight / md-sal / sal-akka-raft / src / main / java / org / opendaylight / controller / cluster / raft / persisted / ApplyJournalEntries.java
1 /*
2  * Copyright (c) 2016 Brocade Communications Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.cluster.raft.persisted;
9
10 import akka.dispatch.ControlMessage;
11 import java.io.Externalizable;
12 import java.io.IOException;
13 import java.io.ObjectInput;
14 import java.io.ObjectOutput;
15 import java.io.Serializable;
16
17 /**
18  * This is an internal message that is stored in the akka's persistent journal. During recovery, this
19  * message is used to apply recovered journal entries to the state whose indexes range from the context's
20  * current lastApplied index to "toIndex" contained in the message. This message is sent internally from a
21  * behavior to the RaftActor to persist.
22  *
23  * @author Thomas Pantelis
24  */
25 public class ApplyJournalEntries implements Serializable, MigratedSerializable, ControlMessage {
26     private static final class Proxy implements Externalizable {
27         private static final long serialVersionUID = 1L;
28
29         private ApplyJournalEntries applyEntries;
30
31         // checkstyle flags the public modifier as redundant which really doesn't make sense since it clearly isn't
32         // redundant. It is explicitly needed for Java serialization to be able to create instances via reflection.
33         @SuppressWarnings("checkstyle:RedundantModifier")
34         public Proxy() {
35             // For Externalizable
36         }
37
38         Proxy(final ApplyJournalEntries applyEntries) {
39             this.applyEntries = applyEntries;
40         }
41
42         @Override
43         public void writeExternal(final ObjectOutput out) throws IOException {
44             out.writeLong(applyEntries.toIndex);
45         }
46
47         @Override
48         public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
49             applyEntries = new ApplyJournalEntries(in.readLong());
50         }
51
52         private Object readResolve() {
53             return applyEntries;
54         }
55     }
56
57     private static final long serialVersionUID = 1L;
58
59     private final long toIndex;
60     private final boolean migrated;
61
62     private ApplyJournalEntries(final long toIndex, final boolean migrated) {
63         this.toIndex = toIndex;
64         this.migrated = migrated;
65     }
66
67     public ApplyJournalEntries(final long toIndex) {
68         this(toIndex, false);
69     }
70
71     public long getToIndex() {
72         return toIndex;
73     }
74
75     @Override
76     public boolean isMigrated() {
77         return migrated;
78     }
79
80     @Override
81     public Object writeReplace() {
82         return new Proxy(this);
83     }
84
85     @Deprecated
86     public static ApplyJournalEntries createMigrated(final long fromIndex) {
87         return new ApplyJournalEntries(fromIndex, true);
88     }
89
90     @Override
91     public String toString() {
92         return "ApplyJournalEntries [toIndex=" + toIndex + "]";
93     }
94 }