danny0405 commented on code in PR #13449: URL: https://github.com/apache/hudi/pull/13449#discussion_r2162805927
########## hudi-client/hudi-client-common/src/main/java/org/apache/hudi/io/WriteHandleMetadataUtils.java: ########## @@ -0,0 +1,226 @@ +/* + * 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.io; + +import org.apache.hudi.client.WriteStatus; +import org.apache.hudi.common.engine.HoodieEngineContext; +import org.apache.hudi.common.engine.HoodieLocalEngineContext; +import org.apache.hudi.common.engine.HoodieReaderContext; +import org.apache.hudi.common.engine.TaskContextSupplier; +import org.apache.hudi.common.model.FileSlice; +import org.apache.hudi.common.model.HoodieAvroIndexedRecord; +import org.apache.hudi.common.model.HoodieFileGroupId; +import org.apache.hudi.common.model.HoodieIndexDefinition; +import org.apache.hudi.common.model.HoodieKey; +import org.apache.hudi.common.model.HoodieLogFile; +import org.apache.hudi.common.model.HoodieRecord; +import org.apache.hudi.common.util.Option; +import org.apache.hudi.common.util.collection.Pair; +import org.apache.hudi.config.HoodieWriteConfig; +import org.apache.hudi.exception.HoodieIOException; +import org.apache.hudi.keygen.BaseKeyGenerator; +import org.apache.hudi.metadata.SecondaryIndexRecordGenerationUtils; +import org.apache.hudi.storage.StoragePath; +import org.apache.hudi.table.HoodieTable; + +import org.apache.avro.Schema; + +import javax.annotation.Nullable; + +import java.io.IOException; +import java.util.Collections; +import java.util.List; +import java.util.Map; +import java.util.function.Supplier; + +/** + * Utility class for write handle metadata. + */ +public class WriteHandleMetadataUtils { + + /** + * The utility function used by Append Handle to generate secondary index stats for the file slice + * considering the new log files added by the handle. The function generates secondary index stats by + * reading the file slice without the new log files and comparing it by reading file slice with the + * new log files written by the handle. + * + * @param partitionPath Partition path + * @param fileId Corresponding file id + * @param fileSliceOpt File slice + * @param newLogFiles New log files + * @param status Write status corresponding to the last log file written by the handle + * @param hoodieTable Hoodie Table + * @param taskContextSupplier Task context supplier + * @param secondaryIndexDefns Definitions for secondary index which need to be updated + * @param config Write config + * @param instantTime Instant time of the commit + * @param writeSchemaWithMetaFields Write schema with metadata fields + */ + static void trackMetadataIndexStatsForStreamingMetadataWrites(String partitionPath, String fileId, Option<FileSlice> fileSliceOpt, List<String> newLogFiles, WriteStatus status, + HoodieTable hoodieTable, TaskContextSupplier taskContextSupplier, List<Pair<String, HoodieIndexDefinition>> secondaryIndexDefns, + HoodieWriteConfig config, String instantTime, Schema writeSchemaWithMetaFields) { + // TODO: @see <a href="https://issues.apache.org/jira/browse/HUDI-9533">HUDI-9533</a> Optimise the computation for multiple secondary indexes + HoodieEngineContext engineContext = new HoodieLocalEngineContext(hoodieTable.getStorageConf(), taskContextSupplier); + HoodieReaderContext readerContext = engineContext.getReaderContextFactory(hoodieTable.getMetaClient()).getContext(); + + // For Append handle, we need to merge the records written by new log files with the existing file slice to check + // the corresponding updates for secondary index. It is possible the records written in the new log files are ignored + // by record merger. + secondaryIndexDefns.forEach(secondaryIndexDefnPair -> { + // fetch primary key -> secondary index for prev file slice. + Map<String, String> recordKeyToSecondaryKeyForPreviousFileSlice = fileSliceOpt.map(fileSlice -> { + try { + return SecondaryIndexRecordGenerationUtils.getRecordKeyToSecondaryKey(hoodieTable.getMetaClient(), readerContext, fileSlice, writeSchemaWithMetaFields, + secondaryIndexDefnPair.getValue(), instantTime, config.getProps(), false); + } catch (IOException e) { + throw new HoodieIOException("Failed to generate secondary index stats ", e); + } + }).orElse(Collections.emptyMap()); + + // fetch primary key -> secondary index for latest file slice including inflight. + FileSlice latestIncludingInflight = fileSliceOpt.orElse(new FileSlice(new HoodieFileGroupId(partitionPath, fileId), instantTime)); Review Comment: Why can't we just compare the new log records agains the current file slice to figure out the changes(like what we do in the COW write path)? The current impl makes the cost event higher than `COW`, it does not make sense from high-level. -- 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]
