Skip to content

Conversation

@dkropachev
Copy link

In almost all use cases replicas are getting converted into a List.
To make performance better make these API provide List instead.

@dkropachev dkropachev force-pushed the dk/3.x-change-get-replicas-api branch from d9b71c9 to e88f84e Compare February 20, 2025 20:59
@dkropachev dkropachev force-pushed the dk/3.x-change-get-replicas-api branch from e88f84e to f284924 Compare March 6, 2025 13:38
@dkropachev dkropachev self-assigned this Mar 6, 2025
@dkropachev dkropachev requested a review from Bouncheck March 6, 2025 13:45
Copy link

@Bouncheck Bouncheck left a comment

Choose a reason for hiding this comment

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

Added few comments to consider. I did not thoroughly check not highlighted parts of the code. One thing to look out for would be any calls to methods like contains. Since they are basically the same for sets and lists they are not highlighted as change. With lists they will be linear now, which may introduce unwanted overhead.

I get a feeling that we're moving instantiations deeper into the code instead of making a List copy later, which probably does not bring much benefit

@dkropachev dkropachev force-pushed the dk/3.x-change-get-replicas-api branch 2 times, most recently from 35b1528 to 50c1b05 Compare March 7, 2025 06:08
@dkropachev dkropachev requested a review from Bouncheck March 7, 2025 06:10
@dkropachev dkropachev force-pushed the dk/3.x-change-get-replicas-api branch 2 times, most recently from f4f5e1c to 5070eba Compare March 7, 2025 08:01
Copy link

@Bouncheck Bouncheck left a comment

Choose a reason for hiding this comment

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

I think TokenAwarePolicy has a typo introducing a bug, added a comment.

Metadata#getReplicas(String keyspace, TokenRange range) now creates a new HashSet copy rather than return directly.
Metadata#getReplicas(String keyspace, Token token) now returns a copy too
TokenAwarePolicy#newQueryPlan(final String loggedKeyspace, final Statement) Looks like for token map does a copy and for tablet map does not. Previously it would make a copy for both cases so this is partial improvement.

Overall net -1 collection creations for tablet map but +1 for token map (plus O(1) instanceof check for both) when we call newQueryPlan. For other policies it should be net 0 or +1 in that case.
If we don't care as much about token map performance then this is maybe an ok sacrifice to make.

edit: newQueryPlan does not call those getReplicas so this calculation was wrong. New instantiations affect only users of those specific APIs.

It would also be good to update the javadocs to mention where methods return a shallow copy and where not. Previously they would return underlying token maps collections.

@dkropachev dkropachev force-pushed the dk/3.x-change-get-replicas-api branch 2 times, most recently from e6673a9 to eeaaac3 Compare March 7, 2025 12:44
@dkropachev
Copy link
Author

dkropachev commented Mar 7, 2025

I think TokenAwarePolicy has a typo introducing a bug, added a comment.

Metadata#getReplicas(String keyspace, TokenRange range) now creates a new HashSet copy rather than return directly. Metadata#getReplicas(String keyspace, Token token) now returns a copy too TokenAwarePolicy#newQueryPlan(final String loggedKeyspace, final Statement) Looks like for token map does a copy and for tablet map does not. Previously it would make a copy for both cases so this is partial improvement.

Overall net -1 collection creations for tablet map but +1 for token map (plus O(1) instanceof check for both) when we call newQueryPlan. For other policies it should be net 0 or +1 in that case. If we don't care as much about token map performance then this is maybe an ok sacrifice to make.

It would also be good to update the javadocs to mention where methods return a shallow copy and where not. Previously they would return underlying token maps collections.

I don't think that it makes sense, it is only confuse API user, let's just state that you are not suppose to write to it, which we alredy do.

getReplicas - marked as depricated in favor of getReplicasList it is not used in the driver internals, only tests.
TabletMap does not return copy, it recreates it, later when/if we change it to contain host instance, it will return valu from tabletmap.

@dkropachev dkropachev force-pushed the dk/3.x-change-get-replicas-api branch from eeaaac3 to ccf8d18 Compare March 7, 2025 12:53
@Bouncheck
Copy link

getReplicas - marked as depricated in favor of getReplicasList it is not used in the driver internals, only tests.

You're right, this is actually not on the path to newQueryPlan, so this does not impact it. It uses getReplicas from TokenMap which is defined inside Metadata too, but those are different methods.

@dkropachev
Copy link
Author

@Bouncheck , please check on it.

@dkropachev dkropachev requested a review from Bouncheck March 7, 2025 16:33
Copy link

@Bouncheck Bouncheck left a comment

Choose a reason for hiding this comment

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

See comments

@dkropachev dkropachev force-pushed the dk/3.x-change-get-replicas-api branch 2 times, most recently from 6204b05 to 43e5291 Compare March 7, 2025 18:38
@dkropachev
Copy link
Author

See comments

All adressed

@dkropachev dkropachev requested a review from Bouncheck March 8, 2025 01:46
Copy link

@Bouncheck Bouncheck left a comment

Choose a reason for hiding this comment

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

Generally LGTM, although this does not avoid converting into ArrayList in the end, which i thought was the purpose.

In almost all use cases replicas are getting converted into a List.
To make performance better make these API provide List instead.
@dkropachev dkropachev force-pushed the dk/3.x-change-get-replicas-api branch from 43e5291 to 54f9276 Compare March 10, 2025 10:43
@dkropachev dkropachev merged commit 3adff13 into scylladb:scylla-3.x Mar 10, 2025
6 checks passed
@dkropachev
Copy link
Author

Generally LGTM, although this does not avoid converting into ArrayList in the end, which i thought was the purpose.

Thanks, it will be addressed in another PR after we introduce bench tests

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.

2 participants