blob: 07b89fb87f9024b0a8fc2f35167320ae993aed5e [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
Madan Jampani7f72c3f2015-03-01 17:34:59 -080036import com.google.common.base.Objects;
37
Jonathan Hartdc9d7b82015-02-22 17:59:50 -080038import java.util.List;
Jonathan Hartf2fda812015-02-17 15:21:03 -080039import java.util.concurrent.Executors;
40import java.util.concurrent.ScheduledExecutorService;
41import java.util.concurrent.TimeUnit;
Jonathan Hartdc9d7b82015-02-22 17:59:50 -080042import java.util.stream.Collectors;
Jonathan Hart74c83132015-02-02 18:37:57 -080043
Jonathan Hart74c83132015-02-02 18:37:57 -080044/**
45 * Manages the assignment of intent keyspace partitions to instances.
46 */
47@Component(immediate = true)
48@Service
49public 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'Connor71503cd2015-02-20 22:05:48 -080059 private static final int NUM_PARTITIONS = 14;
Jonathan Hartf2fda812015-02-17 15:21:03 -080060 private static final int BACKOFF_TIME = 2;
61 private static final int CHECK_PERIOD = 10;
Jonathan Hart74c83132015-02-02 18:37:57 -080062
63 private static final String ELECTION_PREFIX = "intent-partition-";
64
65 private LeadershipEventListener leaderListener = new InternalLeadershipListener();
Jonathan Hartf2fda812015-02-17 15:21:03 -080066 private ClusterEventListener clusterListener = new InternalClusterEventListener();
Jonathan Hart74c83132015-02-02 18:37:57 -080067
Jonathan Hartf2fda812015-02-17 15:21:03 -080068 private ScheduledExecutorService executor = Executors
69 .newScheduledThreadPool(1);
Jonathan Hart74c83132015-02-02 18:37:57 -080070
71 @Activate
72 public void activate() {
Jonathan Hart74c83132015-02-02 18:37:57 -080073 leadershipService.addListener(leaderListener);
Jonathan Hartf2fda812015-02-17 15:21:03 -080074 clusterService.addListener(clusterListener);
Jonathan Hart74c83132015-02-02 18:37:57 -080075
76 for (int i = 0; i < NUM_PARTITIONS; i++) {
Jonathan Hartf2fda812015-02-17 15:21:03 -080077 leadershipService.runForLeadership(getPartitionPath(i));
Jonathan Hart74c83132015-02-02 18:37:57 -080078 }
Jonathan Hartf2fda812015-02-17 15:21:03 -080079
80 executor.scheduleAtFixedRate(this::doRelinquish, 0,
81 CHECK_PERIOD, TimeUnit.SECONDS);
Jonathan Hart74c83132015-02-02 18:37:57 -080082 }
83
84 @Deactivate
85 public void deactivate() {
Jonathan Hartac48a952015-02-25 14:11:55 -080086 executor.shutdownNow();
87
Jonathan Hart74c83132015-02-02 18:37:57 -080088 leadershipService.removeListener(leaderListener);
Jonathan Hartf2fda812015-02-17 15:21:03 -080089 clusterService.removeListener(clusterListener);
90 }
91
92 private String getPartitionPath(int i) {
93 return ELECTION_PREFIX + i;
Jonathan Hart74c83132015-02-02 18:37:57 -080094 }
95
Jonathan Hartdc9d7b82015-02-22 17:59:50 -080096 private String getPartitionPath(PartitionId id) {
97 return getPartitionPath(id.value());
98 }
99
Jonathan Hart5ec32ba2015-02-05 13:33:58 -0800100 private PartitionId getPartitionForKey(Key intentKey) {
Brian O'Connor1fdfacd2015-02-18 20:52:06 -0800101 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 Hart74c83132015-02-02 18:37:57 -0800108 }
109
110 @Override
Jonathan Hart5ec32ba2015-02-05 13:33:58 -0800111 public boolean isMine(Key intentKey) {
Madan Jampania88efa42015-03-04 10:45:04 -0800112 return Objects.equal(leadershipService.getLeader(getPartitionPath(getPartitionForKey(intentKey))),
113 clusterService.getLocalNode().id());
Jonathan Hart74c83132015-02-02 18:37:57 -0800114 }
115
Jonathan Hartf2fda812015-02-17 15:21:03 -0800116 private void doRelinquish() {
117 try {
118 relinquish();
119 } catch (Exception e) {
120 log.warn("Exception caught during relinquish task", e);
121 }
122 }
123
Jonathan Hartf2fda812015-02-17 15:21:03 -0800124 /**
125 * Determine whether we have more than our fair share of partitions, and if
126 * so, relinquish leadership of some of them for a little while to let
127 * other instances take over.
128 */
129 private void relinquish() {
130 int activeNodes = (int) clusterService.getNodes()
131 .stream()
132 .filter(n -> clusterService.getState(n.id())
133 == ControllerNode.State.ACTIVE)
134 .count();
135
136 int myShare = (int) Math.ceil((double) NUM_PARTITIONS / activeNodes);
137
Jonathan Hartdc9d7b82015-02-22 17:59:50 -0800138 List<Leadership> myPartitions = leadershipService.getLeaderBoard().values()
139 .stream()
140 .filter(l -> clusterService.getLocalNode().id().equals(l.leader()))
141 .filter(l -> l.topic().startsWith(ELECTION_PREFIX))
142 .collect(Collectors.toList());
Jonathan Hartf2fda812015-02-17 15:21:03 -0800143
Jonathan Hartdc9d7b82015-02-22 17:59:50 -0800144 int relinquish = myPartitions.size() - myShare;
Jonathan Hartf2fda812015-02-17 15:21:03 -0800145
Jonathan Hartdc9d7b82015-02-22 17:59:50 -0800146 if (relinquish <= 0) {
147 return;
148 }
Jonathan Hartf2fda812015-02-17 15:21:03 -0800149
Jonathan Hartdc9d7b82015-02-22 17:59:50 -0800150 for (int i = 0; i < relinquish; i++) {
151 String topic = myPartitions.get(i).topic();
152 leadershipService.withdraw(topic);
Jonathan Hartf2fda812015-02-17 15:21:03 -0800153
Jonathan Hartdc9d7b82015-02-22 17:59:50 -0800154 executor.schedule(() -> recontest(topic),
155 BACKOFF_TIME, TimeUnit.SECONDS);
Jonathan Hartf2fda812015-02-17 15:21:03 -0800156 }
157 }
158
159 /**
160 * Try and recontest for leadership of a partition.
161 *
162 * @param path topic name to recontest
163 */
164 private void recontest(String path) {
165 leadershipService.runForLeadership(path);
166 }
167
Jonathan Hart74c83132015-02-02 18:37:57 -0800168 private final class InternalLeadershipListener implements LeadershipEventListener {
169
170 @Override
171 public void event(LeadershipEvent event) {
172 Leadership leadership = event.subject();
Jonathan Hartdc9d7b82015-02-22 17:59:50 -0800173
Madan Jampani7f72c3f2015-03-01 17:34:59 -0800174 if (Objects.equal(leadership.leader(), clusterService.getLocalNode().id()) &&
Jonathan Hart74c83132015-02-02 18:37:57 -0800175 leadership.topic().startsWith(ELECTION_PREFIX)) {
176
Jonathan Hartf2fda812015-02-17 15:21:03 -0800177 // See if we need to let some partitions go
178 relinquish();
Jonathan Hart74c83132015-02-02 18:37:57 -0800179 }
Jonathan Hart74c83132015-02-02 18:37:57 -0800180 }
181 }
Jonathan Hartf2fda812015-02-17 15:21:03 -0800182
183 private final class InternalClusterEventListener implements
184 ClusterEventListener {
185
186 @Override
187 public void event(ClusterEvent event) {
188 relinquish();
189 }
190 }
Jonathan Hart74c83132015-02-02 18:37:57 -0800191}