blob: 9130f4faa24b1c6b62c5fbbee3e651c95b02b2ea [file] [log] [blame]
Pavlin Radoslavova6b754c2014-11-18 13:55:37 -08001/*
Ray Milkey34c95902015-04-15 09:47:53 -07002 * Copyright 2014-2015 Open Networking Laboratory
Pavlin Radoslavova6b754c2014-11-18 13:55:37 -08003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.cluster;
Pavlin Radoslavova6b754c2014-11-18 13:55:37 -080017
Madan Jampanifd45d5e2015-04-20 13:33:21 -070018import java.util.List;
Yuta HIGUCHIc2bf3d82014-11-28 18:50:41 -080019import java.util.Map;
Madan Jampani59610512015-02-25 15:25:43 -080020import java.util.Set;
Madan Jampanide003d92015-05-11 17:14:20 -070021import java.util.concurrent.CompletableFuture;
Yuta HIGUCHIc2bf3d82014-11-28 18:50:41 -080022
Pavlin Radoslavova6b754c2014-11-18 13:55:37 -080023/**
Madan Jampani1d3494e2014-11-20 11:24:22 -080024 * Service for leader election.
Madan Jampani1ee91782014-11-20 20:24:24 -080025 * Leadership contests are organized around topics. A instance can join the
26 * leadership race for a topic or withdraw from a race it has previously joined.
27 * Listeners can be added to receive notifications asynchronously for various
28 * leadership contests.
Pavlin Radoslavova6b754c2014-11-18 13:55:37 -080029 */
30public interface LeadershipService {
31
32 /**
Madan Jampani59610512015-02-25 15:25:43 -080033 * Returns the current leader for the topic.
Sho SHIMIZU25d843c2015-04-10 16:52:33 -070034 *
Madan Jampani1ee91782014-11-20 20:24:24 -080035 * @param path topic
Madan Jampani8d21c792014-12-01 16:31:07 -080036 * @return nodeId of the leader, null if so such topic exists.
Madan Jampani1ee91782014-11-20 20:24:24 -080037 */
Madan Jampani8d21c792014-12-01 16:31:07 -080038 NodeId getLeader(String path);
Madan Jampani1ee91782014-11-20 20:24:24 -080039
40 /**
Madan Jampani59610512015-02-25 15:25:43 -080041 * Returns the current leadership info for the topic.
Sho SHIMIZU25d843c2015-04-10 16:52:33 -070042 *
Madan Jampani59610512015-02-25 15:25:43 -080043 * @param path topic
44 * @return leadership info or null if so such topic exists.
45 */
46 Leadership getLeadership(String path);
47
48 /**
49 * Returns the set of topics owned by the specified node.
Sho SHIMIZU25d843c2015-04-10 16:52:33 -070050 *
Madan Jampani59610512015-02-25 15:25:43 -080051 * @param nodeId node Id.
52 * @return set of topics for which this node is the current leader.
53 */
54 Set<String> ownedTopics(NodeId nodeId);
55
56 /**
Madan Jampani1d3494e2014-11-20 11:24:22 -080057 * Joins the leadership contest.
Sho SHIMIZU25d843c2015-04-10 16:52:33 -070058 *
Madan Jampanide003d92015-05-11 17:14:20 -070059 * @param path topic for which this controller node wishes to be a leader
60 * @return {@code Leadership} future
Pavlin Radoslavova6b754c2014-11-18 13:55:37 -080061 */
Madan Jampanide003d92015-05-11 17:14:20 -070062 CompletableFuture<Leadership> runForLeadership(String path);
Pavlin Radoslavova6b754c2014-11-18 13:55:37 -080063
64 /**
Madan Jampani1d3494e2014-11-20 11:24:22 -080065 * Withdraws from a leadership contest.
Sho SHIMIZU25d843c2015-04-10 16:52:33 -070066 *
Madan Jampanide003d92015-05-11 17:14:20 -070067 * @param path topic for which this controller node no longer wishes to be a leader
68 * @return future that is successfully completed when withdraw is done
Madan Jampani1d3494e2014-11-20 11:24:22 -080069 */
Madan Jampanide003d92015-05-11 17:14:20 -070070 CompletableFuture<Void> withdraw(String path);
Madan Jampani1d3494e2014-11-20 11:24:22 -080071
Madan Jampani59610512015-02-25 15:25:43 -080072 /**
Madan Jampani1af8e132015-04-30 16:41:18 -070073 * If the local nodeId is the leader for specified topic, this method causes it to
74 * step down temporarily from leadership.
75 * <p>
76 * The node will continue to be in contention for leadership and can
77 * potentially become the leader again if and when it becomes the highest
78 * priority candidate
79 * <p>
Madan Jampani9bd1f152015-04-30 23:33:35 -070080 * If the local nodeId is not the leader, this method will make no changes and
81 * simply return false.
Madan Jampani1af8e132015-04-30 16:41:18 -070082 *
83 * @param path topic for which this controller node should give up leadership
84 * @return true if this node stepped down from leadership, false otherwise
85 */
86 boolean stepdown(String path);
87
88 /**
89 * Moves the specified nodeId to the top of the candidates list for the topic.
90 * <p>
91 * If the node is not a candidate for this topic, this method will be a noop.
92 *
93 * @param path leadership topic
94 * @param nodeId nodeId to make the top candidate
95 * @return true if nodeId is now the top candidate, false otherwise
96 */
97 boolean makeTopCandidate(String path, NodeId nodeId);
98
99 /**
Madan Jampani59610512015-02-25 15:25:43 -0800100 * Returns the current leader board.
Sho SHIMIZU25d843c2015-04-10 16:52:33 -0700101 *
Madan Jampani59610512015-02-25 15:25:43 -0800102 * @return mapping from topic to leadership info.
103 */
Yuta HIGUCHIc2bf3d82014-11-28 18:50:41 -0800104 Map<String, Leadership> getLeaderBoard();
105
Madan Jampani1d3494e2014-11-20 11:24:22 -0800106 /**
Ayaka Koshibec19b8b82015-04-08 15:18:24 -0700107 * Returns the candidates for all known topics.
Sho SHIMIZU25d843c2015-04-10 16:52:33 -0700108 *
Madan Jampanifd45d5e2015-04-20 13:33:21 -0700109 * @return A mapping from topics to corresponding list of candidates.
Ayaka Koshibec19b8b82015-04-08 15:18:24 -0700110 */
Madan Jampanifd45d5e2015-04-20 13:33:21 -0700111 Map<String, List<NodeId>> getCandidates();
Ayaka Koshibec19b8b82015-04-08 15:18:24 -0700112
113 /**
114 * Returns the candidates for a given topic.
Sho SHIMIZU25d843c2015-04-10 16:52:33 -0700115 *
Ayaka Koshibec19b8b82015-04-08 15:18:24 -0700116 * @param path topic
117 * @return A lists of NodeIds, which may be empty.
118 */
119 List<NodeId> getCandidates(String path);
120
121 /**
Madan Jampani1d3494e2014-11-20 11:24:22 -0800122 * Registers a event listener to be notified of leadership events.
Sho SHIMIZU25d843c2015-04-10 16:52:33 -0700123 *
Madan Jampani1d3494e2014-11-20 11:24:22 -0800124 * @param listener listener that will asynchronously notified of leadership events.
Pavlin Radoslavova6b754c2014-11-18 13:55:37 -0800125 */
126 void addListener(LeadershipEventListener listener);
127
128 /**
Madan Jampani1d3494e2014-11-20 11:24:22 -0800129 * Unregisters a event listener for leadership events.
Sho SHIMIZU25d843c2015-04-10 16:52:33 -0700130 *
Madan Jampani1d3494e2014-11-20 11:24:22 -0800131 * @param listener listener to be removed.
Pavlin Radoslavova6b754c2014-11-18 13:55:37 -0800132 */
133 void removeListener(LeadershipEventListener listener);
Brian O'Connorabafb502014-12-02 22:26:20 -0800134}