Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recycle pages used by outgoing publications #77317

Conversation

DaveCTurner
Copy link
Contributor

Today PublicationTransportHandler.PublicationContext allocates a bunch
of memory for serialized cluster states and diffs, but it uses a plain
BytesStreamOutput which means that the backing pages are allocated by
the BigArrays#NON_RECYCLING_INSTANCE. With this commit we pass in a
proper BigArrays so that the pages being used can be recycled.

Today `PublicationTransportHandler.PublicationContext` allocates a bunch
of memory for serialized cluster states and diffs, but it uses a plain
`BytesStreamOutput` which means that the backing pages are allocated by
the `BigArrays#NON_RECYCLING_INSTANCE`. With this commit we pass in a
proper `BigArrays` so that the pages being used can be recycled.
@DaveCTurner DaveCTurner added >enhancement :Distributed Coordination/Cluster Coordination Cluster formation and cluster state publication, including cluster membership and fault detection. v8.0.0 v7.16.0 labels Sep 6, 2021
@elasticmachine elasticmachine added the Team:Distributed (Obsolete) Meta label for distributed team (obsolete). Replaced by Distributed Indexing/Coordination. label Sep 6, 2021
@elasticmachine
Copy link
Collaborator

Pinging @elastic/es-distributed (Team:Distributed)

@DaveCTurner DaveCTurner removed the request for review from original-brownbear September 6, 2021 16:43
@DaveCTurner DaveCTurner marked this pull request as draft September 6, 2021 16:43
@DaveCTurner
Copy link
Contributor Author

Hmm, the failure is a genuine problem: we simulate node reboots in CoordinatorTests and we block any further activity on the rebooted instance, but we don't release any memory they allocated.

@original-brownbear
Copy link
Member

Thanks David, I always wanted this :)

Hmm, the failure is a genuine problem: we simulate node reboots in CoordinatorTests and we block any further activity on the rebooted instance, but we don't release any memory they allocated.

I remember us running into this before when you tried this previously. Maybe it's ok to leave this as a TODO and just not use pooled buffers in tests that block all activity on a node like that? Not the nicest solution, but realistically if we want to block all activity on a node then that means no releasing of memory and we do have all kinds of tests for all kinds of failure scenarios/disconnects/you-name-it that provide coverage for this?

I suppose we could invest more effort and create another version of BigArrays that allows us some more fine grained control over this and selectively release all memory for a blocked node but that seems quite cumbersome?

@DaveCTurner
Copy link
Contributor Author

It's actually not that bad it seems, see DaveCTurner@f5ce155#diff-0681cc9adce47fddd18bdfb39428f871bb4b592a40829fd6b58eeea7e1495358R1533-R1537 although that's not all, we also have to clean up messages that went to a blackhole (waiting a day works but makes the tests super-slow) and also there's another blackhole for messages that get sent to a node that has rebooted. Not sure what else I'll find yet, mostly I'm just working through failures where the cluster gets left in a broken state by the test.

- Track blackholed requests for timely completion
- Deliver blackholed requests at end of runRandomly
- Release memory from rebooted nodes
- Complete messages delivered to rebooted nodes
- Heal cluster from disruptions before end of tests
@DaveCTurner DaveCTurner force-pushed the 2021-09-06-recycle-cluster-state-publication-memory branch from f5ce155 to 7b4961d Compare September 7, 2021 08:16
@DaveCTurner
Copy link
Contributor Author

failure is #58946, @elasticmachine please run elasticsearch-ci/part-1

@DaveCTurner DaveCTurner marked this pull request as ready for review September 7, 2021 12:29
@DaveCTurner
Copy link
Contributor Author

I've run through the CoordinatorTests over 100 times forcing the use of MockBigArrays without seeing any further leaks or other failures, so I believe this is ready to review now.

Copy link
Member

@original-brownbear original-brownbear left a comment

Choose a reason for hiding this comment

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

Spent quite some time thinking this one through and couldn't find a leak I think. This looks really nice, thanks David!

Just one quick question on the concurrency handling (mainly to make sure I'm not missing any detail :)).

if (bytes == null) {
try {
bytes = serializeFullClusterState(newState, destination.getVersion());
serializedStates.put(destination.getVersion(), bytes);
final ReleasableBytesReference existingBytes = serializedStates.putIfAbsent(destination.getVersion(), bytes);
Copy link
Member

Choose a reason for hiding this comment

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

I wonder, can't we just do computeIfAbsent here instead and block on concurrent access? This is all on the generic pool anyways if there's contention isn't it? In any case no matter the thread this runs on, seems like blocking on the locked CHM key is still cheaper on the system overall than serializing twice? (it's an edge case either way but using computeIfAbsent is less code here I think? :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah that's a good point, I wasn't sure if computeIfAbsent ran the supplier twice sometimes but you made me go and read the docs and they say that it won't so it should be fine. Definitely a corner case of a corner case to be blocking here but I suppose we may as well not burn CPU while we wait given that we'll be waiting about as long either way. No risk of deadlock either.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok see 13bfe0c (it triggered a few more changes/simplifications too)

Copy link
Member

@original-brownbear original-brownbear left a comment

Choose a reason for hiding this comment

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

LGTM, if CI is happy I'm happy :) thanks David!

@DaveCTurner DaveCTurner merged commit 8b50fcd into elastic:master Sep 8, 2021
@DaveCTurner DaveCTurner deleted the 2021-09-06-recycle-cluster-state-publication-memory branch September 8, 2021 06:46
DaveCTurner added a commit that referenced this pull request Sep 8, 2021
DaveCTurner added a commit to DaveCTurner/elasticsearch that referenced this pull request Sep 8, 2021
Today `PublicationTransportHandler.PublicationContext` allocates a bunch
of memory for serialized cluster states and diffs, but it uses a plain
`BytesStreamOutput` which means that the backing pages are allocated by
the `BigArrays#NON_RECYCLING_INSTANCE`. With this commit we pass in a
proper `BigArrays` so that the pages being used can be recycled.
DaveCTurner added a commit to DaveCTurner/elasticsearch that referenced this pull request Sep 8, 2021
Today `PublicationTransportHandler.PublicationContext` allocates a bunch
of memory for serialized cluster states and diffs, but it uses a plain
`BytesStreamOutput` which means that the backing pages are allocated by
the `BigArrays#NON_RECYCLING_INSTANCE`. With this commit we pass in a
proper `BigArrays` so that the pages being used can be recycled.
elasticsearchmachine pushed a commit that referenced this pull request Sep 13, 2021
* Recycle pages used by outgoing publications (#77317)

Today `PublicationTransportHandler.PublicationContext` allocates a bunch
of memory for serialized cluster states and diffs, but it uses a plain
`BytesStreamOutput` which means that the backing pages are allocated by
the `BigArrays#NON_RECYCLING_INSTANCE`. With this commit we pass in a
proper `BigArrays` so that the pages being used can be recycled.

* Enable stricter tests always

* Fix one

* Better assertions (and longer run) in testSingleNodeDiscoveryStabilisesEvenWhenDisrupted

* Clean up warnings
elasticsearchmachine pushed a commit that referenced this pull request Sep 13, 2021
* Recycle pages used by outgoing publications (#77317)

Today `PublicationTransportHandler.PublicationContext` allocates a bunch
of memory for serialized cluster states and diffs, but it uses a plain
`BytesStreamOutput` which means that the backing pages are allocated by
the `BigArrays#NON_RECYCLING_INSTANCE`. With this commit we pass in a
proper `BigArrays` so that the pages being used can be recycled.

* Enable stricter tests always

* Fix one

* Better assertions (and longer run) in testSingleNodeDiscoveryStabilisesEvenWhenDisrupted

* Clean up warnings
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
:Distributed Coordination/Cluster Coordination Cluster formation and cluster state publication, including cluster membership and fault detection. >enhancement Team:Distributed (Obsolete) Meta label for distributed team (obsolete). Replaced by Distributed Indexing/Coordination. v7.16.0 v8.0.0-alpha2
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants