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