summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorILYA Khlopotov <iilyak@apache.org>2018-08-29 13:47:43 -0700
committerILYA Khlopotov <iilyak@apache.org>2018-08-30 04:21:45 -0700
commitdff0b9703b211e80b3c1fd93b9a5688dbdf6685d (patch)
tree81f9b21be7a177cb0e556c19e0b8e305dfe42284
parent50be214f0c8d7d0d7e632eac59caed9604c0b703 (diff)
downloadcouchdb-dff0b9703b211e80b3c1fd93b9a5688dbdf6685d.tar.gz
Implement convinience `mem3:ping/2` function
Sometimes in operations it is helpful to re-establish connection between erlang nodes. Usually it is achieved by calling `net_adm:ping/1`. However the `ping` function provided by OTP uses `infinity` timeout. Which causes indefinite hang in some cases. This PR adds convinience function to be used instead of `net_adm:ping/1`.
-rw-r--r--src/mem3/src/mem3.erl24
1 files changed, 24 insertions, 0 deletions
diff --git a/src/mem3/src/mem3.erl b/src/mem3/src/mem3.erl
index 7033faf4b..de633006a 100644
--- a/src/mem3/src/mem3.erl
+++ b/src/mem3/src/mem3.erl
@@ -21,6 +21,7 @@
-export([live_shards/2]).
-export([belongs/2, owner/3]).
-export([get_placement/1]).
+-export([ping/1, ping/2]).
%% For mem3 use only.
-export([name/1, node/1, range/1, engine/1]).
@@ -28,6 +29,8 @@
-include_lib("mem3/include/mem3.hrl").
-include_lib("couch/include/couch_db.hrl").
+-define(PING_TIMEOUT_IN_MS, 60000).
+
start() ->
application:start(mem3).
@@ -331,6 +334,27 @@ engine(Opts) when is_list(Opts) ->
[]
end.
+%% Check whether a node is up or down
+%% side effect: set up a connection to Node if there not yet is one.
+
+-spec ping(Node :: atom()) -> pong | pang.
+
+ping(Node) ->
+ ping(Node, ?PING_TIMEOUT_IN_MS).
+
+-spec ping(Node :: atom(), Timeout :: pos_integer()) -> pong | pang.
+
+ping(Node, Timeout) when is_atom(Node) ->
+ %% The implementation of the function is copied from
+ %% lib/kernel/src/net_adm.erl with addition of a Timeout
+ case catch gen:call({net_kernel, Node},
+ '$gen_call', {is_auth, node()}, Timeout) of
+ {ok, yes} -> pong;
+ _ ->
+ erlang:disconnect_node(Node),
+ pang
+ end.
+
-ifdef(TEST).
-include_lib("eunit/include/eunit.hrl").