Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Spark 3.5: Support executor cache locality
- Loading branch information
1 parent
b1db17d
commit f19a39b
Showing
9 changed files
with
389 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
spark/v3.5/spark/src/main/java/org/apache/iceberg/spark/source/SparkPlanningUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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; | ||
} | ||
} |
Oops, something went wrong.