Skip to content

Fix search reindex running out of memory after a file with a lot of text - #4132

Open
aduffeck wants to merge 2 commits into
opencloud-eu:mainfrom
aduffeck:search-memory-limits
Open

aduffeck wants to merge 2 commits into
opencloud-eu:mainfrom
aduffeck:search-memory-limits

Conversation

@aduffeck

Copy link
Copy Markdown
Member

A forced reindex in #4066 got the server OOM-killed at over 9 GB. The memory went up on a small JPEG, right after a 9.7 MiB Paint.NET file was indexed.

Two problems add up. Tika takes the .pdn file for text/plain, because it starts with an ASCII XML header, and returns the whole file as text: 18.5 MB from a 9.7 MiB file. SEARCH_CONTENT_EXTRACTION_SIZE_LIMIT only checks the file size, so all of it got indexed. That document is bigger than the 16 MiB batch limit, so it gets a segment of its own. zapx then sized the buffer for the next segment as the previous segment's bytes per document times (documents + 100). For the next file that was 101 times the large segment, about 8.5 GB reserved for a single small JPEG.

What changes:

  • The zapx pre-sizing is turned off, and the buffer grows as the segment is written. An index keeps the segment version it was created with, so this is set for every zapx version bleve registers. A test fails when bleve adds a version that isn't covered.
  • The text extracted from one file is cut off at the new SEARCH_CONTENT_EXTRACTION_TEXT_LIMIT, default 16 MiB. That matches the most a batch holds, so a single document never costs more than a full batch. The cut doesn't split a character and goes back to the last space when that's close. The text is cut before language detection, so Tika doesn't get the full text sent back either. 0 turns the limit off.

Peak RSS indexing the extracted text of a synthetic .pdn file with the same layout as the reporter's (PDN3 magic, XML header with a base64 thumbnail, compressed data), between 2000 small documents before it and 3000 after:

before after
SEARCH_BATCH_SIZE=1 9219 MiB 1905 MiB
default batch size OOM-killed at 10 GiB 1885 MiB

Real text needs about 60 MiB per MB at peak, text from a binary taken for text about 110 MiB per MB. So at the 16 MiB limit that comes to roughly 1 GiB for real text and up to 1.9 GiB for binary garbage.

Unit tests cover the cut and the zapx coverage, and a test checks that the document after a large one no longer allocates hundreds of MB.

Fixes #4066

Assisted by Claude Opus 5.5

After a document with a lot of text the next segment reserved a hundred times its size, gigabytes for a small batch.
The file size limit does not bound the text, Tika can turn a binary it takes for text into more text than the file size.
@codacy-production

codacy-production Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

Up to standards ✅

🟢 Issues 0 issues

Results:
0 new issues

View in Codacy

🟢 Metrics 15 complexity

Metric Results
Complexity 15

View in Codacy

🟢 Coverage 88.89% diff coverage · +0.02% coverage variation

Metric Results
Coverage variation ✅ +0.02% coverage variation (-1.00%)
Diff coverage ✅ 88.89% diff coverage

View coverage diff in Codacy

Coverage variation details
Coverable lines Covered lines Coverage
Common ancestor commit (2112327) 95362 29790 31.24%
Head commit (eca787e) 95389 (+27) 29816 (+26) 31.26% (+0.02%)

Coverage variation is the difference between the coverage for the head and common ancestor commits of the pull request branch: <coverage of head commit> - <coverage of common ancestor commit>

Diff coverage details
Coverable lines Covered lines Diff coverage
Pull request (#4132) 27 24 88.89%

Diff coverage is the percentage of lines that are covered by tests out of the coverable lines that the pull request added or modified: <covered lines added or modified>/<coverable lines added or modified> * 100%

NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.

@aduffeck

Copy link
Copy Markdown
Member Author

Release plan for this one:

The text limit adds a new setting, SEARCH_CONTENT_EXTRACTION_TEXT_LIMIT, and changes what gets indexed for a few files: text past 16 MiB in a single file isn't searchable anymore. Within the 20 MB file size limit that only hits large plain text, CSV or log files and text-heavy archives or office files. Nothing breaks: no setting is removed or renamed, the index mapping stays the same and no reindex is needed. Documents already in the index keep their full text until their space is reindexed.

You could argue for shipping the limit as a patch too. It's a safety limit against a memory problem, and the default is chosen so ordinary documents aren't affected. Still, it's a new option and a small change in what gets indexed, so to stay on the safe side I'd put it in the next feature release.

The zapx change is a different case. It adds no config and only changes how much memory indexing uses, and on its own it already prevents the OOM from #4066 (9.2 GiB down to 1.9 GiB in the reproducer, and no more OOM kill with the default batch size). I'd backport just that commit to the stable branch as a patch release, so affected installs get the fix without waiting for the feature release.

@micbar do you agree with that?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

OpenCloud 9.1.1 is killed by the host OOM killer during a forced single-space Bleve reindex

1 participant