|
| 1 | +package org.dcache.poolmanager; |
| 2 | + |
| 3 | +import com.google.common.base.Predicate; |
| 4 | +import com.google.common.collect.Lists; |
| 5 | + |
| 6 | +import java.security.SecureRandom; |
| 7 | +import java.util.Arrays; |
| 8 | +import java.util.Collection; |
| 9 | +import java.util.Collections; |
| 10 | +import java.util.Comparator; |
| 11 | +import java.util.List; |
| 12 | +import java.util.Map; |
| 13 | +import java.util.Random; |
| 14 | + |
| 15 | +import diskCacheV111.poolManager.CostModule; |
| 16 | +import diskCacheV111.util.CacheException; |
| 17 | + |
| 18 | +import org.dcache.vehicles.FileAttributes; |
| 19 | + |
| 20 | +import static com.google.common.collect.Iterables.filter; |
| 21 | + |
| 22 | +/** |
| 23 | + * Yet another weighted random partition. Selects randomly source pool and |
| 24 | + * probabilistic weighted destination pool. P2p transfers never use the same host/tag |
| 25 | + * as source and destination. The weight of a pool calculated as: |
| 26 | + * |
| 27 | + * free space / total free space |
| 28 | + * |
| 29 | + * where 'free space' is a sum of free and removable space. |
| 30 | + * |
| 31 | + */ |
| 32 | +public class WRandomPartition extends Partition |
| 33 | +{ |
| 34 | + public final static String TYPE = "wrandom"; |
| 35 | + private static final long serialVersionUID = 5005233401277944842L; |
| 36 | + private final Random _random = new SecureRandom(); |
| 37 | + |
| 38 | + public WRandomPartition(Map<String, String> inherited) { |
| 39 | + super(NO_PROPERTIES, inherited, NO_PROPERTIES); |
| 40 | + } |
| 41 | + |
| 42 | + @Override |
| 43 | + protected Partition create(Map<String, String> inherited, |
| 44 | + Map<String, String> properties) { |
| 45 | + return new WRandomPartition(inherited); |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + public String getType() { |
| 50 | + return TYPE; |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + public P2pPair selectPool2Pool(CostModule cm, List<PoolInfo> src, List<PoolInfo> dst, FileAttributes attributes, boolean force) throws CacheException { |
| 55 | + |
| 56 | + Collections.shuffle(src); |
| 57 | + |
| 58 | + /* |
| 59 | + * for randomly selected source pool and try to find correct destination. |
| 60 | + */ |
| 61 | + for (PoolInfo srcPoolInfo : src) { |
| 62 | + List<PoolInfo> tryList = Lists.newArrayList( |
| 63 | + filter(dst, new DifferentHost(srcPoolInfo.getHostName()))); |
| 64 | + |
| 65 | + if (!tryList.isEmpty()) { |
| 66 | + PoolInfo destPoolInfo = selectWritePool(cm, tryList, attributes, attributes.getSize()); |
| 67 | + return new P2pPair(srcPoolInfo, destPoolInfo); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + return null; |
| 72 | + } |
| 73 | + |
| 74 | + @Override |
| 75 | + public PoolInfo selectReadPool(CostModule cm, List<PoolInfo> pools, FileAttributes attributes) throws CacheException { |
| 76 | + return pools.get(_random.nextInt(pools.size())); |
| 77 | + } |
| 78 | + |
| 79 | + @Override |
| 80 | + public PoolInfo selectStagePool(CostModule cm, List<PoolInfo> pools, String previousPool, String previousHost, FileAttributes attributes) throws CacheException { |
| 81 | + return selectWritePool(cm, pools, attributes, attributes.getSize()); |
| 82 | + } |
| 83 | + |
| 84 | + @Override |
| 85 | + public PoolInfo selectWritePool(CostModule cm, List<PoolInfo> pools, FileAttributes attributes, long preallocated) throws CacheException { |
| 86 | + WeightedPool weightedPools[] = toWeightedWritePoolsArray(pools); |
| 87 | + int index = selectWrandomIndex(weightedPools); |
| 88 | + return weightedPools[index].getCostInfo(); |
| 89 | + } |
| 90 | + |
| 91 | + private WeightedPool[] toWeightedWritePoolsArray(Collection<PoolInfo> costInfos) { |
| 92 | + |
| 93 | + long totalFree = 0; |
| 94 | + for (PoolInfo costInfo : costInfos) { |
| 95 | + long spaceToUse = costInfo.getCostInfo().getSpaceInfo().getFreeSpace() |
| 96 | + + costInfo.getCostInfo().getSpaceInfo().getRemovableSpace(); |
| 97 | + totalFree += spaceToUse; |
| 98 | + } |
| 99 | + |
| 100 | + WeightedPool[] weghtedPools = new WeightedPool[costInfos.size()]; |
| 101 | + int i = 0; |
| 102 | + for (PoolInfo costInfo : costInfos) { |
| 103 | + long spaceToUse = costInfo.getCostInfo().getSpaceInfo().getFreeSpace() |
| 104 | + + costInfo.getCostInfo().getSpaceInfo().getRemovableSpace(); |
| 105 | + |
| 106 | + weghtedPools[i] = new WeightedPool(costInfo, (double) spaceToUse / totalFree); |
| 107 | + i++; |
| 108 | + } |
| 109 | + |
| 110 | + Arrays.sort(weghtedPools, new CostComparator()); |
| 111 | + return weghtedPools; |
| 112 | + } |
| 113 | + |
| 114 | + private static class CostComparator implements Comparator<WeightedPool> |
| 115 | + { |
| 116 | + @Override |
| 117 | + public int compare(WeightedPool o1, WeightedPool o2) { |
| 118 | + return Double.compare(o1.getWeight(), o2.getWeight()); |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + private static class WeightedPool |
| 123 | + { |
| 124 | + private final PoolInfo _costInfo; |
| 125 | + private final double _weight; |
| 126 | + |
| 127 | + public WeightedPool(PoolInfo costInfo, double weight) { |
| 128 | + _costInfo = costInfo; |
| 129 | + _weight = weight; |
| 130 | + } |
| 131 | + |
| 132 | + public PoolInfo getCostInfo() { |
| 133 | + return _costInfo; |
| 134 | + } |
| 135 | + |
| 136 | + public double getWeight() { |
| 137 | + return _weight; |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + private int selectWrandomIndex(WeightedPool[] weightedPools) { |
| 142 | + double selection = _random.nextDouble(); |
| 143 | + double total = 0; |
| 144 | + int i; |
| 145 | + for (i = 0; (i < weightedPools.length) && (total <= selection); i++) { |
| 146 | + total += weightedPools[i].getWeight(); |
| 147 | + } |
| 148 | + return i - 1; |
| 149 | + } |
| 150 | + |
| 151 | + private class DifferentHost implements Predicate<PoolInfo> |
| 152 | + { |
| 153 | + private final String _host; |
| 154 | + |
| 155 | + DifferentHost(String host) { |
| 156 | + _host = host; |
| 157 | + } |
| 158 | + |
| 159 | + @Override |
| 160 | + public boolean apply(PoolInfo t) { |
| 161 | + String hostname = t.getHostName(); |
| 162 | + return !_host.equals(hostname); |
| 163 | + } |
| 164 | + } |
| 165 | +} |
0 commit comments