danny0405 commented on code in PR #6727: URL: https://github.com/apache/hudi/pull/6727#discussion_r979174108
########## hudi-common/src/main/java/org/apache/hudi/common/table/cdc/HoodieCDCExtractor.java: ########## @@ -0,0 +1,358 @@ +/* + * 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.hudi.common.table.cdc; + +import org.apache.hadoop.fs.FileStatus; +import org.apache.hadoop.fs.FileSystem; +import org.apache.hadoop.fs.Path; + +import org.apache.hudi.common.fs.FSUtils; +import org.apache.hudi.common.model.HoodieBaseFile; +import org.apache.hudi.common.model.HoodieCommitMetadata; +import org.apache.hudi.common.model.HoodieFileFormat; +import org.apache.hudi.common.model.HoodieFileGroupId; +import org.apache.hudi.common.model.HoodieLogFile; +import org.apache.hudi.common.model.HoodieReplaceCommitMetadata; +import org.apache.hudi.common.model.HoodieWriteStat; +import org.apache.hudi.common.model.FileSlice; +import org.apache.hudi.common.model.WriteOperationType; +import org.apache.hudi.common.table.HoodieTableMetaClient; +import org.apache.hudi.common.table.timeline.HoodieActiveTimeline; +import org.apache.hudi.common.table.timeline.HoodieInstant; +import org.apache.hudi.common.table.timeline.HoodieTimeline; +import org.apache.hudi.common.table.view.HoodieTableFileSystemView; +import org.apache.hudi.common.util.Option; +import org.apache.hudi.common.util.StringUtils; +import org.apache.hudi.common.util.collection.Pair; +import org.apache.hudi.exception.HoodieException; +import org.apache.hudi.exception.HoodieIOException; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Locale; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.stream.Collectors; + +import static org.apache.hudi.common.table.cdc.HoodieCDCLogicalFileType.ADD_BASE_FILE; +import static org.apache.hudi.common.table.cdc.HoodieCDCLogicalFileType.CDC_LOG_FILE; +import static org.apache.hudi.common.table.cdc.HoodieCDCLogicalFileType.MOR_LOG_FILE; +import static org.apache.hudi.common.table.cdc.HoodieCDCLogicalFileType.REMOVE_BASE_FILE; +import static org.apache.hudi.common.table.cdc.HoodieCDCLogicalFileType.REPLACED_FILE_GROUP; +import static org.apache.hudi.common.table.timeline.HoodieTimeline.COMMIT_ACTION; +import static org.apache.hudi.common.table.timeline.HoodieTimeline.DELTA_COMMIT_ACTION; +import static org.apache.hudi.common.table.timeline.HoodieTimeline.isInRange; +import static org.apache.hudi.common.table.timeline.HoodieTimeline.REPLACE_COMMIT_ACTION; + +/** + * This class helps to extract all the information which will be used when CDC query. + * + * There are some steps: + * 1. filter out the completed commit instants, and get the related [[HoodieCommitMetadata]] objects. + * 2. initialize the [[HoodieTableFileSystemView]] by the touched data files. + * 3. extract the cdc information: + * generate a [[CDCFileSplit]] object for each of the instant in (startInstant, endInstant) + * and each of the file group which is touched in the range of instants. + */ +public class HoodieCDCExtractor { + + private final HoodieTableMetaClient metaClient; + + private final Path basePath; + + private final FileSystem fs; + + private final HoodieCDCSupplementalLoggingMode supplementalLoggingMode; + + private final String startInstant; + + private final String endInstant; + + private Map<HoodieInstant, HoodieCommitMetadata> commits; + + private HoodieTableFileSystemView fsView; + + public HoodieCDCExtractor( + HoodieTableMetaClient metaClient, + String startInstant, + String endInstant) { + this.metaClient = metaClient; + this.basePath = metaClient.getBasePathV2(); + this.fs = metaClient.getFs().getFileSystem(); + this.supplementalLoggingMode = HoodieCDCSupplementalLoggingMode.parse( + metaClient.getTableConfig().cdcSupplementalLoggingMode()); + this.startInstant = startInstant; + this.endInstant = endInstant; + init(); + } + + private void init() { + initInstantAndCommitMetadatas(); + initFSView(); + } + + /** + * At the granularity of a file group, trace the mapping between + * each commit/instant and changes to this file group. + */ + public Map<HoodieFileGroupId, List<Pair<HoodieInstant, HoodieCDCFileSplit>>> extractCDCFileSplits() { + if (commits == null || fsView == null) { + throw new HoodieException("Fail to init CDCExtractor"); + } + + Map<HoodieFileGroupId, List<Pair<HoodieInstant, HoodieCDCFileSplit>>> fgToCommitChanges = new HashMap<>(); + for (HoodieInstant instant : commits.keySet()) { + HoodieCommitMetadata commitMetadata = commits.get(instant); + + // parse `partitionToWriteStats` in the metadata of commit + Map<String, List<HoodieWriteStat>> ptToWriteStats = commitMetadata.getPartitionToWriteStats(); + for (String partition : ptToWriteStats.keySet()) { + List<HoodieWriteStat> hoodieWriteStats = ptToWriteStats.get(partition); + hoodieWriteStats.forEach(writeStat -> { + HoodieFileGroupId fileGroupId = new HoodieFileGroupId(partition, writeStat.getFileId()); + // Identify the CDC source involved in this commit and + // determine its type for subsequent loading using different methods. + HoodieCDCFileSplit changeFile = + parseWriteStat(fileGroupId, instant, writeStat, commitMetadata.getOperationType()); + if (!fgToCommitChanges.containsKey(fileGroupId)) { + fgToCommitChanges.put(fileGroupId, new ArrayList<>()); + } + fgToCommitChanges.get(fileGroupId).add(Pair.of(instant, changeFile)); + }); + } + + if (commitMetadata instanceof HoodieReplaceCommitMetadata) { + HoodieReplaceCommitMetadata replaceCommitMetadata = (HoodieReplaceCommitMetadata) commitMetadata; + Map<String, List<String>> ptToReplacedFileId = replaceCommitMetadata.getPartitionToReplaceFileIds(); + for (String partition : ptToReplacedFileId.keySet()) { + List<String> fileIds = ptToReplacedFileId.get(partition); + fileIds.forEach(fileId -> { + Option<FileSlice> latestFileSliceOpt = fsView.fetchLatestFileSlice(partition, fileId); + if (latestFileSliceOpt.isPresent()) { + HoodieFileGroupId fileGroupId = new HoodieFileGroupId(partition, fileId); + HoodieCDCFileSplit changeFile = new HoodieCDCFileSplit( + REPLACED_FILE_GROUP, null, latestFileSliceOpt, Option.empty()); + if (!fgToCommitChanges.containsKey(fileGroupId)) { + fgToCommitChanges.put(fileGroupId, new ArrayList<>()); + } + fgToCommitChanges.get(fileGroupId).add(Pair.of(instant, changeFile)); + } + }); + } + } + } + return fgToCommitChanges; + } + + /** + * Parse the commit metadata between (startInstant, endInstant], and extract the touched partitions + * and files to build the filesystem view. + */ + private void initFSView() { + Set<String> touchedPartitions = new HashSet<>(); + for (Map.Entry<HoodieInstant, HoodieCommitMetadata> entry : commits.entrySet()) { + HoodieCommitMetadata commitMetadata = entry.getValue(); + touchedPartitions.addAll(commitMetadata.getPartitionToWriteStats().keySet()); + if (commitMetadata instanceof HoodieReplaceCommitMetadata) { + touchedPartitions.addAll( + ((HoodieReplaceCommitMetadata) commitMetadata).getPartitionToReplaceFileIds().keySet() + ); + } + } + try { + List<FileStatus> touchedFiles = new ArrayList<>(); + for (String touchedPartition : touchedPartitions) { + Path partitionPath = FSUtils.getPartitionPath(basePath, touchedPartition); + touchedFiles.addAll(Arrays.asList(fs.listStatus(partitionPath))); Review Comment: Can improve to only add the files that belongs to the touched fileGroups. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
