blob: 9b94056e827dbc1d2fedbace996685c9f419aa95 [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;
Yuta HIGUCHIc2bf3d82014-11-28 18:50:41 -080021
Pavlin Radoslavova6b754c2014-11-18 13:55:37 -080022/**
Madan Jampani1d3494e2014-11-20 11:24:22 -080023 * Service for leader election.
Madan Jampani1ee91782014-11-20 20:24:24 -080024 * Leadership contests are organized around topics. A instance can join the
25 * leadership race for a topic or withdraw from a race it has previously joined.
26 * Listeners can be added to receive notifications asynchronously for various
27 * leadership contests.
Pavlin Radoslavova6b754c2014-11-18 13:55:37 -080028 */
29public interface LeadershipService {
30
31 /**
Madan Jampani59610512015-02-25 15:25:43 -080032 * Returns the current leader for the topic.
Sho SHIMIZU25d843c2015-04-10 16:52:33 -070033 *
Madan Jampani1ee91782014-11-20 20:24:24 -080034 * @param path topic
Madan Jampani8d21c792014-12-01 16:31:07 -080035 * @return nodeId of the leader, null if so such topic exists.
Madan Jampani1ee91782014-11-20 20:24:24 -080036 */
Madan Jampani8d21c792014-12-01 16:31:07 -080037 NodeId getLeader(String path);
Madan Jampani1ee91782014-11-20 20:24:24 -080038
39 /**
Madan Jampani59610512015-02-25 15:25:43 -080040 * Returns the current leadership info for the topic.
Sho SHIMIZU25d843c2015-04-10 16:52:33 -070041 *
Madan Jampani59610512015-02-25 15:25:43 -080042 * @param path topic
43 * @return leadership info or null if so such topic exists.
44 */
45 Leadership getLeadership(String path);
46
47 /**
48 * Returns the set of topics owned by the specified node.
Sho SHIMIZU25d843c2015-04-10 16:52:33 -070049 *
Madan Jampani59610512015-02-25 15:25:43 -080050 * @param nodeId node Id.
51 * @return set of topics for which this node is the current leader.
52 */
53 Set<String> ownedTopics(NodeId nodeId);
54
55 /**
Madan Jampani1d3494e2014-11-20 11:24:22 -080056 * Joins the leadership contest.
Sho SHIMIZU25d843c2015-04-10 16:52:33 -070057 *
Madan Jampani1d3494e2014-11-20 11:24:22 -080058 * @param path topic for which this controller node wishes to be a leader.
Pavlin Radoslavova6b754c2014-11-18 13:55:37 -080059 */
Madan Jampani1d3494e2014-11-20 11:24:22 -080060 void runForLeadership(String path);
Pavlin Radoslavova6b754c2014-11-18 13:55:37 -080061
62 /**
Madan Jampani1d3494e2014-11-20 11:24:22 -080063 * Withdraws from a leadership contest.
Sho SHIMIZU25d843c2015-04-10 16:52:33 -070064 *
Madan Jampani1d3494e2014-11-20 11:24:22 -080065 * @param path topic for which this controller node no longer wishes to be a leader.
66 */
67 void withdraw(String path);
68
Madan Jampani59610512015-02-25 15:25:43 -080069 /**
Madan Jampani1af8e132015-04-30 16:41:18 -070070 * If the local nodeId is the leader for specified topic, this method causes it to
71 * step down temporarily from leadership.
72 * <p>
73 * The node will continue to be in contention for leadership and can
74 * potentially become the leader again if and when it becomes the highest
75 * priority candidate
76 * <p>
Madan Jampani9bd1f152015-04-30 23:33:35 -070077 * If the local nodeId is not the leader, this method will make no changes and
78 * simply return false.
Madan Jampani1af8e132015-04-30 16:41:18 -070079 *
80 * @param path topic for which this controller node should give up leadership
81 * @return true if this node stepped down from leadership, false otherwise
82 */
83 boolean stepdown(String path);
84
85 /**
86 * Moves the specified nodeId to the top of the candidates list for the topic.
87 * <p>
88 * If the node is not a candidate for this topic, this method will be a noop.
89 *
90 * @param path leadership topic
91 * @param nodeId nodeId to make the top candidate
92 * @return true if nodeId is now the top candidate, false otherwise
93 */
94 boolean makeTopCandidate(String path, NodeId nodeId);
95
96 /**
Madan Jampani59610512015-02-25 15:25:43 -080097 * Returns the current leader board.
Sho SHIMIZU25d843c2015-04-10 16:52:33 -070098 *
Madan Jampani59610512015-02-25 15:25:43 -080099 * @return mapping from topic to leadership info.
100 */
Yuta HIGUCHIc2bf3d82014-11-28 18:50:41 -0800101 Map<String, Leadership> getLeaderBoard();
102
Madan Jampani1d3494e2014-11-20 11:24:22 -0800103 /**
Ayaka Koshibec19b8b82015-04-08 15:18:24 -0700104 * Returns the candidates for all known topics.
Sho SHIMIZU25d843c2015-04-10 16:52:33 -0700105 *
Madan Jampanifd45d5e2015-04-20 13:33:21 -0700106 * @return A mapping from topics to corresponding list of candidates.
Ayaka Koshibec19b8b82015-04-08 15:18:24 -0700107 */
Madan Jampanifd45d5e2015-04-20 13:33:21 -0700108 Map<String, List<NodeId>> getCandidates();
Ayaka Koshibec19b8b82015-04-08 15:18:24 -0700109
110 /**
111 * Returns the candidates for a given topic.
Sho SHIMIZU25d843c2015-04-10 16:52:33 -0700112 *
Ayaka Koshibec19b8b82015-04-08 15:18:24 -0700113 * @param path topic
114 * @return A lists of NodeIds, which may be empty.
115 */
116 List<NodeId> getCandidates(String path);
117
118 /**
Madan Jampani1d3494e2014-11-20 11:24:22 -0800119 * Registers a event listener to be notified of leadership events.
Sho SHIMIZU25d843c2015-04-10 16:52:33 -0700120 *
Madan Jampani1d3494e2014-11-20 11:24:22 -0800121 * @param listener listener that will asynchronously notified of leadership events.
Pavlin Radoslavova6b754c2014-11-18 13:55:37 -0800122 */
123 void addListener(LeadershipEventListener listener);
124
125 /**
Madan Jampani1d3494e2014-11-20 11:24:22 -0800126 * Unregisters a event listener for leadership events.
Sho SHIMIZU25d843c2015-04-10 16:52:33 -0700127 *
Madan Jampani1d3494e2014-11-20 11:24:22 -0800128 * @param listener listener to be removed.
Pavlin Radoslavova6b754c2014-11-18 13:55:37 -0800129 */
130 void removeListener(LeadershipEventListener listener);
Brian O'Connorabafb502014-12-02 22:26:20 -0800131}