blob: 27268fb8f3a9b2c52df1b3f470081db60cf95e26 [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
Ray Milkey3bceb012018-09-07 13:42:46 -070018import com.google.common.collect.ImmutableList;
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070019import org.onosproject.event.ListenerService;
20
Madan Jampanifd45d5e2015-04-20 13:33:21 -070021import java.util.List;
Madan Jampani59610512015-02-25 15:25:43 -080022import java.util.Set;
Yuta HIGUCHIc2bf3d82014-11-28 18:50:41 -080023
Pavlin Radoslavova6b754c2014-11-18 13:55:37 -080024/**
Madan Jampani1d3494e2014-11-20 11:24:22 -080025 * Service for leader election.
Madan Jampani620f70d2016-01-30 22:22:47 -080026 * <p>
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.
Madan Jampani620f70d2016-01-30 22:22:47 -080029 * <p>
Madan Jampani1ee91782014-11-20 20:24:24 -080030 * Listeners can be added to receive notifications asynchronously for various
31 * leadership contests.
Madan Jampani620f70d2016-01-30 22:22:47 -080032 * <p>
33 * When a node gets elected as a leader for a topic, all nodes receive notifications
34 * indicating a change in leadership.
Pavlin Radoslavova6b754c2014-11-18 13:55:37 -080035 */
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070036public interface LeadershipService
37 extends ListenerService<LeadershipEvent, LeadershipEventListener> {
Pavlin Radoslavova6b754c2014-11-18 13:55:37 -080038
39 /**
Madan Jampani620f70d2016-01-30 22:22:47 -080040 * Returns the {@link NodeId node identifier} that is the current leader for a topic.
Sho SHIMIZU25d843c2015-04-10 16:52:33 -070041 *
Madan Jampani620f70d2016-01-30 22:22:47 -080042 * @param topic leadership topic
43 * @return node identifier of the current leader; {@code null} if there is no leader for the topic
Madan Jampani1ee91782014-11-20 20:24:24 -080044 */
Madan Jampani620f70d2016-01-30 22:22:47 -080045 default NodeId getLeader(String topic) {
46 Leadership leadership = getLeadership(topic);
47 return leadership == null ? null : leadership.leaderNodeId();
48 }
Madan Jampani1ee91782014-11-20 20:24:24 -080049
50 /**
Madan Jampani620f70d2016-01-30 22:22:47 -080051 * Returns the current {@link Leadership leadership} for a topic.
Sho SHIMIZU25d843c2015-04-10 16:52:33 -070052 *
Madan Jampani620f70d2016-01-30 22:22:47 -080053 * @param topic leadership topic
54 * @return leadership or {@code null} if no such topic exists
Madan Jampani59610512015-02-25 15:25:43 -080055 */
Madan Jampani620f70d2016-01-30 22:22:47 -080056 Leadership getLeadership(String topic);
Madan Jampani59610512015-02-25 15:25:43 -080057
58 /**
Madan Jampani620f70d2016-01-30 22:22:47 -080059 * Returns the set of topics owned by the specified {@link NodeId node}.
Sho SHIMIZU25d843c2015-04-10 16:52:33 -070060 *
Madan Jampani620f70d2016-01-30 22:22:47 -080061 * @param nodeId node identifier.
Madan Jampani59610512015-02-25 15:25:43 -080062 * @return set of topics for which this node is the current leader.
63 */
Ray Milkey3bceb012018-09-07 13:42:46 -070064 Set<String> ownedTopics(NodeId nodeId);
Madan Jampani59610512015-02-25 15:25:43 -080065
66 /**
Madan Jampani620f70d2016-01-30 22:22:47 -080067 * Enters a leadership contest.
Sho SHIMIZU25d843c2015-04-10 16:52:33 -070068 *
Madan Jampani620f70d2016-01-30 22:22:47 -080069 * @param topic leadership topic
Madan Jampanide003d92015-05-11 17:14:20 -070070 * @return {@code Leadership} future
Pavlin Radoslavova6b754c2014-11-18 13:55:37 -080071 */
Madan Jampani620f70d2016-01-30 22:22:47 -080072 Leadership runForLeadership(String topic);
Pavlin Radoslavova6b754c2014-11-18 13:55:37 -080073
74 /**
Madan Jampani1d3494e2014-11-20 11:24:22 -080075 * Withdraws from a leadership contest.
Sho SHIMIZU25d843c2015-04-10 16:52:33 -070076 *
Madan Jampani620f70d2016-01-30 22:22:47 -080077 * @param topic leadership topic
Madan Jampani1d3494e2014-11-20 11:24:22 -080078 */
Madan Jampani620f70d2016-01-30 22:22:47 -080079 void withdraw(String topic);
Madan Jampani1af8e132015-04-30 16:41:18 -070080
81 /**
Madan Jampani620f70d2016-01-30 22:22:47 -080082 * Returns the candidate nodes for a given topic.
Sho SHIMIZU25d843c2015-04-10 16:52:33 -070083 *
Madan Jampani620f70d2016-01-30 22:22:47 -080084 * @param topic leadership topic
85 * @return A lists of {@link NodeId nodeIds}, which may be empty.
Ayaka Koshibec19b8b82015-04-08 15:18:24 -070086 */
Madan Jampani620f70d2016-01-30 22:22:47 -080087 default List<NodeId> getCandidates(String topic) {
88 Leadership leadership = getLeadership(topic);
89 return leadership == null ? ImmutableList.of() : ImmutableList.copyOf(leadership.candidates());
90 }
91}