Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 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 | */ |
Sho SHIMIZU | e18cb12 | 2016-02-22 21:04:56 -0800 | [diff] [blame] | 16 | package org.onosproject.store.resource.impl; |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 17 | |
| 18 | import com.google.common.annotations.Beta; |
Sho SHIMIZU | 83258ae | 2016-01-29 17:39:07 -0800 | [diff] [blame] | 19 | import com.google.common.collect.ImmutableSet; |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 20 | import org.apache.felix.scr.annotations.Activate; |
| 21 | import org.apache.felix.scr.annotations.Component; |
| 22 | import org.apache.felix.scr.annotations.Reference; |
| 23 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 24 | import org.apache.felix.scr.annotations.Service; |
Sho SHIMIZU | e18cb12 | 2016-02-22 21:04:56 -0800 | [diff] [blame] | 25 | import org.onosproject.net.resource.ContinuousResource; |
| 26 | import org.onosproject.net.resource.ContinuousResourceId; |
| 27 | import org.onosproject.net.resource.DiscreteResource; |
| 28 | import org.onosproject.net.resource.DiscreteResourceId; |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 29 | import org.onosproject.net.resource.Resource; |
Sho SHIMIZU | e18cb12 | 2016-02-22 21:04:56 -0800 | [diff] [blame] | 30 | import org.onosproject.net.resource.ResourceAllocation; |
| 31 | import org.onosproject.net.resource.ResourceConsumer; |
| 32 | import org.onosproject.net.resource.ResourceEvent; |
| 33 | import org.onosproject.net.resource.ResourceId; |
Sho SHIMIZU | e18cb12 | 2016-02-22 21:04:56 -0800 | [diff] [blame] | 34 | import org.onosproject.net.resource.ResourceStore; |
| 35 | import org.onosproject.net.resource.ResourceStoreDelegate; |
| 36 | import org.onosproject.net.resource.Resources; |
Sho SHIMIZU | fa62b47 | 2015-11-02 17:35:46 -0800 | [diff] [blame] | 37 | import org.onosproject.store.AbstractStore; |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 38 | import org.onosproject.store.serializers.KryoNamespaces; |
Madan Jampani | 3780d4b | 2016-04-04 18:18:24 -0700 | [diff] [blame] | 39 | import org.onosproject.store.service.CommitStatus; |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 40 | import org.onosproject.store.service.Serializer; |
| 41 | import org.onosproject.store.service.StorageService; |
| 42 | import org.onosproject.store.service.TransactionContext; |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 43 | import org.slf4j.Logger; |
| 44 | import org.slf4j.LoggerFactory; |
| 45 | |
Sho SHIMIZU | ba41fc1 | 2015-08-12 15:43:22 -0700 | [diff] [blame] | 46 | import java.util.Arrays; |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 47 | import java.util.Collection; |
Sho SHIMIZU | 69420fe | 2016-02-09 15:01:07 -0800 | [diff] [blame] | 48 | import java.util.LinkedHashMap; |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 49 | import java.util.List; |
| 50 | import java.util.Map; |
| 51 | import java.util.Optional; |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 52 | import java.util.Set; |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 53 | import java.util.stream.Collectors; |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 54 | import java.util.stream.Stream; |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 55 | |
| 56 | import static com.google.common.base.Preconditions.checkArgument; |
| 57 | import static com.google.common.base.Preconditions.checkNotNull; |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 58 | import static org.onosproject.net.resource.ResourceEvent.Type.RESOURCE_ADDED; |
| 59 | import static org.onosproject.net.resource.ResourceEvent.Type.RESOURCE_REMOVED; |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 60 | |
| 61 | /** |
| 62 | * Implementation of ResourceStore using TransactionalMap. |
| 63 | */ |
Sho SHIMIZU | 9a2b829 | 2015-10-28 13:00:16 -0700 | [diff] [blame] | 64 | @Component(immediate = true) |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 65 | @Service |
| 66 | @Beta |
Sho SHIMIZU | fa62b47 | 2015-11-02 17:35:46 -0800 | [diff] [blame] | 67 | public class ConsistentResourceStore extends AbstractStore<ResourceEvent, ResourceStoreDelegate> |
| 68 | implements ResourceStore { |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 69 | private static final Logger log = LoggerFactory.getLogger(ConsistentResourceStore.class); |
| 70 | |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 71 | static final Serializer SERIALIZER = Serializer.using( |
HIGUCHI Yuta | 6f58422 | 2016-05-06 11:15:38 -0700 | [diff] [blame] | 72 | Arrays.asList(KryoNamespaces.API), |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 73 | ContinuousResourceAllocation.class); |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 74 | |
Thomas Vachuska | 762a2d8 | 2016-01-04 10:25:20 -0800 | [diff] [blame] | 75 | // TODO: We should provide centralized values for this |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 76 | static final int MAX_RETRIES = 5; |
| 77 | static final int RETRY_DELAY = 1_000; // millis |
Thomas Vachuska | 762a2d8 | 2016-01-04 10:25:20 -0800 | [diff] [blame] | 78 | |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 79 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 80 | protected StorageService service; |
| 81 | |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 82 | private ConsistentDiscreteResourceStore discreteStore; |
| 83 | private ConsistentContinuousResourceStore continuousStore; |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 84 | |
| 85 | @Activate |
| 86 | public void activate() { |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 87 | discreteStore = new ConsistentDiscreteResourceStore(service); |
| 88 | continuousStore = new ConsistentContinuousResourceStore(service); |
Sho SHIMIZU | e7db614 | 2015-11-04 11:24:22 -0800 | [diff] [blame] | 89 | |
Madan Jampani | c7f49f9 | 2015-12-10 11:35:06 -0800 | [diff] [blame] | 90 | log.info("Started"); |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 91 | } |
| 92 | |
Sho SHIMIZU | dd3750c | 2016-02-01 11:37:04 -0800 | [diff] [blame] | 93 | // Computational complexity: O(1) if the resource is discrete type. |
| 94 | // O(n) if the resource is continuous type where n is the number of the existing allocations for the resource |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 95 | @Override |
Sho SHIMIZU | dd3750c | 2016-02-01 11:37:04 -0800 | [diff] [blame] | 96 | public List<ResourceAllocation> getResourceAllocations(ResourceId id) { |
| 97 | checkNotNull(id); |
| 98 | checkArgument(id instanceof DiscreteResourceId || id instanceof ContinuousResourceId); |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 99 | |
Sho SHIMIZU | dd3750c | 2016-02-01 11:37:04 -0800 | [diff] [blame] | 100 | if (id instanceof DiscreteResourceId) { |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 101 | return discreteStore.getResourceAllocations((DiscreteResourceId) id); |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 102 | } else { |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 103 | return continuousStore.getResourceAllocations((ContinuousResourceId) id); |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 104 | } |
| 105 | } |
| 106 | |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 107 | @Override |
Jonathan Hart | 5615126 | 2016-02-11 09:48:50 -0800 | [diff] [blame] | 108 | public boolean register(List<Resource> resources) { |
Sho SHIMIZU | 83e17a0 | 2015-08-20 14:07:05 -0700 | [diff] [blame] | 109 | checkNotNull(resources); |
HIGUCHI Yuta | 6f828c3 | 2016-01-20 18:11:05 -0800 | [diff] [blame] | 110 | if (log.isTraceEnabled()) { |
| 111 | resources.forEach(r -> log.trace("registering {}", r)); |
| 112 | } |
Sho SHIMIZU | ba41fc1 | 2015-08-12 15:43:22 -0700 | [diff] [blame] | 113 | |
| 114 | TransactionContext tx = service.transactionContextBuilder().build(); |
| 115 | tx.begin(); |
| 116 | |
Sho SHIMIZU | 69420fe | 2016-02-09 15:01:07 -0800 | [diff] [blame] | 117 | // the order is preserved by LinkedHashMap |
Sho SHIMIZU | f33b893 | 2016-01-25 18:43:32 -0800 | [diff] [blame] | 118 | Map<DiscreteResource, List<Resource>> resourceMap = resources.stream() |
Sho SHIMIZU | 1e0a34c | 2015-11-02 16:52:29 -0800 | [diff] [blame] | 119 | .filter(x -> x.parent().isPresent()) |
Sho SHIMIZU | 69420fe | 2016-02-09 15:01:07 -0800 | [diff] [blame] | 120 | .collect(Collectors.groupingBy(x -> x.parent().get(), LinkedHashMap::new, Collectors.toList())); |
Sho SHIMIZU | ba41fc1 | 2015-08-12 15:43:22 -0700 | [diff] [blame] | 121 | |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 122 | TransactionalDiscreteResourceStore discreteTxStore = discreteStore.transactional(tx); |
| 123 | TransactionalContinuousResourceStore continuousTxStore = continuousStore.transactional(tx); |
| 124 | for (Map.Entry<DiscreteResource, List<Resource>> entry : resourceMap.entrySet()) { |
Sho SHIMIZU | 001e3f2 | 2016-05-06 14:46:15 -0700 | [diff] [blame] | 125 | DiscreteResourceId parentId = entry.getKey().id(); |
Sho SHIMIZU | d66148a | 2016-05-06 14:47:47 -0700 | [diff] [blame] | 126 | if (!discreteTxStore.lookup(parentId).isPresent()) { |
Sho SHIMIZU | 1e0a34c | 2015-11-02 16:52:29 -0800 | [diff] [blame] | 127 | return abortTransaction(tx); |
Sho SHIMIZU | ba41fc1 | 2015-08-12 15:43:22 -0700 | [diff] [blame] | 128 | } |
| 129 | |
Sho SHIMIZU | a2d99eb | 2016-05-06 14:52:55 -0700 | [diff] [blame] | 130 | if (!register(discreteTxStore, continuousTxStore, parentId, entry.getValue())) { |
Sho SHIMIZU | 1e0a34c | 2015-11-02 16:52:29 -0800 | [diff] [blame] | 131 | return abortTransaction(tx); |
| 132 | } |
Sho SHIMIZU | ba41fc1 | 2015-08-12 15:43:22 -0700 | [diff] [blame] | 133 | } |
Sho SHIMIZU | 1e0a34c | 2015-11-02 16:52:29 -0800 | [diff] [blame] | 134 | |
Madan Jampani | 3780d4b | 2016-04-04 18:18:24 -0700 | [diff] [blame] | 135 | return tx.commit().whenComplete((status, error) -> { |
| 136 | if (status == CommitStatus.SUCCESS) { |
| 137 | log.trace("Transaction commit succeeded on registration: resources={}", resources); |
| 138 | List<ResourceEvent> events = resources.stream() |
| 139 | .filter(x -> x.parent().isPresent()) |
| 140 | .map(x -> new ResourceEvent(RESOURCE_ADDED, x)) |
| 141 | .collect(Collectors.toList()); |
| 142 | notifyDelegate(events); |
| 143 | } else { |
| 144 | log.warn("Transaction commit failed on registration", error); |
| 145 | } |
| 146 | }).join() == CommitStatus.SUCCESS; |
Sho SHIMIZU | ba41fc1 | 2015-08-12 15:43:22 -0700 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | @Override |
Jonathan Hart | 5615126 | 2016-02-11 09:48:50 -0800 | [diff] [blame] | 150 | public boolean unregister(List<ResourceId> ids) { |
Sho SHIMIZU | 72f81b1 | 2016-02-09 09:26:17 -0800 | [diff] [blame] | 151 | checkNotNull(ids); |
Sho SHIMIZU | 2d8a13a | 2015-08-18 22:37:41 -0700 | [diff] [blame] | 152 | |
| 153 | TransactionContext tx = service.transactionContextBuilder().build(); |
| 154 | tx.begin(); |
| 155 | |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 156 | TransactionalDiscreteResourceStore discreteTxStore = discreteStore.transactional(tx); |
| 157 | TransactionalContinuousResourceStore continuousTxStore = continuousStore.transactional(tx); |
Sho SHIMIZU | 7d54d9c | 2016-02-17 13:58:46 -0800 | [diff] [blame] | 158 | // Look up resources by resource IDs |
Sho SHIMIZU | 72f81b1 | 2016-02-09 09:26:17 -0800 | [diff] [blame] | 159 | List<Resource> resources = ids.stream() |
Sho SHIMIZU | 1e0a34c | 2015-11-02 16:52:29 -0800 | [diff] [blame] | 160 | .filter(x -> x.parent().isPresent()) |
Sho SHIMIZU | 7d54d9c | 2016-02-17 13:58:46 -0800 | [diff] [blame] | 161 | .map(x -> { |
| 162 | // avoid access to consistent map in the case of discrete resource |
| 163 | if (x instanceof DiscreteResourceId) { |
| 164 | return Optional.of(Resources.discrete((DiscreteResourceId) x).resource()); |
| 165 | } else { |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 166 | return continuousTxStore.lookup((ContinuousResourceId) x); |
Sho SHIMIZU | 7d54d9c | 2016-02-17 13:58:46 -0800 | [diff] [blame] | 167 | } |
| 168 | }) |
HIGUCHI Yuta | 315179a | 2016-02-18 14:01:22 -0800 | [diff] [blame] | 169 | .filter(Optional::isPresent) |
| 170 | .map(Optional::get) |
Sho SHIMIZU | 72f81b1 | 2016-02-09 09:26:17 -0800 | [diff] [blame] | 171 | .collect(Collectors.toList()); |
Sho SHIMIZU | 69420fe | 2016-02-09 15:01:07 -0800 | [diff] [blame] | 172 | // the order is preserved by LinkedHashMap |
Sho SHIMIZU | 72f81b1 | 2016-02-09 09:26:17 -0800 | [diff] [blame] | 173 | Map<DiscreteResourceId, List<Resource>> resourceMap = resources.stream() |
Sho SHIMIZU | 69420fe | 2016-02-09 15:01:07 -0800 | [diff] [blame] | 174 | .collect(Collectors.groupingBy(x -> x.parent().get().id(), LinkedHashMap::new, Collectors.toList())); |
Sho SHIMIZU | 83e17a0 | 2015-08-20 14:07:05 -0700 | [diff] [blame] | 175 | |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 176 | for (Map.Entry<DiscreteResourceId, List<Resource>> entry : resourceMap.entrySet()) { |
Sho SHIMIZU | a2d99eb | 2016-05-06 14:52:55 -0700 | [diff] [blame] | 177 | if (!unregister(discreteTxStore, continuousTxStore, entry.getKey(), entry.getValue())) { |
HIGUCHI Yuta | 6acdfd0 | 2016-02-18 10:39:43 -0800 | [diff] [blame] | 178 | log.warn("Failed to unregister {}: Failed to remove {} values.", |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 179 | entry.getKey(), entry.getValue().size()); |
HIGUCHI Yuta | 6acdfd0 | 2016-02-18 10:39:43 -0800 | [diff] [blame] | 180 | log.debug("Failed to unregister {}: Failed to remove values: {}", |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 181 | entry.getKey(), entry.getValue()); |
Sho SHIMIZU | 1e0a34c | 2015-11-02 16:52:29 -0800 | [diff] [blame] | 182 | return abortTransaction(tx); |
| 183 | } |
Sho SHIMIZU | 2d8a13a | 2015-08-18 22:37:41 -0700 | [diff] [blame] | 184 | } |
Sho SHIMIZU | 1e0a34c | 2015-11-02 16:52:29 -0800 | [diff] [blame] | 185 | |
Madan Jampani | 3780d4b | 2016-04-04 18:18:24 -0700 | [diff] [blame] | 186 | return tx.commit().whenComplete((status, error) -> { |
| 187 | if (status == CommitStatus.SUCCESS) { |
| 188 | List<ResourceEvent> events = resources.stream() |
| 189 | .filter(x -> x.parent().isPresent()) |
| 190 | .map(x -> new ResourceEvent(RESOURCE_REMOVED, x)) |
| 191 | .collect(Collectors.toList()); |
| 192 | notifyDelegate(events); |
| 193 | } else { |
| 194 | log.warn("Failed to unregister {}: Commit failed.", ids, error); |
| 195 | } |
| 196 | }).join() == CommitStatus.SUCCESS; |
Sho SHIMIZU | 2d8a13a | 2015-08-18 22:37:41 -0700 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | @Override |
Jonathan Hart | 5615126 | 2016-02-11 09:48:50 -0800 | [diff] [blame] | 200 | public boolean allocate(List<Resource> resources, ResourceConsumer consumer) { |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 201 | checkNotNull(resources); |
| 202 | checkNotNull(consumer); |
| 203 | |
| 204 | TransactionContext tx = service.transactionContextBuilder().build(); |
| 205 | tx.begin(); |
| 206 | |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 207 | TransactionalDiscreteResourceStore discreteTxStore = discreteStore.transactional(tx); |
| 208 | TransactionalContinuousResourceStore continuousTxStore = continuousStore.transactional(tx); |
| 209 | for (Resource resource : resources) { |
Sho SHIMIZU | 171a938 | 2016-02-15 13:56:34 -0800 | [diff] [blame] | 210 | if (resource instanceof DiscreteResource) { |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 211 | if (!discreteTxStore.allocate(consumer, (DiscreteResource) resource)) { |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 212 | return abortTransaction(tx); |
| 213 | } |
Sho SHIMIZU | f33b893 | 2016-01-25 18:43:32 -0800 | [diff] [blame] | 214 | } else if (resource instanceof ContinuousResource) { |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 215 | if (!continuousTxStore.allocate(consumer, (ContinuousResource) resource)) { |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 216 | return abortTransaction(tx); |
| 217 | } |
Sho SHIMIZU | 1e0a34c | 2015-11-02 16:52:29 -0800 | [diff] [blame] | 218 | } |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 219 | } |
Sho SHIMIZU | 1e0a34c | 2015-11-02 16:52:29 -0800 | [diff] [blame] | 220 | |
Madan Jampani | 3780d4b | 2016-04-04 18:18:24 -0700 | [diff] [blame] | 221 | return tx.commit().join() == CommitStatus.SUCCESS; |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | @Override |
Sho SHIMIZU | fc64ffe | 2016-02-10 20:11:09 -0800 | [diff] [blame] | 225 | public boolean release(List<ResourceAllocation> allocations) { |
| 226 | checkNotNull(allocations); |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 227 | |
| 228 | TransactionContext tx = service.transactionContextBuilder().build(); |
| 229 | tx.begin(); |
| 230 | |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 231 | TransactionalDiscreteResourceStore discreteTxStore = discreteStore.transactional(tx); |
| 232 | TransactionalContinuousResourceStore continuousTxStore = continuousStore.transactional(tx); |
Sho SHIMIZU | fc64ffe | 2016-02-10 20:11:09 -0800 | [diff] [blame] | 233 | for (ResourceAllocation allocation : allocations) { |
| 234 | Resource resource = allocation.resource(); |
| 235 | ResourceConsumer consumer = allocation.consumer(); |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 236 | |
Sho SHIMIZU | f33b893 | 2016-01-25 18:43:32 -0800 | [diff] [blame] | 237 | if (resource instanceof DiscreteResource) { |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 238 | if (!discreteTxStore.release((DiscreteResource) resource, consumer)) { |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 239 | return abortTransaction(tx); |
| 240 | } |
Sho SHIMIZU | f33b893 | 2016-01-25 18:43:32 -0800 | [diff] [blame] | 241 | } else if (resource instanceof ContinuousResource) { |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 242 | if (!continuousTxStore.release((ContinuousResource) resource, consumer)) { |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 243 | return abortTransaction(tx); |
| 244 | } |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 245 | } |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 246 | } |
Sho SHIMIZU | 1e0a34c | 2015-11-02 16:52:29 -0800 | [diff] [blame] | 247 | |
Madan Jampani | 3780d4b | 2016-04-04 18:18:24 -0700 | [diff] [blame] | 248 | return tx.commit().join() == CommitStatus.SUCCESS; |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 249 | } |
| 250 | |
Sho SHIMIZU | dd3750c | 2016-02-01 11:37:04 -0800 | [diff] [blame] | 251 | // computational complexity: O(1) if the resource is discrete type. |
| 252 | // O(n) if the resource is continuous type where n is the number of the children of |
| 253 | // the specified resource's parent |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 254 | @Override |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 255 | public boolean isAvailable(Resource resource) { |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 256 | checkNotNull(resource); |
Sho SHIMIZU | f33b893 | 2016-01-25 18:43:32 -0800 | [diff] [blame] | 257 | checkArgument(resource instanceof DiscreteResource || resource instanceof ContinuousResource); |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 258 | |
Sho SHIMIZU | f33b893 | 2016-01-25 18:43:32 -0800 | [diff] [blame] | 259 | if (resource instanceof DiscreteResource) { |
HIGUCHI Yuta | 6f828c3 | 2016-01-20 18:11:05 -0800 | [diff] [blame] | 260 | // check if already consumed |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 261 | return discreteStore.isAvailable((DiscreteResource) resource); |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 262 | } else { |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 263 | return continuousStore.isAvailable((ContinuousResource) resource); |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 264 | } |
| 265 | } |
| 266 | |
Sho SHIMIZU | dd3750c | 2016-02-01 11:37:04 -0800 | [diff] [blame] | 267 | // computational complexity: O(n + m) where n is the number of entries in discreteConsumers |
| 268 | // and m is the number of allocations for all continuous resources |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 269 | @Override |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 270 | public Collection<Resource> getResources(ResourceConsumer consumer) { |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 271 | checkNotNull(consumer); |
| 272 | |
| 273 | // NOTE: getting all entries may become performance bottleneck |
| 274 | // TODO: revisit for better backend data structure |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 275 | Stream<DiscreteResource> discrete = discreteStore.getResources(consumer); |
| 276 | Stream<ContinuousResource> continuous = continuousStore.getResources(consumer); |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 277 | |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 278 | return Stream.concat(discrete, continuous).collect(Collectors.toList()); |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 279 | } |
| 280 | |
Sho SHIMIZU | 82bfe99 | 2016-02-10 09:55:32 -0800 | [diff] [blame] | 281 | // computational complexity: O(1) |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 282 | @Override |
Sho SHIMIZU | dd3750c | 2016-02-01 11:37:04 -0800 | [diff] [blame] | 283 | public Set<Resource> getChildResources(DiscreteResourceId parent) { |
Sho SHIMIZU | e7f4f3f | 2015-10-13 16:27:25 -0700 | [diff] [blame] | 284 | checkNotNull(parent); |
| 285 | |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 286 | return ImmutableSet.<Resource>builder() |
| 287 | .addAll(discreteStore.getChildResources(parent)) |
| 288 | .addAll(continuousStore.getChildResources(parent)) |
| 289 | .build(); |
Sho SHIMIZU | e7f4f3f | 2015-10-13 16:27:25 -0700 | [diff] [blame] | 290 | } |
| 291 | |
Sho SHIMIZU | dd3750c | 2016-02-01 11:37:04 -0800 | [diff] [blame] | 292 | // computational complexity: O(n) where n is the number of the children of the parent |
Sho SHIMIZU | e7f4f3f | 2015-10-13 16:27:25 -0700 | [diff] [blame] | 293 | @Override |
Sho SHIMIZU | dd3750c | 2016-02-01 11:37:04 -0800 | [diff] [blame] | 294 | public <T> Collection<Resource> getAllocatedResources(DiscreteResourceId parent, Class<T> cls) { |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 295 | checkNotNull(parent); |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 296 | checkNotNull(cls); |
| 297 | |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 298 | Stream<DiscreteResource> discrete = discreteStore.getAllocatedResources(parent, cls); |
| 299 | Stream<ContinuousResource> continuous = continuousStore.getAllocatedResources(parent, cls); |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 300 | |
| 301 | return Stream.concat(discrete, continuous).collect(Collectors.toList()); |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 302 | } |
Sho SHIMIZU | d29847f | 2015-08-13 09:10:59 -0700 | [diff] [blame] | 303 | |
| 304 | /** |
| 305 | * Abort the transaction. |
| 306 | * |
| 307 | * @param tx transaction context |
| 308 | * @return always false |
| 309 | */ |
| 310 | private boolean abortTransaction(TransactionContext tx) { |
| 311 | tx.abort(); |
| 312 | return false; |
| 313 | } |
| 314 | |
| 315 | /** |
Sho SHIMIZU | ba41fc1 | 2015-08-12 15:43:22 -0700 | [diff] [blame] | 316 | * Appends the values to the existing values associated with the specified key. |
Sho SHIMIZU | 4568c41 | 2015-08-21 16:39:07 -0700 | [diff] [blame] | 317 | * If the map already has all the given values, appending will not happen. |
Sho SHIMIZU | ba41fc1 | 2015-08-12 15:43:22 -0700 | [diff] [blame] | 318 | * |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 319 | * @param key key specifying values |
Sho SHIMIZU | ba41fc1 | 2015-08-12 15:43:22 -0700 | [diff] [blame] | 320 | * @param values values to be appended |
Sho SHIMIZU | ba41fc1 | 2015-08-12 15:43:22 -0700 | [diff] [blame] | 321 | * @return true if the operation succeeds, false otherwise. |
| 322 | */ |
Sho SHIMIZU | dd3750c | 2016-02-01 11:37:04 -0800 | [diff] [blame] | 323 | // computational complexity: O(n) where n is the number of the specified value |
Sho SHIMIZU | a2d99eb | 2016-05-06 14:52:55 -0700 | [diff] [blame] | 324 | private boolean register(TransactionalDiscreteResourceStore discreteTxStore, |
| 325 | TransactionalContinuousResourceStore continuousTxStore, |
| 326 | DiscreteResourceId key, List<Resource> values) { |
Sho SHIMIZU | 03be266 | 2016-05-04 09:38:45 -0700 | [diff] [blame] | 327 | // it's assumed that the passed "values" is non-empty |
| 328 | |
| 329 | // This is 2-pass scan. Nicer to have 1-pass scan |
| 330 | List<DiscreteResource> discreteValues = values.stream() |
| 331 | .filter(x -> x instanceof DiscreteResource) |
| 332 | .map(x -> (DiscreteResource) x) |
| 333 | .collect(Collectors.toList()); |
| 334 | List<ContinuousResource> continuousValues = values.stream() |
| 335 | .filter(x -> x instanceof ContinuousResource) |
| 336 | .map(x -> (ContinuousResource) x) |
| 337 | .collect(Collectors.toList()); |
| 338 | |
Sho SHIMIZU | a2d99eb | 2016-05-06 14:52:55 -0700 | [diff] [blame] | 339 | return discreteTxStore.register(key, discreteValues) |
| 340 | && continuousTxStore.register(key, continuousValues); |
Sho SHIMIZU | ba41fc1 | 2015-08-12 15:43:22 -0700 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | /** |
Sho SHIMIZU | ba1f83b | 2015-10-14 08:11:20 -0700 | [diff] [blame] | 344 | * Removes the values from the existing values associated with the specified key. |
Sho SHIMIZU | 5618ee5 | 2015-08-21 17:19:44 -0700 | [diff] [blame] | 345 | * If the map doesn't contain the given values, removal will not happen. |
Sho SHIMIZU | 2d8a13a | 2015-08-18 22:37:41 -0700 | [diff] [blame] | 346 | * |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 347 | * @param discreteTxStore map holding multiple discrete resources for a key |
| 348 | * @param continuousTxStore map holding multiple continuous resources for a key |
| 349 | * @param key key specifying values |
| 350 | * @param values values to be removed |
Sho SHIMIZU | 2d8a13a | 2015-08-18 22:37:41 -0700 | [diff] [blame] | 351 | * @return true if the operation succeeds, false otherwise |
| 352 | */ |
Sho SHIMIZU | a2d99eb | 2016-05-06 14:52:55 -0700 | [diff] [blame] | 353 | private boolean unregister(TransactionalDiscreteResourceStore discreteTxStore, |
| 354 | TransactionalContinuousResourceStore continuousTxStore, |
| 355 | DiscreteResourceId key, List<Resource> values) { |
Sho SHIMIZU | 03be266 | 2016-05-04 09:38:45 -0700 | [diff] [blame] | 356 | // it's assumed that the passed "values" is non-empty |
| 357 | |
| 358 | // This is 2-pass scan. Nicer to have 1-pass scan |
| 359 | List<DiscreteResource> discreteValues = values.stream() |
| 360 | .filter(x -> x instanceof DiscreteResource) |
| 361 | .map(x -> (DiscreteResource) x) |
| 362 | .collect(Collectors.toList()); |
| 363 | List<ContinuousResource> continuousValues = values.stream() |
| 364 | .filter(x -> x instanceof ContinuousResource) |
| 365 | .map(x -> (ContinuousResource) x) |
| 366 | .collect(Collectors.toList()); |
| 367 | |
Sho SHIMIZU | a2d99eb | 2016-05-06 14:52:55 -0700 | [diff] [blame] | 368 | return discreteTxStore.unregister(key, discreteValues) |
| 369 | && continuousTxStore.unregister(key, continuousValues); |
Sho SHIMIZU | 03be266 | 2016-05-04 09:38:45 -0700 | [diff] [blame] | 370 | } |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 371 | } |