Packages
ra
3.1.2
3.1.9
3.1.8
3.1.7
3.1.6
3.1.5
3.1.4
3.1.3
3.1.2
3.1.1
3.1.0
3.0.2
3.0.1
3.0.0
3.0.0-beta.1
2.17.3
2.17.2
2.17.1
2.17.0
2.16.13
2.16.12
2.16.11
2.16.10
2.16.9
2.16.8
2.16.7
2.16.6
2.16.5
2.16.4
2.16.3
2.16.2
2.16.1
2.16.0
2.16.0-pre.12
2.16.0-pre.11
2.16.0-pre.10
2.16.0-pre.9
2.16.0-pre.8
2.16.0-pre.7
2.16.0-pre.6
2.16.0-pre.5
2.16.0-pre.4
2.16.0-pre.3
2.16.0-pre.2
2.16.0-pre.1
2.15.4
2.15.3
2.15.2
2.15.1
2.15.0
2.14.0
2.13.6
2.13.5
2.13.4
2.13.3
2.13.2
2.13.1
2.13.0
2.13.0-pre.1
2.12.0
2.11.0
2.11.0-pre.1
2.10.2-pre.2
2.10.2-pre.1
2.10.1
2.10.0
2.10.0-pre.3
2.10.0-pre.2
2.10.0-pre.1
2.9.10-pre.1
2.9.1
2.9.1-pre.2
2.9.1-pre.1
2.9.0
2.8.0
retired
2.7.3
2.7.2
2.7.1
2.7.0
2.7.0-pre.3
2.7.0-pre.2
2.7.0-pre.1
2.6.3
2.6.2
2.6.1
2.6.0-pre.1
2.5.1
2.5.1-pre.1
2.5.0
2.4.9
2.4.8
2.4.7
2.4.6
2.4.5
2.4.4
2.4.3
2.4.2
retired
2.4.1
2.4.0
2.3.0
2.2.0
2.1.0
2.0.13
2.0.12
2.0.11
2.0.10
2.0.9
2.0.8
2.0.7
2.0.6
2.0.5
2.0.4
2.0.3
2.0.2
2.0.1
2.0.0
1.1.9
1.1.8
1.1.7
1.1.6
1.1.5
1.1.4
1.1.3
1.1.2
1.1.1
1.1.0
1.0.8
1.0.7
1.0.6
1.0.5
1.0.4
1.0.3
1.0.2
1.0.1
1.0.0
0.9.6
0.9.5
0.9.4
0.9.2
0.3.3
retired
0.3.2
retired
0.3.1
retired
Raft library
Current section
Files
Jump to
Current section
Files
src/ra_log_sync.erl
%% This Source Code Form is subject to the terms of the Mozilla Public
%% License, v. 2.0. If a copy of the MPL was not distributed with this
%% file, You can obtain one at https://mozilla.org/MPL/2.0/.
%%
%% Copyright (c) 2017-2025 Broadcom. All Rights Reserved. The term Broadcom refers to Broadcom Inc. and/or its subsidiaries.
%%
%% @hidden
-module(ra_log_sync).
-behaviour(gen_batch_server).
-export([start_link/1,
pool_size/0,
worker_name/2,
sync/2]).
-export([init/1,
handle_batch/2,
terminate/2,
format_status/1]).
-type sync_fun() :: fun(() -> ok | {error, term()}).
-type pool_ref() :: {pool, BaseName :: atom(), PoolSize :: pos_integer()}.
-export_type([pool_ref/0]).
-record(state, {}).
%%%===================================================================
%%% API
%%%===================================================================
-spec pool_size() -> pos_integer().
pool_size() ->
max(1, erlang:system_info(schedulers) div 4).
-spec worker_name(atom(), non_neg_integer()) -> atom().
worker_name(BaseName, Idx) ->
list_to_atom(atom_to_list(BaseName) ++ "_" ++ integer_to_list(Idx)).
-spec start_link(#{name := atom()}) ->
{ok, pid()} | {error, term()}.
start_link(#{name := Name} = _Config) ->
gen_batch_server:start_link({local, Name}, ?MODULE, {},
[{reversed_batch, true}]).
-spec sync(pool_ref(), sync_fun()) ->
ok | {error, term()}.
sync({pool, BaseName, PoolSize}, SyncFun) when is_function(SyncFun, 0) ->
Idx = rand:uniform(PoolSize) - 1,
Worker = worker_name(BaseName, Idx),
gen_batch_server:call(Worker, {sync, SyncFun}, infinity).
%%%===================================================================
%%% gen_batch_server callbacks
%%%===================================================================
init({}) ->
{ok, #state{}}.
handle_batch(Ops, State) ->
%% Ops arrive in reverse arrival order (most recent first) because
%% reversed_batch = true. On journaling filesystems (ext4, XFS),
%% syncing the most recently written file first flushes the journal,
%% which tends to make subsequent syncs near-instant since the earlier
%% files are already durable.
lists:foreach(
fun({call, From, {sync, SyncFun}}) ->
Result = try SyncFun()
catch _:Err -> {error, Err}
end,
gen:reply(From, Result);
(_) ->
ok
end, Ops),
{ok, [], State}.
terminate(_Reason, _State) ->
ok.
format_status(State) ->
State.