Madan Jampani | 47b80ba | 2016-01-31 23:05:55 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Madan Jampani | 47b80ba | 2016-01-31 23:05:55 -0800 | [diff] [blame] | 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 |
Madan Jampani | 39fff10 | 2016-02-14 13:17:28 -0800 | [diff] [blame] | 32 | default DistributedPrimitive.Type primitiveType() { |
Madan Jampani | 47b80ba | 2016-01-31 23:05:55 -0800 | [diff] [blame] | 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 | /** |
Madan Jampani | 0c0cdc6 | 2016-02-22 16:54:06 -0800 | [diff] [blame] | 61 | * 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 Jampani | 47b80ba | 2016-01-31 23:05:55 -0800 | [diff] [blame] | 80 | * 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 | } |