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()) { |
| 126 | if (!lookup(discreteTxStore, continuousTxStore, entry.getKey().id()).isPresent()) { |
Sho SHIMIZU | 1e0a34c | 2015-11-02 16:52:29 -0800 | [diff] [blame] | 127 | return abortTransaction(tx); |
Sho SHIMIZU | ba41fc1 | 2015-08-12 15:43:22 -0700 | [diff] [blame] | 128 | } |
| 129 | |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame^] | 130 | if (!appendValues(discreteTxStore, continuousTxStore, entry.getKey().id(), entry.getValue())) { |
Sho SHIMIZU | 1e0a34c | 2015-11-02 16:52:29 -0800 | [diff] [blame] | 131 | return abortTransaction(tx); |
| 132 | } |
Sho SHIMIZU | ba41fc1 | 2015-08-12 15:43:22 -0700 | [diff] [blame] | 133 | } |
Sho SHIMIZU | 1e0a34c | 2015-11-02 16:52:29 -0800 | [diff] [blame] | 134 | |
Madan Jampani | 3780d4b | 2016-04-04 18:18:24 -0700 | [diff] [blame] | 135 | return tx.commit().whenComplete((status, error) -> { |
| 136 | if (status == CommitStatus.SUCCESS) { |
| 137 | log.trace("Transaction commit succeeded on registration: resources={}", resources); |
| 138 | List<ResourceEvent> events = resources.stream() |
| 139 | .filter(x -> x.parent().isPresent()) |
| 140 | .map(x -> new ResourceEvent(RESOURCE_ADDED, x)) |
| 141 | .collect(Collectors.toList()); |
| 142 | notifyDelegate(events); |
| 143 | } else { |
| 144 | log.warn("Transaction commit failed on registration", error); |
| 145 | } |
| 146 | }).join() == CommitStatus.SUCCESS; |
Sho SHIMIZU | ba41fc1 | 2015-08-12 15:43:22 -0700 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | @Override |
Jonathan Hart | 5615126 | 2016-02-11 09:48:50 -0800 | [diff] [blame] | 150 | public boolean unregister(List<ResourceId> ids) { |
Sho SHIMIZU | 72f81b1 | 2016-02-09 09:26:17 -0800 | [diff] [blame] | 151 | checkNotNull(ids); |
Sho SHIMIZU | 2d8a13a | 2015-08-18 22:37:41 -0700 | [diff] [blame] | 152 | |
| 153 | TransactionContext tx = service.transactionContextBuilder().build(); |
| 154 | tx.begin(); |
| 155 | |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame^] | 156 | TransactionalDiscreteResourceStore discreteTxStore = discreteStore.transactional(tx); |
| 157 | TransactionalContinuousResourceStore continuousTxStore = continuousStore.transactional(tx); |
Sho SHIMIZU | 7d54d9c | 2016-02-17 13:58:46 -0800 | [diff] [blame] | 158 | // Look up resources by resource IDs |
Sho SHIMIZU | 72f81b1 | 2016-02-09 09:26:17 -0800 | [diff] [blame] | 159 | List<Resource> resources = ids.stream() |
Sho SHIMIZU | 1e0a34c | 2015-11-02 16:52:29 -0800 | [diff] [blame] | 160 | .filter(x -> x.parent().isPresent()) |
Sho SHIMIZU | 7d54d9c | 2016-02-17 13:58:46 -0800 | [diff] [blame] | 161 | .map(x -> { |
| 162 | // avoid access to consistent map in the case of discrete resource |
| 163 | if (x instanceof DiscreteResourceId) { |
| 164 | return Optional.of(Resources.discrete((DiscreteResourceId) x).resource()); |
| 165 | } else { |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame^] | 166 | return continuousTxStore.lookup((ContinuousResourceId) x); |
Sho SHIMIZU | 7d54d9c | 2016-02-17 13:58:46 -0800 | [diff] [blame] | 167 | } |
| 168 | }) |
HIGUCHI Yuta | 315179a | 2016-02-18 14:01:22 -0800 | [diff] [blame] | 169 | .filter(Optional::isPresent) |
| 170 | .map(Optional::get) |
Sho SHIMIZU | 72f81b1 | 2016-02-09 09:26:17 -0800 | [diff] [blame] | 171 | .collect(Collectors.toList()); |
Sho SHIMIZU | 69420fe | 2016-02-09 15:01:07 -0800 | [diff] [blame] | 172 | // the order is preserved by LinkedHashMap |
Sho SHIMIZU | 72f81b1 | 2016-02-09 09:26:17 -0800 | [diff] [blame] | 173 | Map<DiscreteResourceId, List<Resource>> resourceMap = resources.stream() |
Sho SHIMIZU | 69420fe | 2016-02-09 15:01:07 -0800 | [diff] [blame] | 174 | .collect(Collectors.groupingBy(x -> x.parent().get().id(), LinkedHashMap::new, Collectors.toList())); |
Sho SHIMIZU | 83e17a0 | 2015-08-20 14:07:05 -0700 | [diff] [blame] | 175 | |
Sho SHIMIZU | 1e0a34c | 2015-11-02 16:52:29 -0800 | [diff] [blame] | 176 | // even if one of the resources is allocated to a consumer, |
| 177 | // all unregistrations are regarded as failure |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame^] | 178 | for (Map.Entry<DiscreteResourceId, List<Resource>> entry : resourceMap.entrySet()) { |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 179 | boolean allocated = entry.getValue().stream().anyMatch(x -> { |
Sho SHIMIZU | f33b893 | 2016-01-25 18:43:32 -0800 | [diff] [blame] | 180 | if (x instanceof DiscreteResource) { |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame^] | 181 | return discreteTxStore.isAllocated(((DiscreteResource) x).id()); |
Sho SHIMIZU | f33b893 | 2016-01-25 18:43:32 -0800 | [diff] [blame] | 182 | } else if (x instanceof ContinuousResource) { |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame^] | 183 | return continuousTxStore.isAllocated(((ContinuousResource) x).id()); |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 184 | } else { |
| 185 | return false; |
| 186 | } |
| 187 | }); |
| 188 | if (allocated) { |
HIGUCHI Yuta | 5b6dfba | 2016-01-27 14:43:41 -0800 | [diff] [blame] | 189 | log.warn("Failed to unregister {}: allocation exists", entry.getKey()); |
Sho SHIMIZU | 1e0a34c | 2015-11-02 16:52:29 -0800 | [diff] [blame] | 190 | return abortTransaction(tx); |
Sho SHIMIZU | 2d8a13a | 2015-08-18 22:37:41 -0700 | [diff] [blame] | 191 | } |
| 192 | |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame^] | 193 | if (!removeValues(discreteTxStore, continuousTxStore, entry.getKey(), entry.getValue())) { |
HIGUCHI Yuta | 6acdfd0 | 2016-02-18 10:39:43 -0800 | [diff] [blame] | 194 | log.warn("Failed to unregister {}: Failed to remove {} values.", |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame^] | 195 | entry.getKey(), entry.getValue().size()); |
HIGUCHI Yuta | 6acdfd0 | 2016-02-18 10:39:43 -0800 | [diff] [blame] | 196 | log.debug("Failed to unregister {}: Failed to remove values: {}", |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame^] | 197 | entry.getKey(), entry.getValue()); |
Sho SHIMIZU | 1e0a34c | 2015-11-02 16:52:29 -0800 | [diff] [blame] | 198 | return abortTransaction(tx); |
| 199 | } |
Sho SHIMIZU | 2d8a13a | 2015-08-18 22:37:41 -0700 | [diff] [blame] | 200 | } |
Sho SHIMIZU | 1e0a34c | 2015-11-02 16:52:29 -0800 | [diff] [blame] | 201 | |
Madan Jampani | 3780d4b | 2016-04-04 18:18:24 -0700 | [diff] [blame] | 202 | return tx.commit().whenComplete((status, error) -> { |
| 203 | if (status == CommitStatus.SUCCESS) { |
| 204 | List<ResourceEvent> events = resources.stream() |
| 205 | .filter(x -> x.parent().isPresent()) |
| 206 | .map(x -> new ResourceEvent(RESOURCE_REMOVED, x)) |
| 207 | .collect(Collectors.toList()); |
| 208 | notifyDelegate(events); |
| 209 | } else { |
| 210 | log.warn("Failed to unregister {}: Commit failed.", ids, error); |
| 211 | } |
| 212 | }).join() == CommitStatus.SUCCESS; |
Sho SHIMIZU | 2d8a13a | 2015-08-18 22:37:41 -0700 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | @Override |
Jonathan Hart | 5615126 | 2016-02-11 09:48:50 -0800 | [diff] [blame] | 216 | public boolean allocate(List<Resource> resources, ResourceConsumer consumer) { |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 217 | checkNotNull(resources); |
| 218 | checkNotNull(consumer); |
| 219 | |
| 220 | TransactionContext tx = service.transactionContextBuilder().build(); |
| 221 | tx.begin(); |
| 222 | |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame^] | 223 | TransactionalDiscreteResourceStore discreteTxStore = discreteStore.transactional(tx); |
| 224 | TransactionalContinuousResourceStore continuousTxStore = continuousStore.transactional(tx); |
| 225 | for (Resource resource : resources) { |
Sho SHIMIZU | 171a938 | 2016-02-15 13:56:34 -0800 | [diff] [blame] | 226 | if (resource instanceof DiscreteResource) { |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame^] | 227 | if (!discreteTxStore.allocate(consumer, (DiscreteResource) resource)) { |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 228 | return abortTransaction(tx); |
| 229 | } |
Sho SHIMIZU | f33b893 | 2016-01-25 18:43:32 -0800 | [diff] [blame] | 230 | } else if (resource instanceof ContinuousResource) { |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame^] | 231 | if (!continuousTxStore.allocate(consumer, (ContinuousResource) resource)) { |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 232 | return abortTransaction(tx); |
| 233 | } |
Sho SHIMIZU | 1e0a34c | 2015-11-02 16:52:29 -0800 | [diff] [blame] | 234 | } |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 235 | } |
Sho SHIMIZU | 1e0a34c | 2015-11-02 16:52:29 -0800 | [diff] [blame] | 236 | |
Madan Jampani | 3780d4b | 2016-04-04 18:18:24 -0700 | [diff] [blame] | 237 | return tx.commit().join() == CommitStatus.SUCCESS; |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | @Override |
Sho SHIMIZU | fc64ffe | 2016-02-10 20:11:09 -0800 | [diff] [blame] | 241 | public boolean release(List<ResourceAllocation> allocations) { |
| 242 | checkNotNull(allocations); |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 243 | |
| 244 | TransactionContext tx = service.transactionContextBuilder().build(); |
| 245 | tx.begin(); |
| 246 | |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame^] | 247 | TransactionalDiscreteResourceStore discreteTxStore = discreteStore.transactional(tx); |
| 248 | TransactionalContinuousResourceStore continuousTxStore = continuousStore.transactional(tx); |
Sho SHIMIZU | fc64ffe | 2016-02-10 20:11:09 -0800 | [diff] [blame] | 249 | for (ResourceAllocation allocation : allocations) { |
| 250 | Resource resource = allocation.resource(); |
| 251 | ResourceConsumer consumer = allocation.consumer(); |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 252 | |
Sho SHIMIZU | f33b893 | 2016-01-25 18:43:32 -0800 | [diff] [blame] | 253 | if (resource instanceof DiscreteResource) { |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame^] | 254 | if (!discreteTxStore.release((DiscreteResource) resource, consumer)) { |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 255 | return abortTransaction(tx); |
| 256 | } |
Sho SHIMIZU | f33b893 | 2016-01-25 18:43:32 -0800 | [diff] [blame] | 257 | } else if (resource instanceof ContinuousResource) { |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame^] | 258 | if (!continuousTxStore.release((ContinuousResource) resource, consumer)) { |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 259 | return abortTransaction(tx); |
| 260 | } |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 261 | } |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 262 | } |
Sho SHIMIZU | 1e0a34c | 2015-11-02 16:52:29 -0800 | [diff] [blame] | 263 | |
Madan Jampani | 3780d4b | 2016-04-04 18:18:24 -0700 | [diff] [blame] | 264 | return tx.commit().join() == CommitStatus.SUCCESS; |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 265 | } |
| 266 | |
Sho SHIMIZU | dd3750c | 2016-02-01 11:37:04 -0800 | [diff] [blame] | 267 | // computational complexity: O(1) if the resource is discrete type. |
| 268 | // O(n) if the resource is continuous type where n is the number of the children of |
| 269 | // the specified resource's parent |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 270 | @Override |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 271 | public boolean isAvailable(Resource resource) { |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 272 | checkNotNull(resource); |
Sho SHIMIZU | f33b893 | 2016-01-25 18:43:32 -0800 | [diff] [blame] | 273 | checkArgument(resource instanceof DiscreteResource || resource instanceof ContinuousResource); |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 274 | |
Sho SHIMIZU | f33b893 | 2016-01-25 18:43:32 -0800 | [diff] [blame] | 275 | if (resource instanceof DiscreteResource) { |
HIGUCHI Yuta | 6f828c3 | 2016-01-20 18:11:05 -0800 | [diff] [blame] | 276 | // check if already consumed |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame^] | 277 | return discreteStore.isAvailable((DiscreteResource) resource); |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 278 | } else { |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame^] | 279 | return continuousStore.isAvailable((ContinuousResource) resource); |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 280 | } |
| 281 | } |
| 282 | |
Sho SHIMIZU | dd3750c | 2016-02-01 11:37:04 -0800 | [diff] [blame] | 283 | // computational complexity: O(n + m) where n is the number of entries in discreteConsumers |
| 284 | // and m is the number of allocations for all continuous resources |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 285 | @Override |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 286 | public Collection<Resource> getResources(ResourceConsumer consumer) { |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 287 | checkNotNull(consumer); |
| 288 | |
| 289 | // NOTE: getting all entries may become performance bottleneck |
| 290 | // TODO: revisit for better backend data structure |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame^] | 291 | Stream<DiscreteResource> discrete = discreteStore.getResources(consumer); |
| 292 | Stream<ContinuousResource> continuous = continuousStore.getResources(consumer); |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 293 | |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame^] | 294 | return Stream.concat(discrete, continuous).collect(Collectors.toList()); |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 295 | } |
| 296 | |
Sho SHIMIZU | 82bfe99 | 2016-02-10 09:55:32 -0800 | [diff] [blame] | 297 | // computational complexity: O(1) |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 298 | @Override |
Sho SHIMIZU | dd3750c | 2016-02-01 11:37:04 -0800 | [diff] [blame] | 299 | public Set<Resource> getChildResources(DiscreteResourceId parent) { |
Sho SHIMIZU | e7f4f3f | 2015-10-13 16:27:25 -0700 | [diff] [blame] | 300 | checkNotNull(parent); |
| 301 | |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame^] | 302 | return ImmutableSet.<Resource>builder() |
| 303 | .addAll(discreteStore.getChildResources(parent)) |
| 304 | .addAll(continuousStore.getChildResources(parent)) |
| 305 | .build(); |
Sho SHIMIZU | e7f4f3f | 2015-10-13 16:27:25 -0700 | [diff] [blame] | 306 | } |
| 307 | |
Sho SHIMIZU | dd3750c | 2016-02-01 11:37:04 -0800 | [diff] [blame] | 308 | // 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] | 309 | @Override |
Sho SHIMIZU | dd3750c | 2016-02-01 11:37:04 -0800 | [diff] [blame] | 310 | public <T> Collection<Resource> getAllocatedResources(DiscreteResourceId parent, Class<T> cls) { |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 311 | checkNotNull(parent); |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 312 | checkNotNull(cls); |
| 313 | |
Sho SHIMIZU | 03be266 | 2016-05-04 09:38:45 -0700 | [diff] [blame] | 314 | Set<Resource> children = getChildResources(parent); |
| 315 | if (children.isEmpty()) { |
| 316 | return children; |
Sho SHIMIZU | ba41fc1 | 2015-08-12 15:43:22 -0700 | [diff] [blame] | 317 | } |
| 318 | |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame^] | 319 | Stream<DiscreteResource> discrete = discreteStore.getAllocatedResources(parent, cls); |
| 320 | Stream<ContinuousResource> continuous = continuousStore.getAllocatedResources(parent, cls); |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 321 | |
| 322 | return Stream.concat(discrete, continuous).collect(Collectors.toList()); |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 323 | } |
Sho SHIMIZU | d29847f | 2015-08-13 09:10:59 -0700 | [diff] [blame] | 324 | |
| 325 | /** |
| 326 | * Abort the transaction. |
| 327 | * |
| 328 | * @param tx transaction context |
| 329 | * @return always false |
| 330 | */ |
| 331 | private boolean abortTransaction(TransactionContext tx) { |
| 332 | tx.abort(); |
| 333 | return false; |
| 334 | } |
| 335 | |
| 336 | /** |
Sho SHIMIZU | ba41fc1 | 2015-08-12 15:43:22 -0700 | [diff] [blame] | 337 | * Appends the values to the existing values associated with the specified key. |
Sho SHIMIZU | 4568c41 | 2015-08-21 16:39:07 -0700 | [diff] [blame] | 338 | * 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] | 339 | * |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame^] | 340 | * @param key key specifying values |
Sho SHIMIZU | ba41fc1 | 2015-08-12 15:43:22 -0700 | [diff] [blame] | 341 | * @param values values to be appended |
Sho SHIMIZU | ba41fc1 | 2015-08-12 15:43:22 -0700 | [diff] [blame] | 342 | * @return true if the operation succeeds, false otherwise. |
| 343 | */ |
Sho SHIMIZU | dd3750c | 2016-02-01 11:37:04 -0800 | [diff] [blame] | 344 | // computational complexity: O(n) where n is the number of the specified value |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame^] | 345 | private boolean appendValues(TransactionalDiscreteResourceStore discreteTxStore, |
| 346 | TransactionalContinuousResourceStore continuousTxStore, |
Sho SHIMIZU | dd3750c | 2016-02-01 11:37:04 -0800 | [diff] [blame] | 347 | DiscreteResourceId key, List<Resource> values) { |
Sho SHIMIZU | 03be266 | 2016-05-04 09:38:45 -0700 | [diff] [blame] | 348 | // it's assumed that the passed "values" is non-empty |
| 349 | |
| 350 | // This is 2-pass scan. Nicer to have 1-pass scan |
| 351 | List<DiscreteResource> discreteValues = values.stream() |
| 352 | .filter(x -> x instanceof DiscreteResource) |
| 353 | .map(x -> (DiscreteResource) x) |
| 354 | .collect(Collectors.toList()); |
| 355 | List<ContinuousResource> continuousValues = values.stream() |
| 356 | .filter(x -> x instanceof ContinuousResource) |
| 357 | .map(x -> (ContinuousResource) x) |
| 358 | .collect(Collectors.toList()); |
| 359 | |
| 360 | // short-circuit decision avoiding unnecessary distributed map operations |
| 361 | if (continuousValues.isEmpty()) { |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame^] | 362 | return discreteTxStore.appendValues(key, discreteValues); |
Sho SHIMIZU | 03be266 | 2016-05-04 09:38:45 -0700 | [diff] [blame] | 363 | } |
| 364 | if (discreteValues.isEmpty()) { |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame^] | 365 | return continuousTxStore.appendValues(key, continuousValues); |
Sho SHIMIZU | 03be266 | 2016-05-04 09:38:45 -0700 | [diff] [blame] | 366 | } |
| 367 | |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame^] | 368 | return discreteTxStore.appendValues(key, discreteValues) |
| 369 | && continuousTxStore.appendValues(key, continuousValues); |
Sho SHIMIZU | ba41fc1 | 2015-08-12 15:43:22 -0700 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | /** |
Sho SHIMIZU | ba1f83b | 2015-10-14 08:11:20 -0700 | [diff] [blame] | 373 | * Removes the values from the existing values associated with the specified key. |
Sho SHIMIZU | 5618ee5 | 2015-08-21 17:19:44 -0700 | [diff] [blame] | 374 | * 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] | 375 | * |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame^] | 376 | * @param discreteTxStore map holding multiple discrete resources for a key |
| 377 | * @param continuousTxStore map holding multiple continuous resources for a key |
| 378 | * @param key key specifying values |
| 379 | * @param values values to be removed |
Sho SHIMIZU | 2d8a13a | 2015-08-18 22:37:41 -0700 | [diff] [blame] | 380 | * @return true if the operation succeeds, false otherwise |
| 381 | */ |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame^] | 382 | private boolean removeValues(TransactionalDiscreteResourceStore discreteTxStore, |
| 383 | TransactionalContinuousResourceStore continuousTxStore, |
Sho SHIMIZU | dd3750c | 2016-02-01 11:37:04 -0800 | [diff] [blame] | 384 | DiscreteResourceId key, List<Resource> values) { |
Sho SHIMIZU | 03be266 | 2016-05-04 09:38:45 -0700 | [diff] [blame] | 385 | // it's assumed that the passed "values" is non-empty |
| 386 | |
| 387 | // This is 2-pass scan. Nicer to have 1-pass scan |
| 388 | List<DiscreteResource> discreteValues = values.stream() |
| 389 | .filter(x -> x instanceof DiscreteResource) |
| 390 | .map(x -> (DiscreteResource) x) |
| 391 | .collect(Collectors.toList()); |
| 392 | List<ContinuousResource> continuousValues = values.stream() |
| 393 | .filter(x -> x instanceof ContinuousResource) |
| 394 | .map(x -> (ContinuousResource) x) |
| 395 | .collect(Collectors.toList()); |
| 396 | |
| 397 | // short-circuit decision avoiding unnecessary distributed map operations |
| 398 | if (continuousValues.isEmpty()) { |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame^] | 399 | return discreteTxStore.removeValues(key, discreteValues); |
Sho SHIMIZU | 03be266 | 2016-05-04 09:38:45 -0700 | [diff] [blame] | 400 | } |
| 401 | if (discreteValues.isEmpty()) { |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame^] | 402 | return continuousTxStore.removeValues(key, continuousValues); |
Sho SHIMIZU | 03be266 | 2016-05-04 09:38:45 -0700 | [diff] [blame] | 403 | } |
| 404 | |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame^] | 405 | return discreteTxStore.removeValues(key, discreteValues) |
| 406 | && continuousTxStore.removeValues(key, continuousValues); |
Sho SHIMIZU | 03be266 | 2016-05-04 09:38:45 -0700 | [diff] [blame] | 407 | } |
| 408 | |
Sho SHIMIZU | 2d8a13a | 2015-08-18 22:37:41 -0700 | [diff] [blame] | 409 | /** |
Sho SHIMIZU | 72f81b1 | 2016-02-09 09:26:17 -0800 | [diff] [blame] | 410 | * Returns the resource which has the same key as the specified resource ID |
| 411 | * in the set as a value of the map. |
Sho SHIMIZU | ba41fc1 | 2015-08-12 15:43:22 -0700 | [diff] [blame] | 412 | * |
Sho SHIMIZU | 72f81b1 | 2016-02-09 09:26:17 -0800 | [diff] [blame] | 413 | * @param id ID of resource to be checked |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 414 | * @return the resource which is regarded as the same as the specified resource |
Sho SHIMIZU | ba41fc1 | 2015-08-12 15:43:22 -0700 | [diff] [blame] | 415 | */ |
Sho SHIMIZU | a6a6fd3 | 2016-02-10 18:36:44 -0800 | [diff] [blame] | 416 | // Naive implementation, which traverses all elements in the set when continuous resource |
| 417 | // computational complexity: O(1) when discrete resource. O(n) when continuous resource |
| 418 | // where n is the number of elements in the associated set |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame^] | 419 | private Optional<Resource> lookup(TransactionalDiscreteResourceStore discreteTxStore, |
| 420 | TransactionalContinuousResourceStore continuousTxStore, |
Sho SHIMIZU | 03be266 | 2016-05-04 09:38:45 -0700 | [diff] [blame] | 421 | ResourceId id) { |
| 422 | if (id instanceof DiscreteResourceId) { |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame^] | 423 | return discreteTxStore.lookup((DiscreteResourceId) id); |
Sho SHIMIZU | 03be266 | 2016-05-04 09:38:45 -0700 | [diff] [blame] | 424 | } else if (id instanceof ContinuousResourceId) { |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame^] | 425 | return continuousTxStore.lookup((ContinuousResourceId) id); |
Sho SHIMIZU | 03be266 | 2016-05-04 09:38:45 -0700 | [diff] [blame] | 426 | } else { |
| 427 | return Optional.empty(); |
| 428 | } |
| 429 | } |
| 430 | |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 431 | // internal use only |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame^] | 432 | static final class ContinuousResourceAllocation { |
Sho SHIMIZU | f33b893 | 2016-01-25 18:43:32 -0800 | [diff] [blame] | 433 | private final ContinuousResource original; |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 434 | private final ImmutableList<ResourceAllocation> allocations; |
| 435 | |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame^] | 436 | ContinuousResourceAllocation(ContinuousResource original, |
| 437 | ImmutableList<ResourceAllocation> allocations) { |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 438 | this.original = original; |
| 439 | this.allocations = allocations; |
| 440 | } |
| 441 | |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame^] | 442 | ContinuousResource original() { |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 443 | return original; |
| 444 | } |
| 445 | |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame^] | 446 | ImmutableList<ResourceAllocation> allocations() { |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 447 | return allocations; |
| 448 | } |
Sho SHIMIZU | ba41fc1 | 2015-08-12 15:43:22 -0700 | [diff] [blame] | 449 | } |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 450 | } |