blob: 72590aab44e9fd27906e13be5d6580b8e56cf6ef [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;
Jonathan Hart5ec32ba2015-02-05 13:33:58 -080033import org.onosproject.net.intent.Key;
Brian O'Connor87ba7a72015-03-11 14:40:09 -070034import org.onosproject.net.intent.PartitionService;
Jonathan Hart74c83132015-02-02 18:37:57 -080035import org.slf4j.Logger;
36import org.slf4j.LoggerFactory;
37
Jonathan Hartdc9d7b82015-02-22 17:59:50 -080038import java.util.List;
Brian O'Connor5eb77c82015-03-02 18:09:39 -080039import java.util.Objects;
Jonathan Hartf2fda812015-02-17 15:21:03 -080040import java.util.concurrent.Executors;
41import java.util.concurrent.ScheduledExecutorService;
42import java.util.concurrent.TimeUnit;
Jonathan Hartdc9d7b82015-02-22 17:59:50 -080043import java.util.stream.Collectors;
Jonathan Hart74c83132015-02-02 18:37:57 -080044
Jonathan Hart74c83132015-02-02 18:37:57 -080045/**
46 * Manages the assignment of intent keyspace partitions to instances.
47 */
48@Component(immediate = true)
49@Service
50public 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
Brian O'Connor71503cd2015-02-20 22:05:48 -080060 private static final int NUM_PARTITIONS = 14;
Jonathan Hartf2fda812015-02-17 15:21:03 -080061 private static final int BACKOFF_TIME = 2;
62 private static final int CHECK_PERIOD = 10;
Jonathan Hart74c83132015-02-02 18:37:57 -080063
64 private static final String ELECTION_PREFIX = "intent-partition-";
65
66 private LeadershipEventListener leaderListener = new InternalLeadershipListener();
Jonathan Hartf2fda812015-02-17 15:21:03 -080067 private ClusterEventListener clusterListener = new InternalClusterEventListener();
Jonathan Hart74c83132015-02-02 18:37:57 -080068
Jonathan Hartf2fda812015-02-17 15:21:03 -080069 private ScheduledExecutorService executor = Executors
70 .newScheduledThreadPool(1);
Jonathan Hart74c83132015-02-02 18:37:57 -080071
72 @Activate
73 public void activate() {
Jonathan Hart74c83132015-02-02 18:37:57 -080074 leadershipService.addListener(leaderListener);
Jonathan Hartf2fda812015-02-17 15:21:03 -080075 clusterService.addListener(clusterListener);
Jonathan Hart74c83132015-02-02 18:37:57 -080076
77 for (int i = 0; i < NUM_PARTITIONS; i++) {
Jonathan Hartf2fda812015-02-17 15:21:03 -080078 leadershipService.runForLeadership(getPartitionPath(i));
Jonathan Hart74c83132015-02-02 18:37:57 -080079 }
Jonathan Hartf2fda812015-02-17 15:21:03 -080080
81 executor.scheduleAtFixedRate(this::doRelinquish, 0,
82 CHECK_PERIOD, TimeUnit.SECONDS);
Jonathan Hart74c83132015-02-02 18:37:57 -080083 }
84
85 @Deactivate
86 public void deactivate() {
Jonathan Hartac48a952015-02-25 14:11:55 -080087 executor.shutdownNow();
88
Jonathan Hart74c83132015-02-02 18:37:57 -080089 leadershipService.removeListener(leaderListener);
Jonathan Hartf2fda812015-02-17 15:21:03 -080090 clusterService.removeListener(clusterListener);
91 }
92
93 private String getPartitionPath(int i) {
94 return ELECTION_PREFIX + i;
Jonathan Hart74c83132015-02-02 18:37:57 -080095 }
96
Jonathan Hartdc9d7b82015-02-22 17:59:50 -080097 private String getPartitionPath(PartitionId id) {
98 return getPartitionPath(id.value());
99 }
100
Jonathan Hart5ec32ba2015-02-05 13:33:58 -0800101 private PartitionId getPartitionForKey(Key intentKey) {
Brian O'Connor1fdfacd2015-02-18 20:52:06 -0800102 int partition = Math.abs((int) intentKey.hash()) % NUM_PARTITIONS;
103 //TODO investigate Guava consistent hash method
104 // ... does it add significant computational complexity? is it worth it?
105 //int partition = consistentHash(intentKey.hash(), NUM_PARTITIONS);
106 PartitionId id = new PartitionId(partition);
107 log.debug("Getting partition for {}: {}", intentKey, id); //FIXME debug
108 return id;
Jonathan Hart74c83132015-02-02 18:37:57 -0800109 }
110
111 @Override
Jonathan Hart5ec32ba2015-02-05 13:33:58 -0800112 public boolean isMine(Key intentKey) {
Brian O'Connor5eb77c82015-03-02 18:09:39 -0800113 return Objects.equals(leadershipService.getLeader(getPartitionPath(getPartitionForKey(intentKey))),
114 clusterService.getLocalNode().id());
115 }
116
117 @Override
118 public NodeId getLeader(Key intentKey) {
119 return leadershipService.getLeader(getPartitionPath(getPartitionForKey(intentKey)));
Jonathan Hart74c83132015-02-02 18:37:57 -0800120 }
121
Jonathan Hartf2fda812015-02-17 15:21:03 -0800122 private void doRelinquish() {
123 try {
124 relinquish();
125 } catch (Exception e) {
126 log.warn("Exception caught during relinquish task", e);
127 }
128 }
129
Jonathan Hartf2fda812015-02-17 15:21:03 -0800130 /**
131 * Determine whether we have more than our fair share of partitions, and if
132 * so, relinquish leadership of some of them for a little while to let
133 * other instances take over.
134 */
135 private void relinquish() {
136 int activeNodes = (int) clusterService.getNodes()
137 .stream()
138 .filter(n -> clusterService.getState(n.id())
139 == ControllerNode.State.ACTIVE)
140 .count();
141
142 int myShare = (int) Math.ceil((double) NUM_PARTITIONS / activeNodes);
143
Jonathan Hartdc9d7b82015-02-22 17:59:50 -0800144 List<Leadership> myPartitions = leadershipService.getLeaderBoard().values()
145 .stream()
146 .filter(l -> clusterService.getLocalNode().id().equals(l.leader()))
147 .filter(l -> l.topic().startsWith(ELECTION_PREFIX))
148 .collect(Collectors.toList());
Jonathan Hartf2fda812015-02-17 15:21:03 -0800149
Jonathan Hartdc9d7b82015-02-22 17:59:50 -0800150 int relinquish = myPartitions.size() - myShare;
Jonathan Hartf2fda812015-02-17 15:21:03 -0800151
Jonathan Hartdc9d7b82015-02-22 17:59:50 -0800152 if (relinquish <= 0) {
153 return;
154 }
Jonathan Hartf2fda812015-02-17 15:21:03 -0800155
Jonathan Hartdc9d7b82015-02-22 17:59:50 -0800156 for (int i = 0; i < relinquish; i++) {
157 String topic = myPartitions.get(i).topic();
158 leadershipService.withdraw(topic);
Jonathan Hartf2fda812015-02-17 15:21:03 -0800159
Jonathan Hartdc9d7b82015-02-22 17:59:50 -0800160 executor.schedule(() -> recontest(topic),
161 BACKOFF_TIME, TimeUnit.SECONDS);
Jonathan Hartf2fda812015-02-17 15:21:03 -0800162 }
163 }
164
165 /**
166 * Try and recontest for leadership of a partition.
167 *
168 * @param path topic name to recontest
169 */
170 private void recontest(String path) {
171 leadershipService.runForLeadership(path);
172 }
173
Jonathan Hart74c83132015-02-02 18:37:57 -0800174 private final class InternalLeadershipListener implements LeadershipEventListener {
175
176 @Override
177 public void event(LeadershipEvent event) {
178 Leadership leadership = event.subject();
Jonathan Hartdc9d7b82015-02-22 17:59:50 -0800179
Brian O'Connor5eb77c82015-03-02 18:09:39 -0800180 if (Objects.equals(leadership.leader(), clusterService.getLocalNode().id()) &&
Jonathan Hart74c83132015-02-02 18:37:57 -0800181 leadership.topic().startsWith(ELECTION_PREFIX)) {
182
Jonathan Hartf2fda812015-02-17 15:21:03 -0800183 // See if we need to let some partitions go
184 relinquish();
Jonathan Hart74c83132015-02-02 18:37:57 -0800185 }
Jonathan Hart74c83132015-02-02 18:37:57 -0800186 }
187 }
Jonathan Hartf2fda812015-02-17 15:21:03 -0800188
189 private final class InternalClusterEventListener implements
190 ClusterEventListener {
191
192 @Override
193 public void event(ClusterEvent event) {
194 relinquish();
195 }
196 }
Jonathan Hart74c83132015-02-02 18:37:57 -0800197}