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