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; |
Madan Jampani | e14a09c | 2016-02-11 10:43:21 -0800 | [diff] [blame] | 24 | import java.util.Optional; |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 25 | import java.util.Set; |
| 26 | import java.util.concurrent.CompletableFuture; |
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; |
| 31 | import org.apache.felix.scr.annotations.Reference; |
| 32 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 33 | import org.apache.felix.scr.annotations.Service; |
| 34 | import org.onlab.util.Tools; |
| 35 | import org.onosproject.cluster.ClusterMetadataService; |
| 36 | import org.onosproject.cluster.ClusterService; |
| 37 | import org.onosproject.cluster.NodeId; |
| 38 | import org.onosproject.cluster.PartitionId; |
| 39 | import org.onosproject.event.AbstractListenerManager; |
| 40 | import org.onosproject.store.cluster.messaging.MessagingService; |
| 41 | import org.onosproject.store.primitives.DistributedPrimitiveCreator; |
| 42 | import org.onosproject.store.primitives.PartitionAdminService; |
| 43 | import org.onosproject.store.primitives.PartitionEvent; |
| 44 | import org.onosproject.store.primitives.PartitionEventListener; |
| 45 | import org.onosproject.store.primitives.PartitionService; |
Madan Jampani | e14a09c | 2016-02-11 10:43:21 -0800 | [diff] [blame] | 46 | import org.onosproject.store.service.PartitionInfo; |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 47 | import org.slf4j.Logger; |
| 48 | |
| 49 | import com.google.common.collect.ImmutableSet; |
| 50 | import com.google.common.collect.Maps; |
| 51 | |
| 52 | /** |
| 53 | * Implementation of {@code PartitionService} and {@code PartitionAdminService}. |
| 54 | */ |
| 55 | @Component |
| 56 | @Service |
| 57 | public class PartitionManager extends AbstractListenerManager<PartitionEvent, PartitionEventListener> |
| 58 | implements PartitionService, PartitionAdminService { |
| 59 | |
Madan Jampani | 2f9cc71 | 2016-02-15 19:36:21 -0800 | [diff] [blame^] | 60 | public static final String HELLO_MESSAGE_SUBJECT = "partition-manager-hello"; |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 61 | private final Logger log = getLogger(getClass()); |
| 62 | |
| 63 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 64 | protected MessagingService messagingService; |
| 65 | |
| 66 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 67 | protected ClusterMetadataService metadataService; |
| 68 | |
| 69 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 70 | protected ClusterService clusterService; |
| 71 | |
| 72 | Map<PartitionId, StoragePartition> partitions = Maps.newConcurrentMap(); |
| 73 | |
| 74 | @Activate |
| 75 | public void activate() { |
Madan Jampani | 2f9cc71 | 2016-02-15 19:36:21 -0800 | [diff] [blame^] | 76 | messagingService.registerHandler(HELLO_MESSAGE_SUBJECT, |
| 77 | (ep, input) -> CompletableFuture.completedFuture(input)); |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 78 | eventDispatcher.addSink(PartitionEvent.class, listenerRegistry); |
| 79 | |
| 80 | metadataService.getClusterMetadata() |
| 81 | .getPartitions() |
| 82 | .stream() |
| 83 | .forEach(partition -> partitions.put(partition.getId(), new StoragePartition(partition, |
| 84 | messagingService, |
| 85 | clusterService, |
| 86 | CatalystSerializers.getSerializer(), |
| 87 | new File(System.getProperty("karaf.data") + "/data/" + partition.getId())))); |
| 88 | |
| 89 | CompletableFuture<Void> openFuture = CompletableFuture.allOf(partitions.values() |
| 90 | .stream() |
| 91 | .map(StoragePartition::open) |
| 92 | .toArray(CompletableFuture[]::new)); |
| 93 | openFuture.join(); |
| 94 | log.info("Started"); |
| 95 | } |
| 96 | |
| 97 | public void deactivate() { |
Madan Jampani | 2f9cc71 | 2016-02-15 19:36:21 -0800 | [diff] [blame^] | 98 | messagingService.unregisterHandler(HELLO_MESSAGE_SUBJECT); |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 99 | eventDispatcher.removeSink(PartitionEvent.class); |
| 100 | |
| 101 | CompletableFuture<Void> closeFuture = CompletableFuture.allOf(partitions.values() |
| 102 | .stream() |
| 103 | .map(StoragePartition::close) |
| 104 | .toArray(CompletableFuture[]::new)); |
| 105 | closeFuture.join(); |
| 106 | log.info("Stopped"); |
| 107 | } |
| 108 | |
| 109 | @Override |
| 110 | public CompletableFuture<Void> leave(PartitionId partitionId) { |
| 111 | // TODO: Implement |
| 112 | return Tools.exceptionalFuture(new UnsupportedOperationException()); |
| 113 | } |
| 114 | |
| 115 | @Override |
| 116 | public CompletableFuture<Void> join(PartitionId partitionId) { |
| 117 | // TODO: Implement |
| 118 | return Tools.exceptionalFuture(new UnsupportedOperationException()); |
| 119 | } |
| 120 | |
| 121 | @Override |
| 122 | public int getNumberOfPartitions() { |
| 123 | return partitions.size(); |
| 124 | } |
| 125 | |
| 126 | @Override |
| 127 | public Set<PartitionId> getAllPartitionIds() { |
| 128 | return partitions.keySet(); |
| 129 | } |
| 130 | |
| 131 | @Override |
| 132 | public DistributedPrimitiveCreator getDistributedPrimitiveCreator(PartitionId partitionId) { |
| 133 | return partitions.get(partitionId).client(); |
| 134 | } |
| 135 | |
| 136 | @Override |
| 137 | public Set<NodeId> getConfiguredMembers(PartitionId partitionId) { |
| 138 | StoragePartition partition = partitions.get(partitionId); |
| 139 | return ImmutableSet.copyOf(partition.getMembers()); |
| 140 | } |
| 141 | |
| 142 | @Override |
| 143 | public Set<NodeId> getActiveMembersMembers(PartitionId partitionId) { |
| 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() |
| 153 | .map(StoragePartition::info) |
| 154 | .filter(Optional::isPresent) |
| 155 | .map(Optional::get) |
| 156 | .collect(Collectors.toList()); |
| 157 | } |
Madan Jampani | 2f9cc71 | 2016-02-15 19:36:21 -0800 | [diff] [blame^] | 158 | } |