blob: d913a38729d259c237b800c017a2ffc1641e62e8 [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;
Jonathan Hart5ec32ba2015-02-05 13:33:58 -080032import org.onosproject.net.intent.Key;
Jonathan Hart74c83132015-02-02 18:37:57 -080033import org.slf4j.Logger;
34import org.slf4j.LoggerFactory;
35
Jonathan Hartdc9d7b82015-02-22 17:59:50 -080036import java.util.List;
Jonathan Hartf2fda812015-02-17 15:21:03 -080037import java.util.concurrent.Executors;
38import java.util.concurrent.ScheduledExecutorService;
39import java.util.concurrent.TimeUnit;
Jonathan Hartdc9d7b82015-02-22 17:59:50 -080040import java.util.stream.Collectors;
Jonathan Hart74c83132015-02-02 18:37:57 -080041
Jonathan Hart74c83132015-02-02 18:37:57 -080042/**
43 * Manages the assignment of intent keyspace partitions to instances.
44 */
45@Component(immediate = true)
46@Service
47public class PartitionManager implements PartitionService {
48
49 private static final Logger log = LoggerFactory.getLogger(PartitionManager.class);
50
51 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
52 protected LeadershipService leadershipService;
53
54 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
55 protected ClusterService clusterService;
56
Brian O'Connor71503cd2015-02-20 22:05:48 -080057 private static final int NUM_PARTITIONS = 14;
Jonathan Hartf2fda812015-02-17 15:21:03 -080058 private static final int BACKOFF_TIME = 2;
59 private static final int CHECK_PERIOD = 10;
Jonathan Hart74c83132015-02-02 18:37:57 -080060
61 private static final String ELECTION_PREFIX = "intent-partition-";
62
63 private LeadershipEventListener leaderListener = new InternalLeadershipListener();
Jonathan Hartf2fda812015-02-17 15:21:03 -080064 private ClusterEventListener clusterListener = new InternalClusterEventListener();
Jonathan Hart74c83132015-02-02 18:37:57 -080065
Jonathan Hartf2fda812015-02-17 15:21:03 -080066 private ScheduledExecutorService executor = Executors
67 .newScheduledThreadPool(1);
Jonathan Hart74c83132015-02-02 18:37:57 -080068
69 @Activate
70 public void activate() {
Jonathan Hart74c83132015-02-02 18:37:57 -080071 leadershipService.addListener(leaderListener);
Jonathan Hartf2fda812015-02-17 15:21:03 -080072 clusterService.addListener(clusterListener);
Jonathan Hart74c83132015-02-02 18:37:57 -080073
74 for (int i = 0; i < NUM_PARTITIONS; i++) {
Jonathan Hartf2fda812015-02-17 15:21:03 -080075 leadershipService.runForLeadership(getPartitionPath(i));
Jonathan Hart74c83132015-02-02 18:37:57 -080076 }
Jonathan Hartf2fda812015-02-17 15:21:03 -080077
78 executor.scheduleAtFixedRate(this::doRelinquish, 0,
79 CHECK_PERIOD, TimeUnit.SECONDS);
Jonathan Hart74c83132015-02-02 18:37:57 -080080 }
81
82 @Deactivate
83 public void deactivate() {
Jonathan Hartac48a952015-02-25 14:11:55 -080084 executor.shutdownNow();
85
Jonathan Hart74c83132015-02-02 18:37:57 -080086 leadershipService.removeListener(leaderListener);
Jonathan Hartf2fda812015-02-17 15:21:03 -080087 clusterService.removeListener(clusterListener);
88 }
89
90 private String getPartitionPath(int i) {
91 return ELECTION_PREFIX + i;
Jonathan Hart74c83132015-02-02 18:37:57 -080092 }
93
Jonathan Hartdc9d7b82015-02-22 17:59:50 -080094 private String getPartitionPath(PartitionId id) {
95 return getPartitionPath(id.value());
96 }
97
Jonathan Hart5ec32ba2015-02-05 13:33:58 -080098 private PartitionId getPartitionForKey(Key intentKey) {
Brian O'Connor1fdfacd2015-02-18 20:52:06 -080099 int partition = Math.abs((int) intentKey.hash()) % NUM_PARTITIONS;
100 //TODO investigate Guava consistent hash method
101 // ... does it add significant computational complexity? is it worth it?
102 //int partition = consistentHash(intentKey.hash(), NUM_PARTITIONS);
103 PartitionId id = new PartitionId(partition);
104 log.debug("Getting partition for {}: {}", intentKey, id); //FIXME debug
105 return id;
Jonathan Hart74c83132015-02-02 18:37:57 -0800106 }
107
108 @Override
Jonathan Hart5ec32ba2015-02-05 13:33:58 -0800109 public boolean isMine(Key intentKey) {
Jonathan Hartdc9d7b82015-02-22 17:59:50 -0800110 return leadershipService.getLeader(getPartitionPath(getPartitionForKey(intentKey)))
111 .equals(clusterService.getLocalNode().id());
Jonathan Hart74c83132015-02-02 18:37:57 -0800112 }
113
Jonathan Hartf2fda812015-02-17 15:21:03 -0800114 private void doRelinquish() {
115 try {
116 relinquish();
117 } catch (Exception e) {
118 log.warn("Exception caught during relinquish task", e);
119 }
120 }
121
Jonathan Hartf2fda812015-02-17 15:21:03 -0800122 /**
123 * Determine whether we have more than our fair share of partitions, and if
124 * so, relinquish leadership of some of them for a little while to let
125 * other instances take over.
126 */
127 private void relinquish() {
128 int activeNodes = (int) clusterService.getNodes()
129 .stream()
130 .filter(n -> clusterService.getState(n.id())
131 == ControllerNode.State.ACTIVE)
132 .count();
133
134 int myShare = (int) Math.ceil((double) NUM_PARTITIONS / activeNodes);
135
Jonathan Hartdc9d7b82015-02-22 17:59:50 -0800136 List<Leadership> myPartitions = leadershipService.getLeaderBoard().values()
137 .stream()
138 .filter(l -> clusterService.getLocalNode().id().equals(l.leader()))
139 .filter(l -> l.topic().startsWith(ELECTION_PREFIX))
140 .collect(Collectors.toList());
Jonathan Hartf2fda812015-02-17 15:21:03 -0800141
Jonathan Hartdc9d7b82015-02-22 17:59:50 -0800142 int relinquish = myPartitions.size() - myShare;
Jonathan Hartf2fda812015-02-17 15:21:03 -0800143
Jonathan Hartdc9d7b82015-02-22 17:59:50 -0800144 if (relinquish <= 0) {
145 return;
146 }
Jonathan Hartf2fda812015-02-17 15:21:03 -0800147
Jonathan Hartdc9d7b82015-02-22 17:59:50 -0800148 for (int i = 0; i < relinquish; i++) {
149 String topic = myPartitions.get(i).topic();
150 leadershipService.withdraw(topic);
Jonathan Hartf2fda812015-02-17 15:21:03 -0800151
Jonathan Hartdc9d7b82015-02-22 17:59:50 -0800152 executor.schedule(() -> recontest(topic),
153 BACKOFF_TIME, TimeUnit.SECONDS);
Jonathan Hartf2fda812015-02-17 15:21:03 -0800154 }
155 }
156
157 /**
158 * Try and recontest for leadership of a partition.
159 *
160 * @param path topic name to recontest
161 */
162 private void recontest(String path) {
163 leadershipService.runForLeadership(path);
164 }
165
Jonathan Hart74c83132015-02-02 18:37:57 -0800166 private final class InternalLeadershipListener implements LeadershipEventListener {
167
168 @Override
169 public void event(LeadershipEvent event) {
170 Leadership leadership = event.subject();
Jonathan Hartdc9d7b82015-02-22 17:59:50 -0800171
Jonathan Hart74c83132015-02-02 18:37:57 -0800172 if (leadership.leader().equals(clusterService.getLocalNode().id()) &&
173 leadership.topic().startsWith(ELECTION_PREFIX)) {
174
Jonathan Hartf2fda812015-02-17 15:21:03 -0800175 // See if we need to let some partitions go
176 relinquish();
Jonathan Hart74c83132015-02-02 18:37:57 -0800177 }
Jonathan Hart74c83132015-02-02 18:37:57 -0800178 }
179 }
Jonathan Hartf2fda812015-02-17 15:21:03 -0800180
181 private final class InternalClusterEventListener implements
182 ClusterEventListener {
183
184 @Override
185 public void event(ClusterEvent event) {
186 relinquish();
187 }
188 }
Jonathan Hart74c83132015-02-02 18:37:57 -0800189}