Madan Jampani | 47b80ba | 2016-01-31 23:05:55 -0800 | [diff] [blame] | 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 |
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 | /** |
| 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 | } |