blob: b83a9776310127e8da73c12c80949a3ad0540434 [file] [log] [blame]
Madan Jampani7e55c662016-02-15 21:13:53 -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 */
16package org.onosproject.store.primitives.impl;
17
18import static org.slf4j.LoggerFactory.getLogger;
19
20import java.util.Collection;
21import java.util.List;
22import java.util.Map;
23import java.util.UUID;
24import java.util.function.Supplier;
25import java.util.stream.Collectors;
26
27import org.apache.commons.collections.ListUtils;
28import org.apache.felix.scr.annotations.Activate;
29import org.apache.felix.scr.annotations.Component;
30import org.apache.felix.scr.annotations.Deactivate;
31import org.apache.felix.scr.annotations.Reference;
32import org.apache.felix.scr.annotations.ReferenceCardinality;
33import org.apache.felix.scr.annotations.Service;
34import org.onosproject.cluster.ClusterService;
35import org.onosproject.cluster.PartitionId;
36import org.onosproject.persistence.PersistenceService;
37import org.onosproject.store.cluster.messaging.ClusterCommunicationService;
38import org.onosproject.store.primitives.DistributedPrimitiveCreator;
39import org.onosproject.store.primitives.MapUpdate;
Madan Jampani630c8822016-02-24 10:38:21 -080040import org.onosproject.store.primitives.PartitionAdminService;
Madan Jampani7e55c662016-02-15 21:13:53 -080041import org.onosproject.store.primitives.PartitionService;
42import org.onosproject.store.primitives.TransactionId;
43import org.onosproject.store.serializers.KryoNamespaces;
44import org.onosproject.store.service.AsyncConsistentMap;
45import org.onosproject.store.service.AtomicCounterBuilder;
46import org.onosproject.store.service.AtomicValueBuilder;
47import org.onosproject.store.service.ConsistentMap;
48import org.onosproject.store.service.ConsistentMapBuilder;
49import org.onosproject.store.service.DistributedQueueBuilder;
50import org.onosproject.store.service.DistributedSetBuilder;
51import org.onosproject.store.service.EventuallyConsistentMapBuilder;
52import org.onosproject.store.service.LeaderElectorBuilder;
53import org.onosproject.store.service.MapInfo;
54import org.onosproject.store.service.PartitionInfo;
55import org.onosproject.store.service.Serializer;
56import org.onosproject.store.service.StorageAdminService;
57import org.onosproject.store.service.StorageService;
58import org.onosproject.store.service.TransactionContextBuilder;
59import org.slf4j.Logger;
60
Madan Jampani7e55c662016-02-15 21:13:53 -080061import com.google.common.collect.Maps;
62import com.google.common.util.concurrent.Futures;
63
64/**
65 * Implementation for {@code StorageService} and {@code StorageAdminService}.
66 */
67@Service
68@Component(immediate = true, enabled = false)
69public class StorageManager implements StorageService, StorageAdminService {
70
71 private final Logger log = getLogger(getClass());
72
73 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
74 protected ClusterService clusterService;
75
76 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
77 protected ClusterCommunicationService clusterCommunicator;
78
79 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
80 protected PersistenceService persistenceService;
81
82 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
83 protected PartitionService partitionService;
84
Madan Jampani630c8822016-02-24 10:38:21 -080085 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
86 protected PartitionAdminService partitionAdminService;
87
Madan Jampani7e55c662016-02-15 21:13:53 -080088 private final Supplier<TransactionId> transactionIdGenerator =
89 () -> TransactionId.from(UUID.randomUUID().toString());
90 private DistributedPrimitiveCreator basePrimitiveCreator;
91 private DistributedPrimitiveCreator federatedPrimitiveCreator;
92 private AsyncConsistentMap<TransactionId, Transaction.State> transactions;
93 private TransactionCoordinator transactionCoordinator;
94
95 @Activate
Madan Jampani86cb2432016-02-17 11:07:56 -080096 public void activate() {
Madan Jampani7e55c662016-02-15 21:13:53 -080097 basePrimitiveCreator = partitionService.getDistributedPrimitiveCreator(PartitionId.from(0));
98 Map<PartitionId, DistributedPrimitiveCreator> partitionMap = Maps.newHashMap();
99 partitionService.getAllPartitionIds().stream()
100 .filter(id -> !id.equals(PartitionId.from(0)))
101 .forEach(id -> partitionMap.put(id, partitionService.getDistributedPrimitiveCreator(id)));
102 federatedPrimitiveCreator = new FederatedDistributedPrimitiveCreator(partitionMap);
103 transactions = this.<TransactionId, Transaction.State>consistentMapBuilder()
104 .withName("onos-transactions")
105 .withSerializer(Serializer.using(KryoNamespaces.API,
106 MapUpdate.class,
107 MapUpdate.Type.class,
108 Transaction.class,
109 Transaction.State.class))
110 .buildAsyncMap();
111 transactionCoordinator = new TransactionCoordinator(transactions);
112 log.info("Started");
113 }
114
115 @Deactivate
116 public void deactivate() {
117 log.info("Stopped");
118 }
119
120 @Override
121 public <K, V> EventuallyConsistentMapBuilder<K, V> eventuallyConsistentMapBuilder() {
122 return new EventuallyConsistentMapBuilderImpl<>(clusterService,
123 clusterCommunicator,
124 persistenceService);
125 }
126
127 @Override
128 public <K, V> ConsistentMapBuilder<K, V> consistentMapBuilder() {
129 return new NewDefaultConsistentMapBuilder<>(basePrimitiveCreator, federatedPrimitiveCreator);
130 }
131
132 @Override
133 public <E> DistributedSetBuilder<E> setBuilder() {
134 return new DefaultDistributedSetBuilder<>(() -> this.<E, Boolean>consistentMapBuilder());
135 }
136
137 @Override
138 public <E> DistributedQueueBuilder<E> queueBuilder() {
139 // TODO: implement
140 throw new UnsupportedOperationException();
141 }
142
143 @Override
144 public AtomicCounterBuilder atomicCounterBuilder() {
145 return new NewDefaultAtomicCounterBuilder(basePrimitiveCreator, federatedPrimitiveCreator);
146 }
147
148 @Override
149 public <V> AtomicValueBuilder<V> atomicValueBuilder() {
150 Supplier<ConsistentMapBuilder<String, byte[]>> mapBuilderSupplier =
151 () -> this.<String, byte[]>consistentMapBuilder()
152 .withName("onos-atomic-values")
153 .withMeteringDisabled()
154 .withSerializer(Serializer.using(KryoNamespaces.BASIC));
155 return new DefaultAtomicValueBuilder<>(mapBuilderSupplier);
156 }
157
158 @Override
159 public TransactionContextBuilder transactionContextBuilder() {
160 return new NewDefaultTransactionContextBuilder(transactionIdGenerator.get(),
161 basePrimitiveCreator,
162 federatedPrimitiveCreator,
163 transactionCoordinator);
164 }
165
166 @Override
167 public LeaderElectorBuilder leaderElectorBuilder() {
168 return new DefaultLeaderElectorBuilder(basePrimitiveCreator,
169 federatedPrimitiveCreator);
170 }
171
172 @Override
173 public List<MapInfo> getMapInfo() {
174 return ListUtils.union(listMapInfo(basePrimitiveCreator), listMapInfo(federatedPrimitiveCreator));
175 }
176
177 @Override
178 public Map<String, Long> getCounters() {
179 Map<String, Long> result = Maps.newHashMap();
180 result.putAll(getInMemoryDatabaseCounters());
181 result.putAll(getPartitionedDatabaseCounters());
182 return result;
183 }
184
185 @Override
186 public Map<String, Long> getInMemoryDatabaseCounters() {
187 return getCounters(basePrimitiveCreator);
188 }
189
190 @Override
191 public Map<String, Long> getPartitionedDatabaseCounters() {
192 return getCounters(federatedPrimitiveCreator);
193 }
194
195 public Map<String, Long> getCounters(DistributedPrimitiveCreator creator) {
196 Map<String, Long> counters = Maps.newConcurrentMap();
197 creator.getAsyncAtomicCounterNames()
198 .forEach(name -> counters.put(name, creator.newAsyncCounter(name).asAtomicCounter().get()));
199 return counters;
200 }
201
202 @Override
203 public List<PartitionInfo> getPartitionInfo() {
Madan Jampani630c8822016-02-24 10:38:21 -0800204 return partitionAdminService.partitionInfo();
Madan Jampani7e55c662016-02-15 21:13:53 -0800205 }
206
207 @Override
208 public Collection<TransactionId> getPendingTransactions() {
209 return Futures.getUnchecked(transactions.keySet());
210 }
211
212 private List<MapInfo> listMapInfo(DistributedPrimitiveCreator creator) {
213 Serializer serializer = Serializer.using(KryoNamespaces.BASIC);
214 return creator.getAsyncConsistentMapNames()
215 .stream()
216 .map(name -> {
217 ConsistentMap<String, byte[]> map =
218 creator.<String, byte[]>newAsyncConsistentMap(name, serializer)
219 .asConsistentMap();
220 return new MapInfo(name, map.size());
221 }).collect(Collectors.toList());
222 }
Madan Jampani630c8822016-02-24 10:38:21 -0800223}