Skip to content

Conversation

@original-brownbear
Copy link
Contributor

@original-brownbear original-brownbear commented Nov 24, 2024

Same as #114107 but for the transport side.
We are consuming more than 0.5M for these stat instances and a large fraction if not the majority will go unused in many use-cases, given how expensive stats are in terms of volatile operations the overhead of an added getAcquire is trivial in the absence of writes.
Outside of reducing the prod footprint this is also kind of nice in x-pack internal cluster tests in particular where it saves quite a bit of heap.

Same as #114107 but for the transport side.
We consuming more than 0.5M for these stat instances and a large fraction if not the majority
will go unused in many use-cases, given how expensive stats are in terms of volatile operations
the overhead of an added `getAquire` is trivial in the absence of writes.
@original-brownbear original-brownbear added >non-issue :Data Management/Stats Statistics tracking and retrieval APIs :Core/Infra/Metrics Metrics and metering infrastructure labels Nov 24, 2024
@elasticsearchmachine elasticsearchmachine added Team:Core/Infra Meta label for core/infra team Team:Data Management Meta label for data/management team v9.0.0 labels Nov 24, 2024
@elasticsearchmachine
Copy link
Collaborator

Pinging @elastic/es-core-infra (Team:Core/Infra)

@elasticsearchmachine
Copy link
Collaborator

Pinging @elastic/es-data-management (Team:Data Management)

private final Writeable.Reader<Request> requestReader;
private final TransportActionStatsTracker statsTracker = new TransportActionStatsTracker();
@SuppressWarnings("unused") // only accessed via #STATS_TRACKER_HANDLE, lazy initialized because instances consume non-trivial heap
private volatile TransportActionStatsTracker statsTracker;
Copy link
Contributor

Choose a reason for hiding this comment

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

If it's volatile we don't need to use STATS_TRACKER_HANDLE.getAcquire(this) to read it do we?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Right but lets drop the volatile then :D I figured I'd be fun to experiment with weaker ordering a little here and there in obvious spots, might be helpful on ARM in some cases where there's only ever a single write to a variable. Who knows might help init performance either now or on some future ARM or similar hardware.
(admittedly there's more interesting spots for that stuff than this ... :))


private static final VarHandle STATS_TRACKER_HANDLE;

static {
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we add this to the classes passed to org.elasticsearch.bootstrap.Elasticsearch#ensureInitialized to ensure it's always initialized eagerly before the security manager comes along and causes problems?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure thing, dded this one as well as MethodHandlers where I did the same thing now.

Copy link
Contributor

@DaveCTurner DaveCTurner left a comment

Choose a reason for hiding this comment

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

LGTM

}

private TransportActionStatsTracker statsTracker() {
var tracker = existingStatsTracker();
Copy link
Contributor

Choose a reason for hiding this comment

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

If you're looking for yet-weaker memory semantics, I think this one can be a regular (nonvolatile) read. If it's null then the compareAndExchange will establish the right guarantees, and if it's not null then it has to be the right value.

Risks perhaps creating a few more unused TransportActionStatsTracker instances tho I guess.

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 somehow a plain read in this kind of spot weirds me out a little :D but you are right, we could relax these spots further technically I guess I just don't feel adventurous enough for that this evening (I do realise though that we actually do this kind of thing ... sync write -> plain read ... in a couple spots like the CS + routing table computation already).

@original-brownbear original-brownbear added auto-merge-without-approval Automatically merge pull request when CI checks pass (NB doesn't wait for reviews!) auto-backport Automatically create backport pull requests when merged v8.18.0 labels Dec 10, 2024
@elasticsearchmachine elasticsearchmachine merged commit 56379a3 into elastic:main Dec 10, 2024
16 checks passed
@original-brownbear original-brownbear deleted the lazy-stats branch December 10, 2024 20:01
@elasticsearchmachine
Copy link
Collaborator

💚 Backport successful

Status Branch Result
8.x

elasticsearchmachine pushed a commit that referenced this pull request Jul 30, 2025
…7435) (#118381)

Same as #114107 but for the transport side. We are consuming more than
0.5M for these stat instances and a large fraction if not the majority
will go unused in many use-cases, given how expensive stats are in terms
of volatile operations the overhead of an added `getAcquire` is trivial
in the absence of writes. Outside of reducing the prod footprint this is
also kind of nice in x-pack internal cluster tests in particular where
it saves quite a bit of heap.

Co-authored-by: Dimitris Rempapis <[email protected]>
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 auto-merge-without-approval Automatically merge pull request when CI checks pass (NB doesn't wait for reviews!) :Core/Infra/Metrics Metrics and metering infrastructure :Data Management/Stats Statistics tracking and retrieval APIs >non-issue Team:Core/Infra Meta label for core/infra team Team:Data Management Meta label for data/management team v8.18.0 v9.0.0

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants