-
Notifications
You must be signed in to change notification settings - Fork 559
[lake/hudi] Introduce Hudi source split planner #3456
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
base: main
Are you sure you want to change the base?
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,76 @@ | ||
| /* | ||
| * 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.fluss.lake.hudi.source; | ||
|
|
||
| import org.apache.fluss.config.Configuration; | ||
| import org.apache.fluss.lake.serializer.SimpleVersionedSerializer; | ||
| import org.apache.fluss.lake.source.LakeSource; | ||
| import org.apache.fluss.lake.source.Planner; | ||
| import org.apache.fluss.lake.source.RecordReader; | ||
| import org.apache.fluss.metadata.TablePath; | ||
| import org.apache.fluss.predicate.Predicate; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.ArrayList; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
|
|
||
| /** Hudi implementation of {@link LakeSource}. */ | ||
| public class HudiLakeSource implements LakeSource<HudiSplit> { | ||
|
|
||
| private static final long serialVersionUID = 1L; | ||
|
|
||
| private final Configuration hudiConfig; | ||
| private final TablePath tablePath; | ||
|
|
||
| public HudiLakeSource(Configuration hudiConfig, TablePath tablePath) { | ||
| this.hudiConfig = hudiConfig; | ||
| this.tablePath = tablePath; | ||
| } | ||
|
|
||
| @Override | ||
| public void withProject(int[][] project) { | ||
| // Projection is applied by the Hudi record reader, which is not implemented yet. | ||
| } | ||
|
|
||
| @Override | ||
| public void withLimit(int limit) { | ||
| throw new UnsupportedOperationException("Hudi lake source does not support limit yet."); | ||
| } | ||
|
|
||
| @Override | ||
| public FilterPushDownResult withFilters(List<Predicate> predicates) { | ||
| return FilterPushDownResult.of(Collections.emptyList(), new ArrayList<>(predicates)); | ||
| } | ||
|
|
||
| @Override | ||
| public Planner<HudiSplit> createPlanner(PlannerContext context) throws IOException { | ||
| return new HudiSplitPlanner(hudiConfig, tablePath, context.snapshotId()); | ||
| } | ||
|
|
||
| @Override | ||
| public RecordReader createRecordReader(ReaderContext<HudiSplit> context) throws IOException { | ||
| throw new UnsupportedOperationException( | ||
| "Hudi lake source does not support record reading yet."); | ||
| } | ||
|
|
||
| @Override | ||
| public SimpleVersionedSerializer<HudiSplit> getSplitSerializer() { | ||
| return new HudiSplitSerializer(); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| /* | ||
| * 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.fluss.lake.hudi.source; | ||
|
|
||
| import org.apache.fluss.lake.source.LakeSplit; | ||
|
|
||
| import org.apache.hudi.common.model.FileSlice; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
| import java.util.Objects; | ||
|
|
||
| /** A readable split of a Hudi table. */ | ||
| public class HudiSplit implements LakeSplit { | ||
|
|
||
| private static final long serialVersionUID = 1L; | ||
|
|
||
| private final FileSlice fileSlice; | ||
| private final int bucket; | ||
| private final List<String> partition; | ||
|
|
||
| public HudiSplit(FileSlice fileSlice, int bucket, List<String> partition) { | ||
| this.fileSlice = Objects.requireNonNull(fileSlice, "fileSlice cannot be null"); | ||
| this.bucket = bucket; | ||
| this.partition = | ||
| Collections.unmodifiableList( | ||
| new ArrayList<>( | ||
| Objects.requireNonNull(partition, "partition cannot be null"))); | ||
| } | ||
|
|
||
| @Override | ||
| public int bucket() { | ||
| return bucket; | ||
| } | ||
|
|
||
| @Override | ||
| public List<String> partition() { | ||
| return partition; | ||
| } | ||
|
|
||
| public FileSlice getFileSlice() { | ||
| return fileSlice; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object o) { | ||
| if (this == o) { | ||
| return true; | ||
| } | ||
| if (!(o instanceof HudiSplit)) { | ||
| return false; | ||
| } | ||
| HudiSplit hudiSplit = (HudiSplit) o; | ||
| return bucket == hudiSplit.bucket | ||
| && Objects.equals(fileSlice, hudiSplit.fileSlice) | ||
| && Objects.equals(partition, hudiSplit.partition); | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return Objects.hash(fileSlice, bucket, partition); | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return "HudiSplit{" | ||
| + "fileSlice=" | ||
| + fileSlice | ||
| + ", bucket=" | ||
| + bucket | ||
| + ", partition=" | ||
| + partition | ||
| + '}'; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,157 @@ | ||
| /* | ||
| * 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.fluss.lake.hudi.source; | ||
|
|
||
| import org.apache.fluss.config.Configuration; | ||
| import org.apache.fluss.lake.hudi.utils.HudiTableInfo; | ||
| import org.apache.fluss.lake.source.Planner; | ||
| import org.apache.fluss.metadata.TablePath; | ||
|
|
||
| import org.apache.hudi.common.fs.FSUtils; | ||
| import org.apache.hudi.common.model.FileSlice; | ||
| import org.apache.hudi.common.model.HoodieBaseFile; | ||
| import org.apache.hudi.common.model.HoodieFileGroupId; | ||
| import org.apache.hudi.common.model.HoodieTableType; | ||
| import org.apache.hudi.common.table.view.HoodieTableFileSystemView; | ||
| import org.apache.hudi.index.bucket.BucketIdentifier; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.ArrayList; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| /** Planner for creating Hudi splits. */ | ||
| public class HudiSplitPlanner implements Planner<HudiSplit> { | ||
|
|
||
| private static final Logger LOG = LoggerFactory.getLogger(HudiSplitPlanner.class); | ||
|
|
||
| private final Configuration hudiConfig; | ||
| private final TablePath tablePath; | ||
| private final long snapshotId; | ||
|
|
||
| public HudiSplitPlanner(Configuration hudiConfig, TablePath tablePath, long snapshotId) { | ||
| this.hudiConfig = hudiConfig; | ||
| this.tablePath = tablePath; | ||
| this.snapshotId = snapshotId; | ||
| } | ||
|
|
||
| @Override | ||
| public List<HudiSplit> plan() throws IOException { | ||
| String snapshotTime = String.valueOf(snapshotId); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hudi instant time is a string (canonical format is a 17-digit timestamp like
|
||
| try (HudiTableInfo hudiTableInfo = HudiTableInfo.create(tablePath, hudiConfig)) { | ||
| if (!hudiTableInfo.getCompletedTimeline().containsInstant(snapshotTime)) { | ||
| throw new IOException( | ||
| String.format( | ||
| "Hudi instant time %s does not exist in table %s.", | ||
| snapshotTime, tablePath)); | ||
| } | ||
|
|
||
| List<String> partitionPaths = | ||
| FSUtils.getAllPartitionPaths( | ||
| hudiTableInfo.getEngineContext(), hudiTableInfo.getMetaClient(), false); | ||
| if (partitionPaths.isEmpty()) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggestion: Only fall back to |
||
| partitionPaths = Collections.singletonList(""); | ||
| } | ||
|
|
||
| List<HudiSplit> splits = new ArrayList<>(); | ||
| for (String partitionPath : partitionPaths) { | ||
| splits.addAll(planPartition(hudiTableInfo, snapshotTime, partitionPath)); | ||
| } | ||
| LOG.debug( | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Only logging on the success path makes "why is my query empty?" investigations harder. Please log at |
||
| "Planned {} Hudi splits for table {} at instant {}.", | ||
| splits.size(), | ||
| tablePath, | ||
| snapshotTime); | ||
| return splits; | ||
| } catch (IOException e) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The first catch is redundant, and the second one converts every |
||
| throw e; | ||
| } catch (Exception e) { | ||
| throw new IOException("Failed to plan Hudi splits for table " + tablePath + ".", e); | ||
| } | ||
| } | ||
|
|
||
| private List<HudiSplit> planPartition( | ||
| HudiTableInfo hudiTableInfo, String snapshotTime, String partitionPath) | ||
| throws IOException { | ||
| HoodieTableFileSystemView fileSystemView = hudiTableInfo.getFileSystemView(); | ||
| List<HudiSplit> splits = new ArrayList<>(); | ||
| if (hudiTableInfo.getTableType() == HoodieTableType.MERGE_ON_READ) { | ||
| List<FileSlice> fileSlices = | ||
| fileSystemView | ||
| .getLatestMergedFileSlicesBeforeOrOn(partitionPath, snapshotTime) | ||
| .collect(Collectors.toList()); | ||
| for (FileSlice fileSlice : fileSlices) { | ||
| splits.add(toHudiSplit(hudiTableInfo, partitionPath, fileSlice)); | ||
| } | ||
| return splits; | ||
| } | ||
|
|
||
| List<HoodieBaseFile> baseFiles = | ||
| fileSystemView | ||
| .getLatestBaseFilesBeforeOrOn(partitionPath, snapshotTime) | ||
| .collect(Collectors.toList()); | ||
| for (HoodieBaseFile baseFile : baseFiles) { | ||
| splits.add( | ||
| toHudiSplit( | ||
| hudiTableInfo, partitionPath, toFileSlice(partitionPath, baseFile))); | ||
| } | ||
| return splits; | ||
| } | ||
|
|
||
| private FileSlice toFileSlice(String partitionPath, HoodieBaseFile baseFile) { | ||
| return new FileSlice( | ||
| new HoodieFileGroupId(partitionPath, baseFile.getFileId()), | ||
| baseFile.getCommitTime(), | ||
| baseFile, | ||
| Collections.emptyList()); | ||
| } | ||
|
|
||
| private HudiSplit toHudiSplit( | ||
| HudiTableInfo hudiTableInfo, String partitionPath, FileSlice fileSlice) | ||
| throws IOException { | ||
| return new HudiSplit( | ||
| fileSlice, | ||
| extractBucket(hudiTableInfo, fileSlice), | ||
| hudiTableInfo.partitionValues(partitionPath)); | ||
| } | ||
|
|
||
| private int extractBucket(HudiTableInfo hudiTableInfo, FileSlice fileSlice) throws IOException { | ||
| if (!hudiTableInfo.isBucketAware()) { | ||
| return -1; | ||
| } | ||
| String fileId = fileSlice.getFileGroupId().getFileId(); | ||
| if (fileId == null || fileId.isEmpty()) { | ||
| throw new IOException( | ||
| String.format( | ||
| "Failed to extract Hudi bucket id for bucket-aware table %s because file id is empty.", | ||
| tablePath)); | ||
| } | ||
| try { | ||
| return BucketIdentifier.bucketIdFromFileId(fileId); | ||
| } catch (RuntimeException e) { | ||
| throw new IOException( | ||
| String.format( | ||
| "Failed to extract Hudi bucket id from file id '%s' for bucket-aware table %s.", | ||
| fileId, tablePath), | ||
| e); | ||
| } | ||
| } | ||
| } | ||
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.
FileSlice#equals/hashCodeonly compareHoodieFileGroupId + baseInstantTime(log files are not considered). If Hudi changes that contract in a future release, the equality semantics ofHudiSplitwill silently shift.Suggestion: either document this dependency in the class javadoc, or implement equality explicitly using
fileGroupId + baseInstantTime + baseFile.path + logFiles.paths. OK to defer to a follow-up PR if you prefer.