Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 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 | |
| 17 | package org.onosproject.store.primitives.impl; |
| 18 | |
| 19 | import static org.slf4j.LoggerFactory.getLogger; |
| 20 | |
| 21 | import java.io.File; |
Madan Jampani | e14a09c | 2016-02-11 10:43:21 -0800 | [diff] [blame] | 22 | import java.util.List; |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 23 | import java.util.Map; |
| 24 | import java.util.Set; |
| 25 | import java.util.concurrent.CompletableFuture; |
Madan Jampani | 3354745 | 2016-02-29 16:45:04 -0800 | [diff] [blame] | 26 | import java.util.concurrent.atomic.AtomicReference; |
Madan Jampani | e14a09c | 2016-02-11 10:43:21 -0800 | [diff] [blame] | 27 | import java.util.stream.Collectors; |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 28 | |
| 29 | import org.apache.felix.scr.annotations.Activate; |
| 30 | import org.apache.felix.scr.annotations.Component; |
Madan Jampani | 86cb243 | 2016-02-17 11:07:56 -0800 | [diff] [blame] | 31 | import org.apache.felix.scr.annotations.Deactivate; |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 32 | import org.apache.felix.scr.annotations.Reference; |
| 33 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 34 | import org.apache.felix.scr.annotations.Service; |
| 35 | import org.onlab.util.Tools; |
Madan Jampani | 3354745 | 2016-02-29 16:45:04 -0800 | [diff] [blame] | 36 | import org.onosproject.cluster.ClusterMetadata; |
| 37 | import org.onosproject.cluster.ClusterMetadataDiff; |
| 38 | import org.onosproject.cluster.ClusterMetadataEvent; |
| 39 | import org.onosproject.cluster.ClusterMetadataEventListener; |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 40 | import org.onosproject.cluster.ClusterMetadataService; |
| 41 | import org.onosproject.cluster.ClusterService; |
| 42 | import org.onosproject.cluster.NodeId; |
Madan Jampani | 3354745 | 2016-02-29 16:45:04 -0800 | [diff] [blame] | 43 | import org.onosproject.cluster.PartitionDiff; |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 44 | import org.onosproject.cluster.PartitionId; |
| 45 | import org.onosproject.event.AbstractListenerManager; |
| 46 | import org.onosproject.store.cluster.messaging.MessagingService; |
| 47 | import org.onosproject.store.primitives.DistributedPrimitiveCreator; |
| 48 | import org.onosproject.store.primitives.PartitionAdminService; |
| 49 | import org.onosproject.store.primitives.PartitionEvent; |
| 50 | import org.onosproject.store.primitives.PartitionEventListener; |
| 51 | import org.onosproject.store.primitives.PartitionService; |
Madan Jampani | e14a09c | 2016-02-11 10:43:21 -0800 | [diff] [blame] | 52 | import org.onosproject.store.service.PartitionInfo; |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 53 | import org.slf4j.Logger; |
| 54 | |
| 55 | import com.google.common.collect.ImmutableSet; |
| 56 | import com.google.common.collect.Maps; |
| 57 | |
Heedo Kang | 4a47a30 | 2016-02-29 17:40:23 +0900 | [diff] [blame] | 58 | import static org.onosproject.security.AppGuard.checkPermission; |
| 59 | import static org.onosproject.security.AppPermission.Type.PARTITION_READ; |
| 60 | |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 61 | /** |
| 62 | * Implementation of {@code PartitionService} and {@code PartitionAdminService}. |
| 63 | */ |
| 64 | @Component |
| 65 | @Service |
| 66 | public class PartitionManager extends AbstractListenerManager<PartitionEvent, PartitionEventListener> |
| 67 | implements PartitionService, PartitionAdminService { |
| 68 | |
| 69 | private final Logger log = getLogger(getClass()); |
| 70 | |
| 71 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 72 | protected MessagingService messagingService; |
| 73 | |
| 74 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 75 | protected ClusterMetadataService metadataService; |
| 76 | |
| 77 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 78 | protected ClusterService clusterService; |
| 79 | |
Madan Jampani | 3354745 | 2016-02-29 16:45:04 -0800 | [diff] [blame] | 80 | private final Map<PartitionId, StoragePartition> partitions = Maps.newConcurrentMap(); |
| 81 | private final AtomicReference<ClusterMetadata> currentClusterMetadata = new AtomicReference<>(); |
| 82 | private final InternalClusterMetadataListener metadataListener = new InternalClusterMetadataListener(); |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 83 | |
| 84 | @Activate |
| 85 | public void activate() { |
| 86 | eventDispatcher.addSink(PartitionEvent.class, listenerRegistry); |
Madan Jampani | 3354745 | 2016-02-29 16:45:04 -0800 | [diff] [blame] | 87 | currentClusterMetadata.set(metadataService.getClusterMetadata()); |
| 88 | metadataService.addListener(metadataListener); |
| 89 | currentClusterMetadata.get() |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 90 | .getPartitions() |
| 91 | .stream() |
Madan Jampani | 3354745 | 2016-02-29 16:45:04 -0800 | [diff] [blame] | 92 | .filter(partition -> !partition.getId().equals(PartitionId.from(0))) // exclude p0 |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 93 | .forEach(partition -> partitions.put(partition.getId(), new StoragePartition(partition, |
| 94 | messagingService, |
| 95 | clusterService, |
| 96 | CatalystSerializers.getSerializer(), |
| 97 | new File(System.getProperty("karaf.data") + "/data/" + partition.getId())))); |
| 98 | |
| 99 | CompletableFuture<Void> openFuture = CompletableFuture.allOf(partitions.values() |
| 100 | .stream() |
| 101 | .map(StoragePartition::open) |
| 102 | .toArray(CompletableFuture[]::new)); |
| 103 | openFuture.join(); |
| 104 | log.info("Started"); |
| 105 | } |
| 106 | |
Madan Jampani | 86cb243 | 2016-02-17 11:07:56 -0800 | [diff] [blame] | 107 | @Deactivate |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 108 | public void deactivate() { |
Madan Jampani | 3354745 | 2016-02-29 16:45:04 -0800 | [diff] [blame] | 109 | metadataService.removeListener(metadataListener); |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 110 | eventDispatcher.removeSink(PartitionEvent.class); |
| 111 | |
| 112 | CompletableFuture<Void> closeFuture = CompletableFuture.allOf(partitions.values() |
| 113 | .stream() |
| 114 | .map(StoragePartition::close) |
| 115 | .toArray(CompletableFuture[]::new)); |
| 116 | closeFuture.join(); |
| 117 | log.info("Stopped"); |
| 118 | } |
| 119 | |
| 120 | @Override |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 121 | public int getNumberOfPartitions() { |
Heedo Kang | 4a47a30 | 2016-02-29 17:40:23 +0900 | [diff] [blame] | 122 | checkPermission(PARTITION_READ); |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 123 | return partitions.size(); |
| 124 | } |
| 125 | |
| 126 | @Override |
| 127 | public Set<PartitionId> getAllPartitionIds() { |
Heedo Kang | 4a47a30 | 2016-02-29 17:40:23 +0900 | [diff] [blame] | 128 | checkPermission(PARTITION_READ); |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 129 | return partitions.keySet(); |
| 130 | } |
| 131 | |
| 132 | @Override |
| 133 | public DistributedPrimitiveCreator getDistributedPrimitiveCreator(PartitionId partitionId) { |
Heedo Kang | 4a47a30 | 2016-02-29 17:40:23 +0900 | [diff] [blame] | 134 | checkPermission(PARTITION_READ); |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 135 | return partitions.get(partitionId).client(); |
| 136 | } |
| 137 | |
| 138 | @Override |
| 139 | public Set<NodeId> getConfiguredMembers(PartitionId partitionId) { |
Heedo Kang | 4a47a30 | 2016-02-29 17:40:23 +0900 | [diff] [blame] | 140 | checkPermission(PARTITION_READ); |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 141 | StoragePartition partition = partitions.get(partitionId); |
| 142 | return ImmutableSet.copyOf(partition.getMembers()); |
| 143 | } |
| 144 | |
| 145 | @Override |
| 146 | public Set<NodeId> getActiveMembersMembers(PartitionId partitionId) { |
Heedo Kang | 4a47a30 | 2016-02-29 17:40:23 +0900 | [diff] [blame] | 147 | checkPermission(PARTITION_READ); |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 148 | // TODO: This needs to query metadata to determine currently active |
| 149 | // members of partition |
| 150 | return getConfiguredMembers(partitionId); |
| 151 | } |
Madan Jampani | e14a09c | 2016-02-11 10:43:21 -0800 | [diff] [blame] | 152 | |
| 153 | @Override |
| 154 | public List<PartitionInfo> partitionInfo() { |
| 155 | return partitions.values() |
| 156 | .stream() |
Sho SHIMIZU | 5fab6e5 | 2016-02-15 11:54:15 -0800 | [diff] [blame] | 157 | .flatMap(x -> Tools.stream(x.info())) |
Madan Jampani | e14a09c | 2016-02-11 10:43:21 -0800 | [diff] [blame] | 158 | .collect(Collectors.toList()); |
| 159 | } |
Madan Jampani | 3354745 | 2016-02-29 16:45:04 -0800 | [diff] [blame] | 160 | |
| 161 | private void processMetadataUpdate(ClusterMetadata clusterMetadata) { |
| 162 | ClusterMetadataDiff diffExaminer = |
| 163 | new ClusterMetadataDiff(currentClusterMetadata.get(), clusterMetadata); |
| 164 | diffExaminer.partitionDiffs() |
| 165 | .values() |
| 166 | .stream() |
| 167 | // TODO: Remove after partition 0 is removed from cluster metadata. |
| 168 | .filter(diff -> !diff.partitionId().equals(PartitionId.from(0))) |
| 169 | .filter(PartitionDiff::hasChanged) |
| 170 | .forEach(diff -> partitions.get(diff.partitionId()).onUpdate(diff.newValue())); |
| 171 | } |
| 172 | |
| 173 | private class InternalClusterMetadataListener implements ClusterMetadataEventListener { |
| 174 | @Override |
| 175 | public void event(ClusterMetadataEvent event) { |
| 176 | processMetadataUpdate(event.subject()); |
| 177 | } |
| 178 | } |
Madan Jampani | 2f9cc71 | 2016-02-15 19:36:21 -0800 | [diff] [blame] | 179 | } |