4.5 Module classy_lib

Misc. utility functions.

4.5.1 Types

wakeup_timer()
-type wakeup_timer() :: undefined |
                        {integer(), reference()}.
unix_time_s()
-type unix_time_s() :: integer().
multicall_result(Result)
-type multicall_result(Result) :: #{multicall_target()
                                        => {ok, Result} | multicall_error()}.
multicall_error()
-type multicall_error() :: {error, site_is_down} |
                           {error, {erpc, _}} |
                           wrapped_exception().
wrapped_exception()
-type wrapped_exception() :: {error, {throw, _Reason}} |
                             {error, {error, _Reason, _Stacktrace :: list()}} |
                             {error, {exit, _Reason}}.
multicall_args()
-type multicall_args() :: #{multicall_target() =>
                                {module(), atom(), list()}}.
multicall_target()
-type multicall_target() :: classy:site() |
                            {classy:site(), _Token}.
callback()
-type callback() :: mfargs() | {function(), list()}.

Not exported

mfargs()
-type mfargs() :: {module(), atom(), list()}.

4.5.2 Functions

split_node_name(Name)
-spec split_node_name(node()) -> {ok,
                                  binary(),
                                  binary()} |
                                 {error, _}.
map_deep_insert/3
-spec map_deep_insert(list(), term(), map()) -> map().
is_normal_exit(_)
-spec is_normal_exit(_) -> boolean().
ensure_list/1
-spec ensure_list(binary() | string()) -> string().

If input is a binary, convert it to a list. Keep input list as is.

sync_stop_proc/3
-spec sync_stop_proc(pid() | atom(), _ExitReason,
                     timeout()) -> ok | {error, timeout}.

Send exit signal Reason to a process and wait for the shutdown

cancel_wakeup/1
-spec cancel_wakeup(wakeup_timer()) -> undefined.

Cancel a timer created by classy_lib:wakeup_after/3. This function is idempotent.

wakeup_after/3
-spec wakeup_after(term(), integer(),
                   wakeup_timer()) -> wakeup_timer().

Set up a wakeup timer that sends message Msg to the calling process.

If the timer was previously set up to fire at a later time, this function resets it to the earlier time.

time_s()
-spec time_s() -> unix_time_s().

Return Unix time in seconds.

adjust_time_s_skew(RemoteTimeS, Val)

Adjust a local timestamp Val to the remote nodes’s clock, given the remote’s “current” time RemoteTimeS at the time of the call.

Note: it only makes sense to use this function in a call with a finite timeout. Then the error will be limited by the roundtrip timeout.

n_sites()

Return value of n_sites environment variable (with default)

table_dir()

Return value of table_dir (with default)

discovery_complete_sets()
-spec
     discovery_complete_sets() -> [classy:node_set_name(), ...].

Return value of discovery_complete_sets (with default)

quorum_sets()
-spec quorum_sets() -> [classy:node_set_name(), ...].

Return value of quorum_sets (with default)

to_cluster_sets()
-spec
     to_cluster_sets() -> [classy:node_set_name(), ...].

Return value of to_cluster_sets (with default)

rpc_timeout()

Return value rpc_timeout environment variable (with default)

fold_per_cluster/3
-spec fold_per_cluster(Fun, Acc,
                       classy:cluster_info()) -> #{classy:cluster_id() =>
                                                       Acc} when Fun ::
                                                                     fun((node(),
                                                                          classy:info(),
                                                                          Acc) -> Acc).

Perform a fold over the result of classy:info/1 with a separate accumulator per cluster.

InitialAcc parameter is used as the initial value of the accumulator for each cluster.

Sites that are not part of any cluster or don’t have a site ID are ignored.

sites_to_nodes(Sites)
-spec sites_to_nodes([classy:site()]) -> {[node()],
                                          _BadSites :: [classy:site()]}.

Translate site IDs to node names of connected nodes.

Return list of unreachable nodes in the second element of the tuple.

n_connected_peers(Peers)
-spec n_connected_peers(#{classy:site() =>
                              classy:peer_info()}) -> non_neg_integer().
multicall(SitesWithArgs, Timeout)
-spec multicall(multicall_args(),
                timeout()) -> multicall_result(term()).

Call a function on multiple sites in parallel.

Arguments:

  1. SitesWithArgs: a map from RPC target to {Module, Function, Args} tuple.

    Multicall target could be a site ID that is automatically translated to Erlang node, or a tuple containing site ID and an arbitrary term. The latter form allows to make multiple requests towards the same site. Each multicall target can call a different function.

  2. Timeout: abandon waiting for replies after the specified timeout.

Return value:

A map from RPC target to {ok, Return} tuple if the call was successful or an error tuple t:classy_lib:multicall_error/0.

Example:

classy_lib:multicall(
  #{S => {erlang, node, []} || S <- [S1, S2, SBad]},
  5_000)

returns

#{ S1 => {ok, 'node1@localhost'}
 , S2 => {ok, 'node2@localhost'}
 , SBad => {error, site_is_down}
 }
multicall(SitesWithArgs)
-spec
     multicall(multicall_args()) -> multicall_result(term()).

See classy_lib:multicall/2. Uses the default timeout.

multicast(SitesWithArgs)
-spec multicast(multicall_args()) -> ok.

Call functions on multiple sites similarly to classy_lib:multicall/2 without waiting for the results.

safe_apply_with_timeout(Callback, Timeout)
-spec safe_apply_with_timeout(callback(),
                              timeout()) -> {ok, term()} |
                                            wrapped_exception() |
                                            {error,
                                             {timeout, Info}} when Info ::
                                                                       list() |
                                                                       undefined.

Apply a function in a separate process with a timeout.

safe_apply(Module, Function, Args)
-spec safe_apply(module(), atom(), list()) -> {ok,
                                               term()} |
                                              wrapped_exception().

Apply a function while catching all exceptions and returning them as a term.

safe_apply/1
-spec safe_apply(callback()) -> {ok, term()} |
                                wrapped_exception().

See classy_lib:safe_apply/3


JavaScript license information