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 | e7db614 | 2015-11-04 11:24:22 -0800 | [diff] [blame] | 19 | import com.google.common.collect.ImmutableList; |
Sho SHIMIZU | 83258ae | 2016-01-29 17:39:07 -0800 | [diff] [blame] | 20 | import com.google.common.collect.ImmutableSet; |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 21 | import org.apache.felix.scr.annotations.Activate; |
| 22 | import org.apache.felix.scr.annotations.Component; |
| 23 | import org.apache.felix.scr.annotations.Reference; |
| 24 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 25 | import org.apache.felix.scr.annotations.Service; |
Sho SHIMIZU | e18cb12 | 2016-02-22 21:04:56 -0800 | [diff] [blame] | 26 | import org.onosproject.net.resource.ContinuousResource; |
| 27 | import org.onosproject.net.resource.ContinuousResourceId; |
| 28 | import org.onosproject.net.resource.DiscreteResource; |
| 29 | import org.onosproject.net.resource.DiscreteResourceId; |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 30 | import org.onosproject.net.resource.Resource; |
Sho SHIMIZU | e18cb12 | 2016-02-22 21:04:56 -0800 | [diff] [blame] | 31 | import org.onosproject.net.resource.ResourceAllocation; |
| 32 | import org.onosproject.net.resource.ResourceConsumer; |
| 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 | 1e0a34c | 2015-11-02 16:52:29 -0800 | [diff] [blame] | 177 | // even if one of the resources is allocated to a consumer, |
| 178 | // all unregistrations are regarded as failure |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 179 | for (Map.Entry<DiscreteResourceId, List<Resource>> entry : resourceMap.entrySet()) { |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 180 | boolean allocated = entry.getValue().stream().anyMatch(x -> { |
Sho SHIMIZU | f33b893 | 2016-01-25 18:43:32 -0800 | [diff] [blame] | 181 | if (x instanceof DiscreteResource) { |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 182 | return discreteTxStore.isAllocated(((DiscreteResource) x).id()); |
Sho SHIMIZU | f33b893 | 2016-01-25 18:43:32 -0800 | [diff] [blame] | 183 | } else if (x instanceof ContinuousResource) { |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 184 | return continuousTxStore.isAllocated(((ContinuousResource) x).id()); |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 185 | } else { |
| 186 | return false; |
| 187 | } |
| 188 | }); |
| 189 | if (allocated) { |
HIGUCHI Yuta | 5b6dfba | 2016-01-27 14:43:41 -0800 | [diff] [blame] | 190 | log.warn("Failed to unregister {}: allocation exists", entry.getKey()); |
Sho SHIMIZU | 1e0a34c | 2015-11-02 16:52:29 -0800 | [diff] [blame] | 191 | return abortTransaction(tx); |
Sho SHIMIZU | 2d8a13a | 2015-08-18 22:37:41 -0700 | [diff] [blame] | 192 | } |
| 193 | |
Sho SHIMIZU | a2d99eb | 2016-05-06 14:52:55 -0700 | [diff] [blame^] | 194 | if (!unregister(discreteTxStore, continuousTxStore, entry.getKey(), entry.getValue())) { |
HIGUCHI Yuta | 6acdfd0 | 2016-02-18 10:39:43 -0800 | [diff] [blame] | 195 | log.warn("Failed to unregister {}: Failed to remove {} values.", |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 196 | entry.getKey(), entry.getValue().size()); |
HIGUCHI Yuta | 6acdfd0 | 2016-02-18 10:39:43 -0800 | [diff] [blame] | 197 | log.debug("Failed to unregister {}: Failed to remove values: {}", |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 198 | entry.getKey(), entry.getValue()); |
Sho SHIMIZU | 1e0a34c | 2015-11-02 16:52:29 -0800 | [diff] [blame] | 199 | return abortTransaction(tx); |
| 200 | } |
Sho SHIMIZU | 2d8a13a | 2015-08-18 22:37:41 -0700 | [diff] [blame] | 201 | } |
Sho SHIMIZU | 1e0a34c | 2015-11-02 16:52:29 -0800 | [diff] [blame] | 202 | |
Madan Jampani | 3780d4b | 2016-04-04 18:18:24 -0700 | [diff] [blame] | 203 | return tx.commit().whenComplete((status, error) -> { |
| 204 | if (status == CommitStatus.SUCCESS) { |
| 205 | List<ResourceEvent> events = resources.stream() |
| 206 | .filter(x -> x.parent().isPresent()) |
| 207 | .map(x -> new ResourceEvent(RESOURCE_REMOVED, x)) |
| 208 | .collect(Collectors.toList()); |
| 209 | notifyDelegate(events); |
| 210 | } else { |
| 211 | log.warn("Failed to unregister {}: Commit failed.", ids, error); |
| 212 | } |
| 213 | }).join() == CommitStatus.SUCCESS; |
Sho SHIMIZU | 2d8a13a | 2015-08-18 22:37:41 -0700 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | @Override |
Jonathan Hart | 5615126 | 2016-02-11 09:48:50 -0800 | [diff] [blame] | 217 | public boolean allocate(List<Resource> resources, ResourceConsumer consumer) { |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 218 | checkNotNull(resources); |
| 219 | checkNotNull(consumer); |
| 220 | |
| 221 | TransactionContext tx = service.transactionContextBuilder().build(); |
| 222 | tx.begin(); |
| 223 | |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 224 | TransactionalDiscreteResourceStore discreteTxStore = discreteStore.transactional(tx); |
| 225 | TransactionalContinuousResourceStore continuousTxStore = continuousStore.transactional(tx); |
| 226 | for (Resource resource : resources) { |
Sho SHIMIZU | 171a938 | 2016-02-15 13:56:34 -0800 | [diff] [blame] | 227 | if (resource instanceof DiscreteResource) { |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 228 | if (!discreteTxStore.allocate(consumer, (DiscreteResource) resource)) { |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 229 | return abortTransaction(tx); |
| 230 | } |
Sho SHIMIZU | f33b893 | 2016-01-25 18:43:32 -0800 | [diff] [blame] | 231 | } else if (resource instanceof ContinuousResource) { |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 232 | if (!continuousTxStore.allocate(consumer, (ContinuousResource) resource)) { |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 233 | return abortTransaction(tx); |
| 234 | } |
Sho SHIMIZU | 1e0a34c | 2015-11-02 16:52:29 -0800 | [diff] [blame] | 235 | } |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 236 | } |
Sho SHIMIZU | 1e0a34c | 2015-11-02 16:52:29 -0800 | [diff] [blame] | 237 | |
Madan Jampani | 3780d4b | 2016-04-04 18:18:24 -0700 | [diff] [blame] | 238 | return tx.commit().join() == CommitStatus.SUCCESS; |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | @Override |
Sho SHIMIZU | fc64ffe | 2016-02-10 20:11:09 -0800 | [diff] [blame] | 242 | public boolean release(List<ResourceAllocation> allocations) { |
| 243 | checkNotNull(allocations); |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 244 | |
| 245 | TransactionContext tx = service.transactionContextBuilder().build(); |
| 246 | tx.begin(); |
| 247 | |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 248 | TransactionalDiscreteResourceStore discreteTxStore = discreteStore.transactional(tx); |
| 249 | TransactionalContinuousResourceStore continuousTxStore = continuousStore.transactional(tx); |
Sho SHIMIZU | fc64ffe | 2016-02-10 20:11:09 -0800 | [diff] [blame] | 250 | for (ResourceAllocation allocation : allocations) { |
| 251 | Resource resource = allocation.resource(); |
| 252 | ResourceConsumer consumer = allocation.consumer(); |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 253 | |
Sho SHIMIZU | f33b893 | 2016-01-25 18:43:32 -0800 | [diff] [blame] | 254 | if (resource instanceof DiscreteResource) { |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 255 | if (!discreteTxStore.release((DiscreteResource) resource, consumer)) { |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 256 | return abortTransaction(tx); |
| 257 | } |
Sho SHIMIZU | f33b893 | 2016-01-25 18:43:32 -0800 | [diff] [blame] | 258 | } else if (resource instanceof ContinuousResource) { |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 259 | if (!continuousTxStore.release((ContinuousResource) resource, consumer)) { |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 260 | return abortTransaction(tx); |
| 261 | } |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 262 | } |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 263 | } |
Sho SHIMIZU | 1e0a34c | 2015-11-02 16:52:29 -0800 | [diff] [blame] | 264 | |
Madan Jampani | 3780d4b | 2016-04-04 18:18:24 -0700 | [diff] [blame] | 265 | return tx.commit().join() == CommitStatus.SUCCESS; |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 266 | } |
| 267 | |
Sho SHIMIZU | dd3750c | 2016-02-01 11:37:04 -0800 | [diff] [blame] | 268 | // computational complexity: O(1) if the resource is discrete type. |
| 269 | // O(n) if the resource is continuous type where n is the number of the children of |
| 270 | // the specified resource's parent |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 271 | @Override |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 272 | public boolean isAvailable(Resource resource) { |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 273 | checkNotNull(resource); |
Sho SHIMIZU | f33b893 | 2016-01-25 18:43:32 -0800 | [diff] [blame] | 274 | checkArgument(resource instanceof DiscreteResource || resource instanceof ContinuousResource); |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 275 | |
Sho SHIMIZU | f33b893 | 2016-01-25 18:43:32 -0800 | [diff] [blame] | 276 | if (resource instanceof DiscreteResource) { |
HIGUCHI Yuta | 6f828c3 | 2016-01-20 18:11:05 -0800 | [diff] [blame] | 277 | // check if already consumed |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 278 | return discreteStore.isAvailable((DiscreteResource) resource); |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 279 | } else { |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 280 | return continuousStore.isAvailable((ContinuousResource) resource); |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 281 | } |
| 282 | } |
| 283 | |
Sho SHIMIZU | dd3750c | 2016-02-01 11:37:04 -0800 | [diff] [blame] | 284 | // computational complexity: O(n + m) where n is the number of entries in discreteConsumers |
| 285 | // and m is the number of allocations for all continuous resources |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 286 | @Override |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 287 | public Collection<Resource> getResources(ResourceConsumer consumer) { |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 288 | checkNotNull(consumer); |
| 289 | |
| 290 | // NOTE: getting all entries may become performance bottleneck |
| 291 | // TODO: revisit for better backend data structure |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 292 | Stream<DiscreteResource> discrete = discreteStore.getResources(consumer); |
| 293 | Stream<ContinuousResource> continuous = continuousStore.getResources(consumer); |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 294 | |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 295 | return Stream.concat(discrete, continuous).collect(Collectors.toList()); |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 296 | } |
| 297 | |
Sho SHIMIZU | 82bfe99 | 2016-02-10 09:55:32 -0800 | [diff] [blame] | 298 | // computational complexity: O(1) |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 299 | @Override |
Sho SHIMIZU | dd3750c | 2016-02-01 11:37:04 -0800 | [diff] [blame] | 300 | public Set<Resource> getChildResources(DiscreteResourceId parent) { |
Sho SHIMIZU | e7f4f3f | 2015-10-13 16:27:25 -0700 | [diff] [blame] | 301 | checkNotNull(parent); |
| 302 | |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 303 | return ImmutableSet.<Resource>builder() |
| 304 | .addAll(discreteStore.getChildResources(parent)) |
| 305 | .addAll(continuousStore.getChildResources(parent)) |
| 306 | .build(); |
Sho SHIMIZU | e7f4f3f | 2015-10-13 16:27:25 -0700 | [diff] [blame] | 307 | } |
| 308 | |
Sho SHIMIZU | dd3750c | 2016-02-01 11:37:04 -0800 | [diff] [blame] | 309 | // 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] | 310 | @Override |
Sho SHIMIZU | dd3750c | 2016-02-01 11:37:04 -0800 | [diff] [blame] | 311 | public <T> Collection<Resource> getAllocatedResources(DiscreteResourceId parent, Class<T> cls) { |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 312 | checkNotNull(parent); |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 313 | checkNotNull(cls); |
| 314 | |
Sho SHIMIZU | 03be266 | 2016-05-04 09:38:45 -0700 | [diff] [blame] | 315 | Set<Resource> children = getChildResources(parent); |
| 316 | if (children.isEmpty()) { |
| 317 | return children; |
Sho SHIMIZU | ba41fc1 | 2015-08-12 15:43:22 -0700 | [diff] [blame] | 318 | } |
| 319 | |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 320 | Stream<DiscreteResource> discrete = discreteStore.getAllocatedResources(parent, cls); |
| 321 | Stream<ContinuousResource> continuous = continuousStore.getAllocatedResources(parent, cls); |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 322 | |
| 323 | return Stream.concat(discrete, continuous).collect(Collectors.toList()); |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 324 | } |
Sho SHIMIZU | d29847f | 2015-08-13 09:10:59 -0700 | [diff] [blame] | 325 | |
| 326 | /** |
| 327 | * Abort the transaction. |
| 328 | * |
| 329 | * @param tx transaction context |
| 330 | * @return always false |
| 331 | */ |
| 332 | private boolean abortTransaction(TransactionContext tx) { |
| 333 | tx.abort(); |
| 334 | return false; |
| 335 | } |
| 336 | |
| 337 | /** |
Sho SHIMIZU | ba41fc1 | 2015-08-12 15:43:22 -0700 | [diff] [blame] | 338 | * Appends the values to the existing values associated with the specified key. |
Sho SHIMIZU | 4568c41 | 2015-08-21 16:39:07 -0700 | [diff] [blame] | 339 | * 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] | 340 | * |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 341 | * @param key key specifying values |
Sho SHIMIZU | ba41fc1 | 2015-08-12 15:43:22 -0700 | [diff] [blame] | 342 | * @param values values to be appended |
Sho SHIMIZU | ba41fc1 | 2015-08-12 15:43:22 -0700 | [diff] [blame] | 343 | * @return true if the operation succeeds, false otherwise. |
| 344 | */ |
Sho SHIMIZU | dd3750c | 2016-02-01 11:37:04 -0800 | [diff] [blame] | 345 | // 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^] | 346 | private boolean register(TransactionalDiscreteResourceStore discreteTxStore, |
| 347 | TransactionalContinuousResourceStore continuousTxStore, |
| 348 | DiscreteResourceId key, List<Resource> values) { |
Sho SHIMIZU | 03be266 | 2016-05-04 09:38:45 -0700 | [diff] [blame] | 349 | // it's assumed that the passed "values" is non-empty |
| 350 | |
| 351 | // This is 2-pass scan. Nicer to have 1-pass scan |
| 352 | List<DiscreteResource> discreteValues = values.stream() |
| 353 | .filter(x -> x instanceof DiscreteResource) |
| 354 | .map(x -> (DiscreteResource) x) |
| 355 | .collect(Collectors.toList()); |
| 356 | List<ContinuousResource> continuousValues = values.stream() |
| 357 | .filter(x -> x instanceof ContinuousResource) |
| 358 | .map(x -> (ContinuousResource) x) |
| 359 | .collect(Collectors.toList()); |
| 360 | |
| 361 | // short-circuit decision avoiding unnecessary distributed map operations |
| 362 | if (continuousValues.isEmpty()) { |
Sho SHIMIZU | a2d99eb | 2016-05-06 14:52:55 -0700 | [diff] [blame^] | 363 | return discreteTxStore.register(key, discreteValues); |
Sho SHIMIZU | 03be266 | 2016-05-04 09:38:45 -0700 | [diff] [blame] | 364 | } |
| 365 | if (discreteValues.isEmpty()) { |
Sho SHIMIZU | a2d99eb | 2016-05-06 14:52:55 -0700 | [diff] [blame^] | 366 | return continuousTxStore.register(key, continuousValues); |
Sho SHIMIZU | 03be266 | 2016-05-04 09:38:45 -0700 | [diff] [blame] | 367 | } |
| 368 | |
Sho SHIMIZU | a2d99eb | 2016-05-06 14:52:55 -0700 | [diff] [blame^] | 369 | return discreteTxStore.register(key, discreteValues) |
| 370 | && continuousTxStore.register(key, continuousValues); |
Sho SHIMIZU | ba41fc1 | 2015-08-12 15:43:22 -0700 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | /** |
Sho SHIMIZU | ba1f83b | 2015-10-14 08:11:20 -0700 | [diff] [blame] | 374 | * Removes the values from the existing values associated with the specified key. |
Sho SHIMIZU | 5618ee5 | 2015-08-21 17:19:44 -0700 | [diff] [blame] | 375 | * 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] | 376 | * |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 377 | * @param discreteTxStore map holding multiple discrete resources for a key |
| 378 | * @param continuousTxStore map holding multiple continuous resources for a key |
| 379 | * @param key key specifying values |
| 380 | * @param values values to be removed |
Sho SHIMIZU | 2d8a13a | 2015-08-18 22:37:41 -0700 | [diff] [blame] | 381 | * @return true if the operation succeeds, false otherwise |
| 382 | */ |
Sho SHIMIZU | a2d99eb | 2016-05-06 14:52:55 -0700 | [diff] [blame^] | 383 | private boolean unregister(TransactionalDiscreteResourceStore discreteTxStore, |
| 384 | TransactionalContinuousResourceStore continuousTxStore, |
| 385 | DiscreteResourceId key, List<Resource> values) { |
Sho SHIMIZU | 03be266 | 2016-05-04 09:38:45 -0700 | [diff] [blame] | 386 | // it's assumed that the passed "values" is non-empty |
| 387 | |
| 388 | // This is 2-pass scan. Nicer to have 1-pass scan |
| 389 | List<DiscreteResource> discreteValues = values.stream() |
| 390 | .filter(x -> x instanceof DiscreteResource) |
| 391 | .map(x -> (DiscreteResource) x) |
| 392 | .collect(Collectors.toList()); |
| 393 | List<ContinuousResource> continuousValues = values.stream() |
| 394 | .filter(x -> x instanceof ContinuousResource) |
| 395 | .map(x -> (ContinuousResource) x) |
| 396 | .collect(Collectors.toList()); |
| 397 | |
| 398 | // short-circuit decision avoiding unnecessary distributed map operations |
| 399 | if (continuousValues.isEmpty()) { |
Sho SHIMIZU | a2d99eb | 2016-05-06 14:52:55 -0700 | [diff] [blame^] | 400 | return discreteTxStore.unregister(key, discreteValues); |
Sho SHIMIZU | 03be266 | 2016-05-04 09:38:45 -0700 | [diff] [blame] | 401 | } |
| 402 | if (discreteValues.isEmpty()) { |
Sho SHIMIZU | a2d99eb | 2016-05-06 14:52:55 -0700 | [diff] [blame^] | 403 | return continuousTxStore.unregister(key, continuousValues); |
Sho SHIMIZU | 03be266 | 2016-05-04 09:38:45 -0700 | [diff] [blame] | 404 | } |
| 405 | |
Sho SHIMIZU | a2d99eb | 2016-05-06 14:52:55 -0700 | [diff] [blame^] | 406 | return discreteTxStore.unregister(key, discreteValues) |
| 407 | && continuousTxStore.unregister(key, continuousValues); |
Sho SHIMIZU | 03be266 | 2016-05-04 09:38:45 -0700 | [diff] [blame] | 408 | } |
| 409 | |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 410 | // internal use only |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 411 | static final class ContinuousResourceAllocation { |
Sho SHIMIZU | f33b893 | 2016-01-25 18:43:32 -0800 | [diff] [blame] | 412 | private final ContinuousResource original; |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 413 | private final ImmutableList<ResourceAllocation> allocations; |
| 414 | |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 415 | ContinuousResourceAllocation(ContinuousResource original, |
| 416 | ImmutableList<ResourceAllocation> allocations) { |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 417 | this.original = original; |
| 418 | this.allocations = allocations; |
| 419 | } |
| 420 | |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 421 | ContinuousResource original() { |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 422 | return original; |
| 423 | } |
| 424 | |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 425 | ImmutableList<ResourceAllocation> allocations() { |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 426 | return allocations; |
| 427 | } |
Sho SHIMIZU | ba41fc1 | 2015-08-12 15:43:22 -0700 | [diff] [blame] | 428 | } |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 429 | } |