blob: d95d1becbe55242a572ef11b17c83cde515331f1 [file] [log] [blame]
Pavlin Radoslavova6b754c2014-11-18 13:55:37 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2014-present Open Networking Foundation
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 Jampani620f70d2016-01-30 22:22:47 -080020import com.google.common.base.Objects;
21import com.google.common.collect.ImmutableList;
22import com.google.common.collect.ImmutableMap;
23import com.google.common.collect.Maps;
24
Madan Jampanifd45d5e2015-04-20 13:33:21 -070025import java.util.List;
Yuta HIGUCHIc2bf3d82014-11-28 18:50:41 -080026import java.util.Map;
Madan Jampani59610512015-02-25 15:25:43 -080027import java.util.Set;
Yuta HIGUCHIc2bf3d82014-11-28 18:50:41 -080028
Pavlin Radoslavova6b754c2014-11-18 13:55:37 -080029/**
Madan Jampani1d3494e2014-11-20 11:24:22 -080030 * Service for leader election.
Madan Jampani620f70d2016-01-30 22:22:47 -080031 * <p>
Madan Jampani1ee91782014-11-20 20:24:24 -080032 * Leadership contests are organized around topics. A instance can join the
33 * leadership race for a topic or withdraw from a race it has previously joined.
Madan Jampani620f70d2016-01-30 22:22:47 -080034 * <p>
Madan Jampani1ee91782014-11-20 20:24:24 -080035 * Listeners can be added to receive notifications asynchronously for various
36 * leadership contests.
Madan Jampani620f70d2016-01-30 22:22:47 -080037 * <p>
38 * When a node gets elected as a leader for a topic, all nodes receive notifications
39 * indicating a change in leadership.
Pavlin Radoslavova6b754c2014-11-18 13:55:37 -080040 */
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070041public interface LeadershipService
42 extends ListenerService<LeadershipEvent, LeadershipEventListener> {
Pavlin Radoslavova6b754c2014-11-18 13:55:37 -080043
44 /**
Madan Jampani620f70d2016-01-30 22:22:47 -080045 * Returns the {@link NodeId node identifier} that is the current leader for a topic.
Sho SHIMIZU25d843c2015-04-10 16:52:33 -070046 *
Madan Jampani620f70d2016-01-30 22:22:47 -080047 * @param topic leadership topic
48 * @return node identifier of the current leader; {@code null} if there is no leader for the topic
Madan Jampani1ee91782014-11-20 20:24:24 -080049 */
Madan Jampani620f70d2016-01-30 22:22:47 -080050 default NodeId getLeader(String topic) {
51 Leadership leadership = getLeadership(topic);
52 return leadership == null ? null : leadership.leaderNodeId();
53 }
Madan Jampani1ee91782014-11-20 20:24:24 -080054
55 /**
Madan Jampani620f70d2016-01-30 22:22:47 -080056 * Returns the current {@link Leadership leadership} for a topic.
Sho SHIMIZU25d843c2015-04-10 16:52:33 -070057 *
Madan Jampani620f70d2016-01-30 22:22:47 -080058 * @param topic leadership topic
59 * @return leadership or {@code null} if no such topic exists
Madan Jampani59610512015-02-25 15:25:43 -080060 */
Madan Jampani620f70d2016-01-30 22:22:47 -080061 Leadership getLeadership(String topic);
Madan Jampani59610512015-02-25 15:25:43 -080062
63 /**
Madan Jampani620f70d2016-01-30 22:22:47 -080064 * Returns the set of topics owned by the specified {@link NodeId node}.
Sho SHIMIZU25d843c2015-04-10 16:52:33 -070065 *
Madan Jampani620f70d2016-01-30 22:22:47 -080066 * @param nodeId node identifier.
Madan Jampani59610512015-02-25 15:25:43 -080067 * @return set of topics for which this node is the current leader.
68 */
Madan Jampani620f70d2016-01-30 22:22:47 -080069 default Set<String> ownedTopics(NodeId nodeId) {
70 return Maps.filterValues(getLeaderBoard(), v -> Objects.equal(nodeId, v.leaderNodeId())).keySet();
71 }
Madan Jampani59610512015-02-25 15:25:43 -080072
73 /**
Madan Jampani620f70d2016-01-30 22:22:47 -080074 * Enters a leadership contest.
Sho SHIMIZU25d843c2015-04-10 16:52:33 -070075 *
Madan Jampani620f70d2016-01-30 22:22:47 -080076 * @param topic leadership topic
Madan Jampanide003d92015-05-11 17:14:20 -070077 * @return {@code Leadership} future
Pavlin Radoslavova6b754c2014-11-18 13:55:37 -080078 */
Madan Jampani620f70d2016-01-30 22:22:47 -080079 Leadership runForLeadership(String topic);
Pavlin Radoslavova6b754c2014-11-18 13:55:37 -080080
81 /**
Madan Jampani1d3494e2014-11-20 11:24:22 -080082 * Withdraws from a leadership contest.
Sho SHIMIZU25d843c2015-04-10 16:52:33 -070083 *
Madan Jampani620f70d2016-01-30 22:22:47 -080084 * @param topic leadership topic
Madan Jampani1d3494e2014-11-20 11:24:22 -080085 */
Madan Jampani620f70d2016-01-30 22:22:47 -080086 void withdraw(String topic);
Madan Jampani1af8e132015-04-30 16:41:18 -070087
88 /**
Madan Jampani59610512015-02-25 15:25:43 -080089 * Returns the current leader board.
Sho SHIMIZU25d843c2015-04-10 16:52:33 -070090 *
Madan Jampani59610512015-02-25 15:25:43 -080091 * @return mapping from topic to leadership info.
Madan Jampania4a59942016-05-02 11:25:34 -070092 * @deprecated 1.6.0 Goldeneye release. Replace usages with {@link #getLeadership(String)}
Madan Jampani59610512015-02-25 15:25:43 -080093 */
Madan Jampania4a59942016-05-02 11:25:34 -070094 @Deprecated
Yuta HIGUCHIc2bf3d82014-11-28 18:50:41 -080095 Map<String, Leadership> getLeaderBoard();
96
Madan Jampani1d3494e2014-11-20 11:24:22 -080097 /**
Madan Jampani620f70d2016-01-30 22:22:47 -080098 * Returns the candidate nodes for each topic.
Sho SHIMIZU25d843c2015-04-10 16:52:33 -070099 *
Madan Jampanifd45d5e2015-04-20 13:33:21 -0700100 * @return A mapping from topics to corresponding list of candidates.
Madan Jampania4a59942016-05-02 11:25:34 -0700101 * @deprecated 1.6.0 Goldeneye release. Replace usages with {@link #getLeadership(String)}
Ayaka Koshibec19b8b82015-04-08 15:18:24 -0700102 */
Madan Jampania4a59942016-05-02 11:25:34 -0700103 @Deprecated
Madan Jampani620f70d2016-01-30 22:22:47 -0800104 default Map<String, List<NodeId>> getCandidates() {
105 return ImmutableMap.copyOf(Maps.transformValues(getLeaderBoard(), v -> ImmutableList.copyOf(v.candidates())));
106 }
Ayaka Koshibec19b8b82015-04-08 15:18:24 -0700107
108 /**
Madan Jampani620f70d2016-01-30 22:22:47 -0800109 * Returns the candidate nodes for a given topic.
Sho SHIMIZU25d843c2015-04-10 16:52:33 -0700110 *
Madan Jampani620f70d2016-01-30 22:22:47 -0800111 * @param topic leadership topic
112 * @return A lists of {@link NodeId nodeIds}, which may be empty.
Ayaka Koshibec19b8b82015-04-08 15:18:24 -0700113 */
Madan Jampani620f70d2016-01-30 22:22:47 -0800114 default List<NodeId> getCandidates(String topic) {
115 Leadership leadership = getLeadership(topic);
116 return leadership == null ? ImmutableList.of() : ImmutableList.copyOf(leadership.candidates());
117 }
118}