-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Implement Parallel Partition Pruning for Glue Hive Metastore #13729
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| /* | ||
| * 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 | ||
| * | ||
| * 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 com.facebook.presto.hive.metastore.glue; | ||
|
|
||
| import javax.inject.Qualifier; | ||
|
|
||
| import java.lang.annotation.Retention; | ||
| import java.lang.annotation.Target; | ||
|
|
||
| import static java.lang.annotation.ElementType.FIELD; | ||
| import static java.lang.annotation.ElementType.METHOD; | ||
| import static java.lang.annotation.ElementType.PARAMETER; | ||
| import static java.lang.annotation.RetentionPolicy.RUNTIME; | ||
|
|
||
| @Retention(RUNTIME) | ||
| @Target({FIELD, PARAMETER, METHOD}) | ||
| @Qualifier | ||
| public @interface ForGlueHiveMetastore {} |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -47,6 +47,7 @@ | |
| import com.amazonaws.services.glue.model.PartitionError; | ||
| import com.amazonaws.services.glue.model.PartitionInput; | ||
| import com.amazonaws.services.glue.model.PartitionValueList; | ||
| import com.amazonaws.services.glue.model.Segment; | ||
| import com.amazonaws.services.glue.model.TableInput; | ||
| import com.amazonaws.services.glue.model.UpdateDatabaseRequest; | ||
| import com.amazonaws.services.glue.model.UpdatePartitionRequest; | ||
|
|
@@ -86,15 +87,20 @@ | |
| import com.google.common.collect.Lists; | ||
| import org.apache.hadoop.fs.Path; | ||
|
|
||
| import javax.annotation.Nullable; | ||
| import javax.inject.Inject; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Comparator; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Map.Entry; | ||
| import java.util.Optional; | ||
| import java.util.Set; | ||
| import java.util.concurrent.CompletionService; | ||
| import java.util.concurrent.ExecutionException; | ||
| import java.util.concurrent.Executor; | ||
| import java.util.concurrent.ExecutorCompletionService; | ||
| import java.util.concurrent.Future; | ||
| import java.util.function.Function; | ||
|
|
||
|
|
@@ -115,6 +121,8 @@ | |
| import static com.facebook.presto.spi.StandardErrorCode.NOT_SUPPORTED; | ||
| import static com.facebook.presto.spi.security.PrincipalType.USER; | ||
| import static com.google.common.base.Strings.isNullOrEmpty; | ||
| import static com.google.common.collect.Comparators.lexicographical; | ||
| import static java.util.Comparator.comparing; | ||
| import static java.util.Objects.requireNonNull; | ||
| import static java.util.function.UnaryOperator.identity; | ||
| import static java.util.stream.Collectors.toList; | ||
|
|
@@ -130,26 +138,38 @@ public class GlueHiveMetastore | |
| private static final String WILDCARD_EXPRESSION = ""; | ||
| private static final int BATCH_GET_PARTITION_MAX_PAGE_SIZE = 1000; | ||
| private static final int BATCH_CREATE_PARTITION_MAX_PAGE_SIZE = 100; | ||
| private static final Comparator<Partition> PARTITION_COMPARATOR = comparing(Partition::getValues, lexicographical(String.CASE_INSENSITIVE_ORDER)); | ||
|
|
||
| private final HdfsEnvironment hdfsEnvironment; | ||
| private final HdfsContext hdfsContext; | ||
| private final AWSGlueAsync glueClient; | ||
| private final Optional<String> defaultDir; | ||
| private final String catalogId; | ||
| private final int partitionSegments; | ||
| private final Executor executor; | ||
|
||
|
|
||
| @Inject | ||
| public GlueHiveMetastore(HdfsEnvironment hdfsEnvironment, GlueHiveMetastoreConfig glueConfig) | ||
| public GlueHiveMetastore( | ||
| HdfsEnvironment hdfsEnvironment, | ||
| GlueHiveMetastoreConfig glueConfig, | ||
| @ForGlueHiveMetastore Executor executor) | ||
| { | ||
| this(hdfsEnvironment, glueConfig, createAsyncGlueClient(glueConfig)); | ||
| this(hdfsEnvironment, glueConfig, createAsyncGlueClient(glueConfig), executor); | ||
| } | ||
|
|
||
| public GlueHiveMetastore(HdfsEnvironment hdfsEnvironment, GlueHiveMetastoreConfig glueConfig, AWSGlueAsync glueClient) | ||
| public GlueHiveMetastore( | ||
| HdfsEnvironment hdfsEnvironment, | ||
| GlueHiveMetastoreConfig glueConfig, | ||
| AWSGlueAsync glueClient, | ||
| @ForGlueHiveMetastore Executor executor) | ||
| { | ||
| this.hdfsEnvironment = requireNonNull(hdfsEnvironment, "hdfsEnvironment is null"); | ||
| this.hdfsContext = new HdfsContext(new ConnectorIdentity(DEFAULT_METASTORE_USER, Optional.empty(), Optional.empty())); | ||
| this.glueClient = requireNonNull(glueClient, "glueClient is null"); | ||
| this.defaultDir = glueConfig.getDefaultWarehouseDir(); | ||
| this.catalogId = glueConfig.getCatalogId().orElse(null); | ||
| this.partitionSegments = glueConfig.getPartitionSegments(); | ||
| this.executor = requireNonNull(executor, "executor is null"); | ||
| } | ||
|
|
||
| private static AWSGlueAsync createAsyncGlueClient(GlueHiveMetastoreConfig config) | ||
|
|
@@ -649,6 +669,37 @@ public Optional<List<String>> getPartitionNamesByParts(String databaseName, Stri | |
| } | ||
|
|
||
| private List<Partition> getPartitions(String databaseName, String tableName, String expression) | ||
| { | ||
| if (partitionSegments == 1) { | ||
| return getPartitions(databaseName, tableName, expression, null); | ||
| } | ||
|
|
||
| // Do parallel partition fetch. | ||
| CompletionService<List<Partition>> completionService = new ExecutorCompletionService<>(executor); | ||
| for (int i = 0; i < partitionSegments; i++) { | ||
| Segment segment = new Segment().withSegmentNumber(i).withTotalSegments(partitionSegments); | ||
| completionService.submit(() -> getPartitions(databaseName, tableName, expression, segment)); | ||
| } | ||
|
|
||
| List<Partition> partitions = new ArrayList<>(); | ||
| try { | ||
| for (int i = 0; i < partitionSegments; i++) { | ||
| Future<List<Partition>> futurePartitions = completionService.take(); | ||
| partitions.addAll(futurePartitions.get()); | ||
| } | ||
| } | ||
| catch (ExecutionException | InterruptedException e) { | ||
| if (e instanceof InterruptedException) { | ||
| Thread.currentThread().interrupt(); | ||
| } | ||
| throw new PrestoException(HIVE_METASTORE_ERROR, "Failed to fetch partitions from Glue Data Catalog", e); | ||
| } | ||
|
|
||
| partitions.sort(PARTITION_COMPARATOR); | ||
| return partitions; | ||
| } | ||
|
|
||
| private List<Partition> getPartitions(String databaseName, String tableName, String expression, @Nullable Segment segment) | ||
| { | ||
| try { | ||
| List<Partition> partitions = new ArrayList<>(); | ||
|
|
@@ -660,6 +711,7 @@ private List<Partition> getPartitions(String databaseName, String tableName, Str | |
| .withDatabaseName(databaseName) | ||
| .withTableName(tableName) | ||
| .withExpression(expression) | ||
| .withSegment(segment) | ||
| .withNextToken(nextToken)); | ||
| result.getPartitions() | ||
| .forEach(partition -> partitions.add(GlueToPrestoConverter.convertPartition(partition))); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,6 +16,7 @@ | |
| import com.facebook.airlift.configuration.Config; | ||
| import com.facebook.airlift.configuration.ConfigDescription; | ||
|
|
||
| import javax.validation.constraints.Max; | ||
| import javax.validation.constraints.Min; | ||
|
|
||
| import java.util.Optional; | ||
|
|
@@ -27,6 +28,8 @@ public class GlueHiveMetastoreConfig | |
| private int maxGlueConnections = 5; | ||
| private Optional<String> defaultWarehouseDir = Optional.empty(); | ||
| private Optional<String> catalogId = Optional.empty(); | ||
| private int partitionSegments = 5; | ||
| private int getPartitionThreads = 20; | ||
|
||
|
|
||
| public Optional<String> getGlueRegion() | ||
| { | ||
|
|
@@ -93,4 +96,33 @@ public GlueHiveMetastoreConfig setCatalogId(String catalogId) | |
| this.catalogId = Optional.ofNullable(catalogId); | ||
| return this; | ||
| } | ||
|
|
||
| @Min(1) | ||
| @Max(10) | ||
| public int getPartitionSegments() | ||
| { | ||
| return partitionSegments; | ||
| } | ||
|
|
||
| @Config("hive.metastore.glue.partitions-segments") | ||
| @ConfigDescription("Number of segments for partitioned Glue tables") | ||
| public GlueHiveMetastoreConfig setPartitionSegments(int partitionSegments) | ||
| { | ||
| this.partitionSegments = partitionSegments; | ||
| return this; | ||
| } | ||
|
|
||
| @Min(1) | ||
| public int getGetPartitionThreads() | ||
|
||
| { | ||
| return getPartitionThreads; | ||
| } | ||
|
|
||
| @Config("hive.metastore.glue.get-partition-threads") | ||
| @ConfigDescription("Number of threads for parallel partition fetches from Glue") | ||
| public GlueHiveMetastoreConfig setGetPartitionThreads(int getPartitionThreads) | ||
| { | ||
| this.getPartitionThreads = getPartitionThreads; | ||
| return this; | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: What about
partitionSegmentCount?