blob: 45bd1719c067f387843f19ac6dbc7b96eebbd5bd [file] [log] [blame]
Madan Jampani15b8ef52016-02-02 17:35:05 -08001/*
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
17package org.onosproject.store.primitives.impl;
18
19import static org.slf4j.LoggerFactory.getLogger;
20
21import java.io.File;
Madan Jampanie14a09c2016-02-11 10:43:21 -080022import java.util.List;
Madan Jampani15b8ef52016-02-02 17:35:05 -080023import java.util.Map;
24import java.util.Set;
25import java.util.concurrent.CompletableFuture;
Madan Jampanie14a09c2016-02-11 10:43:21 -080026import java.util.stream.Collectors;
Madan Jampani15b8ef52016-02-02 17:35:05 -080027
28import org.apache.felix.scr.annotations.Activate;
29import org.apache.felix.scr.annotations.Component;
Madan Jampani86cb2432016-02-17 11:07:56 -080030import org.apache.felix.scr.annotations.Deactivate;
Madan Jampani15b8ef52016-02-02 17:35:05 -080031import org.apache.felix.scr.annotations.Reference;
32import org.apache.felix.scr.annotations.ReferenceCardinality;
33import org.apache.felix.scr.annotations.Service;
34import org.onlab.util.Tools;
35import org.onosproject.cluster.ClusterMetadataService;
36import org.onosproject.cluster.ClusterService;
37import org.onosproject.cluster.NodeId;
38import org.onosproject.cluster.PartitionId;
39import org.onosproject.event.AbstractListenerManager;
40import org.onosproject.store.cluster.messaging.MessagingService;
41import org.onosproject.store.primitives.DistributedPrimitiveCreator;
42import org.onosproject.store.primitives.PartitionAdminService;
43import org.onosproject.store.primitives.PartitionEvent;
44import org.onosproject.store.primitives.PartitionEventListener;
45import org.onosproject.store.primitives.PartitionService;
Madan Jampanie14a09c2016-02-11 10:43:21 -080046import org.onosproject.store.service.PartitionInfo;
Madan Jampani15b8ef52016-02-02 17:35:05 -080047import org.slf4j.Logger;
48
49import com.google.common.collect.ImmutableSet;
50import com.google.common.collect.Maps;
51
52/**
53 * Implementation of {@code PartitionService} and {@code PartitionAdminService}.
54 */
55@Component
56@Service
57public class PartitionManager extends AbstractListenerManager<PartitionEvent, PartitionEventListener>
58 implements PartitionService, PartitionAdminService {
59
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() {
75 eventDispatcher.addSink(PartitionEvent.class, listenerRegistry);
76
77 metadataService.getClusterMetadata()
78 .getPartitions()
79 .stream()
80 .forEach(partition -> partitions.put(partition.getId(), new StoragePartition(partition,
81 messagingService,
82 clusterService,
83 CatalystSerializers.getSerializer(),
84 new File(System.getProperty("karaf.data") + "/data/" + partition.getId()))));
85
86 CompletableFuture<Void> openFuture = CompletableFuture.allOf(partitions.values()
87 .stream()
88 .map(StoragePartition::open)
89 .toArray(CompletableFuture[]::new));
90 openFuture.join();
91 log.info("Started");
92 }
93
Madan Jampani86cb2432016-02-17 11:07:56 -080094 @Deactivate
Madan Jampani15b8ef52016-02-02 17:35:05 -080095 public void deactivate() {
96 eventDispatcher.removeSink(PartitionEvent.class);
97
98 CompletableFuture<Void> closeFuture = CompletableFuture.allOf(partitions.values()
99 .stream()
100 .map(StoragePartition::close)
101 .toArray(CompletableFuture[]::new));
102 closeFuture.join();
103 log.info("Stopped");
104 }
105
106 @Override
107 public CompletableFuture<Void> leave(PartitionId partitionId) {
Madan Jampani3a9911c2016-02-21 11:25:45 -0800108 return partitions.get(partitionId)
109 .server()
110 .map(server -> server.close())
111 .orElse(CompletableFuture.completedFuture(null));
Madan Jampani15b8ef52016-02-02 17:35:05 -0800112 }
113
114 @Override
115 public CompletableFuture<Void> join(PartitionId partitionId) {
Madan Jampani3a9911c2016-02-21 11:25:45 -0800116 return partitions.get(partitionId)
117 .open();
Madan Jampani15b8ef52016-02-02 17:35:05 -0800118 }
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 Jampanie14a09c2016-02-11 10:43:21 -0800147
148 @Override
149 public List<PartitionInfo> partitionInfo() {
150 return partitions.values()
151 .stream()
Sho SHIMIZU5fab6e52016-02-15 11:54:15 -0800152 .flatMap(x -> Tools.stream(x.info()))
Madan Jampanie14a09c2016-02-11 10:43:21 -0800153 .collect(Collectors.toList());
154 }
Madan Jampani2f9cc712016-02-15 19:36:21 -0800155}