blob: 000d10f8526bffa535b15fe7c4a3b292ec8b3fcc [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;
25
26/**
27 * Distributed mutual exclusion primitive.
28 * <p>
29 * {@code AsyncLeaderElector} facilitates mutually exclusive access to a shared resource by various cluster members.
30 * Each resource is identified by a unique topic name and members register their desire to access the resource by
31 * calling the {@link AsyncLeaderElector#run run} method. Access is grated on a FIFO basis. An instance can
32 * unregister itself from the leadership election by calling {@link AsyncLeaderElector#withdraw withdraw} method.
33 * If an instance currently holding the resource dies then the next instance waiting to be leader (in FIFO order)
34 * will be automatically granted access to the resource.
35 * <p>
36 * One can register listeners to be notified when a leadership change occurs. The Listeners are notified via a
37 * {@link Leadership} {@link Change change} subject.
38 * <p>
39 * Additionally, {@code AsyncLeaderElector} provides methods to query the current state of leadership for topics.
40 * <p>
41 * All methods of this interface return a {@link CompletableFuture future} immediately after a successful invocation.
42 * The operation itself is executed asynchronous and the returned future will be
43 * {@link CompletableFuture#complete completed} when the operation finishes.
44 */
45public interface AsyncLeaderElector extends DistributedPrimitive {
46
47 @Override
48 default DistributedPrimitive.Type type() {
49 return DistributedPrimitive.Type.LEADER_ELECTOR;
50 }
51
52 /**
53 * Attempts to become leader for a topic.
54 * @param topic leadership topic
55 * @param nodeId instance identifier of the node
56 * @return CompletableFuture that is completed with the current Leadership state of the topic
57 */
58 CompletableFuture<Leadership> run(String topic, NodeId nodeId);
59
60 /**
61 * Withdraws from leadership race for a topic.
62 * @param topic leadership topic
63 * @return CompletableFuture that is completed when the withdraw is done
64 */
65 CompletableFuture<Void> withdraw(String topic);
66
67 /**
68 * Attempts to promote a node to leadership displacing the current leader.
69 * @param topic leadership topic
70 * @param nodeId instance identifier of the new leader
71 * @return CompletableFuture that is completed with a boolean when the operation is done. Boolean is true if
72 * leadership transfer was successfully executed; false if it failed. This operation can fail (i.e. return false)
73 * if the node to be made new leader is not registering to run for election for the topic.
74 */
75 CompletableFuture<Boolean> anoint(String topic, NodeId nodeId);
76
77 /**
78 * Returns the {@link Leadership} for the specified topic.
79 * @param topic leadership topic
80 * @return CompletableFuture that is completed with the current Leadership state of the topic
81 */
82 CompletableFuture<Leadership> getLeadership(String topic);
83
84 /**
85 * Returns the current {@link Leadership}s for all topics.
86 * @return CompletableFuture that is completed with the topic to Leadership mapping
87 */
88 CompletableFuture<Map<String, Leadership>> getLeaderships();
89
90 /**
91 * Registers a listener to be notified of Leadership changes for all topics.
92 * @param consumer listener to notify
93 * @return CompletableFuture that is completed when the operation completes
94 */
95 CompletableFuture<Void> addChangeListener(Consumer<Change<Leadership>> consumer);
96
97 /**
98 * Unregisters a previously registered change notification listener.
99 * <p>
100 * If the specified listener was not previously registered, this operation will be a noop.
101 * @param consumer listener to remove
102 * @return CompletableFuture that is completed when the operation completes
103 */
104 CompletableFuture<Void> removeChangeListener(Consumer<Change<Leadership>> consumer);
105}