diff --git a/modules/dcache/src/main/java/org/dcache/pool/migration/PoolSelectionStrategy.java b/modules/dcache/src/main/java/org/dcache/pool/migration/PoolSelectionStrategy.java index 3059e619358..cf2c37334d3 100644 --- a/modules/dcache/src/main/java/org/dcache/pool/migration/PoolSelectionStrategy.java +++ b/modules/dcache/src/main/java/org/dcache/pool/migration/PoolSelectionStrategy.java @@ -1,5 +1,7 @@ package org.dcache.pool.migration; +import javax.annotation.Nullable; + import java.util.List; import diskCacheV111.vehicles.PoolManagerPoolInformation; @@ -9,5 +11,6 @@ */ public interface PoolSelectionStrategy { + @Nullable PoolManagerPoolInformation select(List pools); } diff --git a/modules/dcache/src/main/java/org/dcache/pool/migration/RandomPoolSelectionStrategy.java b/modules/dcache/src/main/java/org/dcache/pool/migration/RandomPoolSelectionStrategy.java index 343a9e22745..a77c04144c1 100644 --- a/modules/dcache/src/main/java/org/dcache/pool/migration/RandomPoolSelectionStrategy.java +++ b/modules/dcache/src/main/java/org/dcache/pool/migration/RandomPoolSelectionStrategy.java @@ -1,10 +1,15 @@ package org.dcache.pool.migration; +import com.google.common.base.Predicate; + import java.util.List; import java.util.Random; +import diskCacheV111.pools.PoolCostInfo; import diskCacheV111.vehicles.PoolManagerPoolInformation; +import static com.google.common.collect.Iterables.*; + /** * Implements a pool selection strategy in which the pool is selected * randomly from the list. @@ -18,6 +23,19 @@ public class RandomPoolSelectionStrategy public PoolManagerPoolInformation select(List pools) { - return pools.get(_random.nextInt(pools.size())); + Iterable nonFullPools = + filter(pools, new Predicate() + { + @Override + public boolean apply(PoolManagerPoolInformation pool) + { + PoolCostInfo.PoolSpaceInfo info = pool.getPoolCostInfo().getSpaceInfo(); + return info.getFreeSpace() >= info.getGap(); + } + }); + if (isEmpty(nonFullPools)) { + return null; + } + return get(nonFullPools, _random.nextInt(size(nonFullPools))); } } diff --git a/modules/dcache/src/main/java/org/dcache/pool/migration/Task.java b/modules/dcache/src/main/java/org/dcache/pool/migration/Task.java index 92b5ac7a46b..ea8e009bc26 100644 --- a/modules/dcache/src/main/java/org/dcache/pool/migration/Task.java +++ b/modules/dcache/src/main/java/org/dcache/pool/migration/Task.java @@ -213,10 +213,14 @@ private CellPath selectPool() List pools = newArrayList(filter(_definition.poolList.getPools(), compose(not(in(_replicas)), PoolManagerPoolInformation.GET_NAME))); - if (pools.isEmpty()) { - throw new NoSuchElementException("No pools available"); + PoolManagerPoolInformation pool = _definition.selectionStrategy.select(pools); + if (pool == null) { + if (pools.isEmpty()) { + throw new NoSuchElementException("No pools available."); + } + throw new NoSuchElementException("All target pools are full."); } - return new CellPath(_definition.selectionStrategy.select(pools).getName()); + return new CellPath(pool.getName()); }