Skip to content

Commit c411aed

Browse files
committed
poolmanager: fix wrandom partition
Motivation: the merge of commit a778f97 added silent conflict which introduced infinite loop due to loop index freez, that ended in infinite loop. Modification: fix the loop using iterator and index (as original code aimed to to). Added extra unit test that check loop in action. Result: fixed infinite loop in wrandom partition Acked-by: Dmitry Litvintsev Target: master, 11.1, 11.0, 10.2, 10.1, 10.0, 9.2 Require-book: no Require-notes: yes (cherry picked from commit cd23f59) Signed-off-by: Tigran Mkrtchyan <[email protected]>
1 parent ee923f7 commit c411aed

File tree

2 files changed

+28
-10
lines changed

2 files changed

+28
-10
lines changed

modules/dcache/src/main/java/org/dcache/poolmanager/WRandomPartition.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,7 @@ public SelectedPool selectWritePool(CostModule cm, List<PoolInfo> pools, FileAtt
9191
return new SelectedPool(weightedPools[index].getCostInfo());
9292
}
9393

94-
private WeightedPool[] toWeightedWritePoolsArray(List<PoolInfo> costInfos, long fileSize)
95-
throws CacheException {
94+
private WeightedPool[] toWeightedWritePoolsArray(List<PoolInfo> costInfos, long fileSize) {
9695

9796
long totalFree = 0;
9897
int validCount = 0;
@@ -108,24 +107,21 @@ private WeightedPool[] toWeightedWritePoolsArray(List<PoolInfo> costInfos, long
108107
validCount++;
109108
}
110109

111-
// the validCount should macht the number of pools that have enough space, thus elegible for selection
110+
// the validCount should mach the number of pools that have enough space, thus eligible for selection
112111
WeightedPool[] weghtedPools = new WeightedPool[validCount];
113-
114-
for (int i = 0; i < weghtedPools.length; /* incremented in the loop */) {
115-
var costInfo = costInfos.get(i);
112+
int i = 0;
113+
for (PoolInfo costInfo : costInfos) {
116114

117115
long gap = costInfo.getCostInfo().getSpaceInfo().getGap();
118116

119117
long spaceToUse = costInfo.getCostInfo().getSpaceInfo().getFreeSpace()
120118
+ costInfo.getCostInfo().getSpaceInfo().getRemovableSpace();
121119

122-
weghtedPools[i] = new WeightedPool(costInfo, (double) spaceToUse / totalFree);
123120
if (fileSize > spaceToUse - gap) {
124121
continue; // skip pools that do not have enough space
125122
}
126123

127124
weghtedPools[i] = new WeightedPool(costInfo, (double) spaceToUse / totalFree);
128-
129125
i++;
130126
}
131127

modules/dcache/src/test/java/org/dcache/poolmanager/WRandomPartitionTest.java

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public void shouldFailIfAllocatesIntoGap() throws CacheException {
3232
).collect(Collectors.toList());
3333

3434
long fileSize = 1000L;
35-
var selectedPool = wrandom.selectWritePool(null, pools, null, fileSize);
35+
wrandom.selectWritePool(null, pools, null, fileSize);
3636
}
3737

3838
@Test
@@ -52,7 +52,29 @@ public void shouldSelectValidPool() throws CacheException {
5252
var selectedPool = wrandom.selectWritePool(null, pools, null, fileSize);
5353

5454
var spaceInfo = selectedPool.info().getCostInfo().getSpaceInfo();
55-
assertTrue("selected pool has no sufficient space", spaceInfo.getFreeSpace() + spaceInfo.getRemovableSpace() - fileSize > spaceInfo.getGap());
55+
assertTrue("selected pool has no sufficient space", spaceInfo.getFreeSpace() + spaceInfo.getRemovableSpace() - fileSize >= spaceInfo.getGap());
5656
}
5757

58+
59+
@Test
60+
public void shouldSkipFullPool() throws CacheException {
61+
62+
var wrandom = new WRandomPartition(Map.of());
63+
64+
// starting the 5th pools have enough space
65+
var pools = IntStream.range(0, 10).mapToObj(i -> {
66+
var cost = new PoolCostInfo("pool" + i, "default-queue");
67+
cost.setSpaceUsage(10_000L, 3_000L + 100L*i, 0L, 0L);
68+
cost.getSpaceInfo().setParameter(0.0d, 2500L);
69+
70+
return new PoolInfo(new CellAddressCore("pool" + i), cost, ImmutableMap.of());
71+
}
72+
).collect(Collectors.toList());
73+
74+
long fileSize = 1000L;
75+
var selectedPool = wrandom.selectWritePool(null, pools, null, fileSize);
76+
77+
var spaceInfo = selectedPool.info().getCostInfo().getSpaceInfo();
78+
assertTrue("selected pool has no sufficient space", spaceInfo.getFreeSpace() + spaceInfo.getRemovableSpace() - fileSize >= spaceInfo.getGap());
79+
}
5880
}

0 commit comments

Comments
 (0)