Madan Jampani | 7e55c66 | 2016-02-15 21:13:53 -0800 | [diff] [blame] | 1 | /* |
| 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 | package org.onosproject.store.primitives.impl; |
| 17 | |
| 18 | import static org.slf4j.LoggerFactory.getLogger; |
| 19 | |
| 20 | import java.util.Collection; |
| 21 | import java.util.List; |
| 22 | import java.util.Map; |
| 23 | import java.util.UUID; |
| 24 | import java.util.function.Supplier; |
| 25 | import java.util.stream.Collectors; |
| 26 | |
| 27 | import org.apache.commons.collections.ListUtils; |
| 28 | import org.apache.felix.scr.annotations.Activate; |
| 29 | import org.apache.felix.scr.annotations.Component; |
| 30 | import org.apache.felix.scr.annotations.Deactivate; |
| 31 | import org.apache.felix.scr.annotations.Reference; |
| 32 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 33 | import org.apache.felix.scr.annotations.Service; |
| 34 | import org.onosproject.cluster.ClusterService; |
| 35 | import org.onosproject.cluster.PartitionId; |
| 36 | import org.onosproject.persistence.PersistenceService; |
| 37 | import org.onosproject.store.cluster.messaging.ClusterCommunicationService; |
| 38 | import org.onosproject.store.primitives.DistributedPrimitiveCreator; |
| 39 | import org.onosproject.store.primitives.MapUpdate; |
Madan Jampani | 630c882 | 2016-02-24 10:38:21 -0800 | [diff] [blame^] | 40 | import org.onosproject.store.primitives.PartitionAdminService; |
Madan Jampani | 7e55c66 | 2016-02-15 21:13:53 -0800 | [diff] [blame] | 41 | import org.onosproject.store.primitives.PartitionService; |
| 42 | import org.onosproject.store.primitives.TransactionId; |
| 43 | import org.onosproject.store.serializers.KryoNamespaces; |
| 44 | import org.onosproject.store.service.AsyncConsistentMap; |
| 45 | import org.onosproject.store.service.AtomicCounterBuilder; |
| 46 | import org.onosproject.store.service.AtomicValueBuilder; |
| 47 | import org.onosproject.store.service.ConsistentMap; |
| 48 | import org.onosproject.store.service.ConsistentMapBuilder; |
| 49 | import org.onosproject.store.service.DistributedQueueBuilder; |
| 50 | import org.onosproject.store.service.DistributedSetBuilder; |
| 51 | import org.onosproject.store.service.EventuallyConsistentMapBuilder; |
| 52 | import org.onosproject.store.service.LeaderElectorBuilder; |
| 53 | import org.onosproject.store.service.MapInfo; |
| 54 | import org.onosproject.store.service.PartitionInfo; |
| 55 | import org.onosproject.store.service.Serializer; |
| 56 | import org.onosproject.store.service.StorageAdminService; |
| 57 | import org.onosproject.store.service.StorageService; |
| 58 | import org.onosproject.store.service.TransactionContextBuilder; |
| 59 | import org.slf4j.Logger; |
| 60 | |
Madan Jampani | 7e55c66 | 2016-02-15 21:13:53 -0800 | [diff] [blame] | 61 | import com.google.common.collect.Maps; |
| 62 | import 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) |
| 69 | public 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 Jampani | 630c882 | 2016-02-24 10:38:21 -0800 | [diff] [blame^] | 85 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 86 | protected PartitionAdminService partitionAdminService; |
| 87 | |
Madan Jampani | 7e55c66 | 2016-02-15 21:13:53 -0800 | [diff] [blame] | 88 | 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 Jampani | 86cb243 | 2016-02-17 11:07:56 -0800 | [diff] [blame] | 96 | public void activate() { |
Madan Jampani | 7e55c66 | 2016-02-15 21:13:53 -0800 | [diff] [blame] | 97 | 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 Jampani | 630c882 | 2016-02-24 10:38:21 -0800 | [diff] [blame^] | 204 | return partitionAdminService.partitionInfo(); |
Madan Jampani | 7e55c66 | 2016-02-15 21:13:53 -0800 | [diff] [blame] | 205 | } |
| 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 Jampani | 630c882 | 2016-02-24 10:38:21 -0800 | [diff] [blame^] | 223 | } |