Pavlin Radoslavov | a6b754c | 2014-11-18 13:55:37 -0800 | [diff] [blame] | 1 | /* |
Ray Milkey | 34c9590 | 2015-04-15 09:47:53 -0700 | [diff] [blame] | 2 | * Copyright 2014-2015 Open Networking Laboratory |
Pavlin Radoslavov | a6b754c | 2014-11-18 13:55:37 -0800 | [diff] [blame] | 3 | * |
| 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'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.cluster; |
Pavlin Radoslavov | a6b754c | 2014-11-18 13:55:37 -0800 | [diff] [blame] | 17 | |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 18 | import org.onosproject.event.ListenerService; |
| 19 | |
Madan Jampani | fd45d5e | 2015-04-20 13:33:21 -0700 | [diff] [blame] | 20 | import java.util.List; |
Yuta HIGUCHI | c2bf3d8 | 2014-11-28 18:50:41 -0800 | [diff] [blame] | 21 | import java.util.Map; |
Madan Jampani | 5961051 | 2015-02-25 15:25:43 -0800 | [diff] [blame] | 22 | import java.util.Set; |
Madan Jampani | de003d9 | 2015-05-11 17:14:20 -0700 | [diff] [blame] | 23 | import java.util.concurrent.CompletableFuture; |
Yuta HIGUCHI | c2bf3d8 | 2014-11-28 18:50:41 -0800 | [diff] [blame] | 24 | |
Pavlin Radoslavov | a6b754c | 2014-11-18 13:55:37 -0800 | [diff] [blame] | 25 | /** |
Madan Jampani | 1d3494e | 2014-11-20 11:24:22 -0800 | [diff] [blame] | 26 | * Service for leader election. |
Madan Jampani | 1ee9178 | 2014-11-20 20:24:24 -0800 | [diff] [blame] | 27 | * 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 Radoslavov | a6b754c | 2014-11-18 13:55:37 -0800 | [diff] [blame] | 31 | */ |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 32 | public interface LeadershipService |
| 33 | extends ListenerService<LeadershipEvent, LeadershipEventListener> { |
Pavlin Radoslavov | a6b754c | 2014-11-18 13:55:37 -0800 | [diff] [blame] | 34 | |
| 35 | /** |
Madan Jampani | 5961051 | 2015-02-25 15:25:43 -0800 | [diff] [blame] | 36 | * Returns the current leader for the topic. |
Sho SHIMIZU | 25d843c | 2015-04-10 16:52:33 -0700 | [diff] [blame] | 37 | * |
Madan Jampani | 1ee9178 | 2014-11-20 20:24:24 -0800 | [diff] [blame] | 38 | * @param path topic |
Madan Jampani | 8d21c79 | 2014-12-01 16:31:07 -0800 | [diff] [blame] | 39 | * @return nodeId of the leader, null if so such topic exists. |
Madan Jampani | 1ee9178 | 2014-11-20 20:24:24 -0800 | [diff] [blame] | 40 | */ |
Madan Jampani | 8d21c79 | 2014-12-01 16:31:07 -0800 | [diff] [blame] | 41 | NodeId getLeader(String path); |
Madan Jampani | 1ee9178 | 2014-11-20 20:24:24 -0800 | [diff] [blame] | 42 | |
| 43 | /** |
Madan Jampani | 5961051 | 2015-02-25 15:25:43 -0800 | [diff] [blame] | 44 | * Returns the current leadership info for the topic. |
Sho SHIMIZU | 25d843c | 2015-04-10 16:52:33 -0700 | [diff] [blame] | 45 | * |
Madan Jampani | 5961051 | 2015-02-25 15:25:43 -0800 | [diff] [blame] | 46 | * @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 SHIMIZU | 25d843c | 2015-04-10 16:52:33 -0700 | [diff] [blame] | 53 | * |
Madan Jampani | 5961051 | 2015-02-25 15:25:43 -0800 | [diff] [blame] | 54 | * @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 Jampani | 1d3494e | 2014-11-20 11:24:22 -0800 | [diff] [blame] | 60 | * Joins the leadership contest. |
Sho SHIMIZU | 25d843c | 2015-04-10 16:52:33 -0700 | [diff] [blame] | 61 | * |
Madan Jampani | de003d9 | 2015-05-11 17:14:20 -0700 | [diff] [blame] | 62 | * @param path topic for which this controller node wishes to be a leader |
| 63 | * @return {@code Leadership} future |
Pavlin Radoslavov | a6b754c | 2014-11-18 13:55:37 -0800 | [diff] [blame] | 64 | */ |
Madan Jampani | de003d9 | 2015-05-11 17:14:20 -0700 | [diff] [blame] | 65 | CompletableFuture<Leadership> runForLeadership(String path); |
Pavlin Radoslavov | a6b754c | 2014-11-18 13:55:37 -0800 | [diff] [blame] | 66 | |
| 67 | /** |
Madan Jampani | 1d3494e | 2014-11-20 11:24:22 -0800 | [diff] [blame] | 68 | * Withdraws from a leadership contest. |
Sho SHIMIZU | 25d843c | 2015-04-10 16:52:33 -0700 | [diff] [blame] | 69 | * |
Madan Jampani | de003d9 | 2015-05-11 17:14:20 -0700 | [diff] [blame] | 70 | * @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 Jampani | 1d3494e | 2014-11-20 11:24:22 -0800 | [diff] [blame] | 72 | */ |
Madan Jampani | de003d9 | 2015-05-11 17:14:20 -0700 | [diff] [blame] | 73 | CompletableFuture<Void> withdraw(String path); |
Madan Jampani | 1d3494e | 2014-11-20 11:24:22 -0800 | [diff] [blame] | 74 | |
Madan Jampani | 5961051 | 2015-02-25 15:25:43 -0800 | [diff] [blame] | 75 | /** |
Madan Jampani | 1af8e13 | 2015-04-30 16:41:18 -0700 | [diff] [blame] | 76 | * 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 Jampani | 9bd1f15 | 2015-04-30 23:33:35 -0700 | [diff] [blame] | 83 | * If the local nodeId is not the leader, this method will make no changes and |
| 84 | * simply return false. |
Madan Jampani | 1af8e13 | 2015-04-30 16:41:18 -0700 | [diff] [blame] | 85 | * |
| 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 Jampani | 5961051 | 2015-02-25 15:25:43 -0800 | [diff] [blame] | 103 | * Returns the current leader board. |
Sho SHIMIZU | 25d843c | 2015-04-10 16:52:33 -0700 | [diff] [blame] | 104 | * |
Madan Jampani | 5961051 | 2015-02-25 15:25:43 -0800 | [diff] [blame] | 105 | * @return mapping from topic to leadership info. |
| 106 | */ |
Yuta HIGUCHI | c2bf3d8 | 2014-11-28 18:50:41 -0800 | [diff] [blame] | 107 | Map<String, Leadership> getLeaderBoard(); |
| 108 | |
Madan Jampani | 1d3494e | 2014-11-20 11:24:22 -0800 | [diff] [blame] | 109 | /** |
Ayaka Koshibe | c19b8b8 | 2015-04-08 15:18:24 -0700 | [diff] [blame] | 110 | * Returns the candidates for all known topics. |
Sho SHIMIZU | 25d843c | 2015-04-10 16:52:33 -0700 | [diff] [blame] | 111 | * |
Madan Jampani | fd45d5e | 2015-04-20 13:33:21 -0700 | [diff] [blame] | 112 | * @return A mapping from topics to corresponding list of candidates. |
Ayaka Koshibe | c19b8b8 | 2015-04-08 15:18:24 -0700 | [diff] [blame] | 113 | */ |
Madan Jampani | fd45d5e | 2015-04-20 13:33:21 -0700 | [diff] [blame] | 114 | Map<String, List<NodeId>> getCandidates(); |
Ayaka Koshibe | c19b8b8 | 2015-04-08 15:18:24 -0700 | [diff] [blame] | 115 | |
| 116 | /** |
| 117 | * Returns the candidates for a given topic. |
Sho SHIMIZU | 25d843c | 2015-04-10 16:52:33 -0700 | [diff] [blame] | 118 | * |
Ayaka Koshibe | c19b8b8 | 2015-04-08 15:18:24 -0700 | [diff] [blame] | 119 | * @param path topic |
| 120 | * @return A lists of NodeIds, which may be empty. |
| 121 | */ |
| 122 | List<NodeId> getCandidates(String path); |
| 123 | |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 124 | } |