Bug 5450: Query akka cluster state on Follower ElectionTimeout
[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 akka.cluster.Cluster;
16 import com.google.common.annotations.VisibleForTesting;
17 import java.util.Collection;
18 import java.util.Optional;
19 import java.util.function.LongSupplier;
20 import javax.annotation.Nullable;
21 import org.opendaylight.controller.cluster.DataPersistenceProvider;
22 import org.opendaylight.controller.cluster.raft.behaviors.RaftActorBehavior;
23 import org.opendaylight.controller.cluster.raft.persisted.ServerConfigurationPayload;
24 import org.opendaylight.controller.cluster.raft.policy.RaftPolicy;
25 import org.slf4j.Logger;
26
27 /**
28  * The RaftActorContext contains that portion of the RaftActors state that
29  * needs to be shared with it's behaviors. A RaftActorContext should NEVER be
30  * used in any actor context outside the RaftActor that constructed it.
31  */
32 public interface RaftActorContext {
33     /**
34      * Create a new local actor
35      * @param props
36      * @return a reference to the newly created actor
37      */
38     ActorRef actorOf(Props props);
39
40     /**
41      * Create a actor selection
42      * @param path
43      * @return an actor selection for the given actor path
44      */
45     ActorSelection actorSelection(String path);
46
47     /**
48      * Get the identifier for the RaftActor. This identifier represents the
49      * name of the actor whose common state is being shared. For example the
50      * id could be 'inventory'
51      *
52      * @return the identifier
53      */
54     String getId();
55
56     /**
57      * @return A reference to the RaftActor itself. This could be used to send messages
58      * to the RaftActor
59      */
60     ActorRef getActor();
61
62     /**
63      * The akka Cluster singleton for the actor system if one is configured.
64      *
65      * @return an Optional containing the CLuster instance is present.
66      */
67     Optional<Cluster> getCluster();
68
69     /**
70      * @return the ElectionTerm information
71      */
72     ElectionTerm getTermInformation();
73
74     /**
75      * @return index of highest log entry known to be committed (initialized to 0, increases monotonically)
76      */
77     long getCommitIndex();
78
79
80     /**
81      * @param commitIndex new commit index
82      */
83     void setCommitIndex(long commitIndex);
84
85     /**
86      * @return index of highest log entry applied to state machine (initialized to 0, increases monotonically)
87      */
88     long getLastApplied();
89
90
91     /**
92      * @param lastApplied the index of the last log entry that was applied to the state
93      */
94     void setLastApplied(long lastApplied);
95
96     /**
97      *
98      * @param replicatedLog the replicated log of the current RaftActor
99      */
100     void setReplicatedLog(ReplicatedLog replicatedLog);
101
102     /**
103      * @return A representation of the log
104      */
105     ReplicatedLog getReplicatedLog();
106
107     /**
108      * @return The ActorSystem associated with this context
109      */
110     ActorSystem getActorSystem();
111
112     /**
113      * @return the logger to be used for logging messages to a log file
114      */
115     Logger getLogger();
116
117     /**
118      * Get the address of the peer as a String. This is the same format in
119      * which a consumer would provide the address
120      *
121      * @param peerId
122      * @return The address of the peer or null if the address has not yet been
123      *         resolved
124      */
125     String getPeerAddress(String peerId);
126
127     /**
128      * @param serverCfgPayload
129      */
130     void updatePeerIds(ServerConfigurationPayload serverCfgPayload);
131
132     /**
133      * @return list of PeerInfo
134      */
135     Collection<PeerInfo> getPeers();
136
137     /**
138      * @return the list of peer IDs.
139      */
140     Collection<String> getPeerIds();
141
142     /**
143      * Get the PeerInfo for the given peer.
144      *
145      * @param peerId
146      * @return the PeerInfo
147      */
148     PeerInfo getPeerInfo(String peerId);
149
150     /**
151      * Add to actor peers
152      *
153      * @param name
154      * @param address
155      */
156     void addToPeers(String name, String address, VotingState votingState);
157
158     /**
159      *
160      * @param name
161      */
162     void removePeer(String name);
163
164     /**
165      * Given a peerId return the corresponding actor
166      * <p>
167      *
168      *
169      * @param peerId
170      * @return The actorSelection corresponding to the peer or null if the
171      *         address has not yet been resolved
172      */
173     ActorSelection getPeerActorSelection(String peerId);
174
175     /**
176      * Set Peer Address can be called at a later time to change the address of
177      * a known peer.
178      *
179      * <p>
180      * Throws an IllegalStateException if the peer is unknown
181      *
182      * @param peerId
183      * @param peerAddress
184      */
185     void setPeerAddress(String peerId, String peerAddress);
186
187     /**
188      * @return ConfigParams
189      */
190     ConfigParams getConfigParams();
191
192     /**
193      *
194      * @return the SnapshotManager for this RaftActor
195      */
196     SnapshotManager getSnapshotManager();
197
198     /**
199      *
200      * @return the DataPersistenceProvider for this RaftActor
201      */
202     DataPersistenceProvider getPersistenceProvider();
203
204     /**
205      *
206      * @return true if the RaftActor has followers else false
207      */
208     boolean hasFollowers();
209
210     /**
211      *
212      * @return the total memory used by the ReplicatedLog
213      */
214     long getTotalMemory();
215
216     /**
217      *
218      * @param retriever a supplier of the total memory metric
219      */
220     @VisibleForTesting
221     void setTotalMemoryRetriever(LongSupplier retriever);
222
223     /**
224      *
225      * @return the payload version to be used when replicating data
226      */
227     short getPayloadVersion();
228
229     /**
230      * @return an implementation of the RaftPolicy so that the Raft code can be adapted
231      */
232     RaftPolicy getRaftPolicy();
233
234     /**
235      * @return true if there are any dynamic server configuration changes available,
236      *  false if static peer configurations are still in use
237      */
238     boolean isDynamicServerConfigurationInUse();
239
240     /**
241      * Configures the dynamic server configurations are avaialble for the RaftActor
242      */
243     void setDynamicServerConfigurationInUse();
244
245     /**
246      * @return the RaftActor's peer information as a ServerConfigurationPayload if the
247      * dynamic server configurations are available, otherwise returns null
248      */
249     @Nullable ServerConfigurationPayload getPeerServerInfo(boolean includeSelf);
250
251     /**
252      * @return true if this RaftActor is a voting member of the cluster, false otherwise.
253      */
254     boolean isVotingMember();
255
256     /**
257      * @return true if there are any voting peers, false otherwise.
258      */
259     boolean anyVotingPeers();
260
261     /**
262      * @return current behavior attached to the raft actor.
263      */
264     RaftActorBehavior getCurrentBehavior();
265 }