From 66e0ba4fa912d58e4150f4f675f34113db79625e Mon Sep 17 00:00:00 2001 From: Y Ethan Guo Date: Fri, 10 Jan 2025 18:26:46 -0800 Subject: [PATCH] [HUDI-8857] Log shortened list of files instead of all files in MDT validator to avoid huge log message --- .../apache/hudi/common/util/StringUtils.java | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/hudi-io/src/main/java/org/apache/hudi/common/util/StringUtils.java b/hudi-io/src/main/java/org/apache/hudi/common/util/StringUtils.java index 2b9f1af11b433..63836a788c4a4 100644 --- a/hudi-io/src/main/java/org/apache/hudi/common/util/StringUtils.java +++ b/hudi-io/src/main/java/org/apache/hudi/common/util/StringUtils.java @@ -27,6 +27,7 @@ import java.util.List; import java.util.Map; import java.util.stream.Collectors; +import java.util.stream.IntStream; import java.util.stream.Stream; /** @@ -369,6 +370,53 @@ public static String concatenateWithThreshold(String a, String b, int byteLength return a.substring(0, bestLength) + b; } + /** + * Concatenates the string representation of each object in the list + * and returns a single string. The result is capped at "lengthThreshold" characters. + * + * @param objectList object list + * @param lengthThreshold number of characters to cap the string + * @param type of the object + * + * @return capped string representation of the object list + */ + public static String toStringWithThreshold(List objectList, int lengthThreshold) { + if (objectList == null || objectList.isEmpty()) { + return ""; + } + StringBuilder sb = new StringBuilder(); + + for (Object obj : objectList) { + if (sb.length() >= lengthThreshold) { + setLastThreeDots(sb); + break; + } + + String objString = (sb.length() > 0 ? "," : "") + obj; + + // Check if appending this string would exceed the limit + if (sb.length() + objString.length() > lengthThreshold) { + int remaining = lengthThreshold - sb.length(); + sb.append(objString, 0, remaining); + setLastThreeDots(sb); + break; + } else { + sb.append(objString); + } + } + + return sb.toString(); + } + + private static void setLastThreeDots(StringBuilder sb) { + IntStream.range(1, 4).forEach(i -> { + int loc = sb.length() - i; + if (loc >= 0) { + sb.setCharAt(loc, '.'); + } + }); + } + private static int getBestLength(String a, int threshold) { // Binary search to find the maximum length of substring of 'a' that fits within maxBytesForA int low = 0;