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