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