From e8b0e30257d9c7b9a284fda9a25264bf22eb4f10 Mon Sep 17 00:00:00 2001 From: Mashhur Date: Mon, 23 Mar 2026 10:40:08 -0700 Subject: [PATCH 1/2] DeadLetterQueueUtils: avoid using heavy split operation, replace it with simple index of and substring methods. --- .../java/org/logstash/common/io/DeadLetterQueueUtils.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/logstash-core/src/main/java/org/logstash/common/io/DeadLetterQueueUtils.java b/logstash-core/src/main/java/org/logstash/common/io/DeadLetterQueueUtils.java index 31e9d564426..35535acfaa8 100644 --- a/logstash-core/src/main/java/org/logstash/common/io/DeadLetterQueueUtils.java +++ b/logstash-core/src/main/java/org/logstash/common/io/DeadLetterQueueUtils.java @@ -38,7 +38,9 @@ class DeadLetterQueueUtils { private static final Logger logger = LogManager.getLogger(DeadLetterQueueUtils.class); static int extractSegmentId(Path p) { - return Integer.parseInt(p.getFileName().toString().split("\\.log")[0]); + final String fileName = p.getFileName().toString(); + final int dotIndex = fileName.indexOf(".log"); + return Integer.parseInt(fileName.substring(0, dotIndex)); } static Stream listFiles(Path path, String suffix) throws IOException { From c1182d2a908be579a2ae4520eb0f1aa87affae08 Mon Sep 17 00:00:00 2001 From: Mashhur Date: Wed, 25 Mar 2026 12:03:16 -0700 Subject: [PATCH 2/2] Handle exception case if file name doesn't end with .log. Add unit test cases. --- .../common/io/DeadLetterQueueUtils.java | 5 ++++- .../common/io/DeadLetterQueueReaderTest.java | 22 +++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/logstash-core/src/main/java/org/logstash/common/io/DeadLetterQueueUtils.java b/logstash-core/src/main/java/org/logstash/common/io/DeadLetterQueueUtils.java index 35535acfaa8..06a6a507462 100644 --- a/logstash-core/src/main/java/org/logstash/common/io/DeadLetterQueueUtils.java +++ b/logstash-core/src/main/java/org/logstash/common/io/DeadLetterQueueUtils.java @@ -37,9 +37,12 @@ class DeadLetterQueueUtils { private static final Logger logger = LogManager.getLogger(DeadLetterQueueUtils.class); - static int extractSegmentId(Path p) { + static int extractSegmentId(final Path p) { final String fileName = p.getFileName().toString(); final int dotIndex = fileName.indexOf(".log"); + if (dotIndex <= 0) { + throw new IllegalArgumentException("Invalid segment file name: " + fileName); + } return Integer.parseInt(fileName.substring(0, dotIndex)); } diff --git a/logstash-core/src/test/java/org/logstash/common/io/DeadLetterQueueReaderTest.java b/logstash-core/src/test/java/org/logstash/common/io/DeadLetterQueueReaderTest.java index da8ae7e91eb..90f53df7739 100644 --- a/logstash-core/src/test/java/org/logstash/common/io/DeadLetterQueueReaderTest.java +++ b/logstash-core/src/test/java/org/logstash/common/io/DeadLetterQueueReaderTest.java @@ -1124,4 +1124,26 @@ public void testReaderLockProhibitMultipleInstances() throws IOException { } } } + + @Test + public void testExtractSegmentIdWithValidFileName() { + Path validPath = Paths.get("123.log"); + assertEquals(123, DeadLetterQueueUtils.extractSegmentId(validPath)); + + Path singleDigitPath = Paths.get("1.log"); + assertEquals(1, DeadLetterQueueUtils.extractSegmentId(singleDigitPath)); + + Path largeNumberPath = Paths.get("999999.log"); + assertEquals(999999, DeadLetterQueueUtils.extractSegmentId(largeNumberPath)); + } + + @Test + public void testExtractSegmentIdWithNoLogExtensionThrowsException() { + Path noExtensionPath = Paths.get("123.txt"); + IllegalArgumentException exception = Assert.assertThrows( + IllegalArgumentException.class, + () -> DeadLetterQueueUtils.extractSegmentId(noExtensionPath) + ); + assertThat(exception.getMessage(), containsString("Invalid segment file name")); + } }