Skip to content

Commit f5c0585

Browse files
authored
[improvement](balance) partition rebalance chose disk by rr #36826 (#36901)
cherry pick from #36826
1 parent 17873d0 commit f5c0585

File tree

3 files changed

+93
-24
lines changed

3 files changed

+93
-24
lines changed

fe/fe-core/src/main/java/org/apache/doris/clone/PartitionRebalancer.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
import java.util.Map;
4040
import java.util.NavigableSet;
4141
import java.util.Random;
42-
import java.util.Set;
4342
import java.util.concurrent.atomic.AtomicLong;
4443
import java.util.stream.Collectors;
4544

@@ -266,9 +265,9 @@ protected void completeSchedCtx(TabletSchedCtx tabletCtx)
266265
Preconditions.checkNotNull(slot, "unable to get slot of toBe " + move.toBe);
267266

268267
List<RootPathLoadStatistic> paths = beStat.getPathStatistics();
269-
Set<Long> availPath = paths.stream().filter(path -> path.getStorageMedium() == tabletCtx.getStorageMedium()
268+
List<Long> availPath = paths.stream().filter(path -> path.getStorageMedium() == tabletCtx.getStorageMedium()
270269
&& path.isFit(tabletCtx.getTabletSize(), false) == BalanceStatus.OK)
271-
.map(RootPathLoadStatistic::getPathHash).collect(Collectors.toSet());
270+
.map(RootPathLoadStatistic::getPathHash).collect(Collectors.toList());
272271
long pathHash = slot.takeAnAvailBalanceSlotFrom(availPath);
273272
if (pathHash == -1) {
274273
throw new SchedException(SchedException.Status.SCHEDULE_FAILED, SchedException.SubCode.WAITING_SLOT,

fe/fe-core/src/main/java/org/apache/doris/clone/TabletScheduler.java

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1933,9 +1933,12 @@ public static class PathSlot {
19331933
// path hash -> slot num
19341934
private Map<Long, Slot> pathSlots = Maps.newConcurrentMap();
19351935
private long beId;
1936+
// only use in takeAnAvailBalanceSlotFrom, make pick RR
1937+
private long lastPickPathHash;
19361938

19371939
public PathSlot(Map<Long, TStorageMedium> paths, long beId) {
19381940
this.beId = beId;
1941+
this.lastPickPathHash = -1;
19391942
for (Map.Entry<Long, TStorageMedium> entry : paths.entrySet()) {
19401943
pathSlots.put(entry.getKey(), new Slot(entry.getValue()));
19411944
}
@@ -2046,19 +2049,6 @@ public synchronized int getTotalAvailBalanceSlotNum() {
20462049
return num;
20472050
}
20482051

2049-
/**
2050-
* get path whose balance slot num is larger than 0
2051-
*/
2052-
public synchronized Set<Long> getAvailPathsForBalance() {
2053-
Set<Long> pathHashs = Sets.newHashSet();
2054-
for (Map.Entry<Long, Slot> entry : pathSlots.entrySet()) {
2055-
if (entry.getValue().getAvailableBalance() > 0) {
2056-
pathHashs.add(entry.getKey());
2057-
}
2058-
}
2059-
return pathHashs;
2060-
}
2061-
20622052
public synchronized List<List<String>> getSlotInfo(long beId) {
20632053
List<List<String>> results = Lists.newArrayList();
20642054
pathSlots.forEach((key, value) -> {
@@ -2091,15 +2081,31 @@ public synchronized long takeBalanceSlot(long pathHash) {
20912081
return -1;
20922082
}
20932083

2094-
public synchronized long takeAnAvailBalanceSlotFrom(Set<Long> pathHashs) {
2095-
for (Long pathHash : pathHashs) {
2096-
Slot slot = pathSlots.get(pathHash);
2097-
if (slot == null) {
2098-
continue;
2084+
public long takeAnAvailBalanceSlotFrom(List<Long> pathHashs) {
2085+
if (pathHashs.isEmpty()) {
2086+
return -1;
2087+
}
2088+
2089+
Collections.sort(pathHashs);
2090+
synchronized (this) {
2091+
int preferSlotIndex = pathHashs.indexOf(lastPickPathHash) + 1;
2092+
if (preferSlotIndex < 0 || preferSlotIndex >= pathHashs.size()) {
2093+
preferSlotIndex = 0;
20992094
}
2100-
if (slot.balanceUsed < slot.getBalanceTotal()) {
2101-
slot.balanceUsed++;
2102-
return pathHash;
2095+
2096+
for (int i = preferSlotIndex; i < pathHashs.size(); i++) {
2097+
long pathHash = pathHashs.get(i);
2098+
if (takeBalanceSlot(pathHash) != -1) {
2099+
lastPickPathHash = pathHash;
2100+
return pathHash;
2101+
}
2102+
}
2103+
for (int i = 0; i < preferSlotIndex; i++) {
2104+
long pathHash = pathHashs.get(i);
2105+
if (takeBalanceSlot(pathHash) != -1) {
2106+
lastPickPathHash = pathHash;
2107+
return pathHash;
2108+
}
21032109
}
21042110
}
21052111
return -1;
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
package org.apache.doris.clone;
19+
20+
import org.apache.doris.clone.TabletScheduler.PathSlot;
21+
import org.apache.doris.common.Config;
22+
import org.apache.doris.thrift.TStorageMedium;
23+
24+
import com.google.common.collect.Lists;
25+
import com.google.common.collect.Maps;
26+
import org.junit.Assert;
27+
import org.junit.Test;
28+
29+
import java.util.Collections;
30+
import java.util.List;
31+
import java.util.Map;
32+
33+
class PathSlotTest {
34+
35+
@Test
36+
public void test() {
37+
Config.balance_slot_num_per_path = 2;
38+
Map<Long, TStorageMedium> paths = Maps.newHashMap();
39+
List<Long> availPathHashs = Lists.newArrayList();
40+
List<Long> expectPathHashs = Lists.newArrayList();
41+
List<Long> gotPathHashs = Lists.newArrayList();
42+
long startPath = 10001L;
43+
long endPath = 10006L;
44+
for (long pathHash = startPath; pathHash < endPath; pathHash++) {
45+
paths.put(pathHash, TStorageMedium.HDD);
46+
availPathHashs.add(pathHash);
47+
expectPathHashs.add(pathHash);
48+
}
49+
for (long pathHash = startPath; pathHash < endPath; pathHash++) {
50+
expectPathHashs.add(pathHash);
51+
}
52+
for (long pathHash = startPath; pathHash < endPath; pathHash++) {
53+
expectPathHashs.add(-1L);
54+
}
55+
56+
PathSlot ps = new PathSlot(paths, 1L);
57+
for (int i = 0; i < expectPathHashs.size(); i++) {
58+
Collections.shuffle(availPathHashs);
59+
gotPathHashs.add(ps.takeAnAvailBalanceSlotFrom(availPathHashs));
60+
}
61+
Assert.assertEquals(expectPathHashs, gotPathHashs);
62+
}
63+
64+
}

0 commit comments

Comments
 (0)