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