-
Notifications
You must be signed in to change notification settings - Fork 149
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#2229] feat(server,client): Introduce OutputUtils class to reduce lo…
…g size (#2230) ### What changes were proposed in this pull request? Introduce OutputUtils class to reduce log size ### Why are the changes needed? Fix: #2229 ### Does this PR introduce _any_ user-facing change? Log become useful but tiny enough. ### How was this patch tested? new UT
- Loading branch information
1 parent
4206541
commit 1364a82
Showing
5 changed files
with
174 additions
and
5 deletions.
There are no files selected for viewing
101 changes: 101 additions & 0 deletions
101
common/src/main/java/org/apache/uniffle/common/util/OutputUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
/* | ||
* 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.uniffle.common.util; | ||
|
||
import java.util.List; | ||
|
||
public class OutputUtils { | ||
|
||
/** | ||
* Convert a list of number into a segment string. | ||
* | ||
* @param numbers the list of number | ||
* @return the segment string | ||
*/ | ||
public static String listToSegment(List<Integer> numbers) { | ||
return listToSegment(numbers, 1, Long.MAX_VALUE); | ||
} | ||
|
||
/** | ||
* Convert a list of number into a segment string. | ||
* | ||
* @param numbers the list of number | ||
* @param threshold if the segment size is larger than this threshold, it will be converted into a | ||
* segment string | ||
* @return the segment string | ||
*/ | ||
public static String listToSegment(List<Integer> numbers, long threshold) { | ||
return listToSegment(numbers, threshold, Long.MAX_VALUE); | ||
} | ||
|
||
/** | ||
* Convert a list of number into a segment string. | ||
* | ||
* @param numbers the list of number | ||
* @param threshold if the segment size is larger than this threshold, it will be converted into a | ||
* segment string | ||
* @param limit the maximum number of elements in the segment | ||
* @return the segment string | ||
*/ | ||
public static String listToSegment(List<Integer> numbers, long threshold, long limit) { | ||
if (numbers == null || numbers.isEmpty()) { | ||
return "[]"; | ||
} | ||
if (threshold < 1 || numbers.size() <= threshold) { | ||
return numbers.toString(); | ||
} | ||
|
||
StringBuilder result = new StringBuilder(); | ||
int start = numbers.get(0); | ||
int end = start; | ||
|
||
long rangeCount = 0; | ||
for (int i = 1; i < numbers.size(); i++) { | ||
if (numbers.get(i) == numbers.get(i - 1) + 1) { | ||
end = numbers.get(i); | ||
} else { | ||
if (rangeCount < limit) { | ||
appendRange(result, start, end); | ||
} | ||
rangeCount++; | ||
start = numbers.get(i); | ||
end = start; | ||
} | ||
} | ||
rangeCount++; | ||
if (rangeCount < limit) { | ||
// Append the last range | ||
appendRange(result, start, end); | ||
} else { | ||
result.append("...").append(rangeCount - limit).append(" more ranges..."); | ||
} | ||
|
||
return result.toString(); | ||
} | ||
|
||
private static void appendRange(StringBuilder result, int start, int end) { | ||
if (result.length() > 0) { | ||
result.append(", "); | ||
} | ||
if (start == end) { | ||
result.append(start); | ||
} else { | ||
result.append("[").append(start).append("~").append(end).append("]"); | ||
} | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
common/src/test/java/org/apache/uniffle/common/util/OutputUtilsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
* 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.uniffle.common.util; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
public class OutputUtilsTest { | ||
@Test | ||
public void test() { | ||
List<Integer> numbers = | ||
Arrays.asList( | ||
9527, 9528, 9529, 9530, 11375, 11376, 11377, 11378, 11379, 12000, 12001, 12002, 12003, | ||
12004); | ||
assertEquals("[9527~9530], [11375~11379], [12000~12004]", OutputUtils.listToSegment(numbers)); | ||
assertEquals( | ||
"[9527~9530], [11375~11379], [12000~12004]", OutputUtils.listToSegment(numbers, 4)); | ||
assertEquals( | ||
"[9527, 9528, 9529, 9530, 11375, 11376, 11377, 11378," | ||
+ " 11379, 12000, 12001, 12002, 12003, 12004]", | ||
OutputUtils.listToSegment(numbers, 20)); | ||
// limit | ||
assertEquals( | ||
"[9527~9530], [11375~11379]...1 more ranges...", OutputUtils.listToSegment(numbers, 1, 2)); | ||
assertEquals("[9527~9530]...2 more ranges...", OutputUtils.listToSegment(numbers, 1, 1)); | ||
assertEquals("...3 more ranges...", OutputUtils.listToSegment(numbers, 1, 0)); | ||
|
||
// corner case | ||
assertEquals("[9527]", OutputUtils.listToSegment(Arrays.asList(9527))); | ||
assertEquals("[]", OutputUtils.listToSegment(Arrays.asList())); | ||
assertEquals("[]", OutputUtils.listToSegment(null)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters