cshuo commented on code in PR #19390: URL: https://github.com/apache/hudi/pull/19390#discussion_r3679631702
########## hudi-flink-datasource/hudi-flink/src/main/java/org/apache/hudi/sink/bulk/sort/LsmBulkInsertSortUtils.java: ########## @@ -0,0 +1,80 @@ +/* + * 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.sink.bulk.sort; + +import org.apache.flink.table.api.DataTypes; +import org.apache.flink.table.types.logical.LogicalType; +import org.apache.flink.table.types.logical.RowType; + +/** + * Utilities for defining and sorting the internal rows used by Flink LSM bulk insert. + * + * <p>Each internal row contains a shuffle key, the actual encoded record key, and the original + * table row. The shuffle key is the partition path for the regular bulk-insert path and the file ID + * for the bucket-index path. The external sorter orders rows by the shuffle key first and the + * record key second, so records for each writer route form a record-key-ordered run. + * + * <p>The original table row remains nested in the internal row and is not part of the sort key. + * After sorting, the LSM writer helpers consume the internal row directly and reuse the retained + * record key. + */ +public final class LsmBulkInsertSortUtils { Review Comment: Move these utils to LsmBucketBulkInsertWriterHelper ########## hudi-flink-datasource/hudi-flink/src/main/java/org/apache/hudi/sink/utils/Pipelines.java: ########## @@ -125,82 +128,204 @@ public class Pipelines { * @return the bulk insert data stream sink */ public static DataStream<RowData> bulkInsert(Configuration conf, RowType rowType, DataStream<RowData> dataStream) { + if (OptionsResolver.isRecordLevelIndex(conf)) { + throw new HoodieException( + "Record level index does not work with bulk insert using FLINK engine."); + } + // TODO support bulk insert for consistent bucket index + if (OptionsResolver.isConsistentHashingBucketIndexType(conf)) { + throw new HoodieException( + "Consistent hashing bucket index does not work with bulk insert using FLINK engine. Use simple bucket index or Spark engine."); + } + // we need same parallelism for all operators, // which is equal to write tasks number, to avoid shuffles - final int PARALLELISM_VALUE = conf.get(FlinkOptions.WRITE_TASKS); + final int writeTasks = conf.get(FlinkOptions.WRITE_TASKS); final boolean isBucketIndexType = OptionsResolver.isBucketIndexType(conf); + final boolean isLsmTreeStorageLayout = OptionsResolver.isLsmTreeStorageLayout(conf); + + DataStream<RowData> preparedDataStream = isBucketIndexType + ? prepareBucketBulkInsert( + conf, rowType, dataStream, writeTasks, isLsmTreeStorageLayout) + : prepareNonBucketBulkInsert( + conf, rowType, dataStream, writeTasks, isLsmTreeStorageLayout); + + String operatorName = + isBucketIndexType ? "bucket_bulk_insert" : "hoodie_bulk_insert_write"; + return preparedDataStream + .transform(opName(operatorName, conf), + TypeInformation.of(RowData.class), BulkInsertWriteOperator.getFactory(conf, rowType)) + .uid(opUID(operatorName, conf)) + .setParallelism(writeTasks); + } - if (OptionsResolver.isRecordLevelIndex(conf)) { - throw new HoodieException( - "Record level index does not work with bulk insert using FLINK engine."); + /** + * Prepares the input stream for a bucket bulk insert writer. + * + * <p>Records are first routed to the write task that owns the target bucket. For the default + * layout, the file ID is appended and the stream is optionally sorted by file ID. For the LSM + * layout, the file ID and record key are appended in the same transform, then the stream is + * sorted by file ID and record key. + */ + private static DataStream<RowData> prepareBucketBulkInsert( + Configuration conf, + RowType rowType, + DataStream<RowData> dataStream, + int writeTasks, + boolean isLsmTreeStorageLayout) { + List<String> indexKeyFieldList = OptionsResolver.getIndexKeyFields(conf); + // Built once and captured by the per-record map closure (NumBucketsFunction is Serializable), + // avoiding a per-record rebuild from conf inside BucketBulkInsertWriterHelper. + NumBucketsFunction numBucketsFunction = new NumBucketsFunction( + conf.get(FlinkOptions.BUCKET_INDEX_PARTITION_EXPRESSIONS), + conf.get(FlinkOptions.BUCKET_INDEX_PARTITION_RULE), + conf.get(FlinkOptions.BUCKET_INDEX_NUM_BUCKETS)); + Partitioner<HoodieKey> partitioner = + BucketIndexPartitionerFactory.create(conf, indexKeyFieldList); + RowDataKeyGen keyGen = RowDataKeyGens.instance(conf, rowType); + boolean needFixedFileIdSuffix = + OptionsResolver.isNonBlockingConcurrencyControl(conf); + + Map<String, String> bucketIdToFileId = new HashMap<>(); + DataStream<RowData> routedDataStream = + dataStream.partitionCustom(partitioner, keyGen::getHoodieKey); + + if (isLsmTreeStorageLayout) { + RowType sortRowType = LsmBulkInsertSortUtils.sortRowType(rowType); + InternalTypeInfo<RowData> sortTypeInfo = InternalTypeInfo.of(sortRowType); + DataStream<RowData> sortInput = routedDataStream + .map(record -> LsmBucketBulkInsertWriterHelper.rowWithFileIdAndRecordKey( + bucketIdToFileId, + keyGen, + record, + indexKeyFieldList, + numBucketsFunction, + needFixedFileIdSuffix), sortTypeInfo) + .name("lsm_bulk_insert_sort_keys") + .setParallelism(writeTasks); + return addBulkInsertSorter( + conf, + sortInput, + sortTypeInfo, + LsmBulkInsertSortUtils.getLsmSorterGen(sortRowType), + "lsm_sorter:(file_group, record_key)", + writeTasks); } - if (isBucketIndexType) { - // TODO support bulk insert for consistent bucket index - if (OptionsResolver.isConsistentHashingBucketIndexType(conf)) { - throw new HoodieException( - "Consistent hashing bucket index does not work with bulk insert using FLINK engine. Use simple bucket index or Spark engine."); - } - List<String> indexKeyFieldList = OptionsResolver.getIndexKeyFields(conf); - // built once and captured by the per-record map closure (NumBucketsFunction is Serializable), - // avoiding a per-record rebuild from conf inside BucketBulkInsertWriterHelper - NumBucketsFunction numBucketsFunction = new NumBucketsFunction(conf.get(FlinkOptions.BUCKET_INDEX_PARTITION_EXPRESSIONS), - conf.get(FlinkOptions.BUCKET_INDEX_PARTITION_RULE), conf.get(FlinkOptions.BUCKET_INDEX_NUM_BUCKETS)); - Partitioner<HoodieKey> partitioner = BucketIndexPartitionerFactory.create(conf, indexKeyFieldList); - RowDataKeyGen keyGen = RowDataKeyGens.instance(conf, rowType); - RowType rowTypeWithFileId = BucketBulkInsertWriterHelper.rowTypeWithFileId(rowType); - InternalTypeInfo<RowData> typeInfo = InternalTypeInfo.of(rowTypeWithFileId); - boolean needFixedFileIdSuffix = OptionsResolver.isNonBlockingConcurrencyControl(conf); - - Map<String, String> bucketIdToFileId = new HashMap<>(); - dataStream = dataStream.partitionCustom(partitioner, keyGen::getHoodieKey) - .map(record -> BucketBulkInsertWriterHelper.rowWithFileId(bucketIdToFileId, keyGen, record, indexKeyFieldList, numBucketsFunction, needFixedFileIdSuffix), typeInfo) - .setParallelism(PARALLELISM_VALUE); - if (conf.get(FlinkOptions.WRITE_BULK_INSERT_SORT_INPUT)) { - SortOperatorGen sortOperatorGen = BucketBulkInsertWriterHelper.getFileIdSorterGen(rowTypeWithFileId); - dataStream = dataStream.transform("file_sorter", typeInfo, sortOperatorGen.createSortOperator(conf)) - .setParallelism(PARALLELISM_VALUE); - FlinkTransformationUtils.setManagedMemoryWeight(dataStream.getTransformation(), - conf.get(FlinkOptions.WRITE_SORT_MEMORY) * 1024L * 1024L); - } - } else if (!FlinkOptions.isDefaultValueDefined(conf, FlinkOptions.PARTITION_PATH_FIELD)) { - // if table is not partitioned then we don't need any shuffles, - // and could add main write operator only - if (conf.get(FlinkOptions.WRITE_BULK_INSERT_SHUFFLE_INPUT)) { - // shuffle by partition keys - // use #partitionCustom instead of #keyBy to avoid duplicate sort operations, - // see BatchExecutionUtils#applyBatchExecutionSettings for details. - Partitioner<String> partitioner = (key, channels) -> KeyGroupRangeAssignment.assignKeyToParallelOperator(key, - KeyGroupRangeAssignment.computeDefaultMaxParallelism(PARALLELISM_VALUE), channels); - RowDataKeyGen rowDataKeyGen = RowDataKeyGens.instance(conf, rowType); - dataStream = dataStream.partitionCustom(partitioner, rowDataKeyGen::getPartitionPath); - } - if (conf.get(FlinkOptions.WRITE_BULK_INSERT_SORT_INPUT)) { - final boolean isNeededSortInput = conf.get(FlinkOptions.WRITE_BULK_INSERT_SORT_INPUT_BY_RECORD_KEY); - final String[] partitionFields = FilePathUtils.extractPartitionKeys(conf); - final String[] recordKeyFields = OptionsResolver.getRecordKeys(conf); - - // if sort input by record key is needed then add record keys to partition keys - String[] sortFields = isNeededSortInput - ? Stream.concat(Arrays.stream(partitionFields), Arrays.stream(recordKeyFields)).toArray(String[]::new) - : partitionFields; - SortOperatorGen sortOperatorGen = new SortOperatorGen(rowType, sortFields); - dataStream = dataStream - .transform(isNeededSortInput ? "sorter:(partition_key, record_key)" : "sorter:(partition_key)", - InternalTypeInfo.of(rowType), sortOperatorGen.createSortOperator(conf)) - .setParallelism(PARALLELISM_VALUE); - FlinkTransformationUtils.setManagedMemoryWeight(dataStream.getTransformation(), - conf.get(FlinkOptions.WRITE_SORT_MEMORY) * 1024L * 1024L); - } + RowType rowTypeWithFileId = BucketBulkInsertWriterHelper.rowTypeWithFileId(rowType); + InternalTypeInfo<RowData> typeInfo = InternalTypeInfo.of(rowTypeWithFileId); + DataStream<RowData> rowsWithFileId = routedDataStream + .map(record -> BucketBulkInsertWriterHelper.rowWithFileId( + bucketIdToFileId, + keyGen, + record, + indexKeyFieldList, + numBucketsFunction, + needFixedFileIdSuffix), typeInfo) + .setParallelism(writeTasks); + + if (!conf.get(FlinkOptions.WRITE_BULK_INSERT_SORT_INPUT)) { + return rowsWithFileId; } - // main write operator with following dummy sink in the end - String opName = isBucketIndexType ? "bucket_bulk_insert" : "hoodie_bulk_insert_write"; - return dataStream - .transform(opName(opName, conf), - TypeInformation.of(RowData.class), BulkInsertWriteOperator.getFactory(conf, rowType)) - .uid(opUID(opName, conf)) - .setParallelism(PARALLELISM_VALUE); + return addBulkInsertSorter( + conf, + rowsWithFileId, + typeInfo, + BucketBulkInsertWriterHelper.getFileIdSorterGen(rowTypeWithFileId), + "file_sorter", + writeTasks); + } + + /** + * Prepares the input stream for a non-bucket bulk insert writer. + * + * <p>Partitioned input is optionally shuffled by partition path. The LSM layout then appends + * the partition path and record key and sorts by both fields; for a non-partitioned table the + * partition path is empty, so the effective ordering is by record key. The default layout keeps + * the existing behavior: non-partitioned input is passed through without shuffle or sort, while + * partitioned input is sorted only when bulk-insert input sorting is enabled. + */ + private static DataStream<RowData> prepareNonBucketBulkInsert( + Configuration conf, + RowType rowType, + DataStream<RowData> dataStream, + int writeTasks, + boolean isLsmTreeStorageLayout) { + final boolean isPartitioned = + !FlinkOptions.isDefaultValueDefined(conf, FlinkOptions.PARTITION_PATH_FIELD); + final boolean shouldShuffle = + isPartitioned && conf.get(FlinkOptions.WRITE_BULK_INSERT_SHUFFLE_INPUT); + final RowDataKeyGen rowDataKeyGen = RowDataKeyGens.instance(conf, rowType); + + DataStream<RowData> routedDataStream = dataStream; + if (shouldShuffle) { + // Use #partitionCustom instead of #keyBy to avoid duplicate sort operations, + // see BatchExecutionUtils#applyBatchExecutionSettings for details. + Partitioner<String> partitioner = + (key, channels) -> KeyGroupRangeAssignment.assignKeyToParallelOperator( + key, + KeyGroupRangeAssignment.computeDefaultMaxParallelism(writeTasks), + channels); + routedDataStream = + dataStream.partitionCustom(partitioner, rowDataKeyGen::getPartitionPath); + } + + if (isLsmTreeStorageLayout) { + RowType sortRowType = LsmBulkInsertSortUtils.sortRowType(rowType); + InternalTypeInfo<RowData> sortTypeInfo = InternalTypeInfo.of(sortRowType); + DataStream<RowData> sortInput = routedDataStream + .map(record -> LsmBulkInsertWriterHelper.rowWithPartitionAndKey( + rowDataKeyGen.getPartitionPath(record), record, rowDataKeyGen), sortTypeInfo) + .name("lsm_bulk_insert_sort_keys") + .setParallelism(writeTasks); + return addBulkInsertSorter( + conf, + sortInput, + sortTypeInfo, + LsmBulkInsertSortUtils.getLsmSorterGen(sortRowType), + "lsm_sorter:(partition_path, record_key)", + writeTasks); + } + + if (!isPartitioned || !conf.get(FlinkOptions.WRITE_BULK_INSERT_SORT_INPUT)) { + return routedDataStream; + } + Review Comment: More detail comments. -- 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]
