Leader elector distributed primitive interfaces
Change-Id: Ieebf5ddfcf8c5c6a55e4d6d328829cc8dbeef38a
Showing
3 changed files
with
200 additions
and
0 deletions
| ... | @@ -19,6 +19,7 @@ import org.onosproject.store.service.AsyncAtomicCounter; | ... | @@ -19,6 +19,7 @@ import org.onosproject.store.service.AsyncAtomicCounter; |
| 19 | import org.onosproject.store.service.AsyncAtomicValue; | 19 | import org.onosproject.store.service.AsyncAtomicValue; |
| 20 | import org.onosproject.store.service.AsyncConsistentMap; | 20 | import org.onosproject.store.service.AsyncConsistentMap; |
| 21 | import org.onosproject.store.service.AsyncDistributedSet; | 21 | import org.onosproject.store.service.AsyncDistributedSet; |
| 22 | +import org.onosproject.store.service.AsyncLeaderElector; | ||
| 22 | import org.onosproject.store.service.DistributedQueue; | 23 | import org.onosproject.store.service.DistributedQueue; |
| 23 | import org.onosproject.store.service.Serializer; | 24 | import org.onosproject.store.service.Serializer; |
| 24 | 25 | ||
| ... | @@ -75,4 +76,12 @@ public interface DistributedPrimitiveCreator { | ... | @@ -75,4 +76,12 @@ public interface DistributedPrimitiveCreator { |
| 75 | * @return set | 76 | * @return set |
| 76 | */ | 77 | */ |
| 77 | <E> AsyncDistributedSet<E> newAsyncDistributedSet(String name, Serializer serializer); | 78 | <E> AsyncDistributedSet<E> newAsyncDistributedSet(String name, Serializer serializer); |
| 79 | + | ||
| 80 | + /** | ||
| 81 | + * Creates a new {@code AsyncLeaderElector}. | ||
| 82 | + * | ||
| 83 | + * @param name leader elector name | ||
| 84 | + * @return leader elector | ||
| 85 | + */ | ||
| 86 | + AsyncLeaderElector newAsyncLeaderElector(String name); | ||
| 78 | } | 87 | } |
| ... | \ No newline at end of file | ... | \ No newline at end of file | ... | ... |
| 1 | +/* | ||
| 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 | + */ | ||
| 16 | +package org.onosproject.store.service; | ||
| 17 | + | ||
| 18 | +import java.util.Map; | ||
| 19 | +import java.util.concurrent.CompletableFuture; | ||
| 20 | +import java.util.function.Consumer; | ||
| 21 | + | ||
| 22 | +import org.onosproject.cluster.Leadership; | ||
| 23 | +import org.onosproject.cluster.NodeId; | ||
| 24 | +import 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 | + */ | ||
| 45 | +public 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 | +} |
| 1 | +/* | ||
| 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 | + */ | ||
| 16 | +package org.onosproject.store.service; | ||
| 17 | + | ||
| 18 | +import java.util.Map; | ||
| 19 | +import java.util.function.Consumer; | ||
| 20 | + | ||
| 21 | +import org.onosproject.cluster.Leadership; | ||
| 22 | +import org.onosproject.cluster.NodeId; | ||
| 23 | +import 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 | + */ | ||
| 29 | +public interface LeaderElector extends DistributedPrimitive { | ||
| 30 | + | ||
| 31 | + @Override | ||
| 32 | + default DistributedPrimitive.Type type() { | ||
| 33 | + 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 | + /** | ||
| 61 | + * Returns the {@link Leadership} for the specified topic. | ||
| 62 | + * @param topic leadership topic | ||
| 63 | + * @return current Leadership state of the topic | ||
| 64 | + */ | ||
| 65 | + Leadership getLeadership(String topic); | ||
| 66 | + | ||
| 67 | + /** | ||
| 68 | + * Returns the current {@link Leadership}s for all topics. | ||
| 69 | + * @return topic name to Leadership mapping | ||
| 70 | + */ | ||
| 71 | + Map<String, Leadership> getLeaderships(); | ||
| 72 | + | ||
| 73 | + /** | ||
| 74 | + * Registers a listener to be notified of Leadership changes for all topics. | ||
| 75 | + * @param consumer listener to add | ||
| 76 | + */ | ||
| 77 | + void addChangeListener(Consumer<Change<Leadership>> consumer); | ||
| 78 | + | ||
| 79 | + /** | ||
| 80 | + * Unregisters a previously registered change notification listener. | ||
| 81 | + * <p> | ||
| 82 | + * If the specified listener was not previously registered, this operation will be a noop. | ||
| 83 | + * @param consumer listener to remove | ||
| 84 | + */ | ||
| 85 | + void removeChangeListener(Consumer<Change<Leadership>> consumer); | ||
| 86 | +} |
-
Please register or login to post a comment