cshuo commented on code in PR #19307:
URL: https://github.com/apache/hudi/pull/19307#discussion_r3629871917


##########
hudi-flink-datasource/hudi-flink/src/main/java/org/apache/hudi/sink/utils/RecordKeySortKeyComputer.java:
##########
@@ -0,0 +1,111 @@
+/*
+ * 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.utils;
+
+import org.apache.hudi.sink.bulk.RowDataKeyGen;
+
+import org.apache.flink.api.common.typeutils.base.StringComparator;
+import org.apache.flink.core.memory.MemorySegment;
+import org.apache.flink.table.data.RowData;
+import org.apache.flink.table.runtime.generated.NormalizedKeyComputer;
+
+/**
+ * Computes a normalized prefix for sorting rows by their encoded Hudi record 
key.
+ *
+ * <p>The common LSM reader compares encoded record keys with {@link 
String#compareTo(String)},
+ * whose order is based on UTF-16 code units. This computer therefore uses 
Flink's normalized-key
+ * encoding for Java strings, which preserves that order while still using 
only one byte for the
+ * common ASCII case.
+ *
+ * <p>The normalized prefix is eight bytes for one record-key field and 
sixteen bytes for two or
+ * more fields. It is packed into one or two big-endian {@code long}s so the 
sort hot path can
+ * compare and swap the prefix with long operations instead of processing 
every byte separately.
+ * Short prefixes are padded with zeroes. Truncated prefixes, as well as 
collisions caused by zero
+ * padding, are resolved by {@link RecordKeySortComparator}; therefore the 
normalized key never
+ * fully determines the result.
+ */
+public class RecordKeySortKeyComputer implements NormalizedKeyComputer {

Review Comment:
   The following **microbenchmark** compares the encoded Hoodie record-key 
computer/comparator with Flink code-generated sorting. Each scenario sorts 
300,000 records using `BinaryInMemorySortBuffer`, with 3 warm-up rounds and 7 
measured rounds; the table reports the median.
   
   <table>
     <thead>
       <tr>
         <th rowspan="2">Key Scenario</th>
         <th colspan="2">Write (ms)</th>
         <th colspan="2">Sort (ms)</th>
         <th colspan="2">Total (ms)</th>
       </tr>
       <tr>
         <th>Codegen</th>
         <th>Encoded</th>
         <th>Codegen</th>
         <th>Encoded</th>
         <th>Codegen</th>
         <th>Encoded</th>
       </tr>
     </thead>
     <tbody>
       <tr>
         <td>LONG</td>
         <td>24.2</td>
         <td>54.4</td>
         <td>198.7</td>
         <td>195.6</td>
         <td>222.9</td>
         <td>250.0</td>
       </tr>
       <tr>
         <td>Composite String, no fallback</td>
         <td>67.7</td>
         <td>167.2</td>
         <td>261.0</td>
         <td>159.5</td>
         <td>328.8</td>
         <td>326.7</td>
       </tr>
       <tr>
         <td>Composite String, 50% fallback</td>
         <td>35.1</td>
         <td>94.1</td>
         <td>574.8</td>
         <td>2,134.8</td>
         <td>609.9</td>
         <td>2,228.9</td>
       </tr>
     </tbody>
   </table>
   
   The encoded path has a higher write cost because it constructs the encoded 
Hoodie record key and computes its normalized prefix for every record. For LONG 
keys, its sort cost is comparable to codegen, resulting in an approximately 12% 
higher total cost.
   
   For composite strings (two keys) without normalized-key collisions, the 
encoded sort is about 39% faster than codegen because comparisons are resolved 
using the packed 16-byte prefix. This offsets the additional write cost, making 
the total costs effectively equal.
   
   Performance degrades when normalized-key collisions require the full 
comparator. With an observed fallback rate of approximately 49%, the encoded 
sort is about 3.7x slower and the total cost is about 3.65x higher. The main 
overhead is repeatedly constructing and comparing complete encoded Hoodie 
record keys in the `O(N log N)` sorting path.
   
   These numbers are from a focused microbenchmark and should be used to 
compare the relative cost of the sorting implementations, rather than as 
end-to-end Flink write-performance measurements.



-- 
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]

Reply via email to