From e899c3b811c759de17419c534e9f926da1005c65 Mon Sep 17 00:00:00 2001 From: "Zaiming (Stone) Shi" Date: Wed, 11 Oct 2023 16:20:12 +0200 Subject: [PATCH] refactor: change default rpc module from 'gen_rpc' to 'rpc' --- src/mria_config.erl | 21 +++++++++++---------- src/mria_lib.erl | 14 +++++++------- src/mria_rlog.erl | 2 +- src/mria_rlog.hrl | 8 ++++++++ src/mria_rlog_replica.erl | 4 ++-- 5 files changed, 29 insertions(+), 20 deletions(-) diff --git a/src/mria_config.erl b/src/mria_config.erl index 1703aa6..04cf588 100644 --- a/src/mria_config.erl +++ b/src/mria_config.erl @@ -57,6 +57,7 @@ , rocksdb_backend_available/0 ]). +-include("mria_rlog.hrl"). -include_lib("snabbkaffe/include/snabbkaffe.hrl"). -include_lib("kernel/include/logger.hrl"). @@ -119,9 +120,9 @@ whoami() -> role() end. --spec rpc_module() -> gen_rpc | rpc. +-spec rpc_module() -> ?GEN_RPC | ?ERL_RPC. rpc_module() -> - persistent_term:get(?mria(rlog_rpc_module), gen_rpc). + persistent_term:get(?mria(rlog_rpc_module), ?DEFAULT_RPC_MODULE). -spec core_rpc_retries() -> integer(). core_rpc_retries() -> @@ -179,15 +180,15 @@ dirty_shard(Shard) -> persistent_term:get(?is_dirty(Shard), false). -spec set_shard_transport(mria_rlog:shard(), mria_rlog:transport()) -> ok. -set_shard_transport(Shard, Transport) when Transport =:= gen_rpc; - Transport =:= distr -> +set_shard_transport(Shard, Transport) when Transport =:= ?TRANSPORT_GEN_RPC; + Transport =:= ?TRANSPORT_ERL_DISTR-> ok = persistent_term:put(?shard_transport(Shard), Transport); set_shard_transport(Shard, Transport) -> error({badarg, Shard, Transport}). -spec shard_transport(mria_rlog:shard()) -> mria_rlog:transport(). shard_transport(Shard) -> - Default = persistent_term:get(?mria(shard_transport), gen_rpc), + Default = persistent_term:get(?mria(shard_transport), ?DEFAULT_SHARD_TRANSPORT), persistent_term:get(?shard_transport(Shard), Default). -spec set_shard_bootstrap_batch_size(mria_rlog:shard(), non_neg_integer()) -> ok. @@ -253,12 +254,12 @@ get_extra_mnesia_diagnostic_checks() -> -spec consistency_check() -> ok. consistency_check() -> case rpc_module() of - gen_rpc -> ok; - rpc -> ok + ?GEN_RPC -> ok; + ?ERL_RPC -> ok end, - case persistent_term:get(?mria(shard_transport), gen_rpc) of - distr -> ok; - gen_rpc -> ok + case persistent_term:get(?mria(shard_transport), ?DEFAULT_SHARD_TRANSPORT) of + ?TRANSPORT_ERL_DISTR -> ok; + ?TRANSPORT_GEN_RPC -> ok end, case {backend(), role(), otp_is_compatible()} of {mnesia, replicant, _} -> diff --git a/src/mria_lib.erl b/src/mria_lib.erl index 3c33865..65f6ec8 100644 --- a/src/mria_lib.erl +++ b/src/mria_lib.erl @@ -50,8 +50,8 @@ , rpc_destination/0 ]). --include_lib("snabbkaffe/include/trace.hrl"). -include("mria_rlog.hrl"). +-include_lib("snabbkaffe/include/trace.hrl"). -include_lib("mnesia/src/mnesia.hrl"). -compile({inline, [node_from_destination/1]}). @@ -99,10 +99,10 @@ make_key(undefined) -> -spec rpc_call(rpc_destination(), module(), atom(), list()) -> term(). rpc_call(Destination, Module, Function, Args) -> Result = case mria_config:rpc_module() of - rpc -> + ?ERL_RPC -> rpc:call(node_from_destination(Destination), ?MODULE, wrap_exception, [Module, Function, Args]); - gen_rpc -> + ?GEN_RPC -> gen_rpc:call(Destination, ?MODULE, wrap_exception, [Module, Function, Args]) end, @@ -111,10 +111,10 @@ rpc_call(Destination, Module, Function, Args) -> -spec rpc_call_nothrow(rpc_destination(), module(), atom(), list()) -> term(). rpc_call_nothrow(Destination, Module, Function, Args) -> case mria_config:rpc_module() of - rpc -> + ?ERL_RPC -> rpc:call(node_from_destination(Destination), Module, Function, Args); - gen_rpc -> + ?GEN_RPC -> gen_rpc:call(Destination, Module, Function, Args) end. @@ -142,9 +142,9 @@ wrap_exception(Mod, Fun, Args) -> -spec rpc_cast(rpc_destination(), module(), atom(), list()) -> term(). rpc_cast(Destination, Module, Function, Args) -> case mria_config:rpc_module() of - rpc -> + ?ERL_RPC -> rpc:cast(node_from_destination(Destination), Module, Function, Args); - gen_rpc -> + ?GEN_RPC -> gen_rpc:cast(Destination, Module, Function, Args) end. diff --git a/src/mria_rlog.erl b/src/mria_rlog.erl index d13a78a..4c1e134 100644 --- a/src/mria_rlog.erl +++ b/src/mria_rlog.erl @@ -78,7 +78,7 @@ %% cluster! -type seqno() :: non_neg_integer(). --type transport() :: gen_rpc | distr. +-type transport() :: ?TRANSPORT_GEN_RPC | ?TRANSPORT_ERL_DISTR. -type sync_reply_to() :: #?rlog_sync{reply_to :: reference(), shard :: shard()}. diff --git a/src/mria_rlog.hrl b/src/mria_rlog.hrl index 686d4ac..f1df041 100644 --- a/src/mria_rlog.hrl +++ b/src/mria_rlog.hrl @@ -49,4 +49,12 @@ , checkpoint }). +-define(ERL_RPC, rpc). +-define(GEN_RPC, gen_rpc). +-define(DEFAULT_RPC_MODULE, ?ERL_RPC). + +-define(TRANSPORT_ERL_DISTR, distr). +-define(TRANSPORT_GEN_RPC, gen_rpc). +-define(DEFAULT_SHARD_TRANSPORT, ?TRANSPORT_ERL_DISTR). + -endif. diff --git a/src/mria_rlog_replica.erl b/src/mria_rlog_replica.erl index 06a796e..5dfdf96 100644 --- a/src/mria_rlog_replica.erl +++ b/src/mria_rlog_replica.erl @@ -159,10 +159,10 @@ format_status(Status) -> %% This function is called by the remote core node. -spec push_tlog_entry(mria_rlog:transport(), mria_rlog:shard(), mria_lib:subscriber(), mria_rlog:entry()) -> ok. -push_tlog_entry(distr, _Shard, {_Node, Pid}, TLOGEntry) -> +push_tlog_entry(?TRANSPORT_ERL_DISTR, _Shard, {_Node, Pid}, TLOGEntry) -> do_push_tlog_entry(Pid, TLOGEntry), %% Note: here Pid is remote ok; -push_tlog_entry(gen_rpc, Shard, {Node, Pid}, TLOGEntry) -> +push_tlog_entry(?TRANSPORT_GEN_RPC, Shard, {Node, Pid}, TLOGEntry) -> gen_rpc:ordered_cast({Node, Shard}, ?MODULE, do_push_tlog_entry, [Pid, TLOGEntry]), ok.