alshabib | ab98466 | 2014-12-04 18:56:18 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
alshabib | ab98466 | 2014-12-04 18:56:18 -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 | */ |
Thomas Vachuska | c97aa61 | 2015-06-23 16:00:18 -0700 | [diff] [blame] | 16 | package org.onosproject.store.trivial; |
Jonathan Hart | 949c284 | 2014-11-28 23:44:09 -0800 | [diff] [blame] | 17 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 18 | import org.onosproject.cluster.ClusterService; |
| 19 | import org.onosproject.cluster.Leader; |
| 20 | import org.onosproject.cluster.Leadership; |
| 21 | import org.onosproject.cluster.LeadershipEvent; |
| 22 | import org.onosproject.cluster.LeadershipEvent.Type; |
| 23 | import org.onosproject.cluster.LeadershipEventListener; |
| 24 | import org.onosproject.cluster.LeadershipService; |
| 25 | import org.onosproject.cluster.NodeId; |
| 26 | import org.osgi.service.component.annotations.Activate; |
| 27 | import org.osgi.service.component.annotations.Component; |
| 28 | import org.osgi.service.component.annotations.Reference; |
| 29 | import org.osgi.service.component.annotations.ReferenceCardinality; |
Madan Jampani | 5961051 | 2015-02-25 15:25:43 -0800 | [diff] [blame] | 30 | |
Madan Jampani | 620f70d | 2016-01-30 22:22:47 -0800 | [diff] [blame] | 31 | import java.util.Arrays; |
Ayaka Koshibe | c19b8b8 | 2015-04-08 15:18:24 -0700 | [diff] [blame] | 32 | import java.util.List; |
Jonathan Hart | 949c284 | 2014-11-28 23:44:09 -0800 | [diff] [blame] | 33 | import java.util.Map; |
Madan Jampani | 5961051 | 2015-02-25 15:25:43 -0800 | [diff] [blame] | 34 | import java.util.Map.Entry; |
Jonathan Hart | 949c284 | 2014-11-28 23:44:09 -0800 | [diff] [blame] | 35 | import java.util.Set; |
| 36 | import java.util.concurrent.ConcurrentHashMap; |
| 37 | import java.util.concurrent.CopyOnWriteArraySet; |
Madan Jampani | 5961051 | 2015-02-25 15:25:43 -0800 | [diff] [blame] | 38 | import java.util.stream.Collectors; |
Jonathan Hart | 949c284 | 2014-11-28 23:44:09 -0800 | [diff] [blame] | 39 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 40 | import static com.google.common.base.Preconditions.checkArgument; |
Jonathan Hart | 949c284 | 2014-11-28 23:44:09 -0800 | [diff] [blame] | 41 | |
| 42 | /** |
| 43 | * A trivial implementation of the leadership service. |
alshabib | a9819bf | 2014-11-30 18:15:52 -0800 | [diff] [blame] | 44 | * <p> |
Jonathan Hart | 949c284 | 2014-11-28 23:44:09 -0800 | [diff] [blame] | 45 | * The service is not distributed, so it can assume there's a single leadership |
| 46 | * contender. This contender is always granted leadership whenever it asks. |
| 47 | */ |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 48 | @Component(immediate = true, service = LeadershipService.class) |
Jonathan Hart | 949c284 | 2014-11-28 23:44:09 -0800 | [diff] [blame] | 49 | public class SimpleLeadershipManager implements LeadershipService { |
| 50 | |
| 51 | private Set<LeadershipEventListener> listeners = new CopyOnWriteArraySet<>(); |
| 52 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 53 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Jonathan Hart | 949c284 | 2014-11-28 23:44:09 -0800 | [diff] [blame] | 54 | private ClusterService clusterService; |
| 55 | |
Madan Jampani | 620f70d | 2016-01-30 22:22:47 -0800 | [diff] [blame] | 56 | private NodeId localNodeId; |
| 57 | |
Jonathan Hart | 949c284 | 2014-11-28 23:44:09 -0800 | [diff] [blame] | 58 | private Map<String, Boolean> elections = new ConcurrentHashMap<>(); |
| 59 | |
Madan Jampani | 620f70d | 2016-01-30 22:22:47 -0800 | [diff] [blame] | 60 | @Activate |
| 61 | public void activate() { |
| 62 | localNodeId = clusterService.getLocalNode().id(); |
| 63 | } |
| 64 | |
Jonathan Hart | 949c284 | 2014-11-28 23:44:09 -0800 | [diff] [blame] | 65 | @Override |
Madan Jampani | 8d21c79 | 2014-12-01 16:31:07 -0800 | [diff] [blame] | 66 | public NodeId getLeader(String path) { |
| 67 | return elections.get(path) ? clusterService.getLocalNode().id() : null; |
Jonathan Hart | 949c284 | 2014-11-28 23:44:09 -0800 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | @Override |
Madan Jampani | 5961051 | 2015-02-25 15:25:43 -0800 | [diff] [blame] | 71 | public Leadership getLeadership(String path) { |
| 72 | checkArgument(path != null); |
Madan Jampani | 620f70d | 2016-01-30 22:22:47 -0800 | [diff] [blame] | 73 | return elections.get(path) ? |
| 74 | new Leadership(path, new Leader(localNodeId, 0, 0), Arrays.asList(localNodeId)) : null; |
Madan Jampani | 5961051 | 2015-02-25 15:25:43 -0800 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | @Override |
| 78 | public Set<String> ownedTopics(NodeId nodeId) { |
| 79 | checkArgument(nodeId != null); |
| 80 | return elections.entrySet() |
| 81 | .stream() |
| 82 | .filter(Entry::getValue) |
| 83 | .map(Entry::getKey) |
| 84 | .collect(Collectors.toSet()); |
| 85 | } |
| 86 | |
| 87 | @Override |
Madan Jampani | 620f70d | 2016-01-30 22:22:47 -0800 | [diff] [blame] | 88 | public Leadership runForLeadership(String path) { |
Jonathan Hart | 949c284 | 2014-11-28 23:44:09 -0800 | [diff] [blame] | 89 | elections.put(path, true); |
Madan Jampani | 620f70d | 2016-01-30 22:22:47 -0800 | [diff] [blame] | 90 | Leadership leadership = new Leadership(path, new Leader(localNodeId, 0, 0), Arrays.asList(localNodeId)); |
Jonathan Hart | 949c284 | 2014-11-28 23:44:09 -0800 | [diff] [blame] | 91 | for (LeadershipEventListener listener : listeners) { |
Madan Jampani | 620f70d | 2016-01-30 22:22:47 -0800 | [diff] [blame] | 92 | listener.event(new LeadershipEvent(Type.LEADER_AND_CANDIDATES_CHANGED, leadership)); |
Jonathan Hart | 949c284 | 2014-11-28 23:44:09 -0800 | [diff] [blame] | 93 | } |
Madan Jampani | 620f70d | 2016-01-30 22:22:47 -0800 | [diff] [blame] | 94 | return leadership; |
Jonathan Hart | 949c284 | 2014-11-28 23:44:09 -0800 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | @Override |
Madan Jampani | 620f70d | 2016-01-30 22:22:47 -0800 | [diff] [blame] | 98 | public void withdraw(String path) { |
Jonathan Hart | 949c284 | 2014-11-28 23:44:09 -0800 | [diff] [blame] | 99 | elections.remove(path); |
| 100 | for (LeadershipEventListener listener : listeners) { |
Madan Jampani | 620f70d | 2016-01-30 22:22:47 -0800 | [diff] [blame] | 101 | listener.event(new LeadershipEvent(Type.LEADER_AND_CANDIDATES_CHANGED, |
| 102 | new Leadership(path, null, Arrays.asList()))); |
Jonathan Hart | 949c284 | 2014-11-28 23:44:09 -0800 | [diff] [blame] | 103 | } |
| 104 | } |
| 105 | |
| 106 | @Override |
Jonathan Hart | 949c284 | 2014-11-28 23:44:09 -0800 | [diff] [blame] | 107 | public void addListener(LeadershipEventListener listener) { |
| 108 | listeners.add(listener); |
| 109 | } |
| 110 | |
| 111 | @Override |
| 112 | public void removeListener(LeadershipEventListener listener) { |
| 113 | listeners.remove(listener); |
| 114 | } |
Ayaka Koshibe | c19b8b8 | 2015-04-08 15:18:24 -0700 | [diff] [blame] | 115 | |
| 116 | @Override |
Ayaka Koshibe | c19b8b8 | 2015-04-08 15:18:24 -0700 | [diff] [blame] | 117 | public List<NodeId> getCandidates(String path) { |
| 118 | return null; |
| 119 | } |
Jonathan Hart | 949c284 | 2014-11-28 23:44:09 -0800 | [diff] [blame] | 120 | } |