From 3dda0c45da9d0b4b3ba44dd608c6b03ab423f465 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E5=AE=B6=E5=90=8D?= Date: Fri, 11 Sep 2026 11:05:44 +0800 Subject: [PATCH 1/2] [Fix-17139][Common] Fix multibyte log preview truncation --- .../common/utils/LogUtils.java | 23 ++++---- .../common/utils/LogUtilsTest.java | 57 +++++++++++++++++++ 2 files changed, 68 insertions(+), 12 deletions(-) create mode 100644 dolphinscheduler-common/src/test/java/org/apache/dolphinscheduler/common/utils/LogUtilsTest.java diff --git a/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/LogUtils.java b/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/LogUtils.java index b007e337ec7b..53f17c72de98 100644 --- a/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/LogUtils.java +++ b/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/LogUtils.java @@ -24,7 +24,6 @@ import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; -import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Paths; import java.util.List; @@ -86,22 +85,22 @@ public static List readPartFileContentFromRemote(String filePath, public static String rollViewLogLines(List lines) { StringBuilder builder = new StringBuilder(); - final int MaxResponseLogSize = 65535; - int totalLogByteSize = 0; + final int maxLogCharSize = 65535; + int totalLogCharSize = 0; for (String line : lines) { - // If a single line of log is exceed max response size, cut off the line - final int lineByteSize = line.getBytes(StandardCharsets.UTF_8).length; - if (lineByteSize >= MaxResponseLogSize) { - builder.append(line, 0, MaxResponseLogSize) - .append(" [this line's size ").append(lineByteSize).append(" bytes is exceed ") - .append(MaxResponseLogSize).append(" bytes, so only ") - .append(MaxResponseLogSize).append(" characters are reserved for performance reasons.]") + // If a single log line exceeds the character limit, cut off the line. + final int lineCharSize = line.length(); + if (lineCharSize >= maxLogCharSize) { + builder.append(line, 0, maxLogCharSize) + .append(" [this line's size ").append(lineCharSize).append(" characters exceeds ") + .append(maxLogCharSize).append(" characters, so only ") + .append(maxLogCharSize).append(" characters are reserved for performance reasons.]") .append("\r\n"); } else { builder.append(line).append("\r\n"); } - totalLogByteSize += lineByteSize; - if (totalLogByteSize >= MaxResponseLogSize) { + totalLogCharSize += lineCharSize; + if (totalLogCharSize >= maxLogCharSize) { break; } } diff --git a/dolphinscheduler-common/src/test/java/org/apache/dolphinscheduler/common/utils/LogUtilsTest.java b/dolphinscheduler-common/src/test/java/org/apache/dolphinscheduler/common/utils/LogUtilsTest.java new file mode 100644 index 000000000000..f4f8300a5e48 --- /dev/null +++ b/dolphinscheduler-common/src/test/java/org/apache/dolphinscheduler/common/utils/LogUtilsTest.java @@ -0,0 +1,57 @@ +/* + * 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.dolphinscheduler.common.utils; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import org.apache.commons.lang3.StringUtils; + +import java.util.Arrays; +import java.util.Collections; + +import org.junit.jupiter.api.Test; + +class LogUtilsTest { + + @Test + void testRollViewLogLinesWithMultibyteCharacters() { + String line = StringUtils.repeat("中", 22000); + + assertEquals(line + "\r\n", LogUtils.rollViewLogLines(Collections.singletonList(line))); + } + + @Test + void testRollViewLogLinesTruncatesLongLines() { + for (String character : Arrays.asList("a", "中")) { + String line = StringUtils.repeat(character, 65536); + String expected = StringUtils.repeat(character, 65535) + + " [this line's size 65536 characters exceeds 65535 characters, so only " + + "65535 characters are reserved for performance reasons.]\r\n"; + + assertEquals(expected, LogUtils.rollViewLogLines(Collections.singletonList(line))); + } + } + + @Test + void testRollViewLogLinesStopsAtCharacterLimit() { + String line = StringUtils.repeat("中", 32768); + + assertEquals(line + "\r\n" + line + "\r\n", + LogUtils.rollViewLogLines(Arrays.asList(line, line, "omitted"))); + } +} From 174727ea1df9da2eedbf6cb6bcc2e1ac7e76ace8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E5=AE=B6=E5=90=8D?= Date: Thu, 17 Sep 2026 19:37:31 +0800 Subject: [PATCH 2/2] test(review): remove Chinese literals from log preview tests for PR #18629 --- .../apache/dolphinscheduler/common/utils/LogUtilsTest.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/dolphinscheduler-common/src/test/java/org/apache/dolphinscheduler/common/utils/LogUtilsTest.java b/dolphinscheduler-common/src/test/java/org/apache/dolphinscheduler/common/utils/LogUtilsTest.java index f4f8300a5e48..37af9524f026 100644 --- a/dolphinscheduler-common/src/test/java/org/apache/dolphinscheduler/common/utils/LogUtilsTest.java +++ b/dolphinscheduler-common/src/test/java/org/apache/dolphinscheduler/common/utils/LogUtilsTest.java @@ -30,14 +30,14 @@ class LogUtilsTest { @Test void testRollViewLogLinesWithMultibyteCharacters() { - String line = StringUtils.repeat("中", 22000); + String line = StringUtils.repeat("\u20AC", 22000); assertEquals(line + "\r\n", LogUtils.rollViewLogLines(Collections.singletonList(line))); } @Test void testRollViewLogLinesTruncatesLongLines() { - for (String character : Arrays.asList("a", "中")) { + for (String character : Arrays.asList("a", "\u20AC")) { String line = StringUtils.repeat(character, 65536); String expected = StringUtils.repeat(character, 65535) + " [this line's size 65536 characters exceeds 65535 characters, so only " @@ -49,7 +49,7 @@ void testRollViewLogLinesTruncatesLongLines() { @Test void testRollViewLogLinesStopsAtCharacterLimit() { - String line = StringUtils.repeat("中", 32768); + String line = StringUtils.repeat("\u20AC", 32768); assertEquals(line + "\r\n" + line + "\r\n", LogUtils.rollViewLogLines(Arrays.asList(line, line, "omitted")));