blob: f55164144f260d6eba4000062c0b56e11ea275b4 [file] [log] [blame]
Madan Jampani47b80ba2016-01-31 23:05:55 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Madan Jampani47b80ba2016-01-31 23:05:55 -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 */
16package org.onosproject.store.service;
17
18import java.util.Map;
19import java.util.function.Consumer;
20
21import org.onosproject.cluster.Leadership;
22import org.onosproject.cluster.NodeId;
23import org.onosproject.event.Change;
24
25/**
26 * {@code LeaderElector} provides the same functionality as {@link AsyncLeaderElector} with
27 * the only difference that all its methods block until the corresponding operation completes.
28 */
29public interface LeaderElector extends DistributedPrimitive {
30
31 @Override
Madan Jampani39fff102016-02-14 13:17:28 -080032 default DistributedPrimitive.Type primitiveType() {
Madan Jampani47b80ba2016-01-31 23:05:55 -080033 return DistributedPrimitive.Type.LEADER_ELECTOR;
34 }
35
36 /**
37 * Attempts to become leader for a topic.
38 * @param topic leadership topic
39 * @param nodeId instance identifier of the node
40 * @return current Leadership state of the topic
41 */
42 Leadership run(String topic, NodeId nodeId);
43
44 /**
45 * Withdraws from leadership race for a topic.
46 * @param topic leadership topic
47 */
48 void withdraw(String topic);
49
50 /**
51 * Attempts to promote a node to leadership displacing the current leader.
52 * @param topic leadership topic
53 * @param nodeId instance identifier of the new leader
54 * @return {@code true} if leadership transfer was successfully executed; {@code false} if it failed.
55 * This operation can return {@code false} if the node to be made new leader is not registered to
56 * run for election for the topic.
57 */
58 boolean anoint(String topic, NodeId nodeId);
59
60 /**
Madan Jampani0c0cdc62016-02-22 16:54:06 -080061 * Attempts to promote a node to top of candidate list.
62 *
63 * @param topic leadership topic
64 * @param nodeId instance identifier of the new top candidate
65 * @return {@code true} if node is now the top candidate. This operation can fail (i.e. return
66 * {@code false}) if the node is not registered to run for election for the topic.
67 */
68 boolean promote(String topic, NodeId nodeId);
69
70 /**
71 * Attempts to evict a node from all leadership elections it is registered for.
72 * <p>
73 * If the node the current leader for a topic, this call will force the next candidate (if one exists)
74 * to be promoted to leadership.
75 * @param nodeId node instance identifier
76 */
77 void evict(NodeId nodeId);
78
79 /**
Madan Jampani47b80ba2016-01-31 23:05:55 -080080 * Returns the {@link Leadership} for the specified topic.
81 * @param topic leadership topic
82 * @return current Leadership state of the topic
83 */
84 Leadership getLeadership(String topic);
85
86 /**
87 * Returns the current {@link Leadership}s for all topics.
88 * @return topic name to Leadership mapping
89 */
90 Map<String, Leadership> getLeaderships();
91
92 /**
93 * Registers a listener to be notified of Leadership changes for all topics.
94 * @param consumer listener to add
95 */
96 void addChangeListener(Consumer<Change<Leadership>> consumer);
97
98 /**
99 * Unregisters a previously registered change notification listener.
100 * <p>
101 * If the specified listener was not previously registered, this operation will be a noop.
102 * @param consumer listener to remove
103 */
104 void removeChangeListener(Consumer<Change<Leadership>> consumer);
105}