diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/mem3_httpd.erl | 69 |
1 files changed, 14 insertions, 55 deletions
diff --git a/src/mem3_httpd.erl b/src/mem3_httpd.erl index 3609de24..958e9ef5 100644 --- a/src/mem3_httpd.erl +++ b/src/mem3_httpd.erl @@ -8,71 +8,30 @@ handle_membership_req(#httpd{method='GET', - path_parts=[<<"_membership">>]} = Req) -> + path_parts=[<<"_membership">>]} = Req) -> ClusterNodes = try mem3:nodes() catch _:_ -> {ok,[]} end, couch_httpd:send_json(Req, {[ {all_nodes, lists:sort([node()|nodes()])}, {cluster_nodes, lists:sort(ClusterNodes)} ]}); - -handle_membership_req(#httpd{method='POST', - path_parts=[<<"_membership">>]} = Req) -> - {JsonProps} = couch_httpd:json_body_obj(Req), - Method = couch_util:get_value(<<"method">>, JsonProps), - Params = couch_util:get_value(<<"params">>, JsonProps), - Id = couch_util:get_value(<<"id">>, JsonProps), - {Result, Error} = membership_dispatch(Method, Params), +handle_membership_req(#httpd{method='GET', + path_parts=[<<"_membership">>, <<"parts">>, DbName]} = Req) -> + ClusterNodes = try mem3:nodes() + catch _:_ -> {ok,[]} end, + Shards = mem3:shards(DbName), + JsonShards = json_shards(Shards, []), couch_httpd:send_json(Req, {[ - {result, Result}, - {error, Error}, - {id, Id} + {all_nodes, lists:sort([node()|nodes()])}, + {cluster_nodes, lists:sort(ClusterNodes)}, + {partitions, JsonShards} ]}). %% %% internal %% -membership_dispatch(<<"replace">>, Params) -> - OldNode = get_oldnode(Params), - NewNodeOpts = get_value_json(<<"newnode_options">>, Params, []), - PingNode = get_pingnode(Params), - send_join(replace, {OldNode, NewNodeOpts}, PingNode); -membership_dispatch(TypeBin, Params) -> - Type = list_to_atom(?b2l(TypeBin)), - NodeList = get_value_json(<<"nodes">>, Params, []), - Nodes = lists:map(fun({List}) -> node_info(List) end, NodeList), - PingNode = get_pingnode(Params), - send_join(Type, Nodes, PingNode). - -get_pingnode(Params) -> - PingNodeBin = get_value_json(<<"pingnode">>, Params, <<"nil">>), - list_to_atom(?b2l(PingNodeBin)). - -get_oldnode(Params) -> - NodeBin = get_value_json(<<"oldnode">>, Params, undefined), - NodeList = ?b2l(NodeBin), - list_to_atom(NodeList). - -%% @doc send join command to mem module -send_join(Type, Payload, PingNode) -> - case mem3:join(Type, Payload, PingNode) of - ok -> {ok, null}; - {error, Error} -> {Type, Error}; - Other -> - ?LOG_ERROR("membership dispatch error ~p", [Other]), - {Type, unknown_error} - end. - -node_info(List) -> - Order = couch_util:get_value(<<"order">>, List), - Node1 = couch_util:get_value(<<"node">>, List), - Node2 = list_to_atom(?b2l(Node1)), - Options = couch_util:get_value(<<"options">>, List), - {Order, Node2, Options}. -get_value_json(_,[], Default) -> Default; -get_value_json(Key, [JsonProp|Rest], Default) -> - case JsonProp of - {[{Key, Value}]} -> Value; - _ -> get_value_json(Key, Rest, Default) - end. +json_shards([], Acc) -> {lists:sort(Acc)}; +json_shards([#shard{node=Node, range=[B,_E]} | Rest], AccIn) -> + HexBeg = couch_util:to_hex(<<B:32/integer>>), + json_shards(Rest, [{HexBeg,[Node]}|AccIn]). |