blob: 4a926829ae040484e9310474213877b0ccdb1f04 [file] [log] [blame]
Madan Jampani15b8ef52016-02-02 17:35:05 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Madan Jampani15b8ef52016-02-02 17:35:05 -08003 *
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
17package org.onosproject.store.primitives.impl;
18
Madan Jampani15b8ef52016-02-02 17:35:05 -080019import java.io.File;
Madan Jampanie14a09c2016-02-11 10:43:21 -080020import java.util.List;
Madan Jampani15b8ef52016-02-02 17:35:05 -080021import java.util.Map;
22import java.util.Set;
23import java.util.concurrent.CompletableFuture;
Madan Jampani33547452016-02-29 16:45:04 -080024import java.util.concurrent.atomic.AtomicReference;
Madan Jampanie14a09c2016-02-11 10:43:21 -080025import java.util.stream.Collectors;
Madan Jampani15b8ef52016-02-02 17:35:05 -080026
Jordan Halterman2bf177c2017-06-29 01:49:08 -070027import com.google.common.collect.ImmutableSet;
28import com.google.common.collect.Maps;
Madan Jampani15b8ef52016-02-02 17:35:05 -080029import org.apache.felix.scr.annotations.Activate;
30import org.apache.felix.scr.annotations.Component;
Madan Jampani86cb2432016-02-17 11:07:56 -080031import org.apache.felix.scr.annotations.Deactivate;
Madan Jampani15b8ef52016-02-02 17:35:05 -080032import org.apache.felix.scr.annotations.Reference;
33import org.apache.felix.scr.annotations.ReferenceCardinality;
34import org.apache.felix.scr.annotations.Service;
35import org.onlab.util.Tools;
Madan Jampani33547452016-02-29 16:45:04 -080036import org.onosproject.cluster.ClusterMetadata;
37import org.onosproject.cluster.ClusterMetadataDiff;
38import org.onosproject.cluster.ClusterMetadataEvent;
39import org.onosproject.cluster.ClusterMetadataEventListener;
Madan Jampani15b8ef52016-02-02 17:35:05 -080040import org.onosproject.cluster.ClusterMetadataService;
41import org.onosproject.cluster.ClusterService;
42import org.onosproject.cluster.NodeId;
Madan Jampani33547452016-02-29 16:45:04 -080043import org.onosproject.cluster.PartitionDiff;
Madan Jampani15b8ef52016-02-02 17:35:05 -080044import org.onosproject.cluster.PartitionId;
Jordan Halterman980a8c12017-09-22 18:01:19 -070045import org.onosproject.core.Version;
46import org.onosproject.core.VersionService;
Madan Jampani15b8ef52016-02-02 17:35:05 -080047import org.onosproject.event.AbstractListenerManager;
Jordan Halterman980a8c12017-09-22 18:01:19 -070048import org.onosproject.store.cluster.messaging.UnifiedClusterCommunicationService;
Madan Jampani15b8ef52016-02-02 17:35:05 -080049import org.onosproject.store.primitives.DistributedPrimitiveCreator;
50import org.onosproject.store.primitives.PartitionAdminService;
51import org.onosproject.store.primitives.PartitionEvent;
52import org.onosproject.store.primitives.PartitionEventListener;
53import org.onosproject.store.primitives.PartitionService;
Madan Jampaniccdf9da2016-05-05 14:37:27 -070054import org.onosproject.store.service.PartitionClientInfo;
Madan Jampanie14a09c2016-02-11 10:43:21 -080055import org.onosproject.store.service.PartitionInfo;
Jordan Halterman980a8c12017-09-22 18:01:19 -070056import org.onosproject.upgrade.UpgradeService;
Madan Jampani15b8ef52016-02-02 17:35:05 -080057import org.slf4j.Logger;
58
Heedo Kang4a47a302016-02-29 17:40:23 +090059import static org.onosproject.security.AppGuard.checkPermission;
60import static org.onosproject.security.AppPermission.Type.PARTITION_READ;
Jordan Halterman2bf177c2017-06-29 01:49:08 -070061import static org.slf4j.LoggerFactory.getLogger;
Heedo Kang4a47a302016-02-29 17:40:23 +090062
Madan Jampani15b8ef52016-02-02 17:35:05 -080063/**
64 * Implementation of {@code PartitionService} and {@code PartitionAdminService}.
65 */
66@Component
67@Service
68public class PartitionManager extends AbstractListenerManager<PartitionEvent, PartitionEventListener>
69 implements PartitionService, PartitionAdminService {
70
71 private final Logger log = getLogger(getClass());
72
73 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Jordan Halterman980a8c12017-09-22 18:01:19 -070074 protected UnifiedClusterCommunicationService clusterCommunicator;
Madan Jampani15b8ef52016-02-02 17:35:05 -080075
76 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
77 protected ClusterMetadataService metadataService;
78
79 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
80 protected ClusterService clusterService;
81
Jordan Halterman980a8c12017-09-22 18:01:19 -070082 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
83 protected UpgradeService upgradeService;
84
85 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
86 protected VersionService versionService;
87
88 private final Map<PartitionId, StoragePartition> inactivePartitions = Maps.newConcurrentMap();
89 private final Map<PartitionId, StoragePartition> activePartitions = Maps.newConcurrentMap();
Madan Jampani33547452016-02-29 16:45:04 -080090 private final AtomicReference<ClusterMetadata> currentClusterMetadata = new AtomicReference<>();
91 private final InternalClusterMetadataListener metadataListener = new InternalClusterMetadataListener();
Madan Jampani15b8ef52016-02-02 17:35:05 -080092
93 @Activate
94 public void activate() {
95 eventDispatcher.addSink(PartitionEvent.class, listenerRegistry);
Madan Jampani33547452016-02-29 16:45:04 -080096 currentClusterMetadata.set(metadataService.getClusterMetadata());
97 metadataService.addListener(metadataListener);
Madan Jampani15b8ef52016-02-02 17:35:05 -080098
Jordan Halterman980a8c12017-09-22 18:01:19 -070099 // If an upgrade is currently in progress and this node is an upgraded node, initialize upgrade partitions.
100 CompletableFuture<Void> openFuture;
101 if (upgradeService.isUpgrading() && upgradeService.isLocalUpgraded()) {
102 Version sourceVersion = upgradeService.getState().source();
103 Version targetVersion = upgradeService.getState().target();
104 currentClusterMetadata.get()
105 .getPartitions()
106 .forEach(partition -> inactivePartitions.put(partition.getId(), new StoragePartition(
107 partition,
108 sourceVersion,
109 null,
110 clusterCommunicator,
111 clusterService,
112 new File(System.getProperty("karaf.data") +
113 "/partitions/" + sourceVersion + "/" + partition.getId()))));
114 currentClusterMetadata.get()
115 .getPartitions()
116 .forEach(partition -> activePartitions.put(partition.getId(), new StoragePartition(
117 partition,
118 targetVersion,
119 sourceVersion,
120 clusterCommunicator,
121 clusterService,
122 new File(System.getProperty("karaf.data") +
123 "/partitions/" + targetVersion + "/" + partition.getId()))));
124
125 // We have to fork existing partitions before we can start inactive partition servers to
126 // avoid duplicate message handlers when both servers are running.
127 openFuture = CompletableFuture.allOf(activePartitions.values().stream()
128 .map(StoragePartition::open)
129 .toArray(CompletableFuture[]::new))
130 .thenCompose(v -> CompletableFuture.allOf(inactivePartitions.values().stream()
131 .map(StoragePartition::open)
132 .toArray(CompletableFuture[]::new)));
133 } else {
134 Version version = versionService.version();
135 currentClusterMetadata.get()
136 .getPartitions()
137 .forEach(partition -> activePartitions.put(partition.getId(), new StoragePartition(
138 partition,
139 version,
140 null,
141 clusterCommunicator,
142 clusterService,
143 new File(System.getProperty("karaf.data") +
144 "/partitions/" + version + "/" + partition.getId()))));
145 openFuture = CompletableFuture.allOf(activePartitions.values().stream()
146 .map(StoragePartition::open)
147 .toArray(CompletableFuture[]::new));
148 }
149
Madan Jampani15b8ef52016-02-02 17:35:05 -0800150 openFuture.join();
151 log.info("Started");
152 }
153
Madan Jampani86cb2432016-02-17 11:07:56 -0800154 @Deactivate
Madan Jampani15b8ef52016-02-02 17:35:05 -0800155 public void deactivate() {
Madan Jampani33547452016-02-29 16:45:04 -0800156 metadataService.removeListener(metadataListener);
Madan Jampani15b8ef52016-02-02 17:35:05 -0800157 eventDispatcher.removeSink(PartitionEvent.class);
158
Jordan Halterman980a8c12017-09-22 18:01:19 -0700159 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 Jampani15b8ef52016-02-02 17:35:05 -0800166 closeFuture.join();
167 log.info("Stopped");
168 }
169
170 @Override
Madan Jampani15b8ef52016-02-02 17:35:05 -0800171 public int getNumberOfPartitions() {
Heedo Kang4a47a302016-02-29 17:40:23 +0900172 checkPermission(PARTITION_READ);
Jordan Halterman980a8c12017-09-22 18:01:19 -0700173 return activePartitions.size();
Madan Jampani15b8ef52016-02-02 17:35:05 -0800174 }
175
176 @Override
177 public Set<PartitionId> getAllPartitionIds() {
Heedo Kang4a47a302016-02-29 17:40:23 +0900178 checkPermission(PARTITION_READ);
Jordan Halterman980a8c12017-09-22 18:01:19 -0700179 return activePartitions.keySet();
Madan Jampani15b8ef52016-02-02 17:35:05 -0800180 }
181
182 @Override
183 public DistributedPrimitiveCreator getDistributedPrimitiveCreator(PartitionId partitionId) {
Heedo Kang4a47a302016-02-29 17:40:23 +0900184 checkPermission(PARTITION_READ);
Jordan Halterman980a8c12017-09-22 18:01:19 -0700185 return activePartitions.get(partitionId).client();
Madan Jampani15b8ef52016-02-02 17:35:05 -0800186 }
187
188 @Override
189 public Set<NodeId> getConfiguredMembers(PartitionId partitionId) {
Heedo Kang4a47a302016-02-29 17:40:23 +0900190 checkPermission(PARTITION_READ);
Jordan Halterman980a8c12017-09-22 18:01:19 -0700191 StoragePartition partition = activePartitions.get(partitionId);
Madan Jampani15b8ef52016-02-02 17:35:05 -0800192 return ImmutableSet.copyOf(partition.getMembers());
193 }
194
195 @Override
196 public Set<NodeId> getActiveMembersMembers(PartitionId partitionId) {
Heedo Kang4a47a302016-02-29 17:40:23 +0900197 checkPermission(PARTITION_READ);
Madan Jampani15b8ef52016-02-02 17:35:05 -0800198 // TODO: This needs to query metadata to determine currently active
199 // members of partition
200 return getConfiguredMembers(partitionId);
201 }
Madan Jampanie14a09c2016-02-11 10:43:21 -0800202
203 @Override
204 public List<PartitionInfo> partitionInfo() {
Jordan Halterman980a8c12017-09-22 18:01:19 -0700205 return activePartitions.values()
Madan Jampanie14a09c2016-02-11 10:43:21 -0800206 .stream()
Sho SHIMIZU5fab6e52016-02-15 11:54:15 -0800207 .flatMap(x -> Tools.stream(x.info()))
Madan Jampanie14a09c2016-02-11 10:43:21 -0800208 .collect(Collectors.toList());
209 }
Madan Jampani33547452016-02-29 16:45:04 -0800210
211 private void processMetadataUpdate(ClusterMetadata clusterMetadata) {
212 ClusterMetadataDiff diffExaminer =
213 new ClusterMetadataDiff(currentClusterMetadata.get(), clusterMetadata);
214 diffExaminer.partitionDiffs()
215 .values()
216 .stream()
Madan Jampani33547452016-02-29 16:45:04 -0800217 .filter(PartitionDiff::hasChanged)
Jordan Halterman980a8c12017-09-22 18:01:19 -0700218 .forEach(diff -> activePartitions.get(diff.partitionId()).onUpdate(diff.newValue()));
Madan Jampani33547452016-02-29 16:45:04 -0800219 }
220
221 private class InternalClusterMetadataListener implements ClusterMetadataEventListener {
222 @Override
223 public void event(ClusterMetadataEvent event) {
224 processMetadataUpdate(event.subject());
225 }
226 }
Madan Jampaniccdf9da2016-05-05 14:37:27 -0700227
228 @Override
229 public List<PartitionClientInfo> partitionClientInfo() {
Jordan Halterman980a8c12017-09-22 18:01:19 -0700230 return activePartitions.values()
Madan Jampaniccdf9da2016-05-05 14:37:27 -0700231 .stream()
232 .map(StoragePartition::client)
233 .map(StoragePartitionClient::clientInfo)
234 .collect(Collectors.toList());
235 }
Madan Jampani2f9cc712016-02-15 19:36:21 -0800236}