blob: 5fa26a779c06bb091d5307c102b2f86d58c28150 [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;
Jonathan Hart74c83132015-02-02 18:37:57 -080034import org.slf4j.Logger;
35import org.slf4j.LoggerFactory;
36
Jonathan Hartdc9d7b82015-02-22 17:59:50 -080037import java.util.List;
Brian O'Connor5eb77c82015-03-02 18:09:39 -080038import java.util.Objects;
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) {
Brian O'Connor5eb77c82015-03-02 18:09:39 -0800112 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 Hart74c83132015-02-02 18:37:57 -0800119 }
120
Jonathan Hartf2fda812015-02-17 15:21:03 -0800121 private void doRelinquish() {
122 try {
123 relinquish();
124 } catch (Exception e) {
125 log.warn("Exception caught during relinquish task", e);
126 }
127 }
128
Jonathan Hartf2fda812015-02-17 15:21:03 -0800129 /**
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 Hartdc9d7b82015-02-22 17:59:50 -0800143 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 Hartf2fda812015-02-17 15:21:03 -0800148
Jonathan Hartdc9d7b82015-02-22 17:59:50 -0800149 int relinquish = myPartitions.size() - myShare;
Jonathan Hartf2fda812015-02-17 15:21:03 -0800150
Jonathan Hartdc9d7b82015-02-22 17:59:50 -0800151 if (relinquish <= 0) {
152 return;
153 }
Jonathan Hartf2fda812015-02-17 15:21:03 -0800154
Jonathan Hartdc9d7b82015-02-22 17:59:50 -0800155 for (int i = 0; i < relinquish; i++) {
156 String topic = myPartitions.get(i).topic();
157 leadershipService.withdraw(topic);
Jonathan Hartf2fda812015-02-17 15:21:03 -0800158
Jonathan Hartdc9d7b82015-02-22 17:59:50 -0800159 executor.schedule(() -> recontest(topic),
160 BACKOFF_TIME, TimeUnit.SECONDS);
Jonathan Hartf2fda812015-02-17 15:21:03 -0800161 }
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 Hart74c83132015-02-02 18:37:57 -0800173 private final class InternalLeadershipListener implements LeadershipEventListener {
174
175 @Override
176 public void event(LeadershipEvent event) {
177 Leadership leadership = event.subject();
Jonathan Hartdc9d7b82015-02-22 17:59:50 -0800178
Brian O'Connor5eb77c82015-03-02 18:09:39 -0800179 if (Objects.equals(leadership.leader(), clusterService.getLocalNode().id()) &&
Jonathan Hart74c83132015-02-02 18:37:57 -0800180 leadership.topic().startsWith(ELECTION_PREFIX)) {
181
Jonathan Hartf2fda812015-02-17 15:21:03 -0800182 // See if we need to let some partitions go
183 relinquish();
Jonathan Hart74c83132015-02-02 18:37:57 -0800184 }
Jonathan Hart74c83132015-02-02 18:37:57 -0800185 }
186 }
Jonathan Hartf2fda812015-02-17 15:21:03 -0800187
188 private final class InternalClusterEventListener implements
189 ClusterEventListener {
190
191 @Override
192 public void event(ClusterEvent event) {
193 relinquish();
194 }
195 }
Jonathan Hart74c83132015-02-02 18:37:57 -0800196}