Jonathan Hart | 74c8313 | 2015-02-02 18:37:57 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
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 | |
| 18 | import org.apache.felix.scr.annotations.Activate; |
| 19 | import org.apache.felix.scr.annotations.Component; |
| 20 | import org.apache.felix.scr.annotations.Deactivate; |
| 21 | import org.apache.felix.scr.annotations.Reference; |
| 22 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 23 | import org.apache.felix.scr.annotations.Service; |
| 24 | import org.onosproject.cluster.ClusterService; |
| 25 | import org.onosproject.cluster.Leadership; |
| 26 | import org.onosproject.cluster.LeadershipEvent; |
| 27 | import org.onosproject.cluster.LeadershipEventListener; |
| 28 | import org.onosproject.cluster.LeadershipService; |
Brian O'Connor | 5eb77c8 | 2015-03-02 18:09:39 -0800 | [diff] [blame] | 29 | import org.onosproject.cluster.NodeId; |
Brian O'Connor | 69d6ac7 | 2015-05-29 16:24:06 -0700 | [diff] [blame] | 30 | import org.onosproject.event.EventDeliveryService; |
| 31 | import org.onosproject.event.ListenerRegistry; |
Madan Jampani | 1c96510 | 2016-01-13 14:34:16 -0800 | [diff] [blame] | 32 | import org.onosproject.net.intent.IntentPartitionEvent; |
| 33 | import org.onosproject.net.intent.IntentPartitionEventListener; |
| 34 | import org.onosproject.net.intent.IntentPartitionService; |
Thomas Vachuska | 7a8de84 | 2016-03-07 20:56:35 -0800 | [diff] [blame] | 35 | import org.onosproject.net.intent.Key; |
Jonathan Hart | 74c8313 | 2015-02-02 18:37:57 -0800 | [diff] [blame] | 36 | import org.slf4j.Logger; |
| 37 | import org.slf4j.LoggerFactory; |
| 38 | |
Jonathan Hart | dc9d7b8 | 2015-02-22 17:59:50 -0800 | [diff] [blame] | 39 | import java.util.List; |
Brian O'Connor | 5eb77c8 | 2015-03-02 18:09:39 -0800 | [diff] [blame] | 40 | import java.util.Objects; |
Jonathan Hart | f2fda81 | 2015-02-17 15:21:03 -0800 | [diff] [blame] | 41 | import java.util.concurrent.Executors; |
| 42 | import java.util.concurrent.ScheduledExecutorService; |
| 43 | import java.util.concurrent.TimeUnit; |
Madan Jampani | 4732c1b | 2015-05-19 17:11:50 -0700 | [diff] [blame] | 44 | import java.util.concurrent.atomic.AtomicBoolean; |
Jonathan Hart | dc9d7b8 | 2015-02-22 17:59:50 -0800 | [diff] [blame] | 45 | import java.util.stream.Collectors; |
Madan Jampani | a4a5994 | 2016-05-02 11:25:34 -0700 | [diff] [blame^] | 46 | import java.util.stream.IntStream; |
Jonathan Hart | 74c8313 | 2015-02-02 18:37:57 -0800 | [diff] [blame] | 47 | |
Jonathan Hart | 74c8313 | 2015-02-02 18:37:57 -0800 | [diff] [blame] | 48 | /** |
| 49 | * Manages the assignment of intent keyspace partitions to instances. |
| 50 | */ |
| 51 | @Component(immediate = true) |
| 52 | @Service |
Madan Jampani | 1c96510 | 2016-01-13 14:34:16 -0800 | [diff] [blame] | 53 | public class IntentPartitionManager implements IntentPartitionService { |
Jonathan Hart | 74c8313 | 2015-02-02 18:37:57 -0800 | [diff] [blame] | 54 | |
Madan Jampani | 1c96510 | 2016-01-13 14:34:16 -0800 | [diff] [blame] | 55 | private static final Logger log = LoggerFactory.getLogger(IntentPartitionManager.class); |
Jonathan Hart | 74c8313 | 2015-02-02 18:37:57 -0800 | [diff] [blame] | 56 | |
| 57 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 58 | protected LeadershipService leadershipService; |
| 59 | |
| 60 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 61 | protected ClusterService clusterService; |
| 62 | |
Brian O'Connor | 69d6ac7 | 2015-05-29 16:24:06 -0700 | [diff] [blame] | 63 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 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 | |
| 73 | private static final String ELECTION_PREFIX = "intent-partition-"; |
| 74 | |
Madan Jampani | a9673fd | 2016-02-02 13:01:29 -0800 | [diff] [blame] | 75 | protected NodeId localNodeId; |
Madan Jampani | 1c96510 | 2016-01-13 14:34:16 -0800 | [diff] [blame] | 76 | private ListenerRegistry<IntentPartitionEvent, IntentPartitionEventListener> 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 |
| 80 | .newScheduledThreadPool(1); |
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 | 1c96510 | 2016-01-13 14:34:16 -0800 | [diff] [blame] | 88 | eventDispatcher.addSink(IntentPartitionEvent.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 | 1c96510 | 2016-01-13 14:34:16 -0800 | [diff] [blame] | 104 | eventDispatcher.removeSink(IntentPartitionEvent.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 |
| 113 | * @return this PartitionManager |
| 114 | */ |
Sho SHIMIZU | b814773 | 2016-01-15 13:13:31 -0800 | [diff] [blame] | 115 | IntentPartitionManager 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 | |
Jonathan Hart | dc9d7b8 | 2015-02-22 17:59:50 -0800 | [diff] [blame] | 124 | private String getPartitionPath(PartitionId id) { |
| 125 | return getPartitionPath(id.value()); |
| 126 | } |
| 127 | |
Jonathan Hart | 5ec32ba | 2015-02-05 13:33:58 -0800 | [diff] [blame] | 128 | private PartitionId getPartitionForKey(Key intentKey) { |
Brian O'Connor | 1fdfacd | 2015-02-18 20:52:06 -0800 | [diff] [blame] | 129 | int partition = Math.abs((int) intentKey.hash()) % NUM_PARTITIONS; |
| 130 | //TODO investigate Guava consistent hash method |
| 131 | // ... does it add significant computational complexity? is it worth it? |
| 132 | //int partition = consistentHash(intentKey.hash(), NUM_PARTITIONS); |
| 133 | PartitionId id = new PartitionId(partition); |
Brian O'Connor | 1fdfacd | 2015-02-18 20:52:06 -0800 | [diff] [blame] | 134 | return id; |
Jonathan Hart | 74c8313 | 2015-02-02 18:37:57 -0800 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | @Override |
Jonathan Hart | 5ec32ba | 2015-02-05 13:33:58 -0800 | [diff] [blame] | 138 | public boolean isMine(Key intentKey) { |
Madan Jampani | a4a5994 | 2016-05-02 11:25:34 -0700 | [diff] [blame^] | 139 | return Objects.equals(leadershipService.getLeadership(getPartitionPath(getPartitionForKey(intentKey))) |
| 140 | .leaderNodeId(), |
Madan Jampani | a9673fd | 2016-02-02 13:01:29 -0800 | [diff] [blame] | 141 | localNodeId); |
Brian O'Connor | 5eb77c8 | 2015-03-02 18:09:39 -0800 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | @Override |
| 145 | public NodeId getLeader(Key intentKey) { |
| 146 | return leadershipService.getLeader(getPartitionPath(getPartitionForKey(intentKey))); |
Jonathan Hart | 74c8313 | 2015-02-02 18:37:57 -0800 | [diff] [blame] | 147 | } |
| 148 | |
Brian O'Connor | 69d6ac7 | 2015-05-29 16:24:06 -0700 | [diff] [blame] | 149 | @Override |
Madan Jampani | 1c96510 | 2016-01-13 14:34:16 -0800 | [diff] [blame] | 150 | public void addListener(IntentPartitionEventListener listener) { |
Brian O'Connor | 69d6ac7 | 2015-05-29 16:24:06 -0700 | [diff] [blame] | 151 | listenerRegistry.addListener(listener); |
| 152 | } |
| 153 | |
| 154 | @Override |
Madan Jampani | 1c96510 | 2016-01-13 14:34:16 -0800 | [diff] [blame] | 155 | public void removeListener(IntentPartitionEventListener listener) { |
Brian O'Connor | 69d6ac7 | 2015-05-29 16:24:06 -0700 | [diff] [blame] | 156 | listenerRegistry.removeListener(listener); |
| 157 | } |
| 158 | |
Sho SHIMIZU | b814773 | 2016-01-15 13:13:31 -0800 | [diff] [blame] | 159 | void doRebalance() { |
Madan Jampani | 4732c1b | 2015-05-19 17:11:50 -0700 | [diff] [blame] | 160 | rebalanceScheduled.set(false); |
Jonathan Hart | f2fda81 | 2015-02-17 15:21:03 -0800 | [diff] [blame] | 161 | try { |
Madan Jampani | 4732c1b | 2015-05-19 17:11:50 -0700 | [diff] [blame] | 162 | rebalance(); |
Jonathan Hart | f2fda81 | 2015-02-17 15:21:03 -0800 | [diff] [blame] | 163 | } catch (Exception e) { |
Madan Jampani | 4732c1b | 2015-05-19 17:11:50 -0700 | [diff] [blame] | 164 | log.warn("Exception caught during rebalance task. Will retry in " + RETRY_AFTER_DELAY_SEC + " seconds", e); |
| 165 | scheduleRebalance(RETRY_AFTER_DELAY_SEC); |
Jonathan Hart | f2fda81 | 2015-02-17 15:21:03 -0800 | [diff] [blame] | 166 | } |
| 167 | } |
| 168 | |
Jonathan Hart | f2fda81 | 2015-02-17 15:21:03 -0800 | [diff] [blame] | 169 | /** |
| 170 | * Determine whether we have more than our fair share of partitions, and if |
| 171 | * so, relinquish leadership of some of them for a little while to let |
| 172 | * other instances take over. |
| 173 | */ |
Madan Jampani | 4732c1b | 2015-05-19 17:11:50 -0700 | [diff] [blame] | 174 | private void rebalance() { |
Jonathan Hart | f2fda81 | 2015-02-17 15:21:03 -0800 | [diff] [blame] | 175 | int activeNodes = (int) clusterService.getNodes() |
| 176 | .stream() |
Thomas Vachuska | 7a8de84 | 2016-03-07 20:56:35 -0800 | [diff] [blame] | 177 | .filter(node -> clusterService.getState(node.id()).isActive()) |
Jonathan Hart | f2fda81 | 2015-02-17 15:21:03 -0800 | [diff] [blame] | 178 | .count(); |
| 179 | |
| 180 | int myShare = (int) Math.ceil((double) NUM_PARTITIONS / activeNodes); |
| 181 | |
Madan Jampani | a4a5994 | 2016-05-02 11:25:34 -0700 | [diff] [blame^] | 182 | List<String> myPartitions = IntStream.range(0, NUM_PARTITIONS) |
| 183 | .mapToObj(this::getPartitionPath) |
| 184 | .map(leadershipService::getLeadership) |
| 185 | .filter(Objects::nonNull) |
| 186 | .filter(leadership -> localNodeId.equals(leadership.leaderNodeId())) |
| 187 | .map(Leadership::topic) |
| 188 | .collect(Collectors.toList()); |
Jonathan Hart | f2fda81 | 2015-02-17 15:21:03 -0800 | [diff] [blame] | 189 | |
Jonathan Hart | dc9d7b8 | 2015-02-22 17:59:50 -0800 | [diff] [blame] | 190 | int relinquish = myPartitions.size() - myShare; |
Jonathan Hart | f2fda81 | 2015-02-17 15:21:03 -0800 | [diff] [blame] | 191 | |
Jonathan Hart | dc9d7b8 | 2015-02-22 17:59:50 -0800 | [diff] [blame] | 192 | for (int i = 0; i < relinquish; i++) { |
Madan Jampani | a4a5994 | 2016-05-02 11:25:34 -0700 | [diff] [blame^] | 193 | String topic = myPartitions.get(i); |
Jonathan Hart | dc9d7b8 | 2015-02-22 17:59:50 -0800 | [diff] [blame] | 194 | leadershipService.withdraw(topic); |
Madan Jampani | a4a5994 | 2016-05-02 11:25:34 -0700 | [diff] [blame^] | 195 | executor.schedule(() -> recontest(topic), BACKOFF_TIME, TimeUnit.SECONDS); |
Jonathan Hart | f2fda81 | 2015-02-17 15:21:03 -0800 | [diff] [blame] | 196 | } |
| 197 | } |
| 198 | |
Madan Jampani | 4732c1b | 2015-05-19 17:11:50 -0700 | [diff] [blame] | 199 | private void scheduleRebalance(int afterDelaySec) { |
| 200 | if (rebalanceScheduled.compareAndSet(false, true)) { |
| 201 | executor.schedule(this::doRebalance, afterDelaySec, TimeUnit.SECONDS); |
| 202 | } |
| 203 | } |
| 204 | |
Jonathan Hart | f2fda81 | 2015-02-17 15:21:03 -0800 | [diff] [blame] | 205 | /** |
| 206 | * Try and recontest for leadership of a partition. |
| 207 | * |
| 208 | * @param path topic name to recontest |
| 209 | */ |
| 210 | private void recontest(String path) { |
| 211 | leadershipService.runForLeadership(path); |
| 212 | } |
| 213 | |
Jonathan Hart | 74c8313 | 2015-02-02 18:37:57 -0800 | [diff] [blame] | 214 | private final class InternalLeadershipListener implements LeadershipEventListener { |
| 215 | |
| 216 | @Override |
| 217 | public void event(LeadershipEvent event) { |
| 218 | Leadership leadership = event.subject(); |
Jonathan Hart | dc9d7b8 | 2015-02-22 17:59:50 -0800 | [diff] [blame] | 219 | |
Madan Jampani | a9673fd | 2016-02-02 13:01:29 -0800 | [diff] [blame] | 220 | if (Objects.equals(leadership.leaderNodeId(), localNodeId) && |
Jonathan Hart | 74c8313 | 2015-02-02 18:37:57 -0800 | [diff] [blame] | 221 | leadership.topic().startsWith(ELECTION_PREFIX)) { |
| 222 | |
Madan Jampani | 1c96510 | 2016-01-13 14:34:16 -0800 | [diff] [blame] | 223 | eventDispatcher.post(new IntentPartitionEvent(IntentPartitionEvent.Type.LEADER_CHANGED, |
Brian O'Connor | 69d6ac7 | 2015-05-29 16:24:06 -0700 | [diff] [blame] | 224 | leadership.topic())); |
Jonathan Hart | 74c8313 | 2015-02-02 18:37:57 -0800 | [diff] [blame] | 225 | } |
Jonathan Hart | f2fda81 | 2015-02-17 15:21:03 -0800 | [diff] [blame] | 226 | |
Madan Jampani | 620f70d | 2016-01-30 22:22:47 -0800 | [diff] [blame] | 227 | if (event.type() == LeadershipEvent.Type.CANDIDATES_CHANGED) { |
| 228 | scheduleRebalance(0); |
| 229 | } |
Jonathan Hart | f2fda81 | 2015-02-17 15:21:03 -0800 | [diff] [blame] | 230 | } |
| 231 | } |
Jonathan Hart | 74c8313 | 2015-02-02 18:37:57 -0800 | [diff] [blame] | 232 | } |