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