Skip to content

Conversation

@original-brownbear
Copy link
Contributor

These can consume non-trivial heap if transport messages start to queue up. Lets release them asap.

These can consume non-trivial heap if transport messages start to queue
up. Lets release them asap.
@elasticsearchmachine elasticsearchmachine added the Team:Search Foundations Meta label for the Search Foundations team in Elasticsearch label Mar 14, 2025
@elasticsearchmachine
Copy link
Collaborator

Pinging @elastic/es-search-foundations (Team:Search Foundations)

Copy link
Member

@javanna javanna left a comment

Choose a reason for hiding this comment

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

The idea looks correct to me, as we have seen cases of e.g. script fields loading _source and filling up document fields. I wish we'd add a test that verifies we do the right thing here.

@original-brownbear
Copy link
Contributor Author

I wish we'd add a test that verifies we do the right thing here.

I. figured the assertions are probably good enough. The possible new bug this introduces would be accessing the fields after release and that's covered by the asserts. Would be nice to have tests for the memory effects though but that's a bit of a bigger project I'm afraid.

Copy link
Contributor

@andreidan andreidan left a comment

Choose a reason for hiding this comment

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

Thanks Armin, this looks good in principle.

Just left a couple of minor suggestions/questions

edit: one more thing, should we have a unit/integration test where we dec ref a searchhit to 0 and assert these fields are empty/null to make sure we don't lose this optimisation in future refactorings

: innerHits.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, e -> e.getValue().asUnpooled())),
documentFields,
metaFields,
cloneIfHashMap(documentFields),
Copy link
Contributor

Choose a reason for hiding this comment

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

Sorry if this is obvious, but why do we need to clone in the unpooled case?

deallocate won't be called at all for unpooled hits

Copy link
Contributor

@andreidan andreidan Mar 17, 2025

Choose a reason for hiding this comment

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

Ah, it's about not sharing the references with pooled instances that will now modify the state - I got there in the end :)

private static void clearIfMutable(Map<String, DocumentField> fields) {
// check that we're dealing with a HashMap, instances read from the wire that are empty be of an immutable type
assert fields instanceof HashMap<?, ?> || fields.isEmpty() : fields;
if (fields instanceof HashMap<?, ?> hm) {
Copy link
Contributor

Choose a reason for hiding this comment

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

should we just check if the fields map is empty as opposed to an instanceof check? (note that EmptyMap.clear() doesn't throw, however a potential replacement with List.of() would)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yea I didn't wanna mess with the specifics of empty and not, this seemed the safest for a stop-gap improvement :) We can do much better here down the line. For now this deals with statistically speaking at least 50% of the issue.

Copy link
Contributor

@andreidan andreidan left a comment

Choose a reason for hiding this comment

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

This LGTM, thanks Armin.

As I said before, I'd like to see a test that verifies deallocate does the correct thing (so this change is resilient to refactorings)

I'll leave it up to you to decide if you'd like to add one or not.

@original-brownbear
Copy link
Contributor Author

Thanks Andrei! I'll leave out the tests for now because it's a lot of test for little gain, but stay tuned I have a follow-up that deals with this problem a little more fundamentally so we can stop worrying about any special cases :)

@original-brownbear original-brownbear added the auto-backport Automatically create backport pull requests when merged label Mar 18, 2025
@original-brownbear original-brownbear merged commit b1e878f into elastic:main Mar 18, 2025
17 checks passed
@original-brownbear original-brownbear deleted the release-doc-fields-asap branch March 18, 2025 08:46
@elasticsearchmachine
Copy link
Collaborator

💚 Backport successful

Status Branch Result
8.x

elasticsearchmachine pushed a commit that referenced this pull request Mar 18, 2025
…25071)

These can consume non-trivial heap if transport messages start to queue
up. Let's release them asap.
afoucret pushed a commit to afoucret/elasticsearch that referenced this pull request Mar 18, 2025
These can consume non-trivial heap if transport messages start to queue
up. Let's release them asap.
omricohenn pushed a commit to omricohenn/elasticsearch that referenced this pull request Mar 28, 2025
These can consume non-trivial heap if transport messages start to queue
up. Let's release them asap.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

auto-backport Automatically create backport pull requests when merged >non-issue :Search Foundations/Search Catch all for Search Foundations Team:Search Foundations Meta label for the Search Foundations team in Elasticsearch v8.19.0 v9.1.0

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants