Skip to content

Improve get_cluster_slots/1 API and add a new API to get cluster nodes. #41

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 70 additions & 13 deletions src/eredis_cluster_monitor.erl
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
-export([start_link/0]).
-export([connect/1]).
-export([refresh_mapping/1]).
-export([get_state/0, get_state_version/1]).
-export([get_state/0, get_state_version/1, update_state_init_nodes/1]).
-export([get_pool_by_slot/1, get_pool_by_slot/2]).
-export([get_all_pools/0]).
-export([get_cluster_slots/0, get_cluster_nodes/0]).

%% gen_server.
-export([init/1]).
Expand All @@ -20,10 +21,10 @@
%% Type definition.
-include("eredis_cluster.hrl").
-record(state, {
init_nodes :: [#node{}],
slots :: tuple(), %% whose elements are integer indexes into slots_maps
slots_maps :: tuple(), %% whose elements are #slots_map{}
version :: integer()
init_nodes = [] :: [#node{}],
slots = {} :: tuple(), %% whose elements are integer indexes into slots_maps
slots_maps = {} :: tuple(), %% whose elements are #slots_map{}
version = 0 :: integer()
}).

%% API.
Expand All @@ -37,6 +38,10 @@ connect(InitServers) ->
refresh_mapping(Version) ->
gen_server:call(?MODULE,{reload_slots_map,Version}).

% Used for testing:
update_state_init_nodes(Nodes) ->
gen_server:call(?MODULE,{update_state_init_nodes, Nodes}).

%% =============================================================================
%% @doc Given a slot return the link (Redis instance) to the mapped
%% node.
Expand All @@ -48,6 +53,12 @@ get_state() ->
[{cluster_state, State}] = ets:lookup(?MODULE, cluster_state),
State.

update_state_init_nodes_(Nodes) ->
State = get_state(),
NewState = State#state{init_nodes = Nodes},
true = ets:insert(?MODULE, [{cluster_state, NewState}]),
NewState.

get_state_version(State) ->
State#state.version.

Expand Down Expand Up @@ -102,26 +113,70 @@ reload_slots_map(State) ->

NewState.


-spec get_cluster_slots() -> [[bitstring() | [bitstring()]]].
get_cluster_slots() ->
State = get_state(),
get_cluster_slots(State#state.init_nodes).

-spec get_cluster_slots([#node{}]) -> [[bitstring() | [bitstring()]]].
get_cluster_slots([]) ->
throw({error,cannot_connect_to_cluster});
get_cluster_slots([Node|T]) ->
get_cluster_slots(InitNodes) ->
get_cluster_slots(InitNodes, []).

get_cluster_slots([], ErrorList) ->
throw({reply, {error, {cannot_connect_to_cluster, ErrorList}}, #state{}});
get_cluster_slots([Node|T], ErrorList) ->
case safe_eredis_start_link(Node#node.address, Node#node.port) of
{ok,Connection} ->
case eredis:q(Connection, ["CLUSTER", "SLOTS"]) of
{error,<<"ERR unknown command 'CLUSTER'">>} ->
get_cluster_slots_from_single_node(Node);
{error,<<"ERR This instance has cluster support disabled">>} ->
get_cluster_slots_from_single_node(Node);
{ok, ClusterInfo} ->
case ClusterInfo of
[] when T == [] ->
get_cluster_slots_from_single_node(Node);
_ ->
eredis:stop(Connection),
ClusterInfo
end;
Reason ->
eredis:stop(Connection),
get_cluster_slots(T, [{Node, Reason} | ErrorList])
end;
Reason ->
get_cluster_slots(T, [{Node, Reason} | ErrorList])
end.

-spec get_cluster_nodes() -> [[bitstring() | [bitstring()]]].
get_cluster_nodes() ->
State = get_state(),
get_cluster_nodes(State#state.init_nodes).

-spec get_cluster_nodes([#node{}]) -> [[bitstring() | [bitstring()]]].
get_cluster_nodes(InitNodes) ->
get_cluster_nodes(InitNodes, []).

get_cluster_nodes([], ErrorList) ->
throw({reply, {error, {cannot_get_cluster_nodes, ErrorList}}, #state{}});
get_cluster_nodes([Node|T], ErrorList) ->
case safe_eredis_start_link(Node#node.address, Node#node.port) of
{ok,Connection} ->
case eredis:q(Connection, ["CLUSTER", "NODES"]) of
{error,<<"ERR unknown command 'CLUSTER'">>} ->
get_cluster_slots_from_single_node(Node);
{error,<<"ERR This instance has cluster support disabled">>} ->
get_cluster_slots_from_single_node(Node);
{ok, ClusterInfo} ->
eredis:stop(Connection),
ClusterInfo;
_ ->
Reason ->
eredis:stop(Connection),
get_cluster_slots(T)
end;
_ ->
get_cluster_slots(T)
get_cluster_nodes(T, [{Node, Reason} | ErrorList])
end;
Reason ->
get_cluster_nodes(T, [{Node, Reason} | ErrorList])
end.

-spec get_cluster_slots_from_single_node(#node{}) ->
Expand Down Expand Up @@ -225,6 +280,8 @@ handle_call({reload_slots_map,_}, _From, State) ->
{reply, ok, State};
handle_call({connect, InitServers}, _From, _State) ->
{reply, ok, connect_(InitServers)};
handle_call({update_state_init_nodes, Nodes}, _From, _State) ->
{reply, ok, update_state_init_nodes_(Nodes)};
handle_call(_Request, _From, State) ->
{reply, ignored, State}.

Expand Down
45 changes: 45 additions & 0 deletions test/eredis_cluster_tests.erl
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,51 @@ basic_test_() ->
eredis_cluster:eval(Script, ScriptHash, ["qrs"], ["evaltest"]),
?assertEqual({ok, <<"evaltest">>}, eredis_cluster:q(["get", "qrs"]))
end
},

{ "get cluster slots and nodes",
fun () ->
NodesInfo = eredis_cluster_monitor:get_cluster_nodes(),
ClusterNodesList = [CNEL || CNEL <- binary:split(NodesInfo,<<"\n">>, [global]), CNEL =/= <<>>],
NodeIdsPL =
lists:flatmap(fun(ClusterNode) ->
ClusterNodeI = binary:split(ClusterNode,<<" ">>,[global]),
[Ip, Port] = binary:split(lists:nth(2, ClusterNodeI), <<":">>,[global]),
Pool = list_to_atom(binary_to_list(Ip) ++ "#" ++ binary_to_list(Port)),
[{binary_to_list(lists:nth(1, ClusterNodeI)), Pool}]
end, ClusterNodesList),

?assertMatch([{_, '127.0.0.1#30001'}, {_, '127.0.0.1#30002'},
{_, '127.0.0.1#30003'}, {_, '127.0.0.1#30004'},
{_, '127.0.0.1#30005'}, {_, '127.0.0.1#30006'}],
lists:keysort(2, NodeIdsPL)),

% Try to get "cluster slots" and "cluster nodes" for non-existing node:
eredis_cluster_monitor:update_state_init_nodes([{node,"127.0.0.1",30016,
'127.0.0.1#30016'}]),

SlotsInfo =
try eredis_cluster_monitor:get_cluster_slots() of
SI -> SI
catch
throw:SIError -> SIError
end,

?assertMatch({reply, {error, {cannot_connect_to_cluster,
[{{node,"127.0.0.1",30016,'127.0.0.1#30016'},
{error,no_connection}}]}},_},
SlotsInfo),

NodesInfo2 = try eredis_cluster_monitor:get_cluster_nodes() of
NI -> NI
catch
throw:NIError -> NIError
end,
?assertMatch({reply,{error,{cannot_get_cluster_nodes,
[{{node,"127.0.0.1",30016,'127.0.0.1#30016'},
{error,no_connection}}]}},_},
NodesInfo2)
end
}

]
Expand Down