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