blob: eb831c525160d3765afe79944bdd10db6cdc19b2 [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;
24import org.onosproject.cluster.ClusterService;
25import org.onosproject.cluster.Leadership;
26import org.onosproject.cluster.LeadershipEvent;
27import org.onosproject.cluster.LeadershipEventListener;
28import org.onosproject.cluster.LeadershipService;
29import org.slf4j.Logger;
30import org.slf4j.LoggerFactory;
31
32import java.util.Collections;
33import java.util.Set;
34import java.util.concurrent.ConcurrentHashMap;
35
36import static com.google.common.base.Preconditions.checkNotNull;
37
38/**
39 * Manages the assignment of intent keyspace partitions to instances.
40 */
41@Component(immediate = true)
42@Service
43public class PartitionManager implements PartitionService {
44
45 private static final Logger log = LoggerFactory.getLogger(PartitionManager.class);
46
47 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
48 protected LeadershipService leadershipService;
49
50 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
51 protected ClusterService clusterService;
52
53 // TODO make configurable
54 private static final int NUM_PARTITIONS = 100;
55
56 private static final String ELECTION_PREFIX = "intent-partition-";
57
58 private LeadershipEventListener leaderListener = new InternalLeadershipListener();
59
60 private Set<PartitionId> myPartitions;
61
62 @Activate
63 public void activate() {
64 myPartitions = Collections.newSetFromMap(new ConcurrentHashMap<>());
65
66 leadershipService.addListener(leaderListener);
67
68 for (int i = 0; i < NUM_PARTITIONS; i++) {
69 leadershipService.runForLeadership(ELECTION_PREFIX + i);
70 }
71 }
72
73 @Deactivate
74 public void deactivate() {
75 leadershipService.removeListener(leaderListener);
76 }
77
78 private PartitionId getPartitionForKey(String intentKey) {
79 return new PartitionId(intentKey.hashCode() % NUM_PARTITIONS);
80 }
81
82 @Override
83 public boolean isMine(String intentKey) {
84 return checkNotNull(
85 myPartitions.contains(getPartitionForKey(intentKey)));
86 }
87
88 private final class InternalLeadershipListener implements LeadershipEventListener {
89
90 @Override
91 public void event(LeadershipEvent event) {
92 Leadership leadership = event.subject();
93 // update internal state about which partitions I'm leader of
94 if (leadership.leader().equals(clusterService.getLocalNode().id()) &&
95 leadership.topic().startsWith(ELECTION_PREFIX)) {
96
97 // Parse out the partition ID
98 String[] splitted = leadership.topic().split("-");
99 if (splitted.length != 3) {
100 log.warn("Couldn't parse leader election topic {}", leadership.topic());
101 return;
102 }
103
104 int partitionId;
105 try {
106 partitionId = Integer.parseInt(splitted[2]);
107 } catch (NumberFormatException e) {
108 log.warn("Couldn't parse partition ID {}", splitted[2]);
109 return;
110 }
111
112 if (event.type() == LeadershipEvent.Type.LEADER_ELECTED) {
113 myPartitions.add(new PartitionId(partitionId));
114 } else if (event.type() == LeadershipEvent.Type.LEADER_BOOTED) {
115 myPartitions.remove(new PartitionId(partitionId));
116 }
117 }
118
119 }
120 }
121}