blob: bfb47549f3d0fd5584d1bf6a70d2fc28df08ecf0 [file] [log] [blame]
Jonathan Hart74c83132015-02-02 18:37:57 -08001/*
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 */
16package org.onosproject.store.intent.impl;
17
18import org.apache.felix.scr.annotations.Activate;
19import org.apache.felix.scr.annotations.Component;
20import org.apache.felix.scr.annotations.Deactivate;
21import org.apache.felix.scr.annotations.Reference;
22import org.apache.felix.scr.annotations.ReferenceCardinality;
23import org.apache.felix.scr.annotations.Service;
Jonathan Hartf2fda812015-02-17 15:21:03 -080024import org.onosproject.cluster.ClusterEvent;
25import org.onosproject.cluster.ClusterEventListener;
Jonathan Hart74c83132015-02-02 18:37:57 -080026import org.onosproject.cluster.ClusterService;
Jonathan Hartf2fda812015-02-17 15:21:03 -080027import org.onosproject.cluster.ControllerNode;
Jonathan Hart74c83132015-02-02 18:37:57 -080028import org.onosproject.cluster.Leadership;
29import org.onosproject.cluster.LeadershipEvent;
30import org.onosproject.cluster.LeadershipEventListener;
31import org.onosproject.cluster.LeadershipService;
Brian O'Connor5eb77c82015-03-02 18:09:39 -080032import org.onosproject.cluster.NodeId;
Brian O'Connor69d6ac72015-05-29 16:24:06 -070033import org.onosproject.event.EventDeliveryService;
34import org.onosproject.event.ListenerRegistry;
Jonathan Hart5ec32ba2015-02-05 13:33:58 -080035import org.onosproject.net.intent.Key;
Madan Jampani1c965102016-01-13 14:34:16 -080036import org.onosproject.net.intent.IntentPartitionEvent;
37import org.onosproject.net.intent.IntentPartitionEventListener;
38import org.onosproject.net.intent.IntentPartitionService;
Jonathan Hart74c83132015-02-02 18:37:57 -080039import org.slf4j.Logger;
40import org.slf4j.LoggerFactory;
41
Jonathan Hartdc9d7b82015-02-22 17:59:50 -080042import java.util.List;
Brian O'Connor5eb77c82015-03-02 18:09:39 -080043import java.util.Objects;
Jonathan Hartf2fda812015-02-17 15:21:03 -080044import java.util.concurrent.Executors;
45import java.util.concurrent.ScheduledExecutorService;
46import java.util.concurrent.TimeUnit;
Madan Jampani4732c1b2015-05-19 17:11:50 -070047import java.util.concurrent.atomic.AtomicBoolean;
Jonathan Hartdc9d7b82015-02-22 17:59:50 -080048import java.util.stream.Collectors;
Jonathan Hart74c83132015-02-02 18:37:57 -080049
Jonathan Hart74c83132015-02-02 18:37:57 -080050/**
51 * Manages the assignment of intent keyspace partitions to instances.
52 */
53@Component(immediate = true)
54@Service
Madan Jampani1c965102016-01-13 14:34:16 -080055public class IntentPartitionManager implements IntentPartitionService {
Jonathan Hart74c83132015-02-02 18:37:57 -080056
Madan Jampani1c965102016-01-13 14:34:16 -080057 private static final Logger log = LoggerFactory.getLogger(IntentPartitionManager.class);
Jonathan Hart74c83132015-02-02 18:37:57 -080058
59 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
60 protected LeadershipService leadershipService;
61
62 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
63 protected ClusterService clusterService;
64
Brian O'Connor69d6ac72015-05-29 16:24:06 -070065 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
66 protected EventDeliveryService eventDispatcher;
67
Madan Jampani4732c1b2015-05-19 17:11:50 -070068 protected final AtomicBoolean rebalanceScheduled = new AtomicBoolean(false);
69
Jonathan Hart7061acd2015-03-04 13:15:32 -080070 static final int NUM_PARTITIONS = 14;
Jonathan Hartf2fda812015-02-17 15:21:03 -080071 private static final int BACKOFF_TIME = 2;
Madan Jampani4732c1b2015-05-19 17:11:50 -070072 private static final int CHECK_PARTITION_BALANCE_PERIOD_SEC = 10;
73 private static final int RETRY_AFTER_DELAY_SEC = 5;
Jonathan Hart74c83132015-02-02 18:37:57 -080074
75 private static final String ELECTION_PREFIX = "intent-partition-";
76
Madan Jampani1c965102016-01-13 14:34:16 -080077 private ListenerRegistry<IntentPartitionEvent, IntentPartitionEventListener> listenerRegistry;
Jonathan Hart74c83132015-02-02 18:37:57 -080078 private LeadershipEventListener leaderListener = new InternalLeadershipListener();
Jonathan Hartf2fda812015-02-17 15:21:03 -080079 private ClusterEventListener clusterListener = new InternalClusterEventListener();
Jonathan Hart74c83132015-02-02 18:37:57 -080080
Jonathan Hartf2fda812015-02-17 15:21:03 -080081 private ScheduledExecutorService executor = Executors
82 .newScheduledThreadPool(1);
Jonathan Hart74c83132015-02-02 18:37:57 -080083
84 @Activate
85 public void activate() {
Jonathan Hart74c83132015-02-02 18:37:57 -080086 leadershipService.addListener(leaderListener);
Jonathan Hartf2fda812015-02-17 15:21:03 -080087 clusterService.addListener(clusterListener);
Jonathan Hart74c83132015-02-02 18:37:57 -080088
Brian O'Connor69d6ac72015-05-29 16:24:06 -070089 listenerRegistry = new ListenerRegistry<>();
Madan Jampani1c965102016-01-13 14:34:16 -080090 eventDispatcher.addSink(IntentPartitionEvent.class, listenerRegistry);
Brian O'Connor69d6ac72015-05-29 16:24:06 -070091
Jonathan Hart74c83132015-02-02 18:37:57 -080092 for (int i = 0; i < NUM_PARTITIONS; i++) {
Jonathan Hartf2fda812015-02-17 15:21:03 -080093 leadershipService.runForLeadership(getPartitionPath(i));
Jonathan Hart74c83132015-02-02 18:37:57 -080094 }
Jonathan Hartf2fda812015-02-17 15:21:03 -080095
Madan Jampani4732c1b2015-05-19 17:11:50 -070096 executor.scheduleAtFixedRate(() -> scheduleRebalance(0), 0,
97 CHECK_PARTITION_BALANCE_PERIOD_SEC, TimeUnit.SECONDS);
Jonathan Hart74c83132015-02-02 18:37:57 -080098 }
99
100 @Deactivate
101 public void deactivate() {
Jonathan Hartac48a952015-02-25 14:11:55 -0800102 executor.shutdownNow();
103
Madan Jampani1c965102016-01-13 14:34:16 -0800104 eventDispatcher.removeSink(IntentPartitionEvent.class);
Jonathan Hart74c83132015-02-02 18:37:57 -0800105 leadershipService.removeListener(leaderListener);
Jonathan Hartf2fda812015-02-17 15:21:03 -0800106 clusterService.removeListener(clusterListener);
107 }
108
Jonathan Hart7061acd2015-03-04 13:15:32 -0800109 /**
110 * Sets the specified executor to be used for scheduling background tasks.
111 *
112 * @param executor scheduled executor service for background tasks
113 * @return this PartitionManager
114 */
Sho SHIMIZUb8147732016-01-15 13:13:31 -0800115 IntentPartitionManager withScheduledExecutor(ScheduledExecutorService executor) {
Jonathan Hart7061acd2015-03-04 13:15:32 -0800116 this.executor = executor;
117 return this;
118 }
119
Jonathan Hartf2fda812015-02-17 15:21:03 -0800120 private String getPartitionPath(int i) {
121 return ELECTION_PREFIX + i;
Jonathan Hart74c83132015-02-02 18:37:57 -0800122 }
123
Jonathan Hartdc9d7b82015-02-22 17:59:50 -0800124 private String getPartitionPath(PartitionId id) {
125 return getPartitionPath(id.value());
126 }
127
Jonathan Hart5ec32ba2015-02-05 13:33:58 -0800128 private PartitionId getPartitionForKey(Key intentKey) {
Brian O'Connor1fdfacd2015-02-18 20:52:06 -0800129 int partition = Math.abs((int) intentKey.hash()) % NUM_PARTITIONS;
130 //TODO investigate Guava consistent hash method
131 // ... does it add significant computational complexity? is it worth it?
132 //int partition = consistentHash(intentKey.hash(), NUM_PARTITIONS);
133 PartitionId id = new PartitionId(partition);
Brian O'Connor1fdfacd2015-02-18 20:52:06 -0800134 return id;
Jonathan Hart74c83132015-02-02 18:37:57 -0800135 }
136
137 @Override
Jonathan Hart5ec32ba2015-02-05 13:33:58 -0800138 public boolean isMine(Key intentKey) {
Brian O'Connor5eb77c82015-03-02 18:09:39 -0800139 return Objects.equals(leadershipService.getLeader(getPartitionPath(getPartitionForKey(intentKey))),
140 clusterService.getLocalNode().id());
141 }
142
143 @Override
144 public NodeId getLeader(Key intentKey) {
145 return leadershipService.getLeader(getPartitionPath(getPartitionForKey(intentKey)));
Jonathan Hart74c83132015-02-02 18:37:57 -0800146 }
147
Brian O'Connor69d6ac72015-05-29 16:24:06 -0700148 @Override
Madan Jampani1c965102016-01-13 14:34:16 -0800149 public void addListener(IntentPartitionEventListener listener) {
Brian O'Connor69d6ac72015-05-29 16:24:06 -0700150 listenerRegistry.addListener(listener);
151 }
152
153 @Override
Madan Jampani1c965102016-01-13 14:34:16 -0800154 public void removeListener(IntentPartitionEventListener listener) {
Brian O'Connor69d6ac72015-05-29 16:24:06 -0700155 listenerRegistry.removeListener(listener);
156 }
157
Sho SHIMIZUb8147732016-01-15 13:13:31 -0800158 void doRebalance() {
Madan Jampani4732c1b2015-05-19 17:11:50 -0700159 rebalanceScheduled.set(false);
Jonathan Hartf2fda812015-02-17 15:21:03 -0800160 try {
Madan Jampani4732c1b2015-05-19 17:11:50 -0700161 rebalance();
Jonathan Hartf2fda812015-02-17 15:21:03 -0800162 } catch (Exception e) {
Madan Jampani4732c1b2015-05-19 17:11:50 -0700163 log.warn("Exception caught during rebalance task. Will retry in " + RETRY_AFTER_DELAY_SEC + " seconds", e);
164 scheduleRebalance(RETRY_AFTER_DELAY_SEC);
Jonathan Hartf2fda812015-02-17 15:21:03 -0800165 }
166 }
167
Jonathan Hartf2fda812015-02-17 15:21:03 -0800168 /**
169 * Determine whether we have more than our fair share of partitions, and if
170 * so, relinquish leadership of some of them for a little while to let
171 * other instances take over.
172 */
Madan Jampani4732c1b2015-05-19 17:11:50 -0700173 private void rebalance() {
Jonathan Hartf2fda812015-02-17 15:21:03 -0800174 int activeNodes = (int) clusterService.getNodes()
175 .stream()
Madan Jampani4732c1b2015-05-19 17:11:50 -0700176 .filter(node -> ControllerNode.State.ACTIVE == clusterService.getState(node.id()))
Jonathan Hartf2fda812015-02-17 15:21:03 -0800177 .count();
178
179 int myShare = (int) Math.ceil((double) NUM_PARTITIONS / activeNodes);
180
Jonathan Hartdc9d7b82015-02-22 17:59:50 -0800181 List<Leadership> myPartitions = leadershipService.getLeaderBoard().values()
182 .stream()
183 .filter(l -> clusterService.getLocalNode().id().equals(l.leader()))
184 .filter(l -> l.topic().startsWith(ELECTION_PREFIX))
185 .collect(Collectors.toList());
Jonathan Hartf2fda812015-02-17 15:21:03 -0800186
Jonathan Hartdc9d7b82015-02-22 17:59:50 -0800187 int relinquish = myPartitions.size() - myShare;
Jonathan Hartf2fda812015-02-17 15:21:03 -0800188
Jonathan Hartdc9d7b82015-02-22 17:59:50 -0800189 if (relinquish <= 0) {
190 return;
191 }
Jonathan Hartf2fda812015-02-17 15:21:03 -0800192
Jonathan Hartdc9d7b82015-02-22 17:59:50 -0800193 for (int i = 0; i < relinquish; i++) {
194 String topic = myPartitions.get(i).topic();
195 leadershipService.withdraw(topic);
Jonathan Hartf2fda812015-02-17 15:21:03 -0800196
Jonathan Hartdc9d7b82015-02-22 17:59:50 -0800197 executor.schedule(() -> recontest(topic),
198 BACKOFF_TIME, TimeUnit.SECONDS);
Jonathan Hartf2fda812015-02-17 15:21:03 -0800199 }
200 }
201
Madan Jampani4732c1b2015-05-19 17:11:50 -0700202 private void scheduleRebalance(int afterDelaySec) {
203 if (rebalanceScheduled.compareAndSet(false, true)) {
204 executor.schedule(this::doRebalance, afterDelaySec, TimeUnit.SECONDS);
205 }
206 }
207
Jonathan Hartf2fda812015-02-17 15:21:03 -0800208 /**
209 * Try and recontest for leadership of a partition.
210 *
211 * @param path topic name to recontest
212 */
213 private void recontest(String path) {
214 leadershipService.runForLeadership(path);
215 }
216
Jonathan Hart74c83132015-02-02 18:37:57 -0800217 private final class InternalLeadershipListener implements LeadershipEventListener {
218
219 @Override
220 public void event(LeadershipEvent event) {
221 Leadership leadership = event.subject();
Jonathan Hartdc9d7b82015-02-22 17:59:50 -0800222
Brian O'Connor5eb77c82015-03-02 18:09:39 -0800223 if (Objects.equals(leadership.leader(), clusterService.getLocalNode().id()) &&
Jonathan Hart74c83132015-02-02 18:37:57 -0800224 leadership.topic().startsWith(ELECTION_PREFIX)) {
225
Jonathan Hartf2fda812015-02-17 15:21:03 -0800226 // See if we need to let some partitions go
Madan Jampani4732c1b2015-05-19 17:11:50 -0700227 scheduleRebalance(0);
Brian O'Connor69d6ac72015-05-29 16:24:06 -0700228
Madan Jampani1c965102016-01-13 14:34:16 -0800229 eventDispatcher.post(new IntentPartitionEvent(IntentPartitionEvent.Type.LEADER_CHANGED,
Brian O'Connor69d6ac72015-05-29 16:24:06 -0700230 leadership.topic()));
Jonathan Hart74c83132015-02-02 18:37:57 -0800231 }
Jonathan Hart74c83132015-02-02 18:37:57 -0800232 }
233 }
Jonathan Hartf2fda812015-02-17 15:21:03 -0800234
235 private final class InternalClusterEventListener implements
236 ClusterEventListener {
237
238 @Override
239 public void event(ClusterEvent event) {
Madan Jampani4732c1b2015-05-19 17:11:50 -0700240 scheduleRebalance(0);
Jonathan Hartf2fda812015-02-17 15:21:03 -0800241 }
242 }
Jonathan Hart74c83132015-02-02 18:37:57 -0800243}