blob: 27c92ec176d0826516853e5693f07c032da28f9a [file] [log] [blame]
Madan Jampani27077eb2016-02-22 14:38:32 -08001/*
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 */
16package org.onosproject.store.primitives.impl;
17
18import static com.google.common.base.Preconditions.checkNotNull;
19
20import java.util.Collection;
21import java.util.Map;
22import java.util.TreeMap;
23import java.util.concurrent.CompletableFuture;
24import java.util.function.Consumer;
25
26import org.onosproject.cluster.Leadership;
27import org.onosproject.cluster.NodeId;
28import org.onosproject.cluster.PartitionId;
29import org.onosproject.event.Change;
30import org.onosproject.store.service.AsyncLeaderElector;
31
32import com.google.common.collect.Maps;
33
34/**
35 * {@link AsyncLeaderElector} that has its topics partitioned horizontally across
36 * several {@link AsyncLeaderElector leader electors}.
37 */
38public class PartitionedAsyncLeaderElector implements AsyncLeaderElector {
39
40 private final String name;
41 private final TreeMap<PartitionId, AsyncLeaderElector> partitions = Maps.newTreeMap();
42 private final Hasher<String> topicHasher;
43
44 public PartitionedAsyncLeaderElector(String name,
45 Map<PartitionId, AsyncLeaderElector> partitions,
46 Hasher<String> topicHasher) {
47 this.name = name;
48 this.partitions.putAll(checkNotNull(partitions));
49 this.topicHasher = checkNotNull(topicHasher);
50 }
51
52 @Override
53 public String name() {
54 return name;
55 }
56
57 @Override
58 public CompletableFuture<Leadership> run(String topic, NodeId nodeId) {
59 return getLeaderElector(topic).run(topic, nodeId);
60 }
61
62 @Override
63 public CompletableFuture<Void> withdraw(String topic) {
64 return getLeaderElector(topic).withdraw(topic);
65 }
66
67 @Override
68 public CompletableFuture<Boolean> anoint(String topic, NodeId nodeId) {
69 return getLeaderElector(topic).anoint(topic, nodeId);
70 }
71
72 @Override
Madan Jampani0c0cdc62016-02-22 16:54:06 -080073 public CompletableFuture<Boolean> promote(String topic, NodeId nodeId) {
74 return getLeaderElector(topic).promote(topic, nodeId);
75 }
76
77 @Override
78 public CompletableFuture<Void> evict(NodeId nodeId) {
79 return CompletableFuture.allOf(getLeaderElectors().stream()
80 .map(le -> le.evict(nodeId))
81 .toArray(CompletableFuture[]::new));
82 }
83
84 @Override
Madan Jampani27077eb2016-02-22 14:38:32 -080085 public CompletableFuture<Leadership> getLeadership(String topic) {
86 return getLeaderElector(topic).getLeadership(topic);
87 }
88
89 @Override
90 public CompletableFuture<Map<String, Leadership>> getLeaderships() {
91 Map<String, Leadership> leaderships = Maps.newConcurrentMap();
92 return CompletableFuture.allOf(getLeaderElectors().stream()
93 .map(le -> le.getLeaderships()
94 .thenAccept(m -> leaderships.putAll(m)))
95 .toArray(CompletableFuture[]::new))
96 .thenApply(v -> leaderships);
97 }
98
99 @Override
100 public CompletableFuture<Void> addChangeListener(Consumer<Change<Leadership>> listener) {
101 return CompletableFuture.allOf(getLeaderElectors().stream()
102 .map(map -> map.addChangeListener(listener))
103 .toArray(CompletableFuture[]::new));
104 }
105
106 @Override
107 public CompletableFuture<Void> removeChangeListener(Consumer<Change<Leadership>> listener) {
108 return CompletableFuture.allOf(getLeaderElectors().stream()
109 .map(map -> map.removeChangeListener(listener))
110 .toArray(CompletableFuture[]::new));
111 }
112
113 /**
114 * Returns the leaderElector (partition) to which the specified topic maps.
115 * @param topic topic name
116 * @return AsyncLeaderElector to which topic maps
117 */
118 private AsyncLeaderElector getLeaderElector(String topic) {
119 return partitions.get(topicHasher.hash(topic));
120 }
121
122 /**
123 * Returns all the constituent leader electors.
124 * @return collection of leader electors.
125 */
126 private Collection<AsyncLeaderElector> getLeaderElectors() {
127 return partitions.values();
128 }
129}