/
SparkPlanningUtil.java
94 lines (77 loc) · 3.18 KB
/
SparkPlanningUtil.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
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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
*
* http://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 org.apache.iceberg.spark.source;
import java.util.List;
import java.util.Objects;
import org.apache.iceberg.FileScanTask;
import org.apache.iceberg.PartitionScanTask;
import org.apache.iceberg.ScanTask;
import org.apache.iceberg.ScanTaskGroup;
import org.apache.iceberg.StructLike;
import org.apache.iceberg.hadoop.Util;
import org.apache.iceberg.io.FileIO;
import org.apache.iceberg.relocated.com.google.common.collect.Lists;
import org.apache.iceberg.util.Tasks;
import org.apache.iceberg.util.ThreadPools;
class SparkPlanningUtil {
public static final String[] NO_LOCATION_PREFERENCE = new String[0];
private SparkPlanningUtil() {}
public static String[][] fetchBlockLocations(
FileIO io, List<? extends ScanTaskGroup<?>> taskGroups) {
String[][] locations = new String[taskGroups.size()][];
Tasks.range(taskGroups.size())
.stopOnFailure()
.executeWith(ThreadPools.getWorkerPool())
.run(index -> locations[index] = Util.blockLocations(io, taskGroups.get(index)));
return locations;
}
public static String[][] assignExecutors(
List<? extends ScanTaskGroup<?>> taskGroups, List<String> executorLocations) {
String[][] locations = new String[taskGroups.size()][];
for (int index = 0; index < taskGroups.size(); index++) {
locations[index] = assign(taskGroups.get(index), executorLocations);
}
return locations;
}
private static String[] assign(ScanTaskGroup<?> taskGroup, List<String> executorLocations) {
List<String> locations = Lists.newArrayList();
for (ScanTask task : taskGroup.tasks()) {
if (task.isFileScanTask()) {
FileScanTask fileTask = task.asFileScanTask();
if (isPartitioned(fileTask) && !fileTask.deletes().isEmpty()) {
int index = Math.floorMod(hash(fileTask.partition()), executorLocations.size());
String executorLocation = executorLocations.get(index);
locations.add(executorLocation);
}
}
}
return locations.toArray(NO_LOCATION_PREFERENCE);
}
private static boolean isPartitioned(PartitionScanTask task) {
return task.partition() != null && task.partition().size() > 0;
}
private static int hash(StructLike struct) {
int hash = 0;
for (int index = 0; index < struct.size(); index++) {
Object value = struct.get(index, Object.class);
hash += 31 * hash + Objects.hashCode(value);
}
return hash;
}
}