Sho SHIMIZU
Committed by Gerrit Code Review

Refactor: change method parameter type of ResourceStore#release()

Use ResourceAllocation as parameter of ResourceStore#release()

Change-Id: I1833b9b23985cd42820093633591fa21daf2f98e
...@@ -63,18 +63,17 @@ public interface ResourceStore extends Store<ResourceEvent, ResourceStoreDelegat ...@@ -63,18 +63,17 @@ public interface ResourceStore extends Store<ResourceEvent, ResourceStoreDelegat
63 boolean allocate(List<? extends Resource> resources, ResourceConsumer consumer); 63 boolean allocate(List<? extends Resource> resources, ResourceConsumer consumer);
64 64
65 /** 65 /**
66 - * Releases the specified resources allocated to the specified corresponding consumers 66 + * Releases the specified allocated resources in transactional way.
67 - * in transactional way. The state after completion of this method is all the resources 67 + * The state after completion of this method is all the resources
68 * are released from the consumer, or no resource is released. The whole release fails 68 * are released from the consumer, or no resource is released. The whole release fails
69 * when any one of the resource can't be released. The size of the list of resources and 69 * when any one of the resource can't be released. The size of the list of resources and
70 * that of consumers must be equal. The resource and consumer with the same position from 70 * that of consumers must be equal. The resource and consumer with the same position from
71 * the head of each list correspond to each other. 71 * the head of each list correspond to each other.
72 * 72 *
73 - * @param resources resources to be released 73 + * @param allocations allocaitons to be released
74 - * @param consumers resource consumers to whom the resource allocated to
75 * @return true if succeeds, otherwise false 74 * @return true if succeeds, otherwise false
76 */ 75 */
77 - boolean release(List<Resource> resources, List<ResourceConsumer> consumers); 76 + boolean release(List<ResourceAllocation> allocations);
78 77
79 /** 78 /**
80 * Returns the resource consumers to whom the specified resource is allocated. 79 * Returns the resource consumers to whom the specified resource is allocated.
......
...@@ -98,14 +98,7 @@ public final class ResourceManager extends AbstractListenerManager<ResourceEvent ...@@ -98,14 +98,7 @@ public final class ResourceManager extends AbstractListenerManager<ResourceEvent
98 public boolean release(List<ResourceAllocation> allocations) { 98 public boolean release(List<ResourceAllocation> allocations) {
99 checkNotNull(allocations); 99 checkNotNull(allocations);
100 100
101 - List<Resource> resources = allocations.stream() 101 + return store.release(allocations);
102 - .map(ResourceAllocation::resource)
103 - .collect(Collectors.toList());
104 - List<ResourceConsumer> consumers = allocations.stream()
105 - .map(ResourceAllocation::consumer)
106 - .collect(Collectors.toList());
107 -
108 - return store.release(resources, consumers);
109 } 102 }
110 103
111 @Override 104 @Override
......
...@@ -52,7 +52,6 @@ import org.slf4j.LoggerFactory; ...@@ -52,7 +52,6 @@ import org.slf4j.LoggerFactory;
52 52
53 import java.util.Arrays; 53 import java.util.Arrays;
54 import java.util.Collection; 54 import java.util.Collection;
55 -import java.util.Iterator;
56 import java.util.LinkedHashMap; 55 import java.util.LinkedHashMap;
57 import java.util.LinkedHashSet; 56 import java.util.LinkedHashSet;
58 import java.util.List; 57 import java.util.List;
...@@ -303,10 +302,8 @@ public class ConsistentResourceStore extends AbstractStore<ResourceEvent, Resour ...@@ -303,10 +302,8 @@ public class ConsistentResourceStore extends AbstractStore<ResourceEvent, Resour
303 } 302 }
304 303
305 @Override 304 @Override
306 - public boolean release(List<Resource> resources, List<ResourceConsumer> consumers) { 305 + public boolean release(List<ResourceAllocation> allocations) {
307 - checkNotNull(resources); 306 + checkNotNull(allocations);
308 - checkNotNull(consumers);
309 - checkArgument(resources.size() == consumers.size());
310 307
311 TransactionContext tx = service.transactionContextBuilder().build(); 308 TransactionContext tx = service.transactionContextBuilder().build();
312 tx.begin(); 309 tx.begin();
...@@ -315,12 +312,10 @@ public class ConsistentResourceStore extends AbstractStore<ResourceEvent, Resour ...@@ -315,12 +312,10 @@ public class ConsistentResourceStore extends AbstractStore<ResourceEvent, Resour
315 tx.getTransactionalMap(DISCRETE_CONSUMER_MAP, SERIALIZER); 312 tx.getTransactionalMap(DISCRETE_CONSUMER_MAP, SERIALIZER);
316 TransactionalMap<ContinuousResourceId, ContinuousResourceAllocation> continuousConsumerTxMap = 313 TransactionalMap<ContinuousResourceId, ContinuousResourceAllocation> continuousConsumerTxMap =
317 tx.getTransactionalMap(CONTINUOUS_CONSUMER_MAP, SERIALIZER); 314 tx.getTransactionalMap(CONTINUOUS_CONSUMER_MAP, SERIALIZER);
318 - Iterator<Resource> resourceIte = resources.iterator();
319 - Iterator<ResourceConsumer> consumerIte = consumers.iterator();
320 315
321 - while (resourceIte.hasNext() && consumerIte.hasNext()) { 316 + for (ResourceAllocation allocation : allocations) {
322 - Resource resource = resourceIte.next(); 317 + Resource resource = allocation.resource();
323 - ResourceConsumer consumer = consumerIte.next(); 318 + ResourceConsumer consumer = allocation.consumer();
324 319
325 if (resource instanceof DiscreteResource) { 320 if (resource instanceof DiscreteResource) {
326 // if this single release fails (because the resource is allocated to another consumer, 321 // if this single release fails (because the resource is allocated to another consumer,
...@@ -330,14 +325,14 @@ public class ConsistentResourceStore extends AbstractStore<ResourceEvent, Resour ...@@ -330,14 +325,14 @@ public class ConsistentResourceStore extends AbstractStore<ResourceEvent, Resour
330 } 325 }
331 } else if (resource instanceof ContinuousResource) { 326 } else if (resource instanceof ContinuousResource) {
332 ContinuousResource continuous = (ContinuousResource) resource; 327 ContinuousResource continuous = (ContinuousResource) resource;
333 - ContinuousResourceAllocation allocation = continuousConsumerTxMap.get(continuous.id()); 328 + ContinuousResourceAllocation continuousAllocation = continuousConsumerTxMap.get(continuous.id());
334 - ImmutableList<ResourceAllocation> newAllocations = allocation.allocations().stream() 329 + ImmutableList<ResourceAllocation> newAllocations = continuousAllocation.allocations().stream()
335 .filter(x -> !(x.consumer().equals(consumer) && 330 .filter(x -> !(x.consumer().equals(consumer) &&
336 ((ContinuousResource) x.resource()).value() == continuous.value())) 331 ((ContinuousResource) x.resource()).value() == continuous.value()))
337 .collect(GuavaCollectors.toImmutableList()); 332 .collect(GuavaCollectors.toImmutableList());
338 333
339 - if (!continuousConsumerTxMap.replace(continuous.id(), allocation, 334 + if (!continuousConsumerTxMap.replace(continuous.id(), continuousAllocation,
340 - new ContinuousResourceAllocation(allocation.original(), newAllocations))) { 335 + new ContinuousResourceAllocation(continuousAllocation.original(), newAllocations))) {
341 return abortTransaction(tx); 336 return abortTransaction(tx);
342 } 337 }
343 } 338 }
......