blob: 7d1f6079b530b77e4098c0dbe722a037a13b2018 [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
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070018import org.onosproject.event.ListenerService;
19
Madan Jampanifd45d5e2015-04-20 13:33:21 -070020import java.util.List;
Yuta HIGUCHIc2bf3d82014-11-28 18:50:41 -080021import java.util.Map;
Madan Jampani59610512015-02-25 15:25:43 -080022import java.util.Set;
Madan Jampanide003d92015-05-11 17:14:20 -070023import java.util.concurrent.CompletableFuture;
Yuta HIGUCHIc2bf3d82014-11-28 18:50:41 -080024
Pavlin Radoslavova6b754c2014-11-18 13:55:37 -080025/**
Madan Jampani1d3494e2014-11-20 11:24:22 -080026 * Service for leader election.
Madan Jampani1ee91782014-11-20 20:24:24 -080027 * Leadership contests are organized around topics. A instance can join the
28 * leadership race for a topic or withdraw from a race it has previously joined.
29 * Listeners can be added to receive notifications asynchronously for various
30 * leadership contests.
Pavlin Radoslavova6b754c2014-11-18 13:55:37 -080031 */
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070032public interface LeadershipService
33 extends ListenerService<LeadershipEvent, LeadershipEventListener> {
Pavlin Radoslavova6b754c2014-11-18 13:55:37 -080034
35 /**
Madan Jampani59610512015-02-25 15:25:43 -080036 * Returns the current leader for the topic.
Sho SHIMIZU25d843c2015-04-10 16:52:33 -070037 *
Madan Jampani1ee91782014-11-20 20:24:24 -080038 * @param path topic
Madan Jampani8d21c792014-12-01 16:31:07 -080039 * @return nodeId of the leader, null if so such topic exists.
Madan Jampani1ee91782014-11-20 20:24:24 -080040 */
Madan Jampani8d21c792014-12-01 16:31:07 -080041 NodeId getLeader(String path);
Madan Jampani1ee91782014-11-20 20:24:24 -080042
43 /**
Madan Jampani59610512015-02-25 15:25:43 -080044 * Returns the current leadership info for the topic.
Sho SHIMIZU25d843c2015-04-10 16:52:33 -070045 *
Madan Jampani59610512015-02-25 15:25:43 -080046 * @param path topic
47 * @return leadership info or null if so such topic exists.
48 */
49 Leadership getLeadership(String path);
50
51 /**
52 * Returns the set of topics owned by the specified node.
Sho SHIMIZU25d843c2015-04-10 16:52:33 -070053 *
Madan Jampani59610512015-02-25 15:25:43 -080054 * @param nodeId node Id.
55 * @return set of topics for which this node is the current leader.
56 */
57 Set<String> ownedTopics(NodeId nodeId);
58
59 /**
Madan Jampani1d3494e2014-11-20 11:24:22 -080060 * Joins the leadership contest.
Sho SHIMIZU25d843c2015-04-10 16:52:33 -070061 *
Madan Jampanide003d92015-05-11 17:14:20 -070062 * @param path topic for which this controller node wishes to be a leader
63 * @return {@code Leadership} future
Pavlin Radoslavova6b754c2014-11-18 13:55:37 -080064 */
Madan Jampanide003d92015-05-11 17:14:20 -070065 CompletableFuture<Leadership> runForLeadership(String path);
Pavlin Radoslavova6b754c2014-11-18 13:55:37 -080066
67 /**
Madan Jampani1d3494e2014-11-20 11:24:22 -080068 * Withdraws from a leadership contest.
Sho SHIMIZU25d843c2015-04-10 16:52:33 -070069 *
Madan Jampanide003d92015-05-11 17:14:20 -070070 * @param path topic for which this controller node no longer wishes to be a leader
71 * @return future that is successfully completed when withdraw is done
Madan Jampani1d3494e2014-11-20 11:24:22 -080072 */
Madan Jampanide003d92015-05-11 17:14:20 -070073 CompletableFuture<Void> withdraw(String path);
Madan Jampani1d3494e2014-11-20 11:24:22 -080074
Madan Jampani59610512015-02-25 15:25:43 -080075 /**
Madan Jampani1af8e132015-04-30 16:41:18 -070076 * If the local nodeId is the leader for specified topic, this method causes it to
77 * step down temporarily from leadership.
78 * <p>
79 * The node will continue to be in contention for leadership and can
80 * potentially become the leader again if and when it becomes the highest
81 * priority candidate
82 * <p>
Madan Jampani9bd1f152015-04-30 23:33:35 -070083 * If the local nodeId is not the leader, this method will make no changes and
84 * simply return false.
Madan Jampani1af8e132015-04-30 16:41:18 -070085 *
86 * @param path topic for which this controller node should give up leadership
87 * @return true if this node stepped down from leadership, false otherwise
88 */
89 boolean stepdown(String path);
90
91 /**
92 * Moves the specified nodeId to the top of the candidates list for the topic.
93 * <p>
94 * If the node is not a candidate for this topic, this method will be a noop.
95 *
96 * @param path leadership topic
97 * @param nodeId nodeId to make the top candidate
98 * @return true if nodeId is now the top candidate, false otherwise
99 */
100 boolean makeTopCandidate(String path, NodeId nodeId);
101
102 /**
Madan Jampani59610512015-02-25 15:25:43 -0800103 * Returns the current leader board.
Sho SHIMIZU25d843c2015-04-10 16:52:33 -0700104 *
Madan Jampani59610512015-02-25 15:25:43 -0800105 * @return mapping from topic to leadership info.
106 */
Yuta HIGUCHIc2bf3d82014-11-28 18:50:41 -0800107 Map<String, Leadership> getLeaderBoard();
108
Madan Jampani1d3494e2014-11-20 11:24:22 -0800109 /**
Ayaka Koshibec19b8b82015-04-08 15:18:24 -0700110 * Returns the candidates for all known topics.
Sho SHIMIZU25d843c2015-04-10 16:52:33 -0700111 *
Madan Jampanifd45d5e2015-04-20 13:33:21 -0700112 * @return A mapping from topics to corresponding list of candidates.
Ayaka Koshibec19b8b82015-04-08 15:18:24 -0700113 */
Madan Jampanifd45d5e2015-04-20 13:33:21 -0700114 Map<String, List<NodeId>> getCandidates();
Ayaka Koshibec19b8b82015-04-08 15:18:24 -0700115
116 /**
117 * Returns the candidates for a given topic.
Sho SHIMIZU25d843c2015-04-10 16:52:33 -0700118 *
Ayaka Koshibec19b8b82015-04-08 15:18:24 -0700119 * @param path topic
120 * @return A lists of NodeIds, which may be empty.
121 */
122 List<NodeId> getCandidates(String path);
123
Brian O'Connorabafb502014-12-02 22:26:20 -0800124}