Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 1 | /* |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 2 | * Copyright 2015-2016 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 | */ |
| 16 | package org.onosproject.store.newresource.impl; |
| 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 | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 21 | import com.google.common.collect.Maps; |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 22 | import org.apache.felix.scr.annotations.Activate; |
| 23 | import org.apache.felix.scr.annotations.Component; |
| 24 | import org.apache.felix.scr.annotations.Reference; |
| 25 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 26 | import org.apache.felix.scr.annotations.Service; |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 27 | import org.onlab.util.GuavaCollectors; |
Thomas Vachuska | 762a2d8 | 2016-01-04 10:25:20 -0800 | [diff] [blame] | 28 | import org.onlab.util.Tools; |
Sho SHIMIZU | f33b893 | 2016-01-25 18:43:32 -0800 | [diff] [blame] | 29 | import org.onosproject.net.newresource.ContinuousResource; |
Sho SHIMIZU | dd3750c | 2016-02-01 11:37:04 -0800 | [diff] [blame] | 30 | import org.onosproject.net.newresource.ContinuousResourceId; |
Sho SHIMIZU | f33b893 | 2016-01-25 18:43:32 -0800 | [diff] [blame] | 31 | import org.onosproject.net.newresource.DiscreteResource; |
Sho SHIMIZU | dd3750c | 2016-02-01 11:37:04 -0800 | [diff] [blame] | 32 | import org.onosproject.net.newresource.DiscreteResourceId; |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 33 | import org.onosproject.net.newresource.ResourceAllocation; |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 34 | import org.onosproject.net.newresource.ResourceConsumer; |
Sho SHIMIZU | fa62b47 | 2015-11-02 17:35:46 -0800 | [diff] [blame] | 35 | import org.onosproject.net.newresource.ResourceEvent; |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 36 | import org.onosproject.net.newresource.ResourceId; |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 37 | import org.onosproject.net.newresource.Resource; |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 38 | import org.onosproject.net.newresource.ResourceStore; |
Sho SHIMIZU | fa62b47 | 2015-11-02 17:35:46 -0800 | [diff] [blame] | 39 | import org.onosproject.net.newresource.ResourceStoreDelegate; |
Sho SHIMIZU | dd3750c | 2016-02-01 11:37:04 -0800 | [diff] [blame] | 40 | import org.onosproject.net.newresource.Resources; |
Sho SHIMIZU | fa62b47 | 2015-11-02 17:35:46 -0800 | [diff] [blame] | 41 | import org.onosproject.store.AbstractStore; |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 42 | import org.onosproject.store.serializers.KryoNamespaces; |
| 43 | import org.onosproject.store.service.ConsistentMap; |
Thomas Vachuska | 762a2d8 | 2016-01-04 10:25:20 -0800 | [diff] [blame] | 44 | import org.onosproject.store.service.ConsistentMapException; |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 45 | import org.onosproject.store.service.Serializer; |
| 46 | import org.onosproject.store.service.StorageService; |
| 47 | import org.onosproject.store.service.TransactionContext; |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 48 | import org.onosproject.store.service.TransactionalMap; |
| 49 | import org.onosproject.store.service.Versioned; |
| 50 | import org.slf4j.Logger; |
| 51 | import org.slf4j.LoggerFactory; |
| 52 | |
Sho SHIMIZU | ba41fc1 | 2015-08-12 15:43:22 -0700 | [diff] [blame] | 53 | import java.util.Arrays; |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 54 | import java.util.Collection; |
Sho SHIMIZU | 69420fe | 2016-02-09 15:01:07 -0800 | [diff] [blame] | 55 | import java.util.LinkedHashMap; |
Sho SHIMIZU | ba41fc1 | 2015-08-12 15:43:22 -0700 | [diff] [blame] | 56 | import java.util.LinkedHashSet; |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 57 | import java.util.List; |
| 58 | import java.util.Map; |
| 59 | import java.util.Optional; |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 60 | import java.util.Set; |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 61 | import java.util.stream.Collectors; |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 62 | import java.util.stream.Stream; |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 63 | |
| 64 | import static com.google.common.base.Preconditions.checkArgument; |
| 65 | import static com.google.common.base.Preconditions.checkNotNull; |
Sho SHIMIZU | fa62b47 | 2015-11-02 17:35:46 -0800 | [diff] [blame] | 66 | import static org.onosproject.net.newresource.ResourceEvent.Type.*; |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 67 | |
| 68 | /** |
| 69 | * Implementation of ResourceStore using TransactionalMap. |
| 70 | */ |
Sho SHIMIZU | 9a2b829 | 2015-10-28 13:00:16 -0700 | [diff] [blame] | 71 | @Component(immediate = true) |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 72 | @Service |
| 73 | @Beta |
Sho SHIMIZU | fa62b47 | 2015-11-02 17:35:46 -0800 | [diff] [blame] | 74 | public class ConsistentResourceStore extends AbstractStore<ResourceEvent, ResourceStoreDelegate> |
| 75 | implements ResourceStore { |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 76 | private static final Logger log = LoggerFactory.getLogger(ConsistentResourceStore.class); |
| 77 | |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 78 | private static final String DISCRETE_CONSUMER_MAP = "onos-discrete-consumers"; |
| 79 | private static final String CONTINUOUS_CONSUMER_MAP = "onos-continuous-consumers"; |
Sho SHIMIZU | ba41fc1 | 2015-08-12 15:43:22 -0700 | [diff] [blame] | 80 | private static final String CHILD_MAP = "onos-resource-children"; |
| 81 | private static final Serializer SERIALIZER = Serializer.using( |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 82 | Arrays.asList(KryoNamespaces.BASIC, KryoNamespaces.API), |
| 83 | ContinuousResourceAllocation.class); |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 84 | |
Thomas Vachuska | 762a2d8 | 2016-01-04 10:25:20 -0800 | [diff] [blame] | 85 | // TODO: We should provide centralized values for this |
| 86 | private static final int MAX_RETRIES = 5; |
| 87 | private static final int RETRY_DELAY = 1_000; // millis |
| 88 | |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 89 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 90 | protected StorageService service; |
| 91 | |
Sho SHIMIZU | dd3750c | 2016-02-01 11:37:04 -0800 | [diff] [blame] | 92 | private ConsistentMap<DiscreteResourceId, ResourceConsumer> discreteConsumers; |
| 93 | private ConsistentMap<ContinuousResourceId, ContinuousResourceAllocation> continuousConsumers; |
| 94 | private ConsistentMap<DiscreteResourceId, Set<Resource>> childMap; |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 95 | |
| 96 | @Activate |
| 97 | public void activate() { |
Sho SHIMIZU | dd3750c | 2016-02-01 11:37:04 -0800 | [diff] [blame] | 98 | discreteConsumers = service.<DiscreteResourceId, ResourceConsumer>consistentMapBuilder() |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 99 | .withName(DISCRETE_CONSUMER_MAP) |
Sho SHIMIZU | ba41fc1 | 2015-08-12 15:43:22 -0700 | [diff] [blame] | 100 | .withSerializer(SERIALIZER) |
| 101 | .build(); |
Sho SHIMIZU | dd3750c | 2016-02-01 11:37:04 -0800 | [diff] [blame] | 102 | continuousConsumers = service.<ContinuousResourceId, ContinuousResourceAllocation>consistentMapBuilder() |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 103 | .withName(CONTINUOUS_CONSUMER_MAP) |
| 104 | .withSerializer(SERIALIZER) |
| 105 | .build(); |
Sho SHIMIZU | dd3750c | 2016-02-01 11:37:04 -0800 | [diff] [blame] | 106 | childMap = service.<DiscreteResourceId, Set<Resource>>consistentMapBuilder() |
Sho SHIMIZU | ba41fc1 | 2015-08-12 15:43:22 -0700 | [diff] [blame] | 107 | .withName(CHILD_MAP) |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 108 | .withSerializer(SERIALIZER) |
| 109 | .build(); |
Sho SHIMIZU | e7db614 | 2015-11-04 11:24:22 -0800 | [diff] [blame] | 110 | |
Sho SHIMIZU | dd3750c | 2016-02-01 11:37:04 -0800 | [diff] [blame] | 111 | Tools.retryable(() -> childMap.put(Resource.ROOT.id(), new LinkedHashSet<>()), |
Thomas Vachuska | 762a2d8 | 2016-01-04 10:25:20 -0800 | [diff] [blame] | 112 | ConsistentMapException.class, MAX_RETRIES, RETRY_DELAY); |
Madan Jampani | c7f49f9 | 2015-12-10 11:35:06 -0800 | [diff] [blame] | 113 | log.info("Started"); |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 114 | } |
| 115 | |
Sho SHIMIZU | dd3750c | 2016-02-01 11:37:04 -0800 | [diff] [blame] | 116 | // Computational complexity: O(1) if the resource is discrete type. |
| 117 | // 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] | 118 | @Override |
Sho SHIMIZU | dd3750c | 2016-02-01 11:37:04 -0800 | [diff] [blame] | 119 | public List<ResourceAllocation> getResourceAllocations(ResourceId id) { |
| 120 | checkNotNull(id); |
| 121 | checkArgument(id instanceof DiscreteResourceId || id instanceof ContinuousResourceId); |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 122 | |
Sho SHIMIZU | dd3750c | 2016-02-01 11:37:04 -0800 | [diff] [blame] | 123 | if (id instanceof DiscreteResourceId) { |
| 124 | return getResourceAllocations((DiscreteResourceId) id); |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 125 | } else { |
Sho SHIMIZU | dd3750c | 2016-02-01 11:37:04 -0800 | [diff] [blame] | 126 | return getResourceAllocations((ContinuousResourceId) id); |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 127 | } |
| 128 | } |
| 129 | |
Sho SHIMIZU | dd3750c | 2016-02-01 11:37:04 -0800 | [diff] [blame] | 130 | // computational complexity: O(1) |
| 131 | private List<ResourceAllocation> getResourceAllocations(DiscreteResourceId resource) { |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 132 | Versioned<ResourceConsumer> consumer = discreteConsumers.get(resource); |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 133 | if (consumer == null) { |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 134 | return ImmutableList.of(); |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 135 | } |
| 136 | |
Sho SHIMIZU | dd3750c | 2016-02-01 11:37:04 -0800 | [diff] [blame] | 137 | return ImmutableList.of(new ResourceAllocation(Resources.discrete(resource).resource(), consumer.value())); |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 138 | } |
| 139 | |
Sho SHIMIZU | dd3750c | 2016-02-01 11:37:04 -0800 | [diff] [blame] | 140 | // computational complexity: O(n) where n is the number of the existing allocations for the resource |
| 141 | private List<ResourceAllocation> getResourceAllocations(ContinuousResourceId resource) { |
| 142 | Versioned<ContinuousResourceAllocation> allocations = continuousConsumers.get(resource); |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 143 | if (allocations == null) { |
| 144 | return ImmutableList.of(); |
| 145 | } |
| 146 | |
| 147 | return allocations.value().allocations().stream() |
Sho SHIMIZU | dd3750c | 2016-02-01 11:37:04 -0800 | [diff] [blame] | 148 | .filter(x -> x.resource().id().equals(resource)) |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 149 | .collect(GuavaCollectors.toImmutableList()); |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | @Override |
Sho SHIMIZU | 84515f3 | 2016-02-10 14:42:33 -0800 | [diff] [blame] | 153 | public boolean register(List<? extends Resource> resources) { |
Sho SHIMIZU | 83e17a0 | 2015-08-20 14:07:05 -0700 | [diff] [blame] | 154 | checkNotNull(resources); |
HIGUCHI Yuta | 6f828c3 | 2016-01-20 18:11:05 -0800 | [diff] [blame] | 155 | if (log.isTraceEnabled()) { |
| 156 | resources.forEach(r -> log.trace("registering {}", r)); |
| 157 | } |
Sho SHIMIZU | ba41fc1 | 2015-08-12 15:43:22 -0700 | [diff] [blame] | 158 | |
| 159 | TransactionContext tx = service.transactionContextBuilder().build(); |
| 160 | tx.begin(); |
| 161 | |
Sho SHIMIZU | dd3750c | 2016-02-01 11:37:04 -0800 | [diff] [blame] | 162 | TransactionalMap<DiscreteResourceId, Set<Resource>> childTxMap = |
Sho SHIMIZU | 1e0a34c | 2015-11-02 16:52:29 -0800 | [diff] [blame] | 163 | tx.getTransactionalMap(CHILD_MAP, SERIALIZER); |
Sho SHIMIZU | ba41fc1 | 2015-08-12 15:43:22 -0700 | [diff] [blame] | 164 | |
Sho SHIMIZU | 69420fe | 2016-02-09 15:01:07 -0800 | [diff] [blame] | 165 | // the order is preserved by LinkedHashMap |
Sho SHIMIZU | f33b893 | 2016-01-25 18:43:32 -0800 | [diff] [blame] | 166 | Map<DiscreteResource, List<Resource>> resourceMap = resources.stream() |
Sho SHIMIZU | 1e0a34c | 2015-11-02 16:52:29 -0800 | [diff] [blame] | 167 | .filter(x -> x.parent().isPresent()) |
Sho SHIMIZU | 69420fe | 2016-02-09 15:01:07 -0800 | [diff] [blame] | 168 | .collect(Collectors.groupingBy(x -> x.parent().get(), LinkedHashMap::new, Collectors.toList())); |
Sho SHIMIZU | ba41fc1 | 2015-08-12 15:43:22 -0700 | [diff] [blame] | 169 | |
Sho SHIMIZU | f33b893 | 2016-01-25 18:43:32 -0800 | [diff] [blame] | 170 | for (Map.Entry<DiscreteResource, List<Resource>> entry: resourceMap.entrySet()) { |
Sho SHIMIZU | 72f81b1 | 2016-02-09 09:26:17 -0800 | [diff] [blame] | 171 | if (!lookup(childTxMap, entry.getKey().id()).isPresent()) { |
Sho SHIMIZU | 1e0a34c | 2015-11-02 16:52:29 -0800 | [diff] [blame] | 172 | return abortTransaction(tx); |
Sho SHIMIZU | ba41fc1 | 2015-08-12 15:43:22 -0700 | [diff] [blame] | 173 | } |
| 174 | |
Sho SHIMIZU | dd3750c | 2016-02-01 11:37:04 -0800 | [diff] [blame] | 175 | if (!appendValues(childTxMap, entry.getKey().id(), entry.getValue())) { |
Sho SHIMIZU | 1e0a34c | 2015-11-02 16:52:29 -0800 | [diff] [blame] | 176 | return abortTransaction(tx); |
| 177 | } |
Sho SHIMIZU | ba41fc1 | 2015-08-12 15:43:22 -0700 | [diff] [blame] | 178 | } |
Sho SHIMIZU | 1e0a34c | 2015-11-02 16:52:29 -0800 | [diff] [blame] | 179 | |
Sho SHIMIZU | fa62b47 | 2015-11-02 17:35:46 -0800 | [diff] [blame] | 180 | boolean success = tx.commit(); |
| 181 | if (success) { |
| 182 | List<ResourceEvent> events = resources.stream() |
| 183 | .filter(x -> x.parent().isPresent()) |
| 184 | .map(x -> new ResourceEvent(RESOURCE_ADDED, x)) |
| 185 | .collect(Collectors.toList()); |
| 186 | notifyDelegate(events); |
| 187 | } |
| 188 | return success; |
Sho SHIMIZU | ba41fc1 | 2015-08-12 15:43:22 -0700 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | @Override |
Sho SHIMIZU | 84515f3 | 2016-02-10 14:42:33 -0800 | [diff] [blame] | 192 | public boolean unregister(List<? extends ResourceId> ids) { |
Sho SHIMIZU | 72f81b1 | 2016-02-09 09:26:17 -0800 | [diff] [blame] | 193 | checkNotNull(ids); |
Sho SHIMIZU | 2d8a13a | 2015-08-18 22:37:41 -0700 | [diff] [blame] | 194 | |
| 195 | TransactionContext tx = service.transactionContextBuilder().build(); |
| 196 | tx.begin(); |
| 197 | |
Sho SHIMIZU | dd3750c | 2016-02-01 11:37:04 -0800 | [diff] [blame] | 198 | TransactionalMap<DiscreteResourceId, Set<Resource>> childTxMap = |
Sho SHIMIZU | 1e0a34c | 2015-11-02 16:52:29 -0800 | [diff] [blame] | 199 | tx.getTransactionalMap(CHILD_MAP, SERIALIZER); |
Sho SHIMIZU | dd3750c | 2016-02-01 11:37:04 -0800 | [diff] [blame] | 200 | TransactionalMap<DiscreteResourceId, ResourceConsumer> discreteConsumerTxMap = |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 201 | tx.getTransactionalMap(DISCRETE_CONSUMER_MAP, SERIALIZER); |
Sho SHIMIZU | dd3750c | 2016-02-01 11:37:04 -0800 | [diff] [blame] | 202 | TransactionalMap<ContinuousResourceId, ContinuousResourceAllocation> continuousConsumerTxMap = |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 203 | tx.getTransactionalMap(CONTINUOUS_CONSUMER_MAP, SERIALIZER); |
Sho SHIMIZU | 2d8a13a | 2015-08-18 22:37:41 -0700 | [diff] [blame] | 204 | |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 205 | // Extract Discrete instances from resources |
Sho SHIMIZU | 72f81b1 | 2016-02-09 09:26:17 -0800 | [diff] [blame] | 206 | List<Resource> resources = ids.stream() |
Sho SHIMIZU | 1e0a34c | 2015-11-02 16:52:29 -0800 | [diff] [blame] | 207 | .filter(x -> x.parent().isPresent()) |
Sho SHIMIZU | 72f81b1 | 2016-02-09 09:26:17 -0800 | [diff] [blame] | 208 | .map(x -> lookup(childTxMap, x)) |
| 209 | .filter(Optional::isPresent) |
| 210 | .map(Optional::get) |
| 211 | .collect(Collectors.toList()); |
Sho SHIMIZU | 69420fe | 2016-02-09 15:01:07 -0800 | [diff] [blame] | 212 | // the order is preserved by LinkedHashMap |
Sho SHIMIZU | 72f81b1 | 2016-02-09 09:26:17 -0800 | [diff] [blame] | 213 | Map<DiscreteResourceId, List<Resource>> resourceMap = resources.stream() |
Sho SHIMIZU | 69420fe | 2016-02-09 15:01:07 -0800 | [diff] [blame] | 214 | .collect(Collectors.groupingBy(x -> x.parent().get().id(), LinkedHashMap::new, Collectors.toList())); |
Sho SHIMIZU | 83e17a0 | 2015-08-20 14:07:05 -0700 | [diff] [blame] | 215 | |
Sho SHIMIZU | 1e0a34c | 2015-11-02 16:52:29 -0800 | [diff] [blame] | 216 | // even if one of the resources is allocated to a consumer, |
| 217 | // all unregistrations are regarded as failure |
Sho SHIMIZU | dd3750c | 2016-02-01 11:37:04 -0800 | [diff] [blame] | 218 | for (Map.Entry<DiscreteResourceId, List<Resource>> entry: resourceMap.entrySet()) { |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 219 | boolean allocated = entry.getValue().stream().anyMatch(x -> { |
Sho SHIMIZU | f33b893 | 2016-01-25 18:43:32 -0800 | [diff] [blame] | 220 | if (x instanceof DiscreteResource) { |
Sho SHIMIZU | dd3750c | 2016-02-01 11:37:04 -0800 | [diff] [blame] | 221 | return discreteConsumerTxMap.get(((DiscreteResource) x).id()) != null; |
Sho SHIMIZU | f33b893 | 2016-01-25 18:43:32 -0800 | [diff] [blame] | 222 | } else if (x instanceof ContinuousResource) { |
Sho SHIMIZU | dd3750c | 2016-02-01 11:37:04 -0800 | [diff] [blame] | 223 | ContinuousResourceAllocation allocations = |
| 224 | continuousConsumerTxMap.get(((ContinuousResource) x).id()); |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 225 | return allocations != null && !allocations.allocations().isEmpty(); |
| 226 | } else { |
| 227 | return false; |
| 228 | } |
| 229 | }); |
| 230 | if (allocated) { |
HIGUCHI Yuta | 5b6dfba | 2016-01-27 14:43:41 -0800 | [diff] [blame] | 231 | log.warn("Failed to unregister {}: allocation exists", entry.getKey()); |
Sho SHIMIZU | 1e0a34c | 2015-11-02 16:52:29 -0800 | [diff] [blame] | 232 | return abortTransaction(tx); |
Sho SHIMIZU | 2d8a13a | 2015-08-18 22:37:41 -0700 | [diff] [blame] | 233 | } |
| 234 | |
Sho SHIMIZU | 1e0a34c | 2015-11-02 16:52:29 -0800 | [diff] [blame] | 235 | if (!removeValues(childTxMap, entry.getKey(), entry.getValue())) { |
HIGUCHI Yuta | 5b6dfba | 2016-01-27 14:43:41 -0800 | [diff] [blame] | 236 | log.warn("Failed to unregister {}: Failed to remove values: {}", |
| 237 | entry.getKey(), entry.getValue()); |
Sho SHIMIZU | 1e0a34c | 2015-11-02 16:52:29 -0800 | [diff] [blame] | 238 | return abortTransaction(tx); |
| 239 | } |
Sho SHIMIZU | 2d8a13a | 2015-08-18 22:37:41 -0700 | [diff] [blame] | 240 | } |
Sho SHIMIZU | 1e0a34c | 2015-11-02 16:52:29 -0800 | [diff] [blame] | 241 | |
Sho SHIMIZU | fa62b47 | 2015-11-02 17:35:46 -0800 | [diff] [blame] | 242 | boolean success = tx.commit(); |
| 243 | if (success) { |
| 244 | List<ResourceEvent> events = resources.stream() |
| 245 | .filter(x -> x.parent().isPresent()) |
| 246 | .map(x -> new ResourceEvent(RESOURCE_REMOVED, x)) |
| 247 | .collect(Collectors.toList()); |
| 248 | notifyDelegate(events); |
HIGUCHI Yuta | 5b6dfba | 2016-01-27 14:43:41 -0800 | [diff] [blame] | 249 | } else { |
Sho SHIMIZU | 72f81b1 | 2016-02-09 09:26:17 -0800 | [diff] [blame] | 250 | log.warn("Failed to unregister {}: Commit failed.", ids); |
Sho SHIMIZU | fa62b47 | 2015-11-02 17:35:46 -0800 | [diff] [blame] | 251 | } |
| 252 | return success; |
Sho SHIMIZU | 2d8a13a | 2015-08-18 22:37:41 -0700 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | @Override |
Sho SHIMIZU | 84515f3 | 2016-02-10 14:42:33 -0800 | [diff] [blame] | 256 | public boolean allocate(List<? extends Resource> resources, ResourceConsumer consumer) { |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 257 | checkNotNull(resources); |
| 258 | checkNotNull(consumer); |
| 259 | |
| 260 | TransactionContext tx = service.transactionContextBuilder().build(); |
| 261 | tx.begin(); |
| 262 | |
Sho SHIMIZU | dd3750c | 2016-02-01 11:37:04 -0800 | [diff] [blame] | 263 | TransactionalMap<DiscreteResourceId, Set<Resource>> childTxMap = |
Sho SHIMIZU | 1e0a34c | 2015-11-02 16:52:29 -0800 | [diff] [blame] | 264 | tx.getTransactionalMap(CHILD_MAP, SERIALIZER); |
Sho SHIMIZU | dd3750c | 2016-02-01 11:37:04 -0800 | [diff] [blame] | 265 | TransactionalMap<DiscreteResourceId, ResourceConsumer> discreteConsumerTxMap = |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 266 | tx.getTransactionalMap(DISCRETE_CONSUMER_MAP, SERIALIZER); |
Sho SHIMIZU | dd3750c | 2016-02-01 11:37:04 -0800 | [diff] [blame] | 267 | TransactionalMap<ContinuousResourceId, ContinuousResourceAllocation> continuousConsumerTxMap = |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 268 | tx.getTransactionalMap(CONTINUOUS_CONSUMER_MAP, SERIALIZER); |
Sho SHIMIZU | ba41fc1 | 2015-08-12 15:43:22 -0700 | [diff] [blame] | 269 | |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 270 | for (Resource resource: resources) { |
Sho SHIMIZU | f33b893 | 2016-01-25 18:43:32 -0800 | [diff] [blame] | 271 | if (resource instanceof DiscreteResource) { |
Sho SHIMIZU | 72f81b1 | 2016-02-09 09:26:17 -0800 | [diff] [blame] | 272 | if (!lookup(childTxMap, resource.id()).isPresent()) { |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 273 | return abortTransaction(tx); |
| 274 | } |
Sho SHIMIZU | d29847f | 2015-08-13 09:10:59 -0700 | [diff] [blame] | 275 | |
Sho SHIMIZU | dd3750c | 2016-02-01 11:37:04 -0800 | [diff] [blame] | 276 | ResourceConsumer oldValue = discreteConsumerTxMap.put(((DiscreteResource) resource).id(), consumer); |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 277 | if (oldValue != null) { |
| 278 | return abortTransaction(tx); |
| 279 | } |
Sho SHIMIZU | f33b893 | 2016-01-25 18:43:32 -0800 | [diff] [blame] | 280 | } else if (resource instanceof ContinuousResource) { |
Sho SHIMIZU | 72f81b1 | 2016-02-09 09:26:17 -0800 | [diff] [blame] | 281 | Optional<Resource> lookedUp = lookup(childTxMap, resource.id()); |
| 282 | if (!lookedUp.isPresent()) { |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 283 | return abortTransaction(tx); |
| 284 | } |
| 285 | |
Sho SHIMIZU | 72f81b1 | 2016-02-09 09:26:17 -0800 | [diff] [blame] | 286 | // Down cast: this must be safe as ContinuousResource is associated with ContinuousResourceId |
| 287 | ContinuousResource continuous = (ContinuousResource) lookedUp.get(); |
| 288 | ContinuousResourceAllocation allocations = continuousConsumerTxMap.get(continuous.id()); |
| 289 | if (!hasEnoughResource(continuous, (ContinuousResource) resource, allocations)) { |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 290 | return abortTransaction(tx); |
| 291 | } |
| 292 | |
| 293 | boolean success = appendValue(continuousConsumerTxMap, |
Sho SHIMIZU | 72f81b1 | 2016-02-09 09:26:17 -0800 | [diff] [blame] | 294 | continuous, new ResourceAllocation(continuous, consumer)); |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 295 | if (!success) { |
| 296 | return abortTransaction(tx); |
| 297 | } |
Sho SHIMIZU | 1e0a34c | 2015-11-02 16:52:29 -0800 | [diff] [blame] | 298 | } |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 299 | } |
Sho SHIMIZU | 1e0a34c | 2015-11-02 16:52:29 -0800 | [diff] [blame] | 300 | |
| 301 | return tx.commit(); |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | @Override |
Sho SHIMIZU | fc64ffe | 2016-02-10 20:11:09 -0800 | [diff] [blame^] | 305 | public boolean release(List<ResourceAllocation> allocations) { |
| 306 | checkNotNull(allocations); |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 307 | |
| 308 | TransactionContext tx = service.transactionContextBuilder().build(); |
| 309 | tx.begin(); |
| 310 | |
Sho SHIMIZU | dd3750c | 2016-02-01 11:37:04 -0800 | [diff] [blame] | 311 | TransactionalMap<DiscreteResourceId, ResourceConsumer> discreteConsumerTxMap = |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 312 | tx.getTransactionalMap(DISCRETE_CONSUMER_MAP, SERIALIZER); |
Sho SHIMIZU | dd3750c | 2016-02-01 11:37:04 -0800 | [diff] [blame] | 313 | TransactionalMap<ContinuousResourceId, ContinuousResourceAllocation> continuousConsumerTxMap = |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 314 | tx.getTransactionalMap(CONTINUOUS_CONSUMER_MAP, SERIALIZER); |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 315 | |
Sho SHIMIZU | fc64ffe | 2016-02-10 20:11:09 -0800 | [diff] [blame^] | 316 | for (ResourceAllocation allocation : allocations) { |
| 317 | Resource resource = allocation.resource(); |
| 318 | ResourceConsumer consumer = allocation.consumer(); |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 319 | |
Sho SHIMIZU | f33b893 | 2016-01-25 18:43:32 -0800 | [diff] [blame] | 320 | if (resource instanceof DiscreteResource) { |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 321 | // if this single release fails (because the resource is allocated to another consumer, |
| 322 | // the whole release fails |
Sho SHIMIZU | dd3750c | 2016-02-01 11:37:04 -0800 | [diff] [blame] | 323 | if (!discreteConsumerTxMap.remove(((DiscreteResource) resource).id(), consumer)) { |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 324 | return abortTransaction(tx); |
| 325 | } |
Sho SHIMIZU | f33b893 | 2016-01-25 18:43:32 -0800 | [diff] [blame] | 326 | } else if (resource instanceof ContinuousResource) { |
| 327 | ContinuousResource continuous = (ContinuousResource) resource; |
Sho SHIMIZU | fc64ffe | 2016-02-10 20:11:09 -0800 | [diff] [blame^] | 328 | ContinuousResourceAllocation continuousAllocation = continuousConsumerTxMap.get(continuous.id()); |
| 329 | ImmutableList<ResourceAllocation> newAllocations = continuousAllocation.allocations().stream() |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 330 | .filter(x -> !(x.consumer().equals(consumer) && |
Sho SHIMIZU | f33b893 | 2016-01-25 18:43:32 -0800 | [diff] [blame] | 331 | ((ContinuousResource) x.resource()).value() == continuous.value())) |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 332 | .collect(GuavaCollectors.toImmutableList()); |
| 333 | |
Sho SHIMIZU | fc64ffe | 2016-02-10 20:11:09 -0800 | [diff] [blame^] | 334 | if (!continuousConsumerTxMap.replace(continuous.id(), continuousAllocation, |
| 335 | new ContinuousResourceAllocation(continuousAllocation.original(), newAllocations))) { |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 336 | return abortTransaction(tx); |
| 337 | } |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 338 | } |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 339 | } |
Sho SHIMIZU | 1e0a34c | 2015-11-02 16:52:29 -0800 | [diff] [blame] | 340 | |
| 341 | return tx.commit(); |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 342 | } |
| 343 | |
Sho SHIMIZU | dd3750c | 2016-02-01 11:37:04 -0800 | [diff] [blame] | 344 | // computational complexity: O(1) if the resource is discrete type. |
| 345 | // O(n) if the resource is continuous type where n is the number of the children of |
| 346 | // the specified resource's parent |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 347 | @Override |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 348 | public boolean isAvailable(Resource resource) { |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 349 | checkNotNull(resource); |
Sho SHIMIZU | f33b893 | 2016-01-25 18:43:32 -0800 | [diff] [blame] | 350 | checkArgument(resource instanceof DiscreteResource || resource instanceof ContinuousResource); |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 351 | |
HIGUCHI Yuta | 6f828c3 | 2016-01-20 18:11:05 -0800 | [diff] [blame] | 352 | // check if it's registered or not. |
Sho SHIMIZU | dd3750c | 2016-02-01 11:37:04 -0800 | [diff] [blame] | 353 | Versioned<Set<Resource>> v = childMap.get(resource.parent().get().id()); |
HIGUCHI Yuta | 6f828c3 | 2016-01-20 18:11:05 -0800 | [diff] [blame] | 354 | if (v == null || !v.value().contains(resource)) { |
| 355 | return false; |
| 356 | } |
| 357 | |
Sho SHIMIZU | f33b893 | 2016-01-25 18:43:32 -0800 | [diff] [blame] | 358 | if (resource instanceof DiscreteResource) { |
HIGUCHI Yuta | 6f828c3 | 2016-01-20 18:11:05 -0800 | [diff] [blame] | 359 | // check if already consumed |
Sho SHIMIZU | dd3750c | 2016-02-01 11:37:04 -0800 | [diff] [blame] | 360 | return getResourceAllocations(resource.id()).isEmpty(); |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 361 | } else { |
Sho SHIMIZU | f33b893 | 2016-01-25 18:43:32 -0800 | [diff] [blame] | 362 | ContinuousResource requested = (ContinuousResource) resource; |
| 363 | ContinuousResource registered = v.value().stream() |
Sho SHIMIZU | 2d31022 | 2016-01-22 11:45:11 -0800 | [diff] [blame] | 364 | .filter(c -> c.id().equals(resource.id())) |
HIGUCHI Yuta | 6f828c3 | 2016-01-20 18:11:05 -0800 | [diff] [blame] | 365 | .findFirst() |
Sho SHIMIZU | f33b893 | 2016-01-25 18:43:32 -0800 | [diff] [blame] | 366 | .map(c -> (ContinuousResource) c) |
HIGUCHI Yuta | 6f828c3 | 2016-01-20 18:11:05 -0800 | [diff] [blame] | 367 | .get(); |
| 368 | if (registered.value() < requested.value()) { |
| 369 | // Capacity < requested, can never satisfy |
| 370 | return false; |
| 371 | } |
| 372 | // check if there's enough left |
| 373 | return isAvailable(requested); |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 374 | } |
| 375 | } |
| 376 | |
Sho SHIMIZU | dd3750c | 2016-02-01 11:37:04 -0800 | [diff] [blame] | 377 | // computational complexity: O(n) where n is the number of existing allocations for the resource |
Sho SHIMIZU | f33b893 | 2016-01-25 18:43:32 -0800 | [diff] [blame] | 378 | private boolean isAvailable(ContinuousResource resource) { |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 379 | Versioned<ContinuousResourceAllocation> allocation = continuousConsumers.get(resource.id()); |
| 380 | if (allocation == null) { |
HIGUCHI Yuta | 6f828c3 | 2016-01-20 18:11:05 -0800 | [diff] [blame] | 381 | // no allocation (=no consumer) full registered resources available |
| 382 | return true; |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | return hasEnoughResource(allocation.value().original(), resource, allocation.value()); |
| 386 | } |
| 387 | |
Sho SHIMIZU | dd3750c | 2016-02-01 11:37:04 -0800 | [diff] [blame] | 388 | // computational complexity: O(n + m) where n is the number of entries in discreteConsumers |
| 389 | // and m is the number of allocations for all continuous resources |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 390 | @Override |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 391 | public Collection<Resource> getResources(ResourceConsumer consumer) { |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 392 | checkNotNull(consumer); |
| 393 | |
| 394 | // NOTE: getting all entries may become performance bottleneck |
| 395 | // TODO: revisit for better backend data structure |
Sho SHIMIZU | f33b893 | 2016-01-25 18:43:32 -0800 | [diff] [blame] | 396 | Stream<DiscreteResource> discreteStream = discreteConsumers.entrySet().stream() |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 397 | .filter(x -> x.getValue().value().equals(consumer)) |
Sho SHIMIZU | dd3750c | 2016-02-01 11:37:04 -0800 | [diff] [blame] | 398 | .map(Map.Entry::getKey) |
| 399 | .map(x -> Resources.discrete(x).resource()); |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 400 | |
Sho SHIMIZU | f33b893 | 2016-01-25 18:43:32 -0800 | [diff] [blame] | 401 | Stream<ContinuousResource> continuousStream = continuousConsumers.values().stream() |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 402 | .flatMap(x -> x.value().allocations().stream() |
| 403 | .map(y -> Maps.immutableEntry(x.value().original(), y))) |
| 404 | .filter(x -> x.getValue().consumer().equals(consumer)) |
| 405 | .map(x -> x.getKey()); |
| 406 | |
| 407 | return Stream.concat(discreteStream, continuousStream).collect(Collectors.toList()); |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 408 | } |
| 409 | |
Sho SHIMIZU | 82bfe99 | 2016-02-10 09:55:32 -0800 | [diff] [blame] | 410 | // computational complexity: O(1) |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 411 | @Override |
Sho SHIMIZU | dd3750c | 2016-02-01 11:37:04 -0800 | [diff] [blame] | 412 | public Set<Resource> getChildResources(DiscreteResourceId parent) { |
Sho SHIMIZU | e7f4f3f | 2015-10-13 16:27:25 -0700 | [diff] [blame] | 413 | checkNotNull(parent); |
| 414 | |
Sho SHIMIZU | dd3750c | 2016-02-01 11:37:04 -0800 | [diff] [blame] | 415 | Versioned<Set<Resource>> children = childMap.get(parent); |
Sho SHIMIZU | e7f4f3f | 2015-10-13 16:27:25 -0700 | [diff] [blame] | 416 | if (children == null) { |
Sho SHIMIZU | 83258ae | 2016-01-29 17:39:07 -0800 | [diff] [blame] | 417 | return ImmutableSet.of(); |
Sho SHIMIZU | e7f4f3f | 2015-10-13 16:27:25 -0700 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | return children.value(); |
| 421 | } |
| 422 | |
Sho SHIMIZU | dd3750c | 2016-02-01 11:37:04 -0800 | [diff] [blame] | 423 | // 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] | 424 | @Override |
Sho SHIMIZU | dd3750c | 2016-02-01 11:37:04 -0800 | [diff] [blame] | 425 | public <T> Collection<Resource> getAllocatedResources(DiscreteResourceId parent, Class<T> cls) { |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 426 | checkNotNull(parent); |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 427 | checkNotNull(cls); |
| 428 | |
Sho SHIMIZU | dd3750c | 2016-02-01 11:37:04 -0800 | [diff] [blame] | 429 | Versioned<Set<Resource>> children = childMap.get(parent); |
Sho SHIMIZU | ba41fc1 | 2015-08-12 15:43:22 -0700 | [diff] [blame] | 430 | if (children == null) { |
Sho SHIMIZU | 2c0ae12 | 2016-01-20 13:14:38 -0800 | [diff] [blame] | 431 | return ImmutableList.of(); |
Sho SHIMIZU | ba41fc1 | 2015-08-12 15:43:22 -0700 | [diff] [blame] | 432 | } |
| 433 | |
Sho SHIMIZU | f33b893 | 2016-01-25 18:43:32 -0800 | [diff] [blame] | 434 | Stream<DiscreteResource> discrete = children.value().stream() |
Sho SHIMIZU | c9546a3 | 2015-11-10 11:22:28 -0800 | [diff] [blame] | 435 | .filter(x -> x.last().getClass().equals(cls)) |
Sho SHIMIZU | f33b893 | 2016-01-25 18:43:32 -0800 | [diff] [blame] | 436 | .filter(x -> x instanceof DiscreteResource) |
Sho SHIMIZU | dd3750c | 2016-02-01 11:37:04 -0800 | [diff] [blame] | 437 | .map(x -> ((DiscreteResource) x)) |
| 438 | .filter(x -> discreteConsumers.containsKey(x.id())); |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 439 | |
Sho SHIMIZU | f33b893 | 2016-01-25 18:43:32 -0800 | [diff] [blame] | 440 | Stream<ContinuousResource> continuous = children.value().stream() |
Sho SHIMIZU | dd3750c | 2016-02-01 11:37:04 -0800 | [diff] [blame] | 441 | .filter(x -> x.id().equals(parent.child(cls))) |
Sho SHIMIZU | f33b893 | 2016-01-25 18:43:32 -0800 | [diff] [blame] | 442 | .filter(x -> x instanceof ContinuousResource) |
| 443 | .map(x -> (ContinuousResource) x) |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 444 | .filter(x -> continuousConsumers.containsKey(x.id())) |
| 445 | .filter(x -> continuousConsumers.get(x.id()) != null) |
| 446 | .filter(x -> !continuousConsumers.get(x.id()).value().allocations().isEmpty()); |
| 447 | |
| 448 | return Stream.concat(discrete, continuous).collect(Collectors.toList()); |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 449 | } |
Sho SHIMIZU | d29847f | 2015-08-13 09:10:59 -0700 | [diff] [blame] | 450 | |
| 451 | /** |
| 452 | * Abort the transaction. |
| 453 | * |
| 454 | * @param tx transaction context |
| 455 | * @return always false |
| 456 | */ |
| 457 | private boolean abortTransaction(TransactionContext tx) { |
| 458 | tx.abort(); |
| 459 | return false; |
| 460 | } |
| 461 | |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 462 | // Appends the specified ResourceAllocation to the existing values stored in the map |
Sho SHIMIZU | dd3750c | 2016-02-01 11:37:04 -0800 | [diff] [blame] | 463 | // computational complexity: O(n) where n is the number of the elements in the associated allocation |
| 464 | private boolean appendValue(TransactionalMap<ContinuousResourceId, ContinuousResourceAllocation> map, |
Sho SHIMIZU | f33b893 | 2016-01-25 18:43:32 -0800 | [diff] [blame] | 465 | ContinuousResource original, ResourceAllocation value) { |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 466 | ContinuousResourceAllocation oldValue = map.putIfAbsent(original.id(), |
| 467 | new ContinuousResourceAllocation(original, ImmutableList.of(value))); |
| 468 | if (oldValue == null) { |
| 469 | return true; |
| 470 | } |
| 471 | |
| 472 | if (oldValue.allocations().contains(value)) { |
| 473 | // don't write to map because all values are already stored |
| 474 | return true; |
| 475 | } |
| 476 | |
| 477 | ContinuousResourceAllocation newValue = new ContinuousResourceAllocation(original, |
| 478 | ImmutableList.<ResourceAllocation>builder() |
| 479 | .addAll(oldValue.allocations()) |
| 480 | .add(value) |
| 481 | .build()); |
| 482 | return map.replace(original.id(), oldValue, newValue); |
| 483 | } |
Sho SHIMIZU | d29847f | 2015-08-13 09:10:59 -0700 | [diff] [blame] | 484 | /** |
Sho SHIMIZU | ba41fc1 | 2015-08-12 15:43:22 -0700 | [diff] [blame] | 485 | * Appends the values to the existing values associated with the specified key. |
Sho SHIMIZU | 4568c41 | 2015-08-21 16:39:07 -0700 | [diff] [blame] | 486 | * 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] | 487 | * |
| 488 | * @param map map holding multiple values for a key |
| 489 | * @param key key specifying values |
| 490 | * @param values values to be appended |
Sho SHIMIZU | ba41fc1 | 2015-08-12 15:43:22 -0700 | [diff] [blame] | 491 | * @return true if the operation succeeds, false otherwise. |
| 492 | */ |
Sho SHIMIZU | dd3750c | 2016-02-01 11:37:04 -0800 | [diff] [blame] | 493 | // computational complexity: O(n) where n is the number of the specified value |
| 494 | private boolean appendValues(TransactionalMap<DiscreteResourceId, Set<Resource>> map, |
| 495 | DiscreteResourceId key, List<Resource> values) { |
Sho SHIMIZU | 07b7bc9 | 2016-01-29 18:27:58 -0800 | [diff] [blame] | 496 | Set<Resource> oldValues = map.putIfAbsent(key, new LinkedHashSet<>(values)); |
Sho SHIMIZU | ba41fc1 | 2015-08-12 15:43:22 -0700 | [diff] [blame] | 497 | if (oldValues == null) { |
Sho SHIMIZU | 93a74b3 | 2015-11-09 11:48:23 -0800 | [diff] [blame] | 498 | return true; |
Sho SHIMIZU | ba41fc1 | 2015-08-12 15:43:22 -0700 | [diff] [blame] | 499 | } |
| 500 | |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 501 | if (oldValues.containsAll(values)) { |
Sho SHIMIZU | 4568c41 | 2015-08-21 16:39:07 -0700 | [diff] [blame] | 502 | // don't write to map because all values are already stored |
| 503 | return true; |
| 504 | } |
| 505 | |
Sho SHIMIZU | 07b7bc9 | 2016-01-29 18:27:58 -0800 | [diff] [blame] | 506 | LinkedHashSet<Resource> newValues = new LinkedHashSet<>(oldValues); |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 507 | newValues.addAll(values); |
| 508 | return map.replace(key, oldValues, newValues); |
Sho SHIMIZU | ba41fc1 | 2015-08-12 15:43:22 -0700 | [diff] [blame] | 509 | } |
| 510 | |
| 511 | /** |
Sho SHIMIZU | ba1f83b | 2015-10-14 08:11:20 -0700 | [diff] [blame] | 512 | * Removes the values from the existing values associated with the specified key. |
Sho SHIMIZU | 5618ee5 | 2015-08-21 17:19:44 -0700 | [diff] [blame] | 513 | * 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] | 514 | * |
| 515 | * @param map map holding multiple values for a key |
| 516 | * @param key key specifying values |
| 517 | * @param values values to be removed |
Sho SHIMIZU | 2d8a13a | 2015-08-18 22:37:41 -0700 | [diff] [blame] | 518 | * @return true if the operation succeeds, false otherwise |
| 519 | */ |
Sho SHIMIZU | dd3750c | 2016-02-01 11:37:04 -0800 | [diff] [blame] | 520 | // computational complexity: O(n) where n is the number of the specified values |
| 521 | private boolean removeValues(TransactionalMap<DiscreteResourceId, Set<Resource>> map, |
| 522 | DiscreteResourceId key, List<Resource> values) { |
Sho SHIMIZU | 07b7bc9 | 2016-01-29 18:27:58 -0800 | [diff] [blame] | 523 | Set<Resource> oldValues = map.putIfAbsent(key, new LinkedHashSet<>()); |
Sho SHIMIZU | 2d8a13a | 2015-08-18 22:37:41 -0700 | [diff] [blame] | 524 | if (oldValues == null) { |
HIGUCHI Yuta | dc4394c | 2016-01-29 15:35:10 -0800 | [diff] [blame] | 525 | log.trace("No-Op removing values. key {} did not exist", key); |
Sho SHIMIZU | 93a74b3 | 2015-11-09 11:48:23 -0800 | [diff] [blame] | 526 | return true; |
Sho SHIMIZU | 2d8a13a | 2015-08-18 22:37:41 -0700 | [diff] [blame] | 527 | } |
| 528 | |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 529 | if (values.stream().allMatch(x -> !oldValues.contains(x))) { |
Sho SHIMIZU | 5618ee5 | 2015-08-21 17:19:44 -0700 | [diff] [blame] | 530 | // don't write map because none of the values are stored |
HIGUCHI Yuta | dc4394c | 2016-01-29 15:35:10 -0800 | [diff] [blame] | 531 | log.trace("No-Op removing values. key {} did not contain {}", key, values); |
Sho SHIMIZU | 5618ee5 | 2015-08-21 17:19:44 -0700 | [diff] [blame] | 532 | return true; |
| 533 | } |
| 534 | |
Sho SHIMIZU | 07b7bc9 | 2016-01-29 18:27:58 -0800 | [diff] [blame] | 535 | LinkedHashSet<Resource> newValues = new LinkedHashSet<>(oldValues); |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 536 | newValues.removeAll(values); |
| 537 | return map.replace(key, oldValues, newValues); |
Sho SHIMIZU | 2d8a13a | 2015-08-18 22:37:41 -0700 | [diff] [blame] | 538 | } |
| 539 | |
| 540 | /** |
Sho SHIMIZU | 72f81b1 | 2016-02-09 09:26:17 -0800 | [diff] [blame] | 541 | * Returns the resource which has the same key as the specified resource ID |
| 542 | * in the set as a value of the map. |
Sho SHIMIZU | ba41fc1 | 2015-08-12 15:43:22 -0700 | [diff] [blame] | 543 | * |
Sho SHIMIZU | 72f81b1 | 2016-02-09 09:26:17 -0800 | [diff] [blame] | 544 | * @param childTxMap map storing parent - child relationship of resources |
| 545 | * @param id ID of resource to be checked |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 546 | * @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] | 547 | */ |
Sho SHIMIZU | 72f81b1 | 2016-02-09 09:26:17 -0800 | [diff] [blame] | 548 | // Naive implementation, which traverses all elements in the set |
Sho SHIMIZU | dd3750c | 2016-02-01 11:37:04 -0800 | [diff] [blame] | 549 | // computational complexity: O(n) where n is the number of elements |
| 550 | // in the associated set |
Sho SHIMIZU | 72f81b1 | 2016-02-09 09:26:17 -0800 | [diff] [blame] | 551 | private Optional<Resource> lookup(TransactionalMap<DiscreteResourceId, Set<Resource>> childTxMap, ResourceId id) { |
| 552 | if (!id.parent().isPresent()) { |
| 553 | return Optional.of(Resource.ROOT); |
Sho SHIMIZU | ba41fc1 | 2015-08-12 15:43:22 -0700 | [diff] [blame] | 554 | } |
| 555 | |
Sho SHIMIZU | 72f81b1 | 2016-02-09 09:26:17 -0800 | [diff] [blame] | 556 | Set<Resource> values = childTxMap.get(id.parent().get()); |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 557 | if (values == null) { |
| 558 | return Optional.empty(); |
| 559 | } |
| 560 | |
Sho SHIMIZU | 72f81b1 | 2016-02-09 09:26:17 -0800 | [diff] [blame] | 561 | return values.stream() |
| 562 | .filter(x -> x.id().equals(id)) |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 563 | .findFirst(); |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 564 | } |
| 565 | |
| 566 | /** |
| 567 | * Checks if there is enough resource volume to allocated the requested resource |
| 568 | * against the specified resource. |
| 569 | * |
| 570 | * @param original original resource |
| 571 | * @param request requested resource |
| 572 | * @param allocation current allocation of the resource |
| 573 | * @return true if there is enough resource volume. Otherwise, false. |
| 574 | */ |
Sho SHIMIZU | dd3750c | 2016-02-01 11:37:04 -0800 | [diff] [blame] | 575 | // computational complexity: O(n) where n is the number of allocations |
Sho SHIMIZU | f33b893 | 2016-01-25 18:43:32 -0800 | [diff] [blame] | 576 | private boolean hasEnoughResource(ContinuousResource original, |
| 577 | ContinuousResource request, |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 578 | ContinuousResourceAllocation allocation) { |
| 579 | if (allocation == null) { |
| 580 | return request.value() <= original.value(); |
| 581 | } |
| 582 | |
| 583 | double allocated = allocation.allocations().stream() |
Sho SHIMIZU | f33b893 | 2016-01-25 18:43:32 -0800 | [diff] [blame] | 584 | .filter(x -> x.resource() instanceof ContinuousResource) |
| 585 | .map(x -> (ContinuousResource) x.resource()) |
| 586 | .mapToDouble(ContinuousResource::value) |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 587 | .sum(); |
| 588 | double left = original.value() - allocated; |
| 589 | return request.value() <= left; |
| 590 | } |
| 591 | |
| 592 | // internal use only |
| 593 | private static final class ContinuousResourceAllocation { |
Sho SHIMIZU | f33b893 | 2016-01-25 18:43:32 -0800 | [diff] [blame] | 594 | private final ContinuousResource original; |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 595 | private final ImmutableList<ResourceAllocation> allocations; |
| 596 | |
Sho SHIMIZU | f33b893 | 2016-01-25 18:43:32 -0800 | [diff] [blame] | 597 | private ContinuousResourceAllocation(ContinuousResource original, |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 598 | ImmutableList<ResourceAllocation> allocations) { |
| 599 | this.original = original; |
| 600 | this.allocations = allocations; |
| 601 | } |
| 602 | |
Sho SHIMIZU | f33b893 | 2016-01-25 18:43:32 -0800 | [diff] [blame] | 603 | private ContinuousResource original() { |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 604 | return original; |
| 605 | } |
| 606 | |
| 607 | private ImmutableList<ResourceAllocation> allocations() { |
| 608 | return allocations; |
| 609 | } |
Sho SHIMIZU | ba41fc1 | 2015-08-12 15:43:22 -0700 | [diff] [blame] | 610 | } |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 611 | } |