blob: e364a7eb578f1de2be512a866b4b78d12fd769fe [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
73 public CompletableFuture<Leadership> getLeadership(String topic) {
74 return getLeaderElector(topic).getLeadership(topic);
75 }
76
77 @Override
78 public CompletableFuture<Map<String, Leadership>> getLeaderships() {
79 Map<String, Leadership> leaderships = Maps.newConcurrentMap();
80 return CompletableFuture.allOf(getLeaderElectors().stream()
81 .map(le -> le.getLeaderships()
82 .thenAccept(m -> leaderships.putAll(m)))
83 .toArray(CompletableFuture[]::new))
84 .thenApply(v -> leaderships);
85 }
86
87 @Override
88 public CompletableFuture<Void> addChangeListener(Consumer<Change<Leadership>> listener) {
89 return CompletableFuture.allOf(getLeaderElectors().stream()
90 .map(map -> map.addChangeListener(listener))
91 .toArray(CompletableFuture[]::new));
92 }
93
94 @Override
95 public CompletableFuture<Void> removeChangeListener(Consumer<Change<Leadership>> listener) {
96 return CompletableFuture.allOf(getLeaderElectors().stream()
97 .map(map -> map.removeChangeListener(listener))
98 .toArray(CompletableFuture[]::new));
99 }
100
101 /**
102 * Returns the leaderElector (partition) to which the specified topic maps.
103 * @param topic topic name
104 * @return AsyncLeaderElector to which topic maps
105 */
106 private AsyncLeaderElector getLeaderElector(String topic) {
107 return partitions.get(topicHasher.hash(topic));
108 }
109
110 /**
111 * Returns all the constituent leader electors.
112 * @return collection of leader electors.
113 */
114 private Collection<AsyncLeaderElector> getLeaderElectors() {
115 return partitions.values();
116 }
117}