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