blob: a709a131217ca86ee597edcfa77304f13f6ffd68 [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;
Jonathan Hart5ec32ba2015-02-05 13:33:58 -080029import org.onosproject.net.intent.Key;
Jonathan Hart74c83132015-02-02 18:37:57 -080030import org.slf4j.Logger;
31import org.slf4j.LoggerFactory;
32
33import java.util.Collections;
34import java.util.Set;
35import java.util.concurrent.ConcurrentHashMap;
36
Jonathan Hart74c83132015-02-02 18:37:57 -080037/**
38 * Manages the assignment of intent keyspace partitions to instances.
39 */
40@Component(immediate = true)
41@Service
42public class PartitionManager implements PartitionService {
43
44 private static final Logger log = LoggerFactory.getLogger(PartitionManager.class);
45
46 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
47 protected LeadershipService leadershipService;
48
49 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
50 protected ClusterService clusterService;
51
52 // TODO make configurable
53 private static final int NUM_PARTITIONS = 100;
54
55 private static final String ELECTION_PREFIX = "intent-partition-";
56
57 private LeadershipEventListener leaderListener = new InternalLeadershipListener();
58
59 private Set<PartitionId> myPartitions;
60
61 @Activate
62 public void activate() {
63 myPartitions = Collections.newSetFromMap(new ConcurrentHashMap<>());
64
65 leadershipService.addListener(leaderListener);
66
67 for (int i = 0; i < NUM_PARTITIONS; i++) {
68 leadershipService.runForLeadership(ELECTION_PREFIX + i);
69 }
70 }
71
72 @Deactivate
73 public void deactivate() {
74 leadershipService.removeListener(leaderListener);
75 }
76
Jonathan Hart5ec32ba2015-02-05 13:33:58 -080077 private PartitionId getPartitionForKey(Key intentKey) {
78 log.debug("Getting partition for {}: {}", intentKey,
79 new PartitionId(Math.abs(intentKey.hash()) % NUM_PARTITIONS));
80 return new PartitionId(Math.abs(intentKey.hash()) % NUM_PARTITIONS);
Jonathan Hart74c83132015-02-02 18:37:57 -080081 }
82
83 @Override
Jonathan Hart5ec32ba2015-02-05 13:33:58 -080084 public boolean isMine(Key intentKey) {
85 return myPartitions.contains(getPartitionForKey(intentKey));
Jonathan Hart74c83132015-02-02 18:37:57 -080086 }
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 }
Jonathan Hart74c83132015-02-02 18:37:57 -0800118 }
119 }
120}