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 | |
Jordan Halterman | 6359c83 | 2017-03-29 16:53:21 -0700 | [diff] [blame] | 18 | import java.util.Collection; |
| 19 | import java.util.LinkedHashMap; |
| 20 | import java.util.LinkedHashSet; |
| 21 | import java.util.List; |
| 22 | import java.util.Map; |
| 23 | import java.util.Optional; |
| 24 | import java.util.Set; |
Jordan Halterman | 7af71da | 2017-05-15 13:41:00 -0700 | [diff] [blame] | 25 | import java.util.concurrent.ExecutionException; |
| 26 | import java.util.concurrent.TimeUnit; |
| 27 | import java.util.concurrent.TimeoutException; |
Jordan Halterman | 6359c83 | 2017-03-29 16:53:21 -0700 | [diff] [blame] | 28 | import java.util.function.Function; |
| 29 | import java.util.stream.Collectors; |
| 30 | import java.util.stream.Stream; |
| 31 | |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 32 | import com.google.common.annotations.Beta; |
Sho SHIMIZU | 83258ae | 2016-01-29 17:39:07 -0800 | [diff] [blame] | 33 | import com.google.common.collect.ImmutableSet; |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 34 | import org.apache.felix.scr.annotations.Activate; |
| 35 | import org.apache.felix.scr.annotations.Component; |
| 36 | import org.apache.felix.scr.annotations.Reference; |
| 37 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 38 | import org.apache.felix.scr.annotations.Service; |
Sho SHIMIZU | 65de961 | 2016-05-19 12:24:20 -0700 | [diff] [blame] | 39 | import org.onlab.util.KryoNamespace; |
Sho SHIMIZU | 0b4e9dd | 2016-06-28 17:42:05 -0700 | [diff] [blame] | 40 | import org.onlab.util.Tools; |
Sho SHIMIZU | e18cb12 | 2016-02-22 21:04:56 -0800 | [diff] [blame] | 41 | import org.onosproject.net.resource.ContinuousResource; |
| 42 | import org.onosproject.net.resource.ContinuousResourceId; |
| 43 | import org.onosproject.net.resource.DiscreteResource; |
| 44 | import org.onosproject.net.resource.DiscreteResourceId; |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 45 | import org.onosproject.net.resource.Resource; |
Sho SHIMIZU | e18cb12 | 2016-02-22 21:04:56 -0800 | [diff] [blame] | 46 | import org.onosproject.net.resource.ResourceAllocation; |
| 47 | import org.onosproject.net.resource.ResourceConsumer; |
Naoki Shiota | bd1974c | 2016-04-29 18:44:17 -0700 | [diff] [blame] | 48 | import org.onosproject.net.resource.ResourceConsumerId; |
Sho SHIMIZU | e18cb12 | 2016-02-22 21:04:56 -0800 | [diff] [blame] | 49 | import org.onosproject.net.resource.ResourceEvent; |
| 50 | import org.onosproject.net.resource.ResourceId; |
Sho SHIMIZU | e18cb12 | 2016-02-22 21:04:56 -0800 | [diff] [blame] | 51 | import org.onosproject.net.resource.ResourceStore; |
| 52 | import org.onosproject.net.resource.ResourceStoreDelegate; |
| 53 | import org.onosproject.net.resource.Resources; |
Sho SHIMIZU | fa62b47 | 2015-11-02 17:35:46 -0800 | [diff] [blame] | 54 | import org.onosproject.store.AbstractStore; |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 55 | import org.onosproject.store.serializers.KryoNamespaces; |
Madan Jampani | 3780d4b | 2016-04-04 18:18:24 -0700 | [diff] [blame] | 56 | import org.onosproject.store.service.CommitStatus; |
Jordan Halterman | 7af71da | 2017-05-15 13:41:00 -0700 | [diff] [blame] | 57 | import org.onosproject.store.service.DistributedPrimitive; |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 58 | import org.onosproject.store.service.Serializer; |
| 59 | import org.onosproject.store.service.StorageService; |
| 60 | import org.onosproject.store.service.TransactionContext; |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 61 | import org.slf4j.Logger; |
| 62 | import org.slf4j.LoggerFactory; |
| 63 | |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 64 | import static com.google.common.base.Preconditions.checkArgument; |
| 65 | import static com.google.common.base.Preconditions.checkNotNull; |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 66 | import static org.onosproject.net.resource.ResourceEvent.Type.RESOURCE_ADDED; |
| 67 | import static org.onosproject.net.resource.ResourceEvent.Type.RESOURCE_REMOVED; |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 68 | |
| 69 | /** |
| 70 | * Implementation of ResourceStore using TransactionalMap. |
| 71 | */ |
Sho SHIMIZU | 9a2b829 | 2015-10-28 13:00:16 -0700 | [diff] [blame] | 72 | @Component(immediate = true) |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 73 | @Service |
| 74 | @Beta |
Sho SHIMIZU | fa62b47 | 2015-11-02 17:35:46 -0800 | [diff] [blame] | 75 | public class ConsistentResourceStore extends AbstractStore<ResourceEvent, ResourceStoreDelegate> |
| 76 | implements ResourceStore { |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 77 | private static final Logger log = LoggerFactory.getLogger(ConsistentResourceStore.class); |
| 78 | |
Sho SHIMIZU | 65de961 | 2016-05-19 12:24:20 -0700 | [diff] [blame] | 79 | static final Serializer SERIALIZER = Serializer.using(KryoNamespace.newBuilder() |
| 80 | .register(KryoNamespaces.API) |
| 81 | .register(UnifiedDiscreteResources.class) |
| 82 | .register(new EncodableDiscreteResourcesSerializer(), EncodableDiscreteResources.class) |
Sho SHIMIZU | f503a62 | 2016-05-25 11:12:23 -0700 | [diff] [blame] | 83 | .register(GenericDiscreteResources.class) |
Sho SHIMIZU | 65de961 | 2016-05-19 12:24:20 -0700 | [diff] [blame] | 84 | .register(EmptyDiscreteResources.class) |
Sho SHIMIZU | 2795d63 | 2016-05-25 14:10:13 -0700 | [diff] [blame] | 85 | .register(new EncodedResourcesSerializer(), EncodedDiscreteResources.class) |
Sho SHIMIZU | 65de961 | 2016-05-19 12:24:20 -0700 | [diff] [blame] | 86 | .register(ContinuousResourceAllocation.class) |
Sho SHIMIZU | 9db6da6 | 2016-06-01 12:31:21 -0700 | [diff] [blame] | 87 | .register(PortNumberCodec.class) |
Sho SHIMIZU | 59512bf | 2016-06-01 11:30:58 -0700 | [diff] [blame] | 88 | .register(VlanIdCodec.class) |
| 89 | .register(MplsLabelCodec.class) |
Sho SHIMIZU | 65de961 | 2016-05-19 12:24:20 -0700 | [diff] [blame] | 90 | .build()); |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 91 | |
| 92 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 93 | protected StorageService service; |
| 94 | |
Sho SHIMIZU | 7ecf5ea | 2016-05-13 15:28:59 -0700 | [diff] [blame] | 95 | private ConsistentDiscreteResourceSubStore discreteStore; |
| 96 | private ConsistentContinuousResourceSubStore continuousStore; |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 97 | |
| 98 | @Activate |
| 99 | public void activate() { |
Sho SHIMIZU | 7ecf5ea | 2016-05-13 15:28:59 -0700 | [diff] [blame] | 100 | discreteStore = new ConsistentDiscreteResourceSubStore(service); |
| 101 | continuousStore = new ConsistentContinuousResourceSubStore(service); |
Sho SHIMIZU | e7db614 | 2015-11-04 11:24:22 -0800 | [diff] [blame] | 102 | |
Madan Jampani | c7f49f9 | 2015-12-10 11:35:06 -0800 | [diff] [blame] | 103 | log.info("Started"); |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 104 | } |
| 105 | |
Sho SHIMIZU | dd3750c | 2016-02-01 11:37:04 -0800 | [diff] [blame] | 106 | // Computational complexity: O(1) if the resource is discrete type. |
| 107 | // 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] | 108 | @Override |
Sho SHIMIZU | dd3750c | 2016-02-01 11:37:04 -0800 | [diff] [blame] | 109 | public List<ResourceAllocation> getResourceAllocations(ResourceId id) { |
| 110 | checkNotNull(id); |
| 111 | checkArgument(id instanceof DiscreteResourceId || id instanceof ContinuousResourceId); |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 112 | |
Sho SHIMIZU | dd3750c | 2016-02-01 11:37:04 -0800 | [diff] [blame] | 113 | if (id instanceof DiscreteResourceId) { |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 114 | return discreteStore.getResourceAllocations((DiscreteResourceId) id); |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 115 | } else { |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 116 | return continuousStore.getResourceAllocations((ContinuousResourceId) id); |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 117 | } |
| 118 | } |
| 119 | |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 120 | @Override |
Sho SHIMIZU | ef835c9 | 2016-08-08 13:51:17 -0700 | [diff] [blame] | 121 | public boolean register(List<? extends Resource> resources) { |
Sho SHIMIZU | 83e17a0 | 2015-08-20 14:07:05 -0700 | [diff] [blame] | 122 | checkNotNull(resources); |
HIGUCHI Yuta | 6f828c3 | 2016-01-20 18:11:05 -0800 | [diff] [blame] | 123 | if (log.isTraceEnabled()) { |
| 124 | resources.forEach(r -> log.trace("registering {}", r)); |
| 125 | } |
Sho SHIMIZU | ba41fc1 | 2015-08-12 15:43:22 -0700 | [diff] [blame] | 126 | |
Jordan Halterman | 7af71da | 2017-05-15 13:41:00 -0700 | [diff] [blame] | 127 | // Retry the transaction until successful. |
| 128 | while (true) { |
| 129 | TransactionContext tx = service.transactionContextBuilder().build(); |
| 130 | tx.begin(); |
Sho SHIMIZU | ba41fc1 | 2015-08-12 15:43:22 -0700 | [diff] [blame] | 131 | |
Jordan Halterman | 7af71da | 2017-05-15 13:41:00 -0700 | [diff] [blame] | 132 | // the order is preserved by LinkedHashMap |
| 133 | Map<DiscreteResource, List<Resource>> resourceMap = resources.stream() |
| 134 | .filter(x -> x.parent().isPresent()) |
| 135 | .collect(Collectors.groupingBy(x -> x.parent().get(), LinkedHashMap::new, Collectors.toList())); |
Sho SHIMIZU | ba41fc1 | 2015-08-12 15:43:22 -0700 | [diff] [blame] | 136 | |
Jordan Halterman | 7af71da | 2017-05-15 13:41:00 -0700 | [diff] [blame] | 137 | TransactionalDiscreteResourceSubStore discreteTxStore = discreteStore.transactional(tx); |
| 138 | TransactionalContinuousResourceSubStore continuousTxStore = continuousStore.transactional(tx); |
| 139 | for (Map.Entry<DiscreteResource, List<Resource>> entry : resourceMap.entrySet()) { |
| 140 | DiscreteResourceId parentId = entry.getKey().id(); |
| 141 | if (!discreteTxStore.lookup(parentId).isPresent()) { |
| 142 | return abortTransaction(tx); |
| 143 | } |
| 144 | |
| 145 | if (!register(discreteTxStore, continuousTxStore, parentId, entry.getValue())) { |
| 146 | return abortTransaction(tx); |
| 147 | } |
Sho SHIMIZU | ba41fc1 | 2015-08-12 15:43:22 -0700 | [diff] [blame] | 148 | } |
| 149 | |
Jordan Halterman | 7af71da | 2017-05-15 13:41:00 -0700 | [diff] [blame] | 150 | try { |
| 151 | CommitStatus status = commitTransaction(tx); |
| 152 | if (status == CommitStatus.SUCCESS) { |
| 153 | log.trace("Transaction commit succeeded on registration: resources={}", resources); |
| 154 | List<ResourceEvent> events = resources.stream() |
| 155 | .filter(x -> x.parent().isPresent()) |
| 156 | .map(x -> new ResourceEvent(RESOURCE_ADDED, x)) |
| 157 | .collect(Collectors.toList()); |
| 158 | notifyDelegate(events); |
| 159 | return true; |
| 160 | } |
| 161 | } catch (InterruptedException | ExecutionException | TimeoutException e) { |
| 162 | log.warn("Transaction commit failed on registration", e); |
| 163 | return false; |
Sho SHIMIZU | 1e0a34c | 2015-11-02 16:52:29 -0800 | [diff] [blame] | 164 | } |
Sho SHIMIZU | ba41fc1 | 2015-08-12 15:43:22 -0700 | [diff] [blame] | 165 | } |
| 166 | } |
| 167 | |
| 168 | @Override |
Sho SHIMIZU | ef835c9 | 2016-08-08 13:51:17 -0700 | [diff] [blame] | 169 | public boolean unregister(List<? extends ResourceId> ids) { |
Sho SHIMIZU | 72f81b1 | 2016-02-09 09:26:17 -0800 | [diff] [blame] | 170 | checkNotNull(ids); |
Sho SHIMIZU | 2d8a13a | 2015-08-18 22:37:41 -0700 | [diff] [blame] | 171 | |
Jordan Halterman | 7af71da | 2017-05-15 13:41:00 -0700 | [diff] [blame] | 172 | // Retry the transaction until successful. |
| 173 | while (true) { |
| 174 | TransactionContext tx = service.transactionContextBuilder().build(); |
| 175 | tx.begin(); |
Sho SHIMIZU | 2d8a13a | 2015-08-18 22:37:41 -0700 | [diff] [blame] | 176 | |
Jordan Halterman | 7af71da | 2017-05-15 13:41:00 -0700 | [diff] [blame] | 177 | TransactionalDiscreteResourceSubStore discreteTxStore = discreteStore.transactional(tx); |
| 178 | TransactionalContinuousResourceSubStore continuousTxStore = continuousStore.transactional(tx); |
| 179 | // Look up resources by resource IDs |
| 180 | List<Resource> resources = ids.stream() |
| 181 | .filter(x -> x.parent().isPresent()) |
| 182 | .map(x -> { |
| 183 | // avoid access to consistent map in the case of discrete resource |
| 184 | if (x instanceof DiscreteResourceId) { |
| 185 | return Optional.of(Resources.discrete((DiscreteResourceId) x).resource()); |
| 186 | } else { |
| 187 | return continuousTxStore.lookup((ContinuousResourceId) x); |
| 188 | } |
| 189 | }) |
| 190 | .flatMap(Tools::stream) |
| 191 | .collect(Collectors.toList()); |
| 192 | // the order is preserved by LinkedHashMap |
| 193 | Map<DiscreteResourceId, List<Resource>> resourceMap = resources.stream().collect( |
| 194 | Collectors.groupingBy(x -> x.parent().get().id(), LinkedHashMap::new, Collectors.toList())); |
Sho SHIMIZU | 83e17a0 | 2015-08-20 14:07:05 -0700 | [diff] [blame] | 195 | |
Jordan Halterman | 7af71da | 2017-05-15 13:41:00 -0700 | [diff] [blame] | 196 | for (Map.Entry<DiscreteResourceId, List<Resource>> entry : resourceMap.entrySet()) { |
| 197 | if (!unregister(discreteTxStore, continuousTxStore, entry.getKey(), entry.getValue())) { |
| 198 | log.warn("Failed to unregister {}: Failed to remove {} values.", |
| 199 | entry.getKey(), entry.getValue().size()); |
| 200 | return abortTransaction(tx); |
| 201 | } |
Sho SHIMIZU | 1e0a34c | 2015-11-02 16:52:29 -0800 | [diff] [blame] | 202 | } |
Sho SHIMIZU | 1e0a34c | 2015-11-02 16:52:29 -0800 | [diff] [blame] | 203 | |
Jordan Halterman | 7af71da | 2017-05-15 13:41:00 -0700 | [diff] [blame] | 204 | try { |
| 205 | CommitStatus status = commitTransaction(tx); |
| 206 | if (status == CommitStatus.SUCCESS) { |
| 207 | List<ResourceEvent> events = resources.stream() |
| 208 | .filter(x -> x.parent().isPresent()) |
| 209 | .map(x -> new ResourceEvent(RESOURCE_REMOVED, x)) |
| 210 | .collect(Collectors.toList()); |
| 211 | notifyDelegate(events); |
| 212 | return true; |
| 213 | } |
| 214 | } catch (InterruptedException | ExecutionException | TimeoutException e) { |
Sho SHIMIZU | 6193621 | 2017-01-09 17:50:56 -0800 | [diff] [blame] | 215 | String message = resources.stream() |
| 216 | .map(Resource::simpleTypeName) |
| 217 | .collect(Collectors.groupingBy(Function.identity(), Collectors.counting())) |
| 218 | .entrySet().stream() |
Jordan Halterman | 7af71da | 2017-05-15 13:41:00 -0700 | [diff] [blame] | 219 | .map(entry -> String.format("%d %s type resources", entry.getValue(), entry.getKey())) |
Sho SHIMIZU | 6193621 | 2017-01-09 17:50:56 -0800 | [diff] [blame] | 220 | .collect(Collectors.joining(", ")); |
Jordan Halterman | 7af71da | 2017-05-15 13:41:00 -0700 | [diff] [blame] | 221 | log.warn("Failed to unregister {}: {}", message, e); |
| 222 | return false; |
Madan Jampani | 3780d4b | 2016-04-04 18:18:24 -0700 | [diff] [blame] | 223 | } |
Jordan Halterman | 7af71da | 2017-05-15 13:41:00 -0700 | [diff] [blame] | 224 | } |
Sho SHIMIZU | 2d8a13a | 2015-08-18 22:37:41 -0700 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | @Override |
Sho SHIMIZU | ef835c9 | 2016-08-08 13:51:17 -0700 | [diff] [blame] | 228 | public boolean allocate(List<? extends Resource> resources, ResourceConsumer consumer) { |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 229 | checkNotNull(resources); |
| 230 | checkNotNull(consumer); |
| 231 | |
Jordan Halterman | 7af71da | 2017-05-15 13:41:00 -0700 | [diff] [blame] | 232 | while (true) { |
| 233 | TransactionContext tx = service.transactionContextBuilder().build(); |
| 234 | tx.begin(); |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 235 | |
Jordan Halterman | 7af71da | 2017-05-15 13:41:00 -0700 | [diff] [blame] | 236 | TransactionalDiscreteResourceSubStore discreteTxStore = discreteStore.transactional(tx); |
| 237 | TransactionalContinuousResourceSubStore continuousTxStore = continuousStore.transactional(tx); |
| 238 | for (Resource resource : resources) { |
| 239 | if (resource instanceof DiscreteResource) { |
| 240 | if (!discreteTxStore.allocate(consumer.consumerId(), (DiscreteResource) resource)) { |
| 241 | return abortTransaction(tx); |
| 242 | } |
| 243 | } else if (resource instanceof ContinuousResource) { |
| 244 | if (!continuousTxStore.allocate(consumer.consumerId(), (ContinuousResource) resource)) { |
| 245 | return abortTransaction(tx); |
| 246 | } |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 247 | } |
Sho SHIMIZU | 1e0a34c | 2015-11-02 16:52:29 -0800 | [diff] [blame] | 248 | } |
Sho SHIMIZU | 1e0a34c | 2015-11-02 16:52:29 -0800 | [diff] [blame] | 249 | |
Jordan Halterman | 7af71da | 2017-05-15 13:41:00 -0700 | [diff] [blame] | 250 | try { |
| 251 | if (commitTransaction(tx) == CommitStatus.SUCCESS) { |
| 252 | return true; |
| 253 | } |
| 254 | } catch (InterruptedException | ExecutionException | TimeoutException e) { |
| 255 | log.warn("Failed to allocate {}: {}", resources, e); |
| 256 | return false; |
| 257 | } |
| 258 | } |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | @Override |
Sho SHIMIZU | fc64ffe | 2016-02-10 20:11:09 -0800 | [diff] [blame] | 262 | public boolean release(List<ResourceAllocation> allocations) { |
| 263 | checkNotNull(allocations); |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 264 | |
Jordan Halterman | 7af71da | 2017-05-15 13:41:00 -0700 | [diff] [blame] | 265 | while (true) { |
| 266 | TransactionContext tx = service.transactionContextBuilder().build(); |
| 267 | tx.begin(); |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 268 | |
Jordan Halterman | 7af71da | 2017-05-15 13:41:00 -0700 | [diff] [blame] | 269 | TransactionalDiscreteResourceSubStore discreteTxStore = discreteStore.transactional(tx); |
| 270 | TransactionalContinuousResourceSubStore continuousTxStore = continuousStore.transactional(tx); |
| 271 | for (ResourceAllocation allocation : allocations) { |
| 272 | Resource resource = allocation.resource(); |
| 273 | ResourceConsumerId consumerId = allocation.consumerId(); |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 274 | |
Jordan Halterman | 7af71da | 2017-05-15 13:41:00 -0700 | [diff] [blame] | 275 | if (resource instanceof DiscreteResource) { |
| 276 | if (!discreteTxStore.release(consumerId, (DiscreteResource) resource)) { |
| 277 | return abortTransaction(tx); |
| 278 | } |
| 279 | } else if (resource instanceof ContinuousResource) { |
| 280 | if (!continuousTxStore.release(consumerId, (ContinuousResource) resource)) { |
| 281 | return abortTransaction(tx); |
| 282 | } |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 283 | } |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 284 | } |
Sho SHIMIZU | 1e0a34c | 2015-11-02 16:52:29 -0800 | [diff] [blame] | 285 | |
Jordan Halterman | 7af71da | 2017-05-15 13:41:00 -0700 | [diff] [blame] | 286 | try { |
| 287 | if (commitTransaction(tx) == CommitStatus.SUCCESS) { |
| 288 | return true; |
| 289 | } |
| 290 | } catch (InterruptedException | ExecutionException | TimeoutException e) { |
| 291 | log.warn("Failed to release {}: {}", allocations, e); |
| 292 | return false; |
| 293 | } |
| 294 | } |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 295 | } |
| 296 | |
Sho SHIMIZU | dd3750c | 2016-02-01 11:37:04 -0800 | [diff] [blame] | 297 | // computational complexity: O(1) if the resource is discrete type. |
| 298 | // O(n) if the resource is continuous type where n is the number of the children of |
| 299 | // the specified resource's parent |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 300 | @Override |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 301 | public boolean isAvailable(Resource resource) { |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 302 | checkNotNull(resource); |
Sho SHIMIZU | f33b893 | 2016-01-25 18:43:32 -0800 | [diff] [blame] | 303 | checkArgument(resource instanceof DiscreteResource || resource instanceof ContinuousResource); |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 304 | |
Sho SHIMIZU | f33b893 | 2016-01-25 18:43:32 -0800 | [diff] [blame] | 305 | if (resource instanceof DiscreteResource) { |
HIGUCHI Yuta | 6f828c3 | 2016-01-20 18:11:05 -0800 | [diff] [blame] | 306 | // check if already consumed |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 307 | return discreteStore.isAvailable((DiscreteResource) resource); |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 308 | } else { |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 309 | return continuousStore.isAvailable((ContinuousResource) resource); |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 310 | } |
| 311 | } |
| 312 | |
Sho SHIMIZU | dd3750c | 2016-02-01 11:37:04 -0800 | [diff] [blame] | 313 | // computational complexity: O(n + m) where n is the number of entries in discreteConsumers |
| 314 | // and m is the number of allocations for all continuous resources |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 315 | @Override |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 316 | public Collection<Resource> getResources(ResourceConsumer consumer) { |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 317 | checkNotNull(consumer); |
| 318 | |
| 319 | // NOTE: getting all entries may become performance bottleneck |
| 320 | // TODO: revisit for better backend data structure |
Naoki Shiota | bd1974c | 2016-04-29 18:44:17 -0700 | [diff] [blame] | 321 | Stream<DiscreteResource> discrete = discreteStore.getResources(consumer.consumerId()); |
| 322 | Stream<ContinuousResource> continuous = continuousStore.getResources(consumer.consumerId()); |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 323 | |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 324 | return Stream.concat(discrete, continuous).collect(Collectors.toList()); |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 325 | } |
| 326 | |
Sho SHIMIZU | 82bfe99 | 2016-02-10 09:55:32 -0800 | [diff] [blame] | 327 | // computational complexity: O(1) |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 328 | @Override |
Sho SHIMIZU | dd3750c | 2016-02-01 11:37:04 -0800 | [diff] [blame] | 329 | public Set<Resource> getChildResources(DiscreteResourceId parent) { |
Sho SHIMIZU | e7f4f3f | 2015-10-13 16:27:25 -0700 | [diff] [blame] | 330 | checkNotNull(parent); |
| 331 | |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 332 | return ImmutableSet.<Resource>builder() |
| 333 | .addAll(discreteStore.getChildResources(parent)) |
| 334 | .addAll(continuousStore.getChildResources(parent)) |
| 335 | .build(); |
Sho SHIMIZU | e7f4f3f | 2015-10-13 16:27:25 -0700 | [diff] [blame] | 336 | } |
| 337 | |
Sho SHIMIZU | 9cc4a24 | 2016-05-26 12:55:35 -0700 | [diff] [blame] | 338 | @Override |
| 339 | public <T> Set<Resource> getChildResources(DiscreteResourceId parent, Class<T> cls) { |
| 340 | checkNotNull(parent); |
| 341 | checkNotNull(cls); |
| 342 | |
| 343 | return ImmutableSet.<Resource>builder() |
| 344 | .addAll(discreteStore.getChildResources(parent, cls)) |
| 345 | .addAll(continuousStore.getChildResources(parent, cls)) |
| 346 | .build(); |
| 347 | } |
| 348 | |
Sho SHIMIZU | dd3750c | 2016-02-01 11:37:04 -0800 | [diff] [blame] | 349 | // 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] | 350 | @Override |
Sho SHIMIZU | dd3750c | 2016-02-01 11:37:04 -0800 | [diff] [blame] | 351 | public <T> Collection<Resource> getAllocatedResources(DiscreteResourceId parent, Class<T> cls) { |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 352 | checkNotNull(parent); |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 353 | checkNotNull(cls); |
| 354 | |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 355 | Stream<DiscreteResource> discrete = discreteStore.getAllocatedResources(parent, cls); |
| 356 | Stream<ContinuousResource> continuous = continuousStore.getAllocatedResources(parent, cls); |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 357 | |
| 358 | return Stream.concat(discrete, continuous).collect(Collectors.toList()); |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 359 | } |
Sho SHIMIZU | d29847f | 2015-08-13 09:10:59 -0700 | [diff] [blame] | 360 | |
| 361 | /** |
Jordan Halterman | 7af71da | 2017-05-15 13:41:00 -0700 | [diff] [blame] | 362 | * Commits a transaction. |
| 363 | * |
| 364 | * @param tx the transaction to commit |
| 365 | * @return the transaction status |
| 366 | */ |
| 367 | private CommitStatus commitTransaction(TransactionContext tx) |
| 368 | throws InterruptedException, ExecutionException, TimeoutException { |
| 369 | return tx.commit().get(DistributedPrimitive.DEFAULT_OPERTATION_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS); |
| 370 | } |
| 371 | |
| 372 | /** |
Sho SHIMIZU | d29847f | 2015-08-13 09:10:59 -0700 | [diff] [blame] | 373 | * Abort the transaction. |
| 374 | * |
| 375 | * @param tx transaction context |
| 376 | * @return always false |
| 377 | */ |
| 378 | private boolean abortTransaction(TransactionContext tx) { |
| 379 | tx.abort(); |
| 380 | return false; |
| 381 | } |
| 382 | |
| 383 | /** |
Sho SHIMIZU | ba41fc1 | 2015-08-12 15:43:22 -0700 | [diff] [blame] | 384 | * Appends the values to the existing values associated with the specified key. |
Sho SHIMIZU | 4568c41 | 2015-08-21 16:39:07 -0700 | [diff] [blame] | 385 | * 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] | 386 | * |
Sho SHIMIZU | 0b4e9dd | 2016-06-28 17:42:05 -0700 | [diff] [blame] | 387 | * @param parent resource ID of the parent under which the given resources are registered |
| 388 | * @param resources resources to be registered |
Sho SHIMIZU | ba41fc1 | 2015-08-12 15:43:22 -0700 | [diff] [blame] | 389 | * @return true if the operation succeeds, false otherwise. |
| 390 | */ |
Sho SHIMIZU | dd3750c | 2016-02-01 11:37:04 -0800 | [diff] [blame] | 391 | // computational complexity: O(n) where n is the number of the specified value |
Sho SHIMIZU | 7ecf5ea | 2016-05-13 15:28:59 -0700 | [diff] [blame] | 392 | private boolean register(TransactionalDiscreteResourceSubStore discreteTxStore, |
| 393 | TransactionalContinuousResourceSubStore continuousTxStore, |
Sho SHIMIZU | 0b4e9dd | 2016-06-28 17:42:05 -0700 | [diff] [blame] | 394 | DiscreteResourceId parent, List<Resource> resources) { |
Sho SHIMIZU | 03be266 | 2016-05-04 09:38:45 -0700 | [diff] [blame] | 395 | // it's assumed that the passed "values" is non-empty |
| 396 | |
| 397 | // This is 2-pass scan. Nicer to have 1-pass scan |
Sho SHIMIZU | 0b4e9dd | 2016-06-28 17:42:05 -0700 | [diff] [blame] | 398 | Set<DiscreteResource> discreteResources = resources.stream() |
Sho SHIMIZU | 03be266 | 2016-05-04 09:38:45 -0700 | [diff] [blame] | 399 | .filter(x -> x instanceof DiscreteResource) |
| 400 | .map(x -> (DiscreteResource) x) |
Sho SHIMIZU | 34847b7 | 2016-06-07 14:31:54 -0700 | [diff] [blame] | 401 | .collect(Collectors.toCollection(LinkedHashSet::new)); |
Sho SHIMIZU | 0b4e9dd | 2016-06-28 17:42:05 -0700 | [diff] [blame] | 402 | Set<ContinuousResource> continuousResources = resources.stream() |
Sho SHIMIZU | 03be266 | 2016-05-04 09:38:45 -0700 | [diff] [blame] | 403 | .filter(x -> x instanceof ContinuousResource) |
| 404 | .map(x -> (ContinuousResource) x) |
Sho SHIMIZU | 34847b7 | 2016-06-07 14:31:54 -0700 | [diff] [blame] | 405 | .collect(Collectors.toCollection(LinkedHashSet::new)); |
Sho SHIMIZU | 03be266 | 2016-05-04 09:38:45 -0700 | [diff] [blame] | 406 | |
Sho SHIMIZU | 0b4e9dd | 2016-06-28 17:42:05 -0700 | [diff] [blame] | 407 | return discreteTxStore.register(parent, discreteResources) |
| 408 | && continuousTxStore.register(parent, continuousResources); |
Sho SHIMIZU | ba41fc1 | 2015-08-12 15:43:22 -0700 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | /** |
Sho SHIMIZU | ba1f83b | 2015-10-14 08:11:20 -0700 | [diff] [blame] | 412 | * Removes the values from the existing values associated with the specified key. |
Sho SHIMIZU | 5618ee5 | 2015-08-21 17:19:44 -0700 | [diff] [blame] | 413 | * 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] | 414 | * |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 415 | * @param discreteTxStore map holding multiple discrete resources for a key |
| 416 | * @param continuousTxStore map holding multiple continuous resources for a key |
Sho SHIMIZU | 0b4e9dd | 2016-06-28 17:42:05 -0700 | [diff] [blame] | 417 | * @param parent resource ID of the parent under which the given resources are unregistered |
| 418 | * @param resources resources to be unregistered |
Sho SHIMIZU | 2d8a13a | 2015-08-18 22:37:41 -0700 | [diff] [blame] | 419 | * @return true if the operation succeeds, false otherwise |
| 420 | */ |
Sho SHIMIZU | 7ecf5ea | 2016-05-13 15:28:59 -0700 | [diff] [blame] | 421 | private boolean unregister(TransactionalDiscreteResourceSubStore discreteTxStore, |
| 422 | TransactionalContinuousResourceSubStore continuousTxStore, |
Sho SHIMIZU | 0b4e9dd | 2016-06-28 17:42:05 -0700 | [diff] [blame] | 423 | DiscreteResourceId parent, List<Resource> resources) { |
Sho SHIMIZU | 03be266 | 2016-05-04 09:38:45 -0700 | [diff] [blame] | 424 | // it's assumed that the passed "values" is non-empty |
| 425 | |
| 426 | // This is 2-pass scan. Nicer to have 1-pass scan |
Sho SHIMIZU | 0b4e9dd | 2016-06-28 17:42:05 -0700 | [diff] [blame] | 427 | Set<DiscreteResource> discreteResources = resources.stream() |
Sho SHIMIZU | 03be266 | 2016-05-04 09:38:45 -0700 | [diff] [blame] | 428 | .filter(x -> x instanceof DiscreteResource) |
| 429 | .map(x -> (DiscreteResource) x) |
Sho SHIMIZU | 34847b7 | 2016-06-07 14:31:54 -0700 | [diff] [blame] | 430 | .collect(Collectors.toCollection(LinkedHashSet::new)); |
Sho SHIMIZU | 0b4e9dd | 2016-06-28 17:42:05 -0700 | [diff] [blame] | 431 | Set<ContinuousResource> continuousResources = resources.stream() |
Sho SHIMIZU | 03be266 | 2016-05-04 09:38:45 -0700 | [diff] [blame] | 432 | .filter(x -> x instanceof ContinuousResource) |
| 433 | .map(x -> (ContinuousResource) x) |
Sho SHIMIZU | 34847b7 | 2016-06-07 14:31:54 -0700 | [diff] [blame] | 434 | .collect(Collectors.toCollection(LinkedHashSet::new)); |
Sho SHIMIZU | 03be266 | 2016-05-04 09:38:45 -0700 | [diff] [blame] | 435 | |
Sho SHIMIZU | 0b4e9dd | 2016-06-28 17:42:05 -0700 | [diff] [blame] | 436 | return discreteTxStore.unregister(parent, discreteResources) |
| 437 | && continuousTxStore.unregister(parent, continuousResources); |
Sho SHIMIZU | 03be266 | 2016-05-04 09:38:45 -0700 | [diff] [blame] | 438 | } |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 439 | } |