blob: 3aed8d2ab3eecdd32e0972c6892652d9393e455a [file] [log] [blame]
Madan Jampani47b80ba2016-01-31 23:05:55 -08001/*
2 * Copyright 2016 Open Networking Laboratory
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 */
16package org.onosproject.store.service;
17
18import java.util.Map;
19import java.util.concurrent.CompletableFuture;
20import java.util.function.Consumer;
21
22import org.onosproject.cluster.Leadership;
23import org.onosproject.cluster.NodeId;
24import org.onosproject.event.Change;
Madan Jampani39fff102016-02-14 13:17:28 -080025import org.onosproject.store.primitives.DefaultLeaderElector;
Madan Jampani47b80ba2016-01-31 23:05:55 -080026
27/**
28 * Distributed mutual exclusion primitive.
29 * <p>
30 * {@code AsyncLeaderElector} facilitates mutually exclusive access to a shared resource by various cluster members.
31 * Each resource is identified by a unique topic name and members register their desire to access the resource by
32 * calling the {@link AsyncLeaderElector#run run} method. Access is grated on a FIFO basis. An instance can
33 * unregister itself from the leadership election by calling {@link AsyncLeaderElector#withdraw withdraw} method.
34 * If an instance currently holding the resource dies then the next instance waiting to be leader (in FIFO order)
35 * will be automatically granted access to the resource.
36 * <p>
37 * One can register listeners to be notified when a leadership change occurs. The Listeners are notified via a
38 * {@link Leadership} {@link Change change} subject.
39 * <p>
40 * Additionally, {@code AsyncLeaderElector} provides methods to query the current state of leadership for topics.
41 * <p>
42 * All methods of this interface return a {@link CompletableFuture future} immediately after a successful invocation.
43 * The operation itself is executed asynchronous and the returned future will be
44 * {@link CompletableFuture#complete completed} when the operation finishes.
45 */
46public interface AsyncLeaderElector extends DistributedPrimitive {
47
48 @Override
Madan Jampani39fff102016-02-14 13:17:28 -080049 default DistributedPrimitive.Type primitiveType() {
Madan Jampani47b80ba2016-01-31 23:05:55 -080050 return DistributedPrimitive.Type.LEADER_ELECTOR;
51 }
52
53 /**
54 * Attempts to become leader for a topic.
55 * @param topic leadership topic
56 * @param nodeId instance identifier of the node
57 * @return CompletableFuture that is completed with the current Leadership state of the topic
58 */
59 CompletableFuture<Leadership> run(String topic, NodeId nodeId);
60
61 /**
62 * Withdraws from leadership race for a topic.
63 * @param topic leadership topic
64 * @return CompletableFuture that is completed when the withdraw is done
65 */
66 CompletableFuture<Void> withdraw(String topic);
67
68 /**
69 * Attempts to promote a node to leadership displacing the current leader.
70 * @param topic leadership topic
71 * @param nodeId instance identifier of the new leader
72 * @return CompletableFuture that is completed with a boolean when the operation is done. Boolean is true if
73 * leadership transfer was successfully executed; false if it failed. This operation can fail (i.e. return false)
74 * if the node to be made new leader is not registering to run for election for the topic.
75 */
76 CompletableFuture<Boolean> anoint(String topic, NodeId nodeId);
77
78 /**
79 * Returns the {@link Leadership} for the specified topic.
80 * @param topic leadership topic
81 * @return CompletableFuture that is completed with the current Leadership state of the topic
82 */
83 CompletableFuture<Leadership> getLeadership(String topic);
84
85 /**
86 * Returns the current {@link Leadership}s for all topics.
87 * @return CompletableFuture that is completed with the topic to Leadership mapping
88 */
89 CompletableFuture<Map<String, Leadership>> getLeaderships();
90
91 /**
92 * Registers a listener to be notified of Leadership changes for all topics.
93 * @param consumer listener to notify
94 * @return CompletableFuture that is completed when the operation completes
95 */
96 CompletableFuture<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 * @return CompletableFuture that is completed when the operation completes
104 */
105 CompletableFuture<Void> removeChangeListener(Consumer<Change<Leadership>> consumer);
Madan Jampani39fff102016-02-14 13:17:28 -0800106
107 /**
108 * Returns a new {@link LeaderElector} that is backed by this instance.
109 *
110 * @param timeoutMillis timeout duration for the returned LeaderElector operations
111 * @return new {@code LeaderElector} instance
112 */
113 default LeaderElector asLeaderElector(long timeoutMillis) {
114 return new DefaultLeaderElector(this, timeoutMillis);
115 }
116
117 /**
118 * Returns a new {@link LeaderElector} that is backed by this instance and with a default operation timeout.
119 *
120 * @return new {@code LeaderElector} instance
121 */
122 default LeaderElector asLeaderElector() {
Madan Jampani3a9911c2016-02-21 11:25:45 -0800123 return asLeaderElector(Long.MAX_VALUE);
Madan Jampani39fff102016-02-14 13:17:28 -0800124 }
Madan Jampani47b80ba2016-01-31 23:05:55 -0800125}