prashantwason commented on code in PR #18083: URL: https://github.com/apache/hudi/pull/18083#discussion_r2955401630
########## hudi-flink-datasource/hudi-flink/src/main/java/org/apache/hudi/sink/append/AppendWriteFunctionWithContinuousSort.java: ########## @@ -0,0 +1,334 @@ +/* + * 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.append; + +import org.apache.hudi.configuration.FlinkOptions; +import org.apache.hudi.exception.HoodieException; +import org.apache.hudi.exception.HoodieIOException; +import org.apache.hudi.sink.StreamWriteOperatorCoordinator; +import org.apache.hudi.sink.bulk.sort.SortOperatorGen; + +import org.apache.flink.configuration.Configuration; +import org.apache.flink.core.memory.MemorySegment; +import org.apache.flink.core.memory.MemorySegmentFactory; +import org.apache.flink.table.data.RowData; +import org.apache.flink.table.planner.codegen.sort.SortCodeGenerator; +import org.apache.flink.table.runtime.generated.GeneratedNormalizedKeyComputer; +import org.apache.flink.table.runtime.generated.GeneratedRecordComparator; +import org.apache.flink.table.runtime.generated.NormalizedKeyComputer; +import org.apache.flink.table.runtime.generated.RecordComparator; +import org.apache.flink.table.types.logical.RowType; +import org.apache.flink.util.Collector; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.IOException; +import java.util.Arrays; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.TreeMap; +import java.util.stream.Collectors; + +/** + * Sink function to write data with continuous sorting for improved compression. + * + * <p>Unlike {@link AppendWriteFunctionWithBufferSort} which uses batch sorting, + * this function maintains sorted order continuously using a TreeMap, providing: + * <ul> + * <li>Non-blocking inserts (O(log n) vs O(1) + periodic O(n log n))</li> + * <li>Incremental draining without re-sorting</li> + * <li>Predictable latency (no sort spikes)</li> + * </ul> + * + * <p>Strategy: + * <ol> + * <li>Records are inserted in sorted order (TreeMap)</li> + * <li>When buffer reaches max capacity, oldest record(s) are drained synchronously</li> + * <li>Drain size is configurable to balance latency vs. throughput vs compression ratio</li> + * </ol> + * + * @param <T> Type of the input record + * @see StreamWriteOperatorCoordinator + */ +public class AppendWriteFunctionWithContinuousSort<T> extends AppendWriteFunction<T> { + + private static final Logger LOG = LoggerFactory.getLogger(AppendWriteFunctionWithContinuousSort.class); + + private final long maxCapacity; + private final int drainSize; + + private transient TreeMap<SortKey, RowData> sortedRecords; + private transient long insertionSequence; + + // Sort key computation + private transient NormalizedKeyComputer normalizedKeyComputer; + private transient RecordComparator recordComparator; + private transient byte[] reusableKeyBuffer; Review Comment: Done. Made `reusableKeyBuffer` a local variable in `open()`. -- 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]
