Jordan Halterman | 00e92da | 2018-05-22 23:05:52 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 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.primitives.impl; |
| 17 | |
| 18 | import java.util.Map; |
| 19 | import java.util.concurrent.CompletableFuture; |
| 20 | import java.util.function.Consumer; |
| 21 | import java.util.stream.Collectors; |
| 22 | |
| 23 | import com.google.common.collect.Maps; |
| 24 | import io.atomix.core.election.LeadershipEventListener; |
| 25 | import org.onosproject.cluster.Leader; |
| 26 | import org.onosproject.cluster.Leadership; |
| 27 | import org.onosproject.cluster.NodeId; |
| 28 | import org.onosproject.event.Change; |
| 29 | import org.onosproject.store.service.AsyncLeaderElector; |
| 30 | |
| 31 | /** |
| 32 | * Atomix leader elector. |
| 33 | */ |
| 34 | public class AtomixLeaderElector implements AsyncLeaderElector { |
| 35 | private final io.atomix.core.election.AsyncLeaderElector<NodeId> atomixElector; |
| 36 | private final NodeId localNodeId; |
| 37 | private final Map<Consumer<Change<Leadership>>, LeadershipEventListener<NodeId>> listenerMap = |
| 38 | Maps.newIdentityHashMap(); |
| 39 | |
| 40 | public AtomixLeaderElector(io.atomix.core.election.AsyncLeaderElector<NodeId> atomixElector, NodeId localNodeId) { |
| 41 | this.atomixElector = atomixElector; |
| 42 | this.localNodeId = localNodeId; |
| 43 | } |
| 44 | |
| 45 | @Override |
| 46 | public String name() { |
| 47 | return atomixElector.name(); |
| 48 | } |
| 49 | |
| 50 | @Override |
| 51 | public CompletableFuture<Leadership> run(String topic, NodeId nodeId) { |
| 52 | return atomixElector.run(topic, nodeId).thenApply(leadership -> toLeadership(topic, leadership)); |
| 53 | } |
| 54 | |
| 55 | @Override |
| 56 | public CompletableFuture<Void> withdraw(String topic) { |
| 57 | return atomixElector.withdraw(topic, localNodeId); |
| 58 | } |
| 59 | |
| 60 | @Override |
| 61 | public CompletableFuture<Boolean> anoint(String topic, NodeId nodeId) { |
| 62 | return atomixElector.anoint(topic, nodeId); |
| 63 | } |
| 64 | |
| 65 | @Override |
| 66 | public CompletableFuture<Void> evict(NodeId nodeId) { |
| 67 | return atomixElector.evict(nodeId); |
| 68 | } |
| 69 | |
| 70 | @Override |
| 71 | public CompletableFuture<Boolean> promote(String topic, NodeId nodeId) { |
| 72 | return atomixElector.promote(topic, nodeId); |
| 73 | } |
| 74 | |
| 75 | @Override |
| 76 | public CompletableFuture<Leadership> getLeadership(String topic) { |
| 77 | return atomixElector.getLeadership(topic).thenApply(leadership -> toLeadership(topic, leadership)); |
| 78 | } |
| 79 | |
| 80 | @Override |
| 81 | public CompletableFuture<Map<String, Leadership>> getLeaderships() { |
| 82 | return atomixElector.getLeaderships() |
| 83 | .thenApply(leaderships -> leaderships.entrySet().stream() |
| 84 | .collect(Collectors.toMap(e -> e.getKey(), e -> toLeadership(e.getKey(), e.getValue())))); |
| 85 | } |
| 86 | |
| 87 | @Override |
| 88 | public CompletableFuture<Void> addChangeListener(Consumer<Change<Leadership>> consumer) { |
| 89 | LeadershipEventListener<NodeId> atomixListener = event -> |
| 90 | consumer.accept(new Change<>( |
| 91 | toLeadership(event.topic(), event.oldLeadership()), |
| 92 | toLeadership(event.topic(), event.newLeadership()))); |
| 93 | listenerMap.put(consumer, atomixListener); |
| 94 | return atomixElector.addListener(atomixListener); |
| 95 | } |
| 96 | |
| 97 | @Override |
| 98 | public CompletableFuture<Void> removeChangeListener(Consumer<Change<Leadership>> consumer) { |
| 99 | LeadershipEventListener<NodeId> atomixListener = listenerMap.remove(consumer); |
| 100 | if (atomixListener != null) { |
| 101 | return atomixElector.removeListener(atomixListener); |
| 102 | } |
| 103 | return CompletableFuture.completedFuture(null); |
| 104 | } |
| 105 | |
| 106 | private Leadership toLeadership(String topic, io.atomix.core.election.Leadership<NodeId> leadership) { |
| 107 | return leadership != null |
| 108 | ? new Leadership(topic, toLeader(leadership.leader()), leadership.candidates()) |
| 109 | : null; |
| 110 | } |
| 111 | |
| 112 | private Leader toLeader(io.atomix.core.election.Leader<NodeId> leader) { |
| 113 | return leader != null ? new Leader(leader.id(), leader.term(), leader.timestamp()) : null; |
| 114 | } |
| 115 | } |