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
boolean allocate(List<? extends Resource> resources, ResourceConsumer consumer);
/**
* Releases the specified resources allocated to the specified corresponding consumers
* in transactional way. The state after completion of this method is all the resources
* Releases the specified allocated resources in transactional way.
* The state after completion of this method is all the resources
* are released from the consumer, or no resource is released. The whole release fails
* when any one of the resource can't be released. The size of the list of resources and
* that of consumers must be equal. The resource and consumer with the same position from
* the head of each list correspond to each other.
*
* @param resources resources to be released
* @param consumers resource consumers to whom the resource allocated to
* @param allocations allocaitons to be released
* @return true if succeeds, otherwise false
*/
boolean release(List<Resource> resources, List<ResourceConsumer> consumers);
boolean release(List<ResourceAllocation> allocations);
/**
* Returns the resource consumers to whom the specified resource is allocated.
......
......@@ -98,14 +98,7 @@ public final class ResourceManager extends AbstractListenerManager<ResourceEvent
public boolean release(List<ResourceAllocation> allocations) {
checkNotNull(allocations);
List<Resource> resources = allocations.stream()
.map(ResourceAllocation::resource)
.collect(Collectors.toList());
List<ResourceConsumer> consumers = allocations.stream()
.map(ResourceAllocation::consumer)
.collect(Collectors.toList());
return store.release(resources, consumers);
return store.release(allocations);
}
@Override
......
......@@ -52,7 +52,6 @@ import org.slf4j.LoggerFactory;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
......@@ -303,10 +302,8 @@ public class ConsistentResourceStore extends AbstractStore<ResourceEvent, Resour
}
@Override
public boolean release(List<Resource> resources, List<ResourceConsumer> consumers) {
checkNotNull(resources);
checkNotNull(consumers);
checkArgument(resources.size() == consumers.size());
public boolean release(List<ResourceAllocation> allocations) {
checkNotNull(allocations);
TransactionContext tx = service.transactionContextBuilder().build();
tx.begin();
......@@ -315,12 +312,10 @@ public class ConsistentResourceStore extends AbstractStore<ResourceEvent, Resour
tx.getTransactionalMap(DISCRETE_CONSUMER_MAP, SERIALIZER);
TransactionalMap<ContinuousResourceId, ContinuousResourceAllocation> continuousConsumerTxMap =
tx.getTransactionalMap(CONTINUOUS_CONSUMER_MAP, SERIALIZER);
Iterator<Resource> resourceIte = resources.iterator();
Iterator<ResourceConsumer> consumerIte = consumers.iterator();
while (resourceIte.hasNext() && consumerIte.hasNext()) {
Resource resource = resourceIte.next();
ResourceConsumer consumer = consumerIte.next();
for (ResourceAllocation allocation : allocations) {
Resource resource = allocation.resource();
ResourceConsumer consumer = allocation.consumer();
if (resource instanceof DiscreteResource) {
// if this single release fails (because the resource is allocated to another consumer,
......@@ -330,14 +325,14 @@ public class ConsistentResourceStore extends AbstractStore<ResourceEvent, Resour
}
} else if (resource instanceof ContinuousResource) {
ContinuousResource continuous = (ContinuousResource) resource;
ContinuousResourceAllocation allocation = continuousConsumerTxMap.get(continuous.id());
ImmutableList<ResourceAllocation> newAllocations = allocation.allocations().stream()
ContinuousResourceAllocation continuousAllocation = continuousConsumerTxMap.get(continuous.id());
ImmutableList<ResourceAllocation> newAllocations = continuousAllocation.allocations().stream()
.filter(x -> !(x.consumer().equals(consumer) &&
((ContinuousResource) x.resource()).value() == continuous.value()))
.collect(GuavaCollectors.toImmutableList());
if (!continuousConsumerTxMap.replace(continuous.id(), allocation,
new ContinuousResourceAllocation(allocation.original(), newAllocations))) {
if (!continuousConsumerTxMap.replace(continuous.id(), continuousAllocation,
new ContinuousResourceAllocation(continuousAllocation.original(), newAllocations))) {
return abortTransaction(tx);
}
}
......