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

perf: use ets table for client ID registration #57

Merged
merged 1 commit into from
Dec 18, 2023

Conversation

zmstone
Copy link
Contributor

@zmstone zmstone commented Dec 15, 2023

This is a performance optimization incorporated with a refactoring.

Performance fix

supervisor:which_children(wolff_client_sup) becomes quite expensive when there are many clients started (thousands).
Main caller is wolff:check_conectivity/1.

The fix is to store client ID -> Pid in a ets table.

Refactoring

1.9.0 made use of the producer name as ETS table key for producer process registration.
However that lead to two minor problems:

  1. Users are still forced to name producer (although no longer has to be an atom)
  2. Name clash is not checked, it might be the case that more than one {ClientId, Topic} uses the same producer name.
    the fix is to use {ClientId, Topic} as the ID, and the old "name" is only used as process registry (when it's an atom) for backward compatibility.

changelog.md Outdated Show resolved Hide resolved
src/wolff.erl Outdated Show resolved Hide resolved
src/wolff_client.erl Outdated Show resolved Hide resolved
%% @doc Insert the client in the registration table.
register_client(ClientId) ->
Pid = self(),
true = ets:insert(?WOLFF_CLIENTS_GLOBAL_TABLE, {ClientId, Pid}),
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: maybe use ets:insert_new to avoid overwriting a previous entry?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

it should always overwrite.
client process may restart (by supervisor) after crash

src/wolff_producers.erl Show resolved Hide resolved
src/wolff_producers.erl Outdated Show resolved Hide resolved
@zmstone zmstone force-pushed the 1215-perf-client-lookup branch 2 times, most recently from 0ceffb1 to a1437b0 Compare December 16, 2023 08:25
@zmstone zmstone merged commit fe8e71e into kafka4beam:main Dec 18, 2023
6 checks passed
@zmstone zmstone deleted the 1215-perf-client-lookup branch December 18, 2023 14:38
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.

3 participants