Skip to content

fix: stop leaking Elasticsearch scroll contexts - #218

Merged
kiivihal merged 2 commits into
mainfrom
fix/es-scroll-hygiene
Nov 4, 2026
Merged

kiivihal merged 2 commits into
mainfrom
fix/es-scroll-hygiene

Conversation

@kiivihal

@kiivihal kiivihal commented Nov 4, 2026

Copy link
Copy Markdown
Member

Incident

On 2026-11-04 around 06:00 the acpt Elasticsearch reached search.max_open_scroll_context (500). Every scroll user then failed with Trying to create too many scroll contexts, including hub3 /api/admin/index-ids and the v1 _delete_by_query. The open contexts were 5m-keepalive scrolls created in a burst. We have not yet confirmed which caller created them. This PR fixes the two hub3 code paths that can leave scroll contexts open.

Changes

ikuzo/service/x/bulk/handle_ids.go: index-ids now pages with a point in time instead of a scroll

  • Opens a PIT through the existing elasticsearch.PITClient (CreatePIT/ClosePIT, which also handles OpenSearch) with a 1m keepalive, then pages with search_after sorted on meta.hubID, the same approach as streamWithPIT and the sitemap. PIT contexts don't count against max_open_scroll_context.
  • The PIT is closed in a defer using context.WithoutCancel(r.Context()) with a 10s timeout. A client disconnect therefore no longer stops the cleanup. Before this change, scroll.Clear(r.Context()) failed on disconnect and the context stayed open for 5 minutes.
  • Output is unchanged: one hubID per line, a flush after each page, and the same logging. A 500 is still returned only if no id has been written yet. A failure to open the PIT also returns a 500.
  • The paging loop is split out as writeIndexIDs(w, fetch) so it can be unit tested without ES (handle_ids_test.go).

hub3/fragments/resource.go: GetPreviousScrollIDs always clears its scroll

  • Every return path (match found, io.EOF, error) used to return from inside the loop without clearing the scroll. Each tree-navigation call therefore left a 5m context open. ES keeps a scroll open even after it returns the empty last page.
  • scroll.Clear now runs in a defer with a background context and a 10s timeout, and the keepalive is set explicitly to 1m. Nothing else changes.

ES-internal scrolls from _delete_by_query are left as they are.

Tests

  • go build ./...: OK
  • go vet ./ikuzo/service/x/bulk/ ./hub3/fragments/: OK
  • go test ./ikuzo/service/x/bulk/: OK, 12 top-level tests pass, including the new TestWriteIndexIDs (paging, search_after propagation, skipping hits without a hubID, flushing) and TestWriteIndexIDsErrorAfterFirstPage.
  • go test ./hub3/fragments/: 57 top-level tests pass and 1 is skipped (Test_TypeClassAsURI, skipped already on main). The Ginkgo suite TestFragments (99 specs) fails in BeforeSuite because Docker isn't available in this environment for testcontainers: get provider: check host "unix:///var/run/docker.sock": docker info: permission denied. This is an environment problem and not caused by this change. It still needs a run where Docker is available.
  • There is no test against a real ES for the PIT path of index-ids or for the scroll clear in GetPreviousScrollIDs. Please verify on acpt after deploy, for example with GET _nodes/stats/indices/search (open_contexts / scroll_current) while calling index-ids and aborting the request partway.

🤖 Generated with Claude Code

kiivihal and others added 2 commits November 4, 2026 10:29
The index-ids handler held a 5m scroll context per request and cleared it
with the request context, so a client disconnect left the context open
until it expired. A burst of these counts against
search.max_open_scroll_context (500), after which every scroll user,
including _delete_by_query, fails with "Trying to create too many scroll
contexts".

Page with a point in time and search_after on meta.hubID (as the sitemap
and Stream already do), keepalive 1m, and close the point in time with a
context detached from the request. Output, per-page flushing and the
500-only-before-the-first-id behaviour are unchanged. The paging loop is
split out as writeIndexIDs and unit tested without Elasticsearch.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
GetPreviousScrollIDs returned from inside its scroll loop (match found,
end of results, or error) without clearing the scroll, so every call left
a 5m scroll context behind. Clear it in a defer with a background context
and lower the keepalive to 1m.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@codecov

codecov Bot commented Nov 4, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 20.00000% with 48 lines in your changes missing coverage. Please review.
✅ Project coverage is 36.10%. Comparing base (1d0f500) to head (7f28c61).

Files with missing lines Patch % Lines
ikuzo/service/x/bulk/handle_ids.go 22.64% 41 Missing ⚠️
hub3/fragments/resource.go 0.00% 7 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #218      +/-   ##
==========================================
+ Coverage   36.08%   36.10%   +0.01%     
==========================================
  Files         412      412              
  Lines       38113    38154      +41     
==========================================
+ Hits        13753    13775      +22     
- Misses      24358    24377      +19     
  Partials        2        2              
Flag Coverage Δ
unittests 36.10% <20.00%> (+0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@kiivihal
kiivihal merged commit 99a31d8 into main Nov 4, 2026
6 checks passed
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.

1 participant