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; |
Jonathan Hart | 74c8313 | 2015-02-02 18:37:57 -0800 | [diff] [blame] | 46 | |
Jonathan Hart | 74c8313 | 2015-02-02 18:37:57 -0800 | [diff] [blame] | 47 | /** |
| 48 | * Manages the assignment of intent keyspace partitions to instances. |
| 49 | */ |
| 50 | @Component(immediate = true) |
| 51 | @Service |
Madan Jampani | 1c96510 | 2016-01-13 14:34:16 -0800 | [diff] [blame] | 52 | public class IntentPartitionManager implements IntentPartitionService { |
Jonathan Hart | 74c8313 | 2015-02-02 18:37:57 -0800 | [diff] [blame] | 53 | |
Madan Jampani | 1c96510 | 2016-01-13 14:34:16 -0800 | [diff] [blame] | 54 | private static final Logger log = LoggerFactory.getLogger(IntentPartitionManager.class); |
Jonathan Hart | 74c8313 | 2015-02-02 18:37:57 -0800 | [diff] [blame] | 55 | |
| 56 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 57 | protected LeadershipService leadershipService; |
| 58 | |
| 59 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 60 | protected ClusterService clusterService; |
| 61 | |
Brian O'Connor | 69d6ac7 | 2015-05-29 16:24:06 -0700 | [diff] [blame] | 62 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 63 | protected EventDeliveryService eventDispatcher; |
| 64 | |
Madan Jampani | 4732c1b | 2015-05-19 17:11:50 -0700 | [diff] [blame] | 65 | protected final AtomicBoolean rebalanceScheduled = new AtomicBoolean(false); |
| 66 | |
Jonathan Hart | 7061acd | 2015-03-04 13:15:32 -0800 | [diff] [blame] | 67 | static final int NUM_PARTITIONS = 14; |
Jonathan Hart | f2fda81 | 2015-02-17 15:21:03 -0800 | [diff] [blame] | 68 | private static final int BACKOFF_TIME = 2; |
Madan Jampani | 4732c1b | 2015-05-19 17:11:50 -0700 | [diff] [blame] | 69 | private static final int CHECK_PARTITION_BALANCE_PERIOD_SEC = 10; |
| 70 | private static final int RETRY_AFTER_DELAY_SEC = 5; |
Jonathan Hart | 74c8313 | 2015-02-02 18:37:57 -0800 | [diff] [blame] | 71 | |
| 72 | private static final String ELECTION_PREFIX = "intent-partition-"; |
| 73 | |
Madan Jampani | a9673fd | 2016-02-02 13:01:29 -0800 | [diff] [blame] | 74 | protected NodeId localNodeId; |
Madan Jampani | 1c96510 | 2016-01-13 14:34:16 -0800 | [diff] [blame] | 75 | private ListenerRegistry<IntentPartitionEvent, IntentPartitionEventListener> listenerRegistry; |
Jonathan Hart | 74c8313 | 2015-02-02 18:37:57 -0800 | [diff] [blame] | 76 | private LeadershipEventListener leaderListener = new InternalLeadershipListener(); |
| 77 | |
Jonathan Hart | f2fda81 | 2015-02-17 15:21:03 -0800 | [diff] [blame] | 78 | private ScheduledExecutorService executor = Executors |
| 79 | .newScheduledThreadPool(1); |
Jonathan Hart | 74c8313 | 2015-02-02 18:37:57 -0800 | [diff] [blame] | 80 | |
| 81 | @Activate |
| 82 | public void activate() { |
Madan Jampani | a9673fd | 2016-02-02 13:01:29 -0800 | [diff] [blame] | 83 | localNodeId = clusterService.getLocalNode().id(); |
Jonathan Hart | 74c8313 | 2015-02-02 18:37:57 -0800 | [diff] [blame] | 84 | leadershipService.addListener(leaderListener); |
| 85 | |
Brian O'Connor | 69d6ac7 | 2015-05-29 16:24:06 -0700 | [diff] [blame] | 86 | listenerRegistry = new ListenerRegistry<>(); |
Madan Jampani | 1c96510 | 2016-01-13 14:34:16 -0800 | [diff] [blame] | 87 | eventDispatcher.addSink(IntentPartitionEvent.class, listenerRegistry); |
Brian O'Connor | 69d6ac7 | 2015-05-29 16:24:06 -0700 | [diff] [blame] | 88 | |
Jonathan Hart | 74c8313 | 2015-02-02 18:37:57 -0800 | [diff] [blame] | 89 | for (int i = 0; i < NUM_PARTITIONS; i++) { |
Jonathan Hart | f2fda81 | 2015-02-17 15:21:03 -0800 | [diff] [blame] | 90 | leadershipService.runForLeadership(getPartitionPath(i)); |
Madan Jampani | a9673fd | 2016-02-02 13:01:29 -0800 | [diff] [blame] | 91 | log.debug("Registered to run for {}", getPartitionPath(i)); |
Jonathan Hart | 74c8313 | 2015-02-02 18:37:57 -0800 | [diff] [blame] | 92 | } |
Jonathan Hart | f2fda81 | 2015-02-17 15:21:03 -0800 | [diff] [blame] | 93 | |
Madan Jampani | 4732c1b | 2015-05-19 17:11:50 -0700 | [diff] [blame] | 94 | executor.scheduleAtFixedRate(() -> scheduleRebalance(0), 0, |
| 95 | CHECK_PARTITION_BALANCE_PERIOD_SEC, TimeUnit.SECONDS); |
Madan Jampani | a9673fd | 2016-02-02 13:01:29 -0800 | [diff] [blame] | 96 | log.info("Started"); |
Jonathan Hart | 74c8313 | 2015-02-02 18:37:57 -0800 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | @Deactivate |
| 100 | public void deactivate() { |
Jonathan Hart | ac48a95 | 2015-02-25 14:11:55 -0800 | [diff] [blame] | 101 | executor.shutdownNow(); |
| 102 | |
Madan Jampani | 1c96510 | 2016-01-13 14:34:16 -0800 | [diff] [blame] | 103 | eventDispatcher.removeSink(IntentPartitionEvent.class); |
Jonathan Hart | 74c8313 | 2015-02-02 18:37:57 -0800 | [diff] [blame] | 104 | leadershipService.removeListener(leaderListener); |
Madan Jampani | a9673fd | 2016-02-02 13:01:29 -0800 | [diff] [blame] | 105 | log.info("Stopped"); |
Jonathan Hart | f2fda81 | 2015-02-17 15:21:03 -0800 | [diff] [blame] | 106 | } |
| 107 | |
Jonathan Hart | 7061acd | 2015-03-04 13:15:32 -0800 | [diff] [blame] | 108 | /** |
| 109 | * Sets the specified executor to be used for scheduling background tasks. |
| 110 | * |
| 111 | * @param executor scheduled executor service for background tasks |
| 112 | * @return this PartitionManager |
| 113 | */ |
Sho SHIMIZU | b814773 | 2016-01-15 13:13:31 -0800 | [diff] [blame] | 114 | IntentPartitionManager withScheduledExecutor(ScheduledExecutorService executor) { |
Jonathan Hart | 7061acd | 2015-03-04 13:15:32 -0800 | [diff] [blame] | 115 | this.executor = executor; |
| 116 | return this; |
| 117 | } |
| 118 | |
Jonathan Hart | f2fda81 | 2015-02-17 15:21:03 -0800 | [diff] [blame] | 119 | private String getPartitionPath(int i) { |
| 120 | return ELECTION_PREFIX + i; |
Jonathan Hart | 74c8313 | 2015-02-02 18:37:57 -0800 | [diff] [blame] | 121 | } |
| 122 | |
Jonathan Hart | dc9d7b8 | 2015-02-22 17:59:50 -0800 | [diff] [blame] | 123 | private String getPartitionPath(PartitionId id) { |
| 124 | return getPartitionPath(id.value()); |
| 125 | } |
| 126 | |
Jonathan Hart | 5ec32ba | 2015-02-05 13:33:58 -0800 | [diff] [blame] | 127 | private PartitionId getPartitionForKey(Key intentKey) { |
Brian O'Connor | 1fdfacd | 2015-02-18 20:52:06 -0800 | [diff] [blame] | 128 | int partition = Math.abs((int) intentKey.hash()) % NUM_PARTITIONS; |
| 129 | //TODO investigate Guava consistent hash method |
| 130 | // ... does it add significant computational complexity? is it worth it? |
| 131 | //int partition = consistentHash(intentKey.hash(), NUM_PARTITIONS); |
| 132 | PartitionId id = new PartitionId(partition); |
Brian O'Connor | 1fdfacd | 2015-02-18 20:52:06 -0800 | [diff] [blame] | 133 | return id; |
Jonathan Hart | 74c8313 | 2015-02-02 18:37:57 -0800 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | @Override |
Jonathan Hart | 5ec32ba | 2015-02-05 13:33:58 -0800 | [diff] [blame] | 137 | public boolean isMine(Key intentKey) { |
Brian O'Connor | 5eb77c8 | 2015-03-02 18:09:39 -0800 | [diff] [blame] | 138 | return Objects.equals(leadershipService.getLeader(getPartitionPath(getPartitionForKey(intentKey))), |
Madan Jampani | a9673fd | 2016-02-02 13:01:29 -0800 | [diff] [blame] | 139 | localNodeId); |
Brian O'Connor | 5eb77c8 | 2015-03-02 18:09:39 -0800 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | @Override |
| 143 | public NodeId getLeader(Key intentKey) { |
| 144 | return leadershipService.getLeader(getPartitionPath(getPartitionForKey(intentKey))); |
Jonathan Hart | 74c8313 | 2015-02-02 18:37:57 -0800 | [diff] [blame] | 145 | } |
| 146 | |
Brian O'Connor | 69d6ac7 | 2015-05-29 16:24:06 -0700 | [diff] [blame] | 147 | @Override |
Madan Jampani | 1c96510 | 2016-01-13 14:34:16 -0800 | [diff] [blame] | 148 | public void addListener(IntentPartitionEventListener listener) { |
Brian O'Connor | 69d6ac7 | 2015-05-29 16:24:06 -0700 | [diff] [blame] | 149 | listenerRegistry.addListener(listener); |
| 150 | } |
| 151 | |
| 152 | @Override |
Madan Jampani | 1c96510 | 2016-01-13 14:34:16 -0800 | [diff] [blame] | 153 | public void removeListener(IntentPartitionEventListener listener) { |
Brian O'Connor | 69d6ac7 | 2015-05-29 16:24:06 -0700 | [diff] [blame] | 154 | listenerRegistry.removeListener(listener); |
| 155 | } |
| 156 | |
Sho SHIMIZU | b814773 | 2016-01-15 13:13:31 -0800 | [diff] [blame] | 157 | void doRebalance() { |
Madan Jampani | 4732c1b | 2015-05-19 17:11:50 -0700 | [diff] [blame] | 158 | rebalanceScheduled.set(false); |
Jonathan Hart | f2fda81 | 2015-02-17 15:21:03 -0800 | [diff] [blame] | 159 | try { |
Madan Jampani | 4732c1b | 2015-05-19 17:11:50 -0700 | [diff] [blame] | 160 | rebalance(); |
Jonathan Hart | f2fda81 | 2015-02-17 15:21:03 -0800 | [diff] [blame] | 161 | } catch (Exception e) { |
Madan Jampani | 4732c1b | 2015-05-19 17:11:50 -0700 | [diff] [blame] | 162 | log.warn("Exception caught during rebalance task. Will retry in " + RETRY_AFTER_DELAY_SEC + " seconds", e); |
| 163 | scheduleRebalance(RETRY_AFTER_DELAY_SEC); |
Jonathan Hart | f2fda81 | 2015-02-17 15:21:03 -0800 | [diff] [blame] | 164 | } |
| 165 | } |
| 166 | |
Jonathan Hart | f2fda81 | 2015-02-17 15:21:03 -0800 | [diff] [blame] | 167 | /** |
| 168 | * Determine whether we have more than our fair share of partitions, and if |
| 169 | * so, relinquish leadership of some of them for a little while to let |
| 170 | * other instances take over. |
| 171 | */ |
Madan Jampani | 4732c1b | 2015-05-19 17:11:50 -0700 | [diff] [blame] | 172 | private void rebalance() { |
Jonathan Hart | f2fda81 | 2015-02-17 15:21:03 -0800 | [diff] [blame] | 173 | int activeNodes = (int) clusterService.getNodes() |
| 174 | .stream() |
Thomas Vachuska | 7a8de84 | 2016-03-07 20:56:35 -0800 | [diff] [blame] | 175 | .filter(node -> clusterService.getState(node.id()).isActive()) |
Jonathan Hart | f2fda81 | 2015-02-17 15:21:03 -0800 | [diff] [blame] | 176 | .count(); |
| 177 | |
| 178 | int myShare = (int) Math.ceil((double) NUM_PARTITIONS / activeNodes); |
| 179 | |
Jonathan Hart | dc9d7b8 | 2015-02-22 17:59:50 -0800 | [diff] [blame] | 180 | List<Leadership> myPartitions = leadershipService.getLeaderBoard().values() |
| 181 | .stream() |
Madan Jampani | a9673fd | 2016-02-02 13:01:29 -0800 | [diff] [blame] | 182 | .filter(l -> localNodeId.equals(l.leaderNodeId())) |
Jonathan Hart | dc9d7b8 | 2015-02-22 17:59:50 -0800 | [diff] [blame] | 183 | .filter(l -> l.topic().startsWith(ELECTION_PREFIX)) |
| 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 | |
Jonathan Hart | dc9d7b8 | 2015-02-22 17:59:50 -0800 | [diff] [blame] | 188 | if (relinquish <= 0) { |
| 189 | return; |
| 190 | } |
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++) { |
| 193 | String topic = myPartitions.get(i).topic(); |
| 194 | leadershipService.withdraw(topic); |
Jonathan Hart | f2fda81 | 2015-02-17 15:21:03 -0800 | [diff] [blame] | 195 | |
Jonathan Hart | dc9d7b8 | 2015-02-22 17:59:50 -0800 | [diff] [blame] | 196 | executor.schedule(() -> recontest(topic), |
| 197 | BACKOFF_TIME, TimeUnit.SECONDS); |
Jonathan Hart | f2fda81 | 2015-02-17 15:21:03 -0800 | [diff] [blame] | 198 | } |
| 199 | } |
| 200 | |
Madan Jampani | 4732c1b | 2015-05-19 17:11:50 -0700 | [diff] [blame] | 201 | private void scheduleRebalance(int afterDelaySec) { |
| 202 | if (rebalanceScheduled.compareAndSet(false, true)) { |
| 203 | executor.schedule(this::doRebalance, afterDelaySec, TimeUnit.SECONDS); |
| 204 | } |
| 205 | } |
| 206 | |
Jonathan Hart | f2fda81 | 2015-02-17 15:21:03 -0800 | [diff] [blame] | 207 | /** |
| 208 | * Try and recontest for leadership of a partition. |
| 209 | * |
| 210 | * @param path topic name to recontest |
| 211 | */ |
| 212 | private void recontest(String path) { |
| 213 | leadershipService.runForLeadership(path); |
| 214 | } |
| 215 | |
Jonathan Hart | 74c8313 | 2015-02-02 18:37:57 -0800 | [diff] [blame] | 216 | private final class InternalLeadershipListener implements LeadershipEventListener { |
| 217 | |
| 218 | @Override |
| 219 | public void event(LeadershipEvent event) { |
| 220 | Leadership leadership = event.subject(); |
Jonathan Hart | dc9d7b8 | 2015-02-22 17:59:50 -0800 | [diff] [blame] | 221 | |
Madan Jampani | a9673fd | 2016-02-02 13:01:29 -0800 | [diff] [blame] | 222 | if (Objects.equals(leadership.leaderNodeId(), localNodeId) && |
Jonathan Hart | 74c8313 | 2015-02-02 18:37:57 -0800 | [diff] [blame] | 223 | leadership.topic().startsWith(ELECTION_PREFIX)) { |
| 224 | |
Madan Jampani | 1c96510 | 2016-01-13 14:34:16 -0800 | [diff] [blame] | 225 | eventDispatcher.post(new IntentPartitionEvent(IntentPartitionEvent.Type.LEADER_CHANGED, |
Brian O'Connor | 69d6ac7 | 2015-05-29 16:24:06 -0700 | [diff] [blame] | 226 | leadership.topic())); |
Jonathan Hart | 74c8313 | 2015-02-02 18:37:57 -0800 | [diff] [blame] | 227 | } |
Jonathan Hart | f2fda81 | 2015-02-17 15:21:03 -0800 | [diff] [blame] | 228 | |
Madan Jampani | 620f70d | 2016-01-30 22:22:47 -0800 | [diff] [blame] | 229 | if (event.type() == LeadershipEvent.Type.CANDIDATES_CHANGED) { |
| 230 | scheduleRebalance(0); |
| 231 | } |
Jonathan Hart | f2fda81 | 2015-02-17 15:21:03 -0800 | [diff] [blame] | 232 | } |
| 233 | } |
Jonathan Hart | 74c8313 | 2015-02-02 18:37:57 -0800 | [diff] [blame] | 234 | } |