Skip to content

Commit f506a12

Browse files
author
Sean Cribbs
committed
Merge pull request #23 from basho/sdc-multiple-listeners
Enable multiple PB listeners.
2 parents ec2d353 + 9ba8a1a commit f506a12

File tree

3 files changed

+90
-46
lines changed

3 files changed

+90
-46
lines changed

src/riak_api_pb_listener.erl

Lines changed: 71 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
2929
terminate/2, code_change/3]).
3030
-export([sock_opts/0, new_connection/2]).
31-
-export([get_port/0, get_ip/0]).
31+
-export([get_listeners/0]).
3232
-record(state, {portnum}).
3333

3434
%% @doc Starts the PB listener
@@ -88,38 +88,80 @@ new_connection(Socket, State) ->
8888
ok = riak_api_pb_server:set_socket(Pid, Socket),
8989
{ok, State}.
9090

91+
get_listeners() ->
92+
DefaultListener = case {get_ip(), get_port()} of
93+
{undefined, _} -> [];
94+
{_, undefined} -> [];
95+
{IP, Port} -> [{IP, Port}]
96+
end,
97+
Listeners = app_helper:get_env(riak_api, pb, []) ++ DefaultListener,
98+
[ {I, P} || {I, P} <- Listeners ].
99+
91100
%% @private
92101
get_port() ->
93-
Envs = [{riak_api, pb_port},
94-
{riak_kv, pb_port}],
95-
case app_helper:try_envs(Envs) of
96-
{riak_api, pb_port, Port} ->
97-
Port;
98-
{riak_kv, pb_port, Port} ->
99-
lager:warning("The config riak_kv/pb_port has been"
100-
" deprecated and will be removed. Use"
101-
" riak_api/pb_port in the future."),
102-
Port;
103-
_ ->
104-
lager:warning("The config riak_api/pb_port is missing,"
105-
" PB connections will be disabled."),
106-
undefined
102+
case app_helper:get_env(riak_api, pb_port) of
103+
undefined ->
104+
undefined;
105+
Port ->
106+
lager:warning("The config riak_api/pb_port has been"
107+
" deprecated and will be removed. Use"
108+
" riak_api/pb (IP/Port pairs) in the future."),
109+
Port
107110
end.
108111

109112
%% @private
110113
get_ip() ->
111-
Envs = [{riak_api, pb_ip},
112-
{riak_kv, pb_ip}],
113-
case app_helper:try_envs(Envs) of
114-
{riak_api, pb_ip, IP} ->
115-
IP;
116-
{riak_kv, pb_ip, IP} ->
117-
lager:warning("The config riak_kv/pb_ip has been"
118-
" deprecated and will be removed. Use"
119-
" riak_api/pb_ip in the future."),
120-
IP;
121-
_ ->
122-
lager:warning("The config riak_api/pb_ip is missing,"
123-
" PB connections will be disabled."),
124-
undefined
114+
case app_helper:get_env(riak_api, pb_ip) of
115+
undefined ->
116+
undefined;
117+
IP ->
118+
lager:warning("The config riak_api/pb_ip has been"
119+
" deprecated and will be removed. Use"
120+
" riak_api/pb (IP/Port pairs) in the future."),
121+
IP
125122
end.
123+
124+
-ifdef(TEST).
125+
-include_lib("eunit/include/eunit.hrl").
126+
-compile(export_all).
127+
128+
listeners_test_() ->
129+
{foreach,
130+
fun() ->
131+
application:load(riak_api),
132+
app_helper:get_env(riak_api, pb, [{"127.0.0.1", 8087}])
133+
end,
134+
fun(OldListeners) ->
135+
application:set_env(riak_api, pb, OldListeners),
136+
application:unset_env(riak_api, pb_ip),
137+
application:unset_env(riak_api, pb_port)
138+
end,
139+
[
140+
{"old config keys get upgraded",
141+
fun() ->
142+
application:unset_env(riak_api, pb),
143+
application:set_env(riak_api, pb_ip, "127.0.0.1"),
144+
application:set_env(riak_api, pb_port, 10887),
145+
?assertEqual([{"127.0.0.1", 10887}], get_listeners())
146+
end},
147+
{"missing old IP config key disables listener",
148+
fun() ->
149+
application:unset_env(riak_api, pb),
150+
%% application:set_env(riak_api, pb_ip, "127.0.0.1"),
151+
application:set_env(riak_api, pb_port, 10887),
152+
?assertEqual([], get_listeners())
153+
end},
154+
{"missing old Port config key disables listener",
155+
fun() ->
156+
application:unset_env(riak_api, pb),
157+
application:set_env(riak_api, pb_ip, "127.0.0.1"),
158+
%% application:set_env(riak_api, pb_port, 10887),
159+
?assertEqual([], get_listeners())
160+
end},
161+
{"bad configs are ignored",
162+
fun() ->
163+
application:set_env(riak_api, pb, [{"0.0.0.0", 8087}, badjuju]),
164+
?assertEqual([{"0.0.0.0", 8087}], get_listeners())
165+
end}]}.
166+
167+
-endif.

src/riak_api_sup.erl

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@
3131

3232
-define(CHILD(I, Type), {I, {I, start_link, []}, permanent, 5000, Type, [I]}).
3333
-define(CHILD(I, Type, Args), {I, {I, start_link, Args}, permanent, 5000, Type, [I]}).
34-
34+
-define(LNAME(IP, Port), lists:flatten(io_lib:format("~p:~p", [IP, Port]))).
35+
-define(LISTENER(IP, Port), {?LNAME(IP, Port),
36+
{riak_api_pb_listener, start_link, [IP, Port]},
37+
permanent, 5000, worker, [riak_api_pb_listener]}).
3538
%% @doc Starts the supervisor.
3639
-spec start_link() -> {ok, pid()} | {error, term()}.
3740
start_link() ->
@@ -44,15 +47,18 @@ start_link() ->
4447
MaxT :: pos_integer(),
4548
ChildSpec :: supervisor:child_spec().
4649
init([]) ->
47-
Port = riak_api_pb_listener:get_port(),
48-
IP = riak_api_pb_listener:get_ip(),
49-
IsPbConfigured = (Port /= undefined) andalso (IP /= undefined),
50+
Listeners = riak_api_pb_listener:get_listeners(),
5051
Helper = ?CHILD(riak_api_pb_registration_helper, worker),
5152
Registrar = ?CHILD(riak_api_pb_registrar, worker),
52-
NetworkProcesses = if IsPbConfigured ->
53-
[?CHILD(riak_api_pb_sup, supervisor),
54-
?CHILD(riak_api_pb_listener, worker, [IP, Port])];
53+
NetworkProcesses = if Listeners /= [] ->
54+
[?CHILD(riak_api_pb_sup, supervisor)] ++
55+
listener_specs(Listeners);
5556
true ->
57+
lager:info("No PB listeners were configured,"
58+
" PB connections will be disabled."),
5659
[]
5760
end,
5861
{ok, {{one_for_one, 10, 10}, [Helper, Registrar|NetworkProcesses]}}.
62+
63+
listener_specs(Pairs) ->
64+
[ ?LISTENER(IP, Port) || {IP, Port} <- Pairs ].

test/pb_service_test.erl

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -97,23 +97,20 @@ setup() ->
9797

9898
application:set_env(riak_core, handoff_port, 0),
9999

100-
OldHost = app_helper:get_env(riak_api, pb_ip, "127.0.0.1"),
101-
OldPort = app_helper:get_env(riak_api, pb_port, 8087),
102-
application:set_env(riak_api, pb_ip, "127.0.0.1"),
103-
application:set_env(riak_api, pb_port, 32767),
100+
OldListeners = app_helper:get_env(riak_api, pb, [{"127.0.0.1", 8087}]),
101+
application:set_env(riak_api, pb, [{"127.0.0.1", 32767}]),
104102

105103
[ application:start(A) || A <- Deps ],
106104
riak_core:wait_for_application(riak_api),
107105
wait_for_port(),
108106
riak_api_pb_service:register(?MODULE, ?MSGMIN, ?MSGMAX),
109107
riak_api_pb_service:register(?MODULE, 111),
110-
{OldHost, OldPort, Deps}.
108+
{OldListeners, Deps}.
111109

112-
cleanup({H, P, Deps}) ->
110+
cleanup({L, Deps}) ->
113111
[ application:stop(A) || A <- lists:reverse(Deps), not is_otp_base_app(A) ],
114112
wait_for_application_shutdown(riak_api),
115-
application:set_env(riak_api, pb_ip, H),
116-
application:set_env(riak_api, pb_port, P),
113+
application:set_env(riak_api, pb, L),
117114
ok.
118115

119116
request_multi(Payloads) when is_list(Payloads) ->
@@ -169,8 +166,7 @@ new_connection() ->
169166
new_connection([{packet,4}, {header, 1}]).
170167

171168
new_connection(Options) ->
172-
Host = app_helper:get_env(riak_api, pb_ip),
173-
Port = app_helper:get_env(riak_api, pb_port),
169+
{Host, Port} = hd(app_helper:get_env(riak_api, pb)),
174170
gen_tcp:connect(Host, Port, [binary, {active, false},{nodelay, true}|Options]).
175171

176172
simple_test_() ->

0 commit comments

Comments
 (0)