Jonathan Hart | 74c8313 | 2015-02-02 18:37:57 -0800 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.store.intent.impl; |
| 17 | |
| 18 | import org.apache.felix.scr.annotations.Activate; |
| 19 | import org.apache.felix.scr.annotations.Component; |
| 20 | import org.apache.felix.scr.annotations.Deactivate; |
| 21 | import org.apache.felix.scr.annotations.Reference; |
| 22 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 23 | import org.apache.felix.scr.annotations.Service; |
| 24 | import org.onosproject.cluster.ClusterService; |
| 25 | import org.onosproject.cluster.Leadership; |
| 26 | import org.onosproject.cluster.LeadershipEvent; |
| 27 | import org.onosproject.cluster.LeadershipEventListener; |
| 28 | import org.onosproject.cluster.LeadershipService; |
Jonathan Hart | 5ec32ba | 2015-02-05 13:33:58 -0800 | [diff] [blame] | 29 | import org.onosproject.net.intent.Key; |
Jonathan Hart | 74c8313 | 2015-02-02 18:37:57 -0800 | [diff] [blame] | 30 | import org.slf4j.Logger; |
| 31 | import org.slf4j.LoggerFactory; |
| 32 | |
| 33 | import java.util.Collections; |
| 34 | import java.util.Set; |
| 35 | import java.util.concurrent.ConcurrentHashMap; |
| 36 | |
Jonathan Hart | 74c8313 | 2015-02-02 18:37:57 -0800 | [diff] [blame] | 37 | /** |
| 38 | * Manages the assignment of intent keyspace partitions to instances. |
| 39 | */ |
| 40 | @Component(immediate = true) |
| 41 | @Service |
| 42 | public 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 Hart | 5ec32ba | 2015-02-05 13:33:58 -0800 | [diff] [blame] | 77 | 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 Hart | 74c8313 | 2015-02-02 18:37:57 -0800 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | @Override |
Jonathan Hart | 5ec32ba | 2015-02-05 13:33:58 -0800 | [diff] [blame] | 84 | public boolean isMine(Key intentKey) { |
| 85 | return myPartitions.contains(getPartitionForKey(intentKey)); |
Jonathan Hart | 74c8313 | 2015-02-02 18:37:57 -0800 | [diff] [blame] | 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 | } |
Jonathan Hart | 74c8313 | 2015-02-02 18:37:57 -0800 | [diff] [blame] | 118 | } |
| 119 | } |
| 120 | } |