Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
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; |
Jordan Halterman | 980a8c1 | 2017-09-22 18:01:19 -0700 | [diff] [blame] | 45 | import org.onosproject.core.Version; |
| 46 | import org.onosproject.core.VersionService; |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 47 | import org.onosproject.event.AbstractListenerManager; |
Jordan Halterman | 28183ee | 2017-10-17 17:29:10 -0700 | [diff] [blame] | 48 | import org.onosproject.store.cluster.messaging.ClusterCommunicationService; |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 49 | import org.onosproject.store.primitives.DistributedPrimitiveCreator; |
| 50 | import org.onosproject.store.primitives.PartitionAdminService; |
| 51 | import org.onosproject.store.primitives.PartitionEvent; |
| 52 | import org.onosproject.store.primitives.PartitionEventListener; |
| 53 | import org.onosproject.store.primitives.PartitionService; |
Madan Jampani | ccdf9da | 2016-05-05 14:37:27 -0700 | [diff] [blame] | 54 | import org.onosproject.store.service.PartitionClientInfo; |
Madan Jampani | e14a09c | 2016-02-11 10:43:21 -0800 | [diff] [blame] | 55 | import org.onosproject.store.service.PartitionInfo; |
Jordan Halterman | 980a8c1 | 2017-09-22 18:01:19 -0700 | [diff] [blame] | 56 | import org.onosproject.upgrade.UpgradeService; |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 57 | import org.slf4j.Logger; |
| 58 | |
Heedo Kang | 4a47a30 | 2016-02-29 17:40:23 +0900 | [diff] [blame] | 59 | import static org.onosproject.security.AppGuard.checkPermission; |
| 60 | import static org.onosproject.security.AppPermission.Type.PARTITION_READ; |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 61 | import static org.slf4j.LoggerFactory.getLogger; |
Heedo Kang | 4a47a30 | 2016-02-29 17:40:23 +0900 | [diff] [blame] | 62 | |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 63 | /** |
| 64 | * Implementation of {@code PartitionService} and {@code PartitionAdminService}. |
| 65 | */ |
| 66 | @Component |
| 67 | @Service |
| 68 | public class PartitionManager extends AbstractListenerManager<PartitionEvent, PartitionEventListener> |
| 69 | implements PartitionService, PartitionAdminService { |
| 70 | |
Thomas Vachuska | 58bf491 | 2017-10-31 12:00:32 -0700 | [diff] [blame^] | 71 | static final String PARTITIONS_DIR = |
| 72 | System.getProperty("karaf.data") + "/db/partitions/"; |
| 73 | |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 74 | private final Logger log = getLogger(getClass()); |
| 75 | |
| 76 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Jordan Halterman | 28183ee | 2017-10-17 17:29:10 -0700 | [diff] [blame] | 77 | protected ClusterCommunicationService clusterCommunicator; |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 78 | |
| 79 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 80 | protected ClusterMetadataService metadataService; |
| 81 | |
| 82 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 83 | protected ClusterService clusterService; |
| 84 | |
Jordan Halterman | 980a8c1 | 2017-09-22 18:01:19 -0700 | [diff] [blame] | 85 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 86 | protected UpgradeService upgradeService; |
| 87 | |
| 88 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 89 | protected VersionService versionService; |
| 90 | |
| 91 | private final Map<PartitionId, StoragePartition> inactivePartitions = Maps.newConcurrentMap(); |
| 92 | private final Map<PartitionId, StoragePartition> activePartitions = Maps.newConcurrentMap(); |
Madan Jampani | 3354745 | 2016-02-29 16:45:04 -0800 | [diff] [blame] | 93 | private final AtomicReference<ClusterMetadata> currentClusterMetadata = new AtomicReference<>(); |
| 94 | private final InternalClusterMetadataListener metadataListener = new InternalClusterMetadataListener(); |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 95 | |
| 96 | @Activate |
| 97 | public void activate() { |
| 98 | eventDispatcher.addSink(PartitionEvent.class, listenerRegistry); |
Madan Jampani | 3354745 | 2016-02-29 16:45:04 -0800 | [diff] [blame] | 99 | currentClusterMetadata.set(metadataService.getClusterMetadata()); |
| 100 | metadataService.addListener(metadataListener); |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 101 | |
Jordan Halterman | 980a8c1 | 2017-09-22 18:01:19 -0700 | [diff] [blame] | 102 | // If an upgrade is currently in progress and this node is an upgraded node, initialize upgrade partitions. |
| 103 | CompletableFuture<Void> openFuture; |
| 104 | if (upgradeService.isUpgrading() && upgradeService.isLocalUpgraded()) { |
| 105 | Version sourceVersion = upgradeService.getState().source(); |
| 106 | Version targetVersion = upgradeService.getState().target(); |
| 107 | currentClusterMetadata.get() |
| 108 | .getPartitions() |
Jordan Halterman | 28183ee | 2017-10-17 17:29:10 -0700 | [diff] [blame] | 109 | .forEach(partition -> { |
| 110 | inactivePartitions.put(partition.getId(), new StoragePartition( |
| 111 | partition, |
| 112 | sourceVersion, |
| 113 | null, |
| 114 | clusterCommunicator, |
| 115 | clusterService, |
Thomas Vachuska | 58bf491 | 2017-10-31 12:00:32 -0700 | [diff] [blame^] | 116 | new File(PARTITIONS_DIR + sourceVersion + "/" + partition.getId()))); |
Jordan Halterman | 28183ee | 2017-10-17 17:29:10 -0700 | [diff] [blame] | 117 | activePartitions.put(partition.getId(), new StoragePartition( |
| 118 | partition, |
| 119 | targetVersion, |
| 120 | sourceVersion, |
| 121 | clusterCommunicator, |
| 122 | clusterService, |
Thomas Vachuska | 58bf491 | 2017-10-31 12:00:32 -0700 | [diff] [blame^] | 123 | new File(PARTITIONS_DIR + targetVersion + "/" + partition.getId()))); |
Jordan Halterman | 28183ee | 2017-10-17 17:29:10 -0700 | [diff] [blame] | 124 | }); |
Jordan Halterman | 980a8c1 | 2017-09-22 18:01:19 -0700 | [diff] [blame] | 125 | |
| 126 | // We have to fork existing partitions before we can start inactive partition servers to |
| 127 | // avoid duplicate message handlers when both servers are running. |
| 128 | openFuture = CompletableFuture.allOf(activePartitions.values().stream() |
| 129 | .map(StoragePartition::open) |
| 130 | .toArray(CompletableFuture[]::new)) |
| 131 | .thenCompose(v -> CompletableFuture.allOf(inactivePartitions.values().stream() |
| 132 | .map(StoragePartition::open) |
| 133 | .toArray(CompletableFuture[]::new))); |
| 134 | } else { |
| 135 | Version version = versionService.version(); |
| 136 | currentClusterMetadata.get() |
| 137 | .getPartitions() |
| 138 | .forEach(partition -> activePartitions.put(partition.getId(), new StoragePartition( |
| 139 | partition, |
| 140 | version, |
| 141 | null, |
| 142 | clusterCommunicator, |
| 143 | clusterService, |
Thomas Vachuska | 58bf491 | 2017-10-31 12:00:32 -0700 | [diff] [blame^] | 144 | new File(PARTITIONS_DIR + version + "/" + partition.getId())))); |
Jordan Halterman | 980a8c1 | 2017-09-22 18:01:19 -0700 | [diff] [blame] | 145 | openFuture = CompletableFuture.allOf(activePartitions.values().stream() |
| 146 | .map(StoragePartition::open) |
| 147 | .toArray(CompletableFuture[]::new)); |
| 148 | } |
| 149 | |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 150 | openFuture.join(); |
| 151 | log.info("Started"); |
| 152 | } |
| 153 | |
Madan Jampani | 86cb243 | 2016-02-17 11:07:56 -0800 | [diff] [blame] | 154 | @Deactivate |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 155 | public void deactivate() { |
Madan Jampani | 3354745 | 2016-02-29 16:45:04 -0800 | [diff] [blame] | 156 | metadataService.removeListener(metadataListener); |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 157 | eventDispatcher.removeSink(PartitionEvent.class); |
| 158 | |
Jordan Halterman | 980a8c1 | 2017-09-22 18:01:19 -0700 | [diff] [blame] | 159 | CompletableFuture<Void> closeFuture = CompletableFuture.allOf( |
| 160 | CompletableFuture.allOf(inactivePartitions.values().stream() |
| 161 | .map(StoragePartition::close) |
| 162 | .toArray(CompletableFuture[]::new)), |
| 163 | CompletableFuture.allOf(activePartitions.values().stream() |
| 164 | .map(StoragePartition::close) |
| 165 | .toArray(CompletableFuture[]::new))); |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 166 | closeFuture.join(); |
| 167 | log.info("Stopped"); |
| 168 | } |
| 169 | |
| 170 | @Override |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 171 | public int getNumberOfPartitions() { |
Heedo Kang | 4a47a30 | 2016-02-29 17:40:23 +0900 | [diff] [blame] | 172 | checkPermission(PARTITION_READ); |
Jordan Halterman | 980a8c1 | 2017-09-22 18:01:19 -0700 | [diff] [blame] | 173 | return activePartitions.size(); |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | @Override |
| 177 | public Set<PartitionId> getAllPartitionIds() { |
Heedo Kang | 4a47a30 | 2016-02-29 17:40:23 +0900 | [diff] [blame] | 178 | checkPermission(PARTITION_READ); |
Jordan Halterman | 980a8c1 | 2017-09-22 18:01:19 -0700 | [diff] [blame] | 179 | return activePartitions.keySet(); |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | @Override |
| 183 | public DistributedPrimitiveCreator getDistributedPrimitiveCreator(PartitionId partitionId) { |
Heedo Kang | 4a47a30 | 2016-02-29 17:40:23 +0900 | [diff] [blame] | 184 | checkPermission(PARTITION_READ); |
Jordan Halterman | 980a8c1 | 2017-09-22 18:01:19 -0700 | [diff] [blame] | 185 | return activePartitions.get(partitionId).client(); |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | @Override |
| 189 | public Set<NodeId> getConfiguredMembers(PartitionId partitionId) { |
Heedo Kang | 4a47a30 | 2016-02-29 17:40:23 +0900 | [diff] [blame] | 190 | checkPermission(PARTITION_READ); |
Jordan Halterman | 980a8c1 | 2017-09-22 18:01:19 -0700 | [diff] [blame] | 191 | StoragePartition partition = activePartitions.get(partitionId); |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 192 | return ImmutableSet.copyOf(partition.getMembers()); |
| 193 | } |
| 194 | |
| 195 | @Override |
| 196 | public Set<NodeId> getActiveMembersMembers(PartitionId partitionId) { |
Heedo Kang | 4a47a30 | 2016-02-29 17:40:23 +0900 | [diff] [blame] | 197 | checkPermission(PARTITION_READ); |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 198 | // TODO: This needs to query metadata to determine currently active |
| 199 | // members of partition |
| 200 | return getConfiguredMembers(partitionId); |
| 201 | } |
Madan Jampani | e14a09c | 2016-02-11 10:43:21 -0800 | [diff] [blame] | 202 | |
| 203 | @Override |
| 204 | public List<PartitionInfo> partitionInfo() { |
Jordan Halterman | 980a8c1 | 2017-09-22 18:01:19 -0700 | [diff] [blame] | 205 | return activePartitions.values() |
Madan Jampani | e14a09c | 2016-02-11 10:43:21 -0800 | [diff] [blame] | 206 | .stream() |
Sho SHIMIZU | 5fab6e5 | 2016-02-15 11:54:15 -0800 | [diff] [blame] | 207 | .flatMap(x -> Tools.stream(x.info())) |
Madan Jampani | e14a09c | 2016-02-11 10:43:21 -0800 | [diff] [blame] | 208 | .collect(Collectors.toList()); |
| 209 | } |
Madan Jampani | 3354745 | 2016-02-29 16:45:04 -0800 | [diff] [blame] | 210 | |
| 211 | private void processMetadataUpdate(ClusterMetadata clusterMetadata) { |
| 212 | ClusterMetadataDiff diffExaminer = |
| 213 | new ClusterMetadataDiff(currentClusterMetadata.get(), clusterMetadata); |
| 214 | diffExaminer.partitionDiffs() |
| 215 | .values() |
| 216 | .stream() |
Madan Jampani | 3354745 | 2016-02-29 16:45:04 -0800 | [diff] [blame] | 217 | .filter(PartitionDiff::hasChanged) |
Jordan Halterman | 980a8c1 | 2017-09-22 18:01:19 -0700 | [diff] [blame] | 218 | .forEach(diff -> activePartitions.get(diff.partitionId()).onUpdate(diff.newValue())); |
Madan Jampani | 3354745 | 2016-02-29 16:45:04 -0800 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | private class InternalClusterMetadataListener implements ClusterMetadataEventListener { |
| 222 | @Override |
| 223 | public void event(ClusterMetadataEvent event) { |
| 224 | processMetadataUpdate(event.subject()); |
| 225 | } |
| 226 | } |
Madan Jampani | ccdf9da | 2016-05-05 14:37:27 -0700 | [diff] [blame] | 227 | |
| 228 | @Override |
| 229 | public List<PartitionClientInfo> partitionClientInfo() { |
Jordan Halterman | 980a8c1 | 2017-09-22 18:01:19 -0700 | [diff] [blame] | 230 | return activePartitions.values() |
Madan Jampani | ccdf9da | 2016-05-05 14:37:27 -0700 | [diff] [blame] | 231 | .stream() |
| 232 | .map(StoragePartition::client) |
| 233 | .map(StoragePartitionClient::clientInfo) |
| 234 | .collect(Collectors.toList()); |
| 235 | } |
Madan Jampani | 2f9cc71 | 2016-02-15 19:36:21 -0800 | [diff] [blame] | 236 | } |