Jonathan Hart | 74c8313 | 2015-02-02 18:37:57 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Jonathan Hart | 74c8313 | 2015-02-02 18:37:57 -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.store.intent.impl; |
| 17 | |
Jonathan Hart | 74c8313 | 2015-02-02 18:37:57 -0800 | [diff] [blame] | 18 | import org.onosproject.cluster.ClusterService; |
| 19 | import org.onosproject.cluster.Leadership; |
| 20 | import org.onosproject.cluster.LeadershipEvent; |
| 21 | import org.onosproject.cluster.LeadershipEventListener; |
| 22 | import org.onosproject.cluster.LeadershipService; |
Brian O'Connor | 5eb77c8 | 2015-03-02 18:09:39 -0800 | [diff] [blame] | 23 | import org.onosproject.cluster.NodeId; |
Brian O'Connor | 69d6ac7 | 2015-05-29 16:24:06 -0700 | [diff] [blame] | 24 | import org.onosproject.event.EventDeliveryService; |
| 25 | import org.onosproject.event.ListenerRegistry; |
Madan Jampani | 3b8101a | 2016-09-15 13:22:01 -0700 | [diff] [blame] | 26 | import org.onosproject.net.intent.WorkPartitionEvent; |
| 27 | import org.onosproject.net.intent.WorkPartitionEventListener; |
| 28 | import org.onosproject.net.intent.WorkPartitionService; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 29 | import org.osgi.service.component.annotations.Activate; |
| 30 | import org.osgi.service.component.annotations.Component; |
| 31 | import org.osgi.service.component.annotations.Deactivate; |
| 32 | import org.osgi.service.component.annotations.Reference; |
| 33 | import org.osgi.service.component.annotations.ReferenceCardinality; |
Jonathan Hart | 74c8313 | 2015-02-02 18:37:57 -0800 | [diff] [blame] | 34 | import org.slf4j.Logger; |
| 35 | import org.slf4j.LoggerFactory; |
| 36 | |
Jonathan Hart | dc9d7b8 | 2015-02-22 17:59:50 -0800 | [diff] [blame] | 37 | import java.util.List; |
Brian O'Connor | 5eb77c8 | 2015-03-02 18:09:39 -0800 | [diff] [blame] | 38 | import java.util.Objects; |
Jonathan Hart | f2fda81 | 2015-02-17 15:21:03 -0800 | [diff] [blame] | 39 | import java.util.concurrent.Executors; |
| 40 | import java.util.concurrent.ScheduledExecutorService; |
| 41 | import java.util.concurrent.TimeUnit; |
Madan Jampani | 4732c1b | 2015-05-19 17:11:50 -0700 | [diff] [blame] | 42 | import java.util.concurrent.atomic.AtomicBoolean; |
Madan Jampani | 3b8101a | 2016-09-15 13:22:01 -0700 | [diff] [blame] | 43 | import java.util.function.Function; |
Jonathan Hart | dc9d7b8 | 2015-02-22 17:59:50 -0800 | [diff] [blame] | 44 | import java.util.stream.Collectors; |
Madan Jampani | a4a5994 | 2016-05-02 11:25:34 -0700 | [diff] [blame] | 45 | import java.util.stream.IntStream; |
Jonathan Hart | 74c8313 | 2015-02-02 18:37:57 -0800 | [diff] [blame] | 46 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 47 | import static org.onlab.util.Tools.groupedThreads; |
| 48 | |
Jonathan Hart | 74c8313 | 2015-02-02 18:37:57 -0800 | [diff] [blame] | 49 | /** |
Madan Jampani | 3b8101a | 2016-09-15 13:22:01 -0700 | [diff] [blame] | 50 | * Manages the assignment of work partitions to instances. |
Jonathan Hart | 74c8313 | 2015-02-02 18:37:57 -0800 | [diff] [blame] | 51 | */ |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 52 | @Component(immediate = true, service = WorkPartitionService.class) |
Madan Jampani | 3b8101a | 2016-09-15 13:22:01 -0700 | [diff] [blame] | 53 | public class WorkPartitionManager implements WorkPartitionService { |
Jonathan Hart | 74c8313 | 2015-02-02 18:37:57 -0800 | [diff] [blame] | 54 | |
Madan Jampani | 3b8101a | 2016-09-15 13:22:01 -0700 | [diff] [blame] | 55 | private static final Logger log = LoggerFactory.getLogger(WorkPartitionManager.class); |
Jonathan Hart | 74c8313 | 2015-02-02 18:37:57 -0800 | [diff] [blame] | 56 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 57 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Jonathan Hart | 74c8313 | 2015-02-02 18:37:57 -0800 | [diff] [blame] | 58 | protected LeadershipService leadershipService; |
| 59 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 60 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Jonathan Hart | 74c8313 | 2015-02-02 18:37:57 -0800 | [diff] [blame] | 61 | protected ClusterService clusterService; |
| 62 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 63 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Brian O'Connor | 69d6ac7 | 2015-05-29 16:24:06 -0700 | [diff] [blame] | 64 | protected EventDeliveryService eventDispatcher; |
| 65 | |
Madan Jampani | 4732c1b | 2015-05-19 17:11:50 -0700 | [diff] [blame] | 66 | protected final AtomicBoolean rebalanceScheduled = new AtomicBoolean(false); |
| 67 | |
Jonathan Hart | 7061acd | 2015-03-04 13:15:32 -0800 | [diff] [blame] | 68 | static final int NUM_PARTITIONS = 14; |
Jonathan Hart | f2fda81 | 2015-02-17 15:21:03 -0800 | [diff] [blame] | 69 | private static final int BACKOFF_TIME = 2; |
Madan Jampani | 4732c1b | 2015-05-19 17:11:50 -0700 | [diff] [blame] | 70 | private static final int CHECK_PARTITION_BALANCE_PERIOD_SEC = 10; |
| 71 | private static final int RETRY_AFTER_DELAY_SEC = 5; |
Jonathan Hart | 74c8313 | 2015-02-02 18:37:57 -0800 | [diff] [blame] | 72 | |
Madan Jampani | 3b8101a | 2016-09-15 13:22:01 -0700 | [diff] [blame] | 73 | private static final String ELECTION_PREFIX = "work-partition-"; |
Jonathan Hart | 74c8313 | 2015-02-02 18:37:57 -0800 | [diff] [blame] | 74 | |
Madan Jampani | a9673fd | 2016-02-02 13:01:29 -0800 | [diff] [blame] | 75 | protected NodeId localNodeId; |
Madan Jampani | 3b8101a | 2016-09-15 13:22:01 -0700 | [diff] [blame] | 76 | private ListenerRegistry<WorkPartitionEvent, WorkPartitionEventListener> listenerRegistry; |
Jonathan Hart | 74c8313 | 2015-02-02 18:37:57 -0800 | [diff] [blame] | 77 | private LeadershipEventListener leaderListener = new InternalLeadershipListener(); |
| 78 | |
Jonathan Hart | f2fda81 | 2015-02-17 15:21:03 -0800 | [diff] [blame] | 79 | private ScheduledExecutorService executor = Executors |
Madan Jampani | 3b8101a | 2016-09-15 13:22:01 -0700 | [diff] [blame] | 80 | .newScheduledThreadPool(1, groupedThreads("work-parition", "balancer-%d", log)); |
Jonathan Hart | 74c8313 | 2015-02-02 18:37:57 -0800 | [diff] [blame] | 81 | |
| 82 | @Activate |
| 83 | public void activate() { |
Madan Jampani | a9673fd | 2016-02-02 13:01:29 -0800 | [diff] [blame] | 84 | localNodeId = clusterService.getLocalNode().id(); |
Jonathan Hart | 74c8313 | 2015-02-02 18:37:57 -0800 | [diff] [blame] | 85 | leadershipService.addListener(leaderListener); |
| 86 | |
Brian O'Connor | 69d6ac7 | 2015-05-29 16:24:06 -0700 | [diff] [blame] | 87 | listenerRegistry = new ListenerRegistry<>(); |
Madan Jampani | 3b8101a | 2016-09-15 13:22:01 -0700 | [diff] [blame] | 88 | eventDispatcher.addSink(WorkPartitionEvent.class, listenerRegistry); |
Brian O'Connor | 69d6ac7 | 2015-05-29 16:24:06 -0700 | [diff] [blame] | 89 | |
Jonathan Hart | 74c8313 | 2015-02-02 18:37:57 -0800 | [diff] [blame] | 90 | for (int i = 0; i < NUM_PARTITIONS; i++) { |
Jonathan Hart | f2fda81 | 2015-02-17 15:21:03 -0800 | [diff] [blame] | 91 | leadershipService.runForLeadership(getPartitionPath(i)); |
Madan Jampani | a9673fd | 2016-02-02 13:01:29 -0800 | [diff] [blame] | 92 | log.debug("Registered to run for {}", getPartitionPath(i)); |
Jonathan Hart | 74c8313 | 2015-02-02 18:37:57 -0800 | [diff] [blame] | 93 | } |
Jonathan Hart | f2fda81 | 2015-02-17 15:21:03 -0800 | [diff] [blame] | 94 | |
Madan Jampani | 4732c1b | 2015-05-19 17:11:50 -0700 | [diff] [blame] | 95 | executor.scheduleAtFixedRate(() -> scheduleRebalance(0), 0, |
| 96 | CHECK_PARTITION_BALANCE_PERIOD_SEC, TimeUnit.SECONDS); |
Madan Jampani | a9673fd | 2016-02-02 13:01:29 -0800 | [diff] [blame] | 97 | log.info("Started"); |
Jonathan Hart | 74c8313 | 2015-02-02 18:37:57 -0800 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | @Deactivate |
| 101 | public void deactivate() { |
Jonathan Hart | ac48a95 | 2015-02-25 14:11:55 -0800 | [diff] [blame] | 102 | executor.shutdownNow(); |
| 103 | |
Madan Jampani | 3b8101a | 2016-09-15 13:22:01 -0700 | [diff] [blame] | 104 | eventDispatcher.removeSink(WorkPartitionEvent.class); |
Jonathan Hart | 74c8313 | 2015-02-02 18:37:57 -0800 | [diff] [blame] | 105 | leadershipService.removeListener(leaderListener); |
Madan Jampani | a9673fd | 2016-02-02 13:01:29 -0800 | [diff] [blame] | 106 | log.info("Stopped"); |
Jonathan Hart | f2fda81 | 2015-02-17 15:21:03 -0800 | [diff] [blame] | 107 | } |
| 108 | |
Jonathan Hart | 7061acd | 2015-03-04 13:15:32 -0800 | [diff] [blame] | 109 | /** |
| 110 | * Sets the specified executor to be used for scheduling background tasks. |
| 111 | * |
| 112 | * @param executor scheduled executor service for background tasks |
Madan Jampani | 3b8101a | 2016-09-15 13:22:01 -0700 | [diff] [blame] | 113 | * @return this WorkPartitionManager |
Jonathan Hart | 7061acd | 2015-03-04 13:15:32 -0800 | [diff] [blame] | 114 | */ |
Madan Jampani | 3b8101a | 2016-09-15 13:22:01 -0700 | [diff] [blame] | 115 | WorkPartitionManager withScheduledExecutor(ScheduledExecutorService executor) { |
Jonathan Hart | 7061acd | 2015-03-04 13:15:32 -0800 | [diff] [blame] | 116 | this.executor = executor; |
| 117 | return this; |
| 118 | } |
| 119 | |
Jonathan Hart | f2fda81 | 2015-02-17 15:21:03 -0800 | [diff] [blame] | 120 | private String getPartitionPath(int i) { |
| 121 | return ELECTION_PREFIX + i; |
Jonathan Hart | 74c8313 | 2015-02-02 18:37:57 -0800 | [diff] [blame] | 122 | } |
| 123 | |
Madan Jampani | 3b8101a | 2016-09-15 13:22:01 -0700 | [diff] [blame] | 124 | @Override |
| 125 | public <K> boolean isMine(K id, Function<K, Long> hasher) { |
| 126 | return Objects.equals(localNodeId, getLeader(id, hasher)); |
Jonathan Hart | 74c8313 | 2015-02-02 18:37:57 -0800 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | @Override |
Madan Jampani | 3b8101a | 2016-09-15 13:22:01 -0700 | [diff] [blame] | 130 | public <K> NodeId getLeader(K id, Function<K, Long> hasher) { |
| 131 | int partition = Math.abs(hasher.apply(id).intValue()) % NUM_PARTITIONS; |
| 132 | PartitionId partitionId = new PartitionId(partition); |
| 133 | return leadershipService.getLeadership(getPartitionPath(partitionId.value())).leaderNodeId(); |
Brian O'Connor | 5eb77c8 | 2015-03-02 18:09:39 -0800 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | @Override |
Madan Jampani | 3b8101a | 2016-09-15 13:22:01 -0700 | [diff] [blame] | 137 | public void addListener(WorkPartitionEventListener listener) { |
Brian O'Connor | 69d6ac7 | 2015-05-29 16:24:06 -0700 | [diff] [blame] | 138 | listenerRegistry.addListener(listener); |
| 139 | } |
| 140 | |
| 141 | @Override |
Madan Jampani | 3b8101a | 2016-09-15 13:22:01 -0700 | [diff] [blame] | 142 | public void removeListener(WorkPartitionEventListener listener) { |
Brian O'Connor | 69d6ac7 | 2015-05-29 16:24:06 -0700 | [diff] [blame] | 143 | listenerRegistry.removeListener(listener); |
| 144 | } |
| 145 | |
Sho SHIMIZU | b814773 | 2016-01-15 13:13:31 -0800 | [diff] [blame] | 146 | void doRebalance() { |
Madan Jampani | 4732c1b | 2015-05-19 17:11:50 -0700 | [diff] [blame] | 147 | rebalanceScheduled.set(false); |
Jonathan Hart | f2fda81 | 2015-02-17 15:21:03 -0800 | [diff] [blame] | 148 | try { |
Madan Jampani | 4732c1b | 2015-05-19 17:11:50 -0700 | [diff] [blame] | 149 | rebalance(); |
Jonathan Hart | f2fda81 | 2015-02-17 15:21:03 -0800 | [diff] [blame] | 150 | } catch (Exception e) { |
Ray Milkey | 07b5935 | 2017-02-16 15:42:41 -0800 | [diff] [blame] | 151 | log.warn("{} caught during rebalance task. Will retry in " + |
Ray Milkey | ac0a82d | 2017-02-16 08:58:59 -0800 | [diff] [blame] | 152 | RETRY_AFTER_DELAY_SEC + " seconds", e.getMessage()); |
Madan Jampani | 4732c1b | 2015-05-19 17:11:50 -0700 | [diff] [blame] | 153 | scheduleRebalance(RETRY_AFTER_DELAY_SEC); |
Jonathan Hart | f2fda81 | 2015-02-17 15:21:03 -0800 | [diff] [blame] | 154 | } |
| 155 | } |
| 156 | |
Jonathan Hart | f2fda81 | 2015-02-17 15:21:03 -0800 | [diff] [blame] | 157 | /** |
| 158 | * Determine whether we have more than our fair share of partitions, and if |
| 159 | * so, relinquish leadership of some of them for a little while to let |
| 160 | * other instances take over. |
| 161 | */ |
Madan Jampani | 4732c1b | 2015-05-19 17:11:50 -0700 | [diff] [blame] | 162 | private void rebalance() { |
Jonathan Hart | f2fda81 | 2015-02-17 15:21:03 -0800 | [diff] [blame] | 163 | int activeNodes = (int) clusterService.getNodes() |
| 164 | .stream() |
Thomas Vachuska | 7a8de84 | 2016-03-07 20:56:35 -0800 | [diff] [blame] | 165 | .filter(node -> clusterService.getState(node.id()).isActive()) |
Jonathan Hart | f2fda81 | 2015-02-17 15:21:03 -0800 | [diff] [blame] | 166 | .count(); |
| 167 | |
| 168 | int myShare = (int) Math.ceil((double) NUM_PARTITIONS / activeNodes); |
| 169 | |
Madan Jampani | a4d2c72 | 2016-06-06 16:39:06 -0700 | [diff] [blame] | 170 | // First make sure this node is a candidate for all partitions. |
| 171 | IntStream.range(0, NUM_PARTITIONS) |
| 172 | .mapToObj(this::getPartitionPath) |
| 173 | .map(leadershipService::getLeadership) |
| 174 | .filter(leadership -> !leadership.candidates().contains(localNodeId)) |
| 175 | .map(Leadership::topic) |
| 176 | .forEach(leadershipService::runForLeadership); |
| 177 | |
Madan Jampani | a4a5994 | 2016-05-02 11:25:34 -0700 | [diff] [blame] | 178 | List<String> myPartitions = IntStream.range(0, NUM_PARTITIONS) |
| 179 | .mapToObj(this::getPartitionPath) |
| 180 | .map(leadershipService::getLeadership) |
| 181 | .filter(Objects::nonNull) |
| 182 | .filter(leadership -> localNodeId.equals(leadership.leaderNodeId())) |
| 183 | .map(Leadership::topic) |
| 184 | .collect(Collectors.toList()); |
Jonathan Hart | f2fda81 | 2015-02-17 15:21:03 -0800 | [diff] [blame] | 185 | |
Jonathan Hart | dc9d7b8 | 2015-02-22 17:59:50 -0800 | [diff] [blame] | 186 | int relinquish = myPartitions.size() - myShare; |
Jonathan Hart | f2fda81 | 2015-02-17 15:21:03 -0800 | [diff] [blame] | 187 | |
Madan Jampani | a4d2c72 | 2016-06-06 16:39:06 -0700 | [diff] [blame] | 188 | |
Jonathan Hart | dc9d7b8 | 2015-02-22 17:59:50 -0800 | [diff] [blame] | 189 | for (int i = 0; i < relinquish; i++) { |
Madan Jampani | a4a5994 | 2016-05-02 11:25:34 -0700 | [diff] [blame] | 190 | String topic = myPartitions.get(i); |
Madan Jampani | 783d3d2 | 2016-06-13 17:40:02 -0700 | [diff] [blame] | 191 | // Wait till all active nodes are in contention for partition ownership. |
| 192 | // This avoids too many relinquish/reclaim cycles. |
| 193 | if (leadershipService.getCandidates(topic).size() == activeNodes) { |
| 194 | leadershipService.withdraw(topic); |
| 195 | executor.schedule(() -> recontest(topic), BACKOFF_TIME, TimeUnit.SECONDS); |
| 196 | } |
Jonathan Hart | f2fda81 | 2015-02-17 15:21:03 -0800 | [diff] [blame] | 197 | } |
| 198 | } |
| 199 | |
Madan Jampani | 4732c1b | 2015-05-19 17:11:50 -0700 | [diff] [blame] | 200 | private void scheduleRebalance(int afterDelaySec) { |
| 201 | if (rebalanceScheduled.compareAndSet(false, true)) { |
| 202 | executor.schedule(this::doRebalance, afterDelaySec, TimeUnit.SECONDS); |
| 203 | } |
| 204 | } |
| 205 | |
Jonathan Hart | f2fda81 | 2015-02-17 15:21:03 -0800 | [diff] [blame] | 206 | /** |
| 207 | * Try and recontest for leadership of a partition. |
| 208 | * |
| 209 | * @param path topic name to recontest |
| 210 | */ |
| 211 | private void recontest(String path) { |
| 212 | leadershipService.runForLeadership(path); |
| 213 | } |
| 214 | |
Jonathan Hart | 74c8313 | 2015-02-02 18:37:57 -0800 | [diff] [blame] | 215 | private final class InternalLeadershipListener implements LeadershipEventListener { |
| 216 | |
| 217 | @Override |
| 218 | public void event(LeadershipEvent event) { |
| 219 | Leadership leadership = event.subject(); |
Jonathan Hart | dc9d7b8 | 2015-02-22 17:59:50 -0800 | [diff] [blame] | 220 | |
Madan Jampani | a9673fd | 2016-02-02 13:01:29 -0800 | [diff] [blame] | 221 | if (Objects.equals(leadership.leaderNodeId(), localNodeId) && |
Jonathan Hart | 74c8313 | 2015-02-02 18:37:57 -0800 | [diff] [blame] | 222 | leadership.topic().startsWith(ELECTION_PREFIX)) { |
| 223 | |
Madan Jampani | 3b8101a | 2016-09-15 13:22:01 -0700 | [diff] [blame] | 224 | eventDispatcher.post(new WorkPartitionEvent(WorkPartitionEvent.Type.LEADER_CHANGED, |
Brian O'Connor | 69d6ac7 | 2015-05-29 16:24:06 -0700 | [diff] [blame] | 225 | leadership.topic())); |
Jonathan Hart | 74c8313 | 2015-02-02 18:37:57 -0800 | [diff] [blame] | 226 | } |
Jonathan Hart | f2fda81 | 2015-02-17 15:21:03 -0800 | [diff] [blame] | 227 | |
Madan Jampani | 620f70d | 2016-01-30 22:22:47 -0800 | [diff] [blame] | 228 | if (event.type() == LeadershipEvent.Type.CANDIDATES_CHANGED) { |
| 229 | scheduleRebalance(0); |
| 230 | } |
Jonathan Hart | f2fda81 | 2015-02-17 15:21:03 -0800 | [diff] [blame] | 231 | } |
| 232 | } |
Jonathan Hart | 74c8313 | 2015-02-02 18:37:57 -0800 | [diff] [blame] | 233 | } |