BUG 2185 : Introduce RaftPolicy & DefaultRaftPolicy
[controller.git] / opendaylight / md-sal / sal-akka-raft / src / main / java / org / opendaylight / controller / cluster / raft / RaftActorContext.java
1 /*
2  * Copyright (c) 2014 Cisco 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
9 package org.opendaylight.controller.cluster.raft;
10
11 import akka.actor.ActorRef;
12 import akka.actor.ActorSelection;
13 import akka.actor.ActorSystem;
14 import akka.actor.Props;
15 import com.google.common.annotations.VisibleForTesting;
16 import com.google.common.base.Supplier;
17 import java.util.Map;
18 import org.opendaylight.controller.cluster.DataPersistenceProvider;
19 import org.opendaylight.controller.cluster.raft.policy.RaftPolicy;
20 import org.slf4j.Logger;
21
22 /**
23  * The RaftActorContext contains that portion of the RaftActors state that
24  * needs to be shared with it's behaviors. A RaftActorContext should NEVER be
25  * used in any actor context outside the RaftActor that constructed it.
26  */
27 public interface RaftActorContext {
28     /**
29      * Create a new local actor
30      * @param props
31      * @return a reference to the newly created actor
32      */
33     ActorRef actorOf(Props props);
34
35     /**
36      * Create a actor selection
37      * @param path
38      * @return an actor selection for the given actor path
39      */
40     ActorSelection actorSelection(String path);
41
42     /**
43      * Get the identifier for the RaftActor. This identifier represents the
44      * name of the actor whose common state is being shared. For example the
45      * id could be 'inventory'
46      *
47      * @return the identifier
48      */
49     String getId();
50
51     /**
52      * @return A reference to the RaftActor itself. This could be used to send messages
53      * to the RaftActor
54      */
55     ActorRef getActor();
56
57     /**
58      * @return the ElectionTerm information
59      */
60     ElectionTerm getTermInformation();
61
62     /**
63      * @return index of highest log entry known to be committed (initialized to 0, increases monotonically)
64      */
65     long getCommitIndex();
66
67
68     /**
69      * @param commitIndex new commit index
70      */
71     void setCommitIndex(long commitIndex);
72
73     /**
74      * @return index of highest log entry applied to state machine (initialized to 0, increases monotonically)
75      */
76     long getLastApplied();
77
78
79     /**
80      * @param lastApplied the index of the last log entry that was applied to the state
81      */
82     void setLastApplied(long lastApplied);
83
84     /**
85      *
86      * @param replicatedLog the replicated log of the current RaftActor
87      */
88     void setReplicatedLog(ReplicatedLog replicatedLog);
89
90     /**
91      * @return A representation of the log
92      */
93     ReplicatedLog getReplicatedLog();
94
95     /**
96      * @return The ActorSystem associated with this context
97      */
98     ActorSystem getActorSystem();
99
100     /**
101      * @return the logger to be used for logging messages to a log file
102      */
103     Logger getLogger();
104
105     /**
106      * @return a mapping of peerId's to their addresses
107      *
108      */
109     Map<String, String> getPeerAddresses();
110
111     /**
112      * Get the address of the peer as a String. This is the same format in
113      * which a consumer would provide the address
114      *
115      * @param peerId
116      * @return The address of the peer or null if the address has not yet been
117      *         resolved
118      */
119     String getPeerAddress(String peerId);
120
121     /**
122      * Add to actor peers
123      *
124      * @param name
125      * @param address
126      */
127     void addToPeers(String name, String address);
128
129     /**
130      *
131      * @param name
132      */
133     void removePeer(String name);
134
135     /**
136      * Given a peerId return the corresponding actor
137      * <p>
138      *
139      *
140      * @param peerId
141      * @return The actorSelection corresponding to the peer or null if the
142      *         address has not yet been resolved
143      */
144     ActorSelection getPeerActorSelection(String peerId);
145
146     /**
147      * Set Peer Address can be called at a later time to change the address of
148      * a known peer.
149      *
150      * <p>
151      * Throws an IllegalStateException if the peer is unknown
152      *
153      * @param peerId
154      * @param peerAddress
155      */
156     void setPeerAddress(String peerId, String peerAddress);
157
158     /**
159      * @return ConfigParams
160      */
161     ConfigParams getConfigParams();
162
163     /**
164      *
165      * @return the SnapshotManager for this RaftActor
166      */
167     SnapshotManager getSnapshotManager();
168
169     /**
170      *
171      * @return the DataPersistenceProvider for this RaftActor
172      */
173     DataPersistenceProvider getPersistenceProvider();
174
175     /**
176      *
177      * @return true if the RaftActor has followers else false
178      */
179     boolean hasFollowers();
180
181     /**
182      *
183      * @return the total memory used by the ReplicatedLog
184      */
185     long getTotalMemory();
186
187     /**
188      *
189      * @param retriever a supplier of the total memory metric
190      */
191     @VisibleForTesting
192     void setTotalMemoryRetriever(Supplier<Long> retriever);
193
194     /**
195      *
196      * @return the payload version to be used when replicating data
197      */
198     short getPayloadVersion();
199
200     /**
201      * @return an implementation of the RaftPolicy so that the Raft code can be adapted
202      */
203     RaftPolicy getRaftPolicy();
204 }