Skip to content

Commit fb04d1c

Browse files
committed
Revert "poolmanager: remove wrandom partition type"
This reverts commit d56c1e0.
1 parent 5403288 commit fb04d1c

File tree

3 files changed

+187
-0
lines changed

3 files changed

+187
-0
lines changed
Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
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+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package org.dcache.poolmanager;
2+
3+
import java.util.Map;
4+
5+
public class WRandomPartitionFactory implements PartitionFactory
6+
{
7+
@Override
8+
public Partition createPartition(Map<String,String> properties) {
9+
return new WRandomPartition(properties);
10+
}
11+
12+
@Override
13+
public String getDescription() {
14+
return "Partition with random read and free, and reclaimable space weighted random write selection";
15+
}
16+
17+
@Override
18+
public String getType() {
19+
return WRandomPartition.TYPE;
20+
}
21+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
org.dcache.poolmanager.RandomPartitionFactory
22
org.dcache.poolmanager.LruPartitionFactory
33
org.dcache.poolmanager.WassPartitionFactory
4+
org.dcache.poolmanager.WRandomPartitionFactory
45
org.dcache.poolmanager.BufferPartitionFactory

0 commit comments

Comments
 (0)