blob: 2a3b834ff523bdf0ff46ecf96030c4a7b4840398 [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;
40import org.onosproject.store.primitives.PartitionService;
41import org.onosproject.store.primitives.TransactionId;
42import org.onosproject.store.serializers.KryoNamespaces;
43import org.onosproject.store.service.AsyncConsistentMap;
44import org.onosproject.store.service.AtomicCounterBuilder;
45import org.onosproject.store.service.AtomicValueBuilder;
46import org.onosproject.store.service.ConsistentMap;
47import org.onosproject.store.service.ConsistentMapBuilder;
48import org.onosproject.store.service.DistributedQueueBuilder;
49import org.onosproject.store.service.DistributedSetBuilder;
50import org.onosproject.store.service.EventuallyConsistentMapBuilder;
51import org.onosproject.store.service.LeaderElectorBuilder;
52import org.onosproject.store.service.MapInfo;
53import org.onosproject.store.service.PartitionInfo;
54import org.onosproject.store.service.Serializer;
55import org.onosproject.store.service.StorageAdminService;
56import org.onosproject.store.service.StorageService;
57import org.onosproject.store.service.TransactionContextBuilder;
58import org.slf4j.Logger;
59
60import com.google.common.collect.Lists;
61import 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
85 private final Supplier<TransactionId> transactionIdGenerator =
86 () -> TransactionId.from(UUID.randomUUID().toString());
87 private DistributedPrimitiveCreator basePrimitiveCreator;
88 private DistributedPrimitiveCreator federatedPrimitiveCreator;
89 private AsyncConsistentMap<TransactionId, Transaction.State> transactions;
90 private TransactionCoordinator transactionCoordinator;
91
92 @Activate
Madan Jampani86cb2432016-02-17 11:07:56 -080093 public void activate() {
Madan Jampani7e55c662016-02-15 21:13:53 -080094 basePrimitiveCreator = partitionService.getDistributedPrimitiveCreator(PartitionId.from(0));
95 Map<PartitionId, DistributedPrimitiveCreator> partitionMap = Maps.newHashMap();
96 partitionService.getAllPartitionIds().stream()
97 .filter(id -> !id.equals(PartitionId.from(0)))
98 .forEach(id -> partitionMap.put(id, partitionService.getDistributedPrimitiveCreator(id)));
99 federatedPrimitiveCreator = new FederatedDistributedPrimitiveCreator(partitionMap);
100 transactions = this.<TransactionId, Transaction.State>consistentMapBuilder()
101 .withName("onos-transactions")
102 .withSerializer(Serializer.using(KryoNamespaces.API,
103 MapUpdate.class,
104 MapUpdate.Type.class,
105 Transaction.class,
106 Transaction.State.class))
107 .buildAsyncMap();
108 transactionCoordinator = new TransactionCoordinator(transactions);
109 log.info("Started");
110 }
111
112 @Deactivate
113 public void deactivate() {
114 log.info("Stopped");
115 }
116
117 @Override
118 public <K, V> EventuallyConsistentMapBuilder<K, V> eventuallyConsistentMapBuilder() {
119 return new EventuallyConsistentMapBuilderImpl<>(clusterService,
120 clusterCommunicator,
121 persistenceService);
122 }
123
124 @Override
125 public <K, V> ConsistentMapBuilder<K, V> consistentMapBuilder() {
126 return new NewDefaultConsistentMapBuilder<>(basePrimitiveCreator, federatedPrimitiveCreator);
127 }
128
129 @Override
130 public <E> DistributedSetBuilder<E> setBuilder() {
131 return new DefaultDistributedSetBuilder<>(() -> this.<E, Boolean>consistentMapBuilder());
132 }
133
134 @Override
135 public <E> DistributedQueueBuilder<E> queueBuilder() {
136 // TODO: implement
137 throw new UnsupportedOperationException();
138 }
139
140 @Override
141 public AtomicCounterBuilder atomicCounterBuilder() {
142 return new NewDefaultAtomicCounterBuilder(basePrimitiveCreator, federatedPrimitiveCreator);
143 }
144
145 @Override
146 public <V> AtomicValueBuilder<V> atomicValueBuilder() {
147 Supplier<ConsistentMapBuilder<String, byte[]>> mapBuilderSupplier =
148 () -> this.<String, byte[]>consistentMapBuilder()
149 .withName("onos-atomic-values")
150 .withMeteringDisabled()
151 .withSerializer(Serializer.using(KryoNamespaces.BASIC));
152 return new DefaultAtomicValueBuilder<>(mapBuilderSupplier);
153 }
154
155 @Override
156 public TransactionContextBuilder transactionContextBuilder() {
157 return new NewDefaultTransactionContextBuilder(transactionIdGenerator.get(),
158 basePrimitiveCreator,
159 federatedPrimitiveCreator,
160 transactionCoordinator);
161 }
162
163 @Override
164 public LeaderElectorBuilder leaderElectorBuilder() {
165 return new DefaultLeaderElectorBuilder(basePrimitiveCreator,
166 federatedPrimitiveCreator);
167 }
168
169 @Override
170 public List<MapInfo> getMapInfo() {
171 return ListUtils.union(listMapInfo(basePrimitiveCreator), listMapInfo(federatedPrimitiveCreator));
172 }
173
174 @Override
175 public Map<String, Long> getCounters() {
176 Map<String, Long> result = Maps.newHashMap();
177 result.putAll(getInMemoryDatabaseCounters());
178 result.putAll(getPartitionedDatabaseCounters());
179 return result;
180 }
181
182 @Override
183 public Map<String, Long> getInMemoryDatabaseCounters() {
184 return getCounters(basePrimitiveCreator);
185 }
186
187 @Override
188 public Map<String, Long> getPartitionedDatabaseCounters() {
189 return getCounters(federatedPrimitiveCreator);
190 }
191
192 public Map<String, Long> getCounters(DistributedPrimitiveCreator creator) {
193 Map<String, Long> counters = Maps.newConcurrentMap();
194 creator.getAsyncAtomicCounterNames()
195 .forEach(name -> counters.put(name, creator.newAsyncCounter(name).asAtomicCounter().get()));
196 return counters;
197 }
198
199 @Override
200 public List<PartitionInfo> getPartitionInfo() {
201 return Lists.newArrayList();
202 }
203
204 @Override
205 public Collection<TransactionId> getPendingTransactions() {
206 return Futures.getUnchecked(transactions.keySet());
207 }
208
209 private List<MapInfo> listMapInfo(DistributedPrimitiveCreator creator) {
210 Serializer serializer = Serializer.using(KryoNamespaces.BASIC);
211 return creator.getAsyncConsistentMapNames()
212 .stream()
213 .map(name -> {
214 ConsistentMap<String, byte[]> map =
215 creator.<String, byte[]>newAsyncConsistentMap(name, serializer)
216 .asConsistentMap();
217 return new MapInfo(name, map.size());
218 }).collect(Collectors.toList());
219 }
220}