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