Madan Jampani | 620f70d | 2016-01-30 22:22:47 -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 | 620f70d | 2016-01-30 22:22:47 -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.cluster.impl; |
| 17 | |
Ray Milkey | 3bceb01 | 2018-09-07 13:42:46 -0700 | [diff] [blame] | 18 | import com.google.common.base.Objects; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 19 | import com.google.common.collect.Maps; |
Madan Jampani | 620f70d | 2016-01-30 22:22:47 -0800 | [diff] [blame] | 20 | import org.onosproject.cluster.ClusterService; |
Madan Jampani | 620f70d | 2016-01-30 22:22:47 -0800 | [diff] [blame] | 21 | import org.onosproject.cluster.Leadership; |
| 22 | import org.onosproject.cluster.LeadershipAdminService; |
| 23 | import org.onosproject.cluster.LeadershipEvent; |
| 24 | import org.onosproject.cluster.LeadershipEventListener; |
| 25 | import org.onosproject.cluster.LeadershipService; |
| 26 | import org.onosproject.cluster.LeadershipStore; |
| 27 | import org.onosproject.cluster.LeadershipStoreDelegate; |
| 28 | import org.onosproject.cluster.NodeId; |
| 29 | import org.onosproject.event.AbstractListenerManager; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 30 | import org.osgi.service.component.annotations.Activate; |
| 31 | import org.osgi.service.component.annotations.Component; |
| 32 | import org.osgi.service.component.annotations.Deactivate; |
| 33 | import org.osgi.service.component.annotations.Reference; |
| 34 | import org.osgi.service.component.annotations.ReferenceCardinality; |
Madan Jampani | 620f70d | 2016-01-30 22:22:47 -0800 | [diff] [blame] | 35 | import org.slf4j.Logger; |
| 36 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 37 | import java.util.Map; |
| 38 | import java.util.Set; |
| 39 | |
| 40 | import static org.slf4j.LoggerFactory.getLogger; |
Madan Jampani | 620f70d | 2016-01-30 22:22:47 -0800 | [diff] [blame] | 41 | |
| 42 | /** |
| 43 | * Implementation of {@link LeadershipService} and {@link LeadershipAdminService}. |
| 44 | */ |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 45 | @Component(immediate = true, service = {LeadershipService.class, LeadershipAdminService.class}) |
Madan Jampani | 620f70d | 2016-01-30 22:22:47 -0800 | [diff] [blame] | 46 | public class LeadershipManager |
| 47 | extends AbstractListenerManager<LeadershipEvent, LeadershipEventListener> |
| 48 | implements LeadershipService, LeadershipAdminService { |
| 49 | |
| 50 | private final Logger log = getLogger(getClass()); |
| 51 | |
| 52 | private LeadershipStoreDelegate delegate = this::post; |
| 53 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 54 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Madan Jampani | 620f70d | 2016-01-30 22:22:47 -0800 | [diff] [blame] | 55 | protected ClusterService clusterService; |
| 56 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 57 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Madan Jampani | 620f70d | 2016-01-30 22:22:47 -0800 | [diff] [blame] | 58 | protected LeadershipStore store; |
| 59 | |
| 60 | private NodeId localNodeId; |
| 61 | |
Madan Jampani | 620f70d | 2016-01-30 22:22:47 -0800 | [diff] [blame] | 62 | @Activate |
| 63 | public void activate() { |
| 64 | localNodeId = clusterService.getLocalNode().id(); |
| 65 | store.setDelegate(delegate); |
| 66 | eventDispatcher.addSink(LeadershipEvent.class, listenerRegistry); |
Madan Jampani | 620f70d | 2016-01-30 22:22:47 -0800 | [diff] [blame] | 67 | log.info("Started"); |
| 68 | } |
| 69 | |
| 70 | @Deactivate |
| 71 | public void deactivate() { |
Madan Jampani | 620f70d | 2016-01-30 22:22:47 -0800 | [diff] [blame] | 72 | Maps.filterValues(store.getLeaderships(), v -> v.candidates().contains(localNodeId)) |
| 73 | .keySet() |
| 74 | .forEach(this::withdraw); |
| 75 | store.unsetDelegate(delegate); |
| 76 | eventDispatcher.removeSink(LeadershipEvent.class); |
| 77 | log.info("Stopped"); |
| 78 | } |
| 79 | |
| 80 | @Override |
| 81 | public Leadership getLeadership(String topic) { |
| 82 | return store.getLeadership(topic); |
| 83 | } |
| 84 | |
| 85 | @Override |
Ray Milkey | 3bceb01 | 2018-09-07 13:42:46 -0700 | [diff] [blame] | 86 | public Set<String> ownedTopics(NodeId nodeId) { |
| 87 | return Maps.filterValues(store.getLeaderships(), v -> Objects.equal(nodeId, v.leaderNodeId())).keySet(); |
| 88 | } |
| 89 | |
| 90 | @Override |
Madan Jampani | 620f70d | 2016-01-30 22:22:47 -0800 | [diff] [blame] | 91 | public Leadership runForLeadership(String topic) { |
| 92 | return store.addRegistration(topic); |
| 93 | } |
| 94 | |
| 95 | @Override |
| 96 | public void withdraw(String topic) { |
| 97 | store.removeRegistration(topic); |
| 98 | } |
| 99 | |
| 100 | @Override |
| 101 | public Map<String, Leadership> getLeaderBoard() { |
| 102 | return store.getLeaderships(); |
| 103 | } |
| 104 | |
| 105 | @Override |
pierventre | cdcd91c | 2021-05-06 09:27:00 +0200 | [diff] [blame] | 106 | public boolean demote(String topic, NodeId nodeId) { |
| 107 | return store.demote(topic, nodeId); |
| 108 | } |
| 109 | |
| 110 | @Override |
Madan Jampani | 620f70d | 2016-01-30 22:22:47 -0800 | [diff] [blame] | 111 | public boolean transferLeadership(String topic, NodeId to) { |
| 112 | return store.moveLeadership(topic, to); |
| 113 | } |
| 114 | |
| 115 | @Override |
| 116 | public void unregister(NodeId nodeId) { |
| 117 | store.removeRegistration(nodeId); |
| 118 | } |
| 119 | |
| 120 | @Override |
| 121 | public boolean promoteToTopOfCandidateList(String topic, NodeId nodeId) { |
| 122 | return store.makeTopCandidate(topic, nodeId); |
| 123 | } |
| 124 | } |