Skip to content

DeadLetterQueueUtils#extractSegmentId improvement: replace split with index of and substring methods.#18874

Open
mashhurs wants to merge 2 commits intoelastic:mainfrom
mashhurs:improve-dlq-segmentid-fetch
Open

DeadLetterQueueUtils#extractSegmentId improvement: replace split with index of and substring methods.#18874
mashhurs wants to merge 2 commits intoelastic:mainfrom
mashhurs:improve-dlq-segmentid-fetch

Conversation

@mashhurs
Copy link
Copy Markdown
Contributor

@mashhurs mashhurs commented Mar 23, 2026

Release notes

What does this PR do?

DLQ flush operation is heavy, under the hood DeadLetterQueueUtils#extractSegmentId will be called. The String#split is also CPU intensive that internally utilizes Pattern#compile. Since the logic is simple, it can be replaceable with indexOf and substring.

Why is it important/What is the impact to the user?

A bit performance improvement when using DLQ.

Checklist

  • My code follows the style guidelines of this project
  • [ ] I have commented my code, particularly in hard-to-understand areas
  • [ ] I have made corresponding changes to the documentation
  • [ ] I have made corresponding change to the default configuration files (and/or docker env variables)
  • I have added tests that prove my fix is effective or that my feature works

Author's Checklist

  • [ ]

How to test this PR locally

Functionality test can be done by enabling DLQ and changing its settings, such as make size small to make DLQ full.
But performance measurement is a bit hard on powerful machines, so here is the benchmark - #18883

Related issues

Use cases

Screenshots

Logs

@github-actions
Copy link
Copy Markdown
Contributor

🤖 GitHub comments

Just comment with:

  • run docs-build : Re-trigger the docs validation. (use unformatted text in the comment!)
  • run exhaustive tests : Run the exhaustive tests Buildkite pipeline.

@mergify
Copy link
Copy Markdown
Contributor

mergify bot commented Mar 23, 2026

This pull request does not have a backport label. Could you fix it @mashhurs? 🙏
To fixup this pull request, you need to add the backport labels for the needed
branches, such as:

  • backport-8./d is the label to automatically backport to the 8./d branch. /d is the digit.
  • If no backport is necessary, please add the backport-skip label

@mashhurs mashhurs marked this pull request as draft March 23, 2026 18:10
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");
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What ensures that we're given a Path p whose filename ends with .log?

If we don't, the dotIndex will be -1, and then we'll get an obscure IndexOutOfBoundsException on the next line when invoking fileName.substring(0, -1).

I'd prefer a capturing regex (e.g., ^([0-9]+)[.]log$), and throwing a clear exception if we don't have a match.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right! Initial intention is to just replace the split with indexOf & substring. There is a listFiles (filters files end with .log) safeguard for current DLQ full situation but I generally agree that the method itself isn't safe. I have added lines to intentionally throw exception for the undesired "file doesn't end with .log" case.

@andsel
Copy link
Copy Markdown
Contributor

andsel commented Mar 24, 2026

FYI I've created #18883 to benchmark the various solutions.

@elasticmachine
Copy link
Copy Markdown

💚 Build Succeeded

History

@mashhurs mashhurs marked this pull request as ready for review March 25, 2026 21:47
@mashhurs mashhurs self-assigned this Mar 25, 2026
@mashhurs mashhurs requested a review from yaauie March 25, 2026 21:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants