-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
HDFSBackendSelector.java
323 lines (297 loc) · 14.8 KB
/
HDFSBackendSelector.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
// Copyright 2021-present StarRocks, Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.starrocks.qe;
import autovalue.shaded.com.google.common.common.annotations.VisibleForTesting;
import com.google.common.base.Preconditions;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Multimap;
import com.google.common.hash.Funnel;
import com.google.common.hash.Hashing;
import com.google.common.hash.PrimitiveSink;
import com.starrocks.catalog.PartitionKey;
import com.starrocks.common.UserException;
import com.starrocks.common.profile.Tracers;
import com.starrocks.common.util.ConsistentHashRing;
import com.starrocks.common.util.HashRing;
import com.starrocks.common.util.RendezvousHashRing;
import com.starrocks.planner.DeltaLakeScanNode;
import com.starrocks.planner.FileTableScanNode;
import com.starrocks.planner.HdfsScanNode;
import com.starrocks.planner.HudiScanNode;
import com.starrocks.planner.IcebergScanNode;
import com.starrocks.planner.OdpsScanNode;
import com.starrocks.planner.PaimonScanNode;
import com.starrocks.planner.ScanNode;
import com.starrocks.qe.scheduler.NonRecoverableException;
import com.starrocks.qe.scheduler.WorkerProvider;
import com.starrocks.sql.plan.HDFSScanNodePredicates;
import com.starrocks.system.ComputeNode;
import com.starrocks.thrift.THdfsScanRange;
import com.starrocks.thrift.TScanRangeLocation;
import com.starrocks.thrift.TScanRangeLocations;
import com.starrocks.thrift.TScanRangeParams;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* Hybrid backend selector for hive table.
* Support hybrid and independent deployment with datanode.
* <p>
* Assign scan ranges to backend:
* 1. local backend first,
* 2. and smallest assigned scan ranges num or scan bytes.
* <p>
* If force_schedule_local variable is set, HybridBackendSelector will force to
* assign scan ranges to local backend if there has one.
*/
public class HDFSBackendSelector implements BackendSelector {
public static final Logger LOG = LogManager.getLogger(HDFSBackendSelector.class);
// be -> assigned scans
Map<ComputeNode, Long> assignedScansPerComputeNode = Maps.newHashMap();
// be -> re-balance bytes
Map<ComputeNode, Long> reBalanceBytesPerComputeNode = Maps.newHashMap();
// be host -> bes
Multimap<String, ComputeNode> hostToBackends = HashMultimap.create();
private final ScanNode scanNode;
private final List<TScanRangeLocations> locations;
private final FragmentScanRangeAssignment assignment;
private final WorkerProvider workerProvider;
private final boolean forceScheduleLocal;
private final boolean shuffleScanRange;
private final int kCandidateNumber = 3;
// After testing, this value can ensure that the scan range size assigned to each BE is as uniform as possible,
// and the largest scan data is not more than 1.1 times of the average value
private final double kMaxImbalanceRatio = 1.1;
public static final int CONSISTENT_HASH_RING_VIRTUAL_NUMBER = 128;
class HdfsScanRangeHasher {
String basePath;
HDFSScanNodePredicates predicates;
public HdfsScanRangeHasher() {
if (scanNode instanceof HdfsScanNode) {
HdfsScanNode node = (HdfsScanNode) scanNode;
predicates = node.getScanNodePredicates();
basePath = node.getHiveTable().getTableLocation();
} else if (scanNode instanceof IcebergScanNode) {
IcebergScanNode node = (IcebergScanNode) scanNode;
predicates = node.getScanNodePredicates();
} else if (scanNode instanceof HudiScanNode) {
HudiScanNode node = (HudiScanNode) scanNode;
predicates = node.getScanNodePredicates();
basePath = node.getHudiTable().getTableLocation();
} else if (scanNode instanceof DeltaLakeScanNode) {
DeltaLakeScanNode node = (DeltaLakeScanNode) scanNode;
predicates = node.getScanNodePredicates();
basePath = node.getDeltaLakeTable().getTableLocation();
} else if (scanNode instanceof FileTableScanNode) {
FileTableScanNode node = (FileTableScanNode) scanNode;
predicates = node.getScanNodePredicates();
basePath = node.getFileTable().getTableLocation();
} else if (scanNode instanceof PaimonScanNode) {
PaimonScanNode node = (PaimonScanNode) scanNode;
predicates = node.getScanNodePredicates();
basePath = node.getPaimonTable().getTableLocation();
} else if (scanNode instanceof OdpsScanNode) {
OdpsScanNode node = (OdpsScanNode) scanNode;
predicates = node.getScanNodePredicates();
} else {
Preconditions.checkState(false);
}
}
public void acceptScanRangeLocations(TScanRangeLocations tScanRangeLocations, PrimitiveSink primitiveSink) {
THdfsScanRange hdfsScanRange = tScanRangeLocations.scan_range.hdfs_scan_range;
if (hdfsScanRange.isSetFull_path()) {
primitiveSink.putString(hdfsScanRange.full_path, StandardCharsets.UTF_8);
} else {
if (hdfsScanRange.isSetPartition_id() &&
predicates.getIdToPartitionKey().containsKey(hdfsScanRange.getPartition_id())) {
PartitionKey partitionKey = predicates.getIdToPartitionKey().get(hdfsScanRange.getPartition_id());
primitiveSink.putInt(partitionKey.hashCode());
}
if (hdfsScanRange.isSetRelative_path()) {
primitiveSink.putString(hdfsScanRange.relative_path, StandardCharsets.UTF_8);
}
}
if (hdfsScanRange.isSetOffset()) {
primitiveSink.putLong(hdfsScanRange.getOffset());
}
}
}
private final HdfsScanRangeHasher hdfsScanRangeHasher;
public HDFSBackendSelector(ScanNode scanNode, List<TScanRangeLocations> locations,
FragmentScanRangeAssignment assignment, WorkerProvider workerProvider,
boolean forceScheduleLocal,
boolean shuffleScanRange) {
this.scanNode = scanNode;
this.locations = locations;
this.assignment = assignment;
this.workerProvider = workerProvider;
this.forceScheduleLocal = forceScheduleLocal;
this.hdfsScanRangeHasher = new HdfsScanRangeHasher();
this.shuffleScanRange = shuffleScanRange;
}
// re-balance scan ranges for compute node if needed, return the compute node which scan range is assigned to
private ComputeNode reBalanceScanRangeForComputeNode(List<ComputeNode> backends, long avgNodeScanRangeBytes,
TScanRangeLocations scanRangeLocations) {
if (backends == null || backends.isEmpty()) {
return null;
}
ComputeNode node = null;
long addedScans = scanRangeLocations.scan_range.hdfs_scan_range.length;
for (ComputeNode backend : backends) {
long assignedScanRanges = assignedScansPerComputeNode.get(backend);
if (assignedScanRanges + addedScans < avgNodeScanRangeBytes * kMaxImbalanceRatio) {
node = backend;
break;
}
}
if (node == null) {
node = backends.get(0);
}
return node;
}
class ComputeNodeFunnel implements Funnel<ComputeNode> {
@Override
public void funnel(ComputeNode computeNode, PrimitiveSink primitiveSink) {
primitiveSink.putString(computeNode.getHost(), StandardCharsets.UTF_8);
primitiveSink.putInt(computeNode.getBePort());
}
}
class TScanRangeLocationsFunnel implements Funnel<TScanRangeLocations> {
@Override
public void funnel(TScanRangeLocations tScanRangeLocations, PrimitiveSink primitiveSink) {
hdfsScanRangeHasher.acceptScanRangeLocations(tScanRangeLocations, primitiveSink);
}
}
@VisibleForTesting
public HashRing makeHashRing() {
Set<ComputeNode> nodes = assignedScansPerComputeNode.keySet();
HashRing hashRing = null;
String hashAlgorithm = ConnectContext.get() != null ? ConnectContext.get().getSessionVariable().
getHdfsBackendSelectorHashAlgorithm() : "consistent";
int virtualNodeNum = ConnectContext.get() != null ? ConnectContext.get().getSessionVariable().
getConsistentHashVirtualNodeNum() : CONSISTENT_HASH_RING_VIRTUAL_NUMBER;
if (hashAlgorithm.equalsIgnoreCase("rendezvous")) {
hashRing = new RendezvousHashRing(Hashing.murmur3_128(), new TScanRangeLocationsFunnel(),
new ComputeNodeFunnel(), nodes);
} else {
hashRing = new ConsistentHashRing(Hashing.murmur3_128(), new TScanRangeLocationsFunnel(),
new ComputeNodeFunnel(), nodes, virtualNodeNum);
}
return hashRing;
}
private long computeTotalSize() {
long size = 0;
for (TScanRangeLocations scanRangeLocations : locations) {
size += scanRangeLocations.scan_range.hdfs_scan_range.getLength();
}
return size;
}
@Override
public void computeScanRangeAssignment() throws UserException {
if (locations.size() == 0) {
return;
}
long totalSize = computeTotalSize();
long avgNodeScanRangeBytes = totalSize / Math.max(workerProvider.getAllWorkers().size(), 1) + 1;
for (ComputeNode computeNode : workerProvider.getAllWorkers()) {
assignedScansPerComputeNode.put(computeNode, 0L);
reBalanceBytesPerComputeNode.put(computeNode, 0L);
hostToBackends.put(computeNode.getHost(), computeNode);
}
// schedule scan ranges to co-located backends.
// and put rest scan ranges into remote scan ranges.
List<TScanRangeLocations> remoteScanRangeLocations = Lists.newArrayList();
if (forceScheduleLocal) {
for (int i = 0; i < locations.size(); ++i) {
TScanRangeLocations scanRangeLocations = locations.get(i);
List<ComputeNode> backends = new ArrayList<>();
// select all backends that are co-located with this scan range.
for (final TScanRangeLocation location : scanRangeLocations.getLocations()) {
Collection<ComputeNode> servers = hostToBackends.get(location.getServer().getHostname());
if (servers == null || servers.isEmpty()) {
continue;
}
backends.addAll(servers);
}
ComputeNode node =
reBalanceScanRangeForComputeNode(backends, avgNodeScanRangeBytes, scanRangeLocations);
if (node == null) {
remoteScanRangeLocations.add(scanRangeLocations);
} else {
recordScanRangeAssignment(node, backends, scanRangeLocations);
}
}
} else {
remoteScanRangeLocations = locations;
}
if (remoteScanRangeLocations.isEmpty()) {
return;
}
// use consistent hashing to schedule remote scan ranges
HashRing hashRing = makeHashRing();
if (shuffleScanRange) {
Collections.shuffle(remoteScanRangeLocations);
}
// assign scan ranges.
for (int i = 0; i < remoteScanRangeLocations.size(); ++i) {
TScanRangeLocations scanRangeLocations = remoteScanRangeLocations.get(i);
List<ComputeNode> backends = hashRing.get(scanRangeLocations, kCandidateNumber);
ComputeNode node = reBalanceScanRangeForComputeNode(backends, avgNodeScanRangeBytes, scanRangeLocations);
if (node == null) {
throw new RuntimeException("Failed to find backend to execute");
}
recordScanRangeAssignment(node, backends, scanRangeLocations);
}
recordScanRangeStatistic();
}
private void recordScanRangeAssignment(ComputeNode worker, List<ComputeNode> backends,
TScanRangeLocations scanRangeLocations)
throws NonRecoverableException {
workerProvider.selectWorker(worker.getId());
// update statistic
long addedScans = scanRangeLocations.scan_range.hdfs_scan_range.length;
assignedScansPerComputeNode.put(worker, assignedScansPerComputeNode.get(worker) + addedScans);
// the fist item in backends will be assigned if there is no re-balance, we compute re-balance bytes
// if the worker is not the first item in backends.
if (worker != backends.get(0)) {
reBalanceBytesPerComputeNode.put(worker, reBalanceBytesPerComputeNode.get(worker) + addedScans);
}
// add scan range params
TScanRangeParams scanRangeParams = new TScanRangeParams();
scanRangeParams.scan_range = scanRangeLocations.scan_range;
assignment.put(worker.getId(), scanNode.getId().asInt(), scanRangeParams);
}
private void recordScanRangeStatistic() {
// record scan range size for each backend
StringBuilder sb = new StringBuilder();
for (Map.Entry<ComputeNode, Long> entry : assignedScansPerComputeNode.entrySet()) {
sb.append(entry.getKey().getAddress().hostname).append(":").append(entry.getValue()).append(",");
}
Tracers.record(Tracers.Module.EXTERNAL, scanNode.getTableName() + " scan_range_bytes", sb.toString());
// record re-balance bytes for each backend
sb = new StringBuilder();
for (Map.Entry<ComputeNode, Long> entry : reBalanceBytesPerComputeNode.entrySet()) {
sb.append(entry.getKey().getAddress().hostname).append(":").append(entry.getValue()).append(",");
}
Tracers.record(Tracers.Module.EXTERNAL, scanNode.getTableName() + " rebalance_bytes", sb.toString());
}
}