Skip to content

Commit

Permalink
Use erlang:whereis/1 for checking if a store is running
Browse files Browse the repository at this point in the history
A store's process uses its `StoreId` as its registered name. This is a
public interface of Ra so we can depend on it. Reading from key metrics
counters is already very fast but switching to `whereis/1` eliminates
basically all overhead of this function. When used heavily (for example
in RabbitMQ while publishing and consuming rapidly) the CPU time spent
on `is_store_running/1` disappears from the output of a perf recording
and a flamegraph.
  • Loading branch information
the-mikedavis committed Sep 11, 2024
1 parent 2d04459 commit 7e51700
Showing 1 changed file with 1 addition and 5 deletions.
6 changes: 1 addition & 5 deletions src/khepri_cluster.erl
Original file line number Diff line number Diff line change
Expand Up @@ -1576,11 +1576,7 @@ get_store_ids() ->
%% @doc Indicates if `StoreId' is running or not.

is_store_running(StoreId) ->
ThisNode = node(),
RaServer = khepri_cluster:node_to_member(StoreId, ThisNode),
Timeout = khepri_app:get_default_timeout(),
KeyMetrics = ra:key_metrics(RaServer, Timeout),
Runs = maps:get(state, KeyMetrics) =/= noproc,
Runs = erlang:whereis(StoreId) =/= noproc,

%% We know the real state of the Ra server. In the case the Ra server
%% stopped behind the back of Khepri, we update the cached list of running
Expand Down

0 comments on commit 7e51700

Please sign in to comment.