Permalink
Browse files

Merge branch 'anders/diameter/shared_transport/OTP-10443' into maint

* anders/diameter/shared_transport/OTP-10443:
  Use multiple connections in traffic suite
  Implement service_opt() restrict_connections
  Document service_opt() restrict_connections
  • Loading branch information...
2 parents 1262a7c + 618642c commit cedda10e06e39d7d08ab98ee342d7c8eed00edc9 Anders Svensson committed Nov 8, 2012
@@ -375,10 +375,15 @@ is meant in the sense of <c>eval([E|A])</c>.</p>
<warning>
<p>
-Beware of using fun expressions of the form <c>fun Name/Arity</c> (not
-fun Mod:Name/Arity) in situations in which the fun is not short-lived
+Beware of using fun expressions of the form <c>fun Name/Arity</c> in
+situations in which the fun is not short-lived
and code is to be upgraded at runtime since any processes retaining
-such a fun will have a reference to old code.</p>
+such a fun will have a reference to old code.
+In particular, such a value is typically inappropriate in
+configuration passed to <seealso
+marker="#start_service">start_service/2</seealso> or
+<seealso
+marker="#add_transport">add_transport/2</seealso>.</p>
</warning>
<marker id="peer_filter"/>
@@ -699,6 +704,35 @@ the application's <seealso marker="diameter_dict">dictionary</seealso>
file.</p>
</item>
+<tag><c>{restrict_connections, false
+ | node
+ | nodes
+ | [node()]
+ | diameter:evaluable()}</c></tag>
+<item>
+<p>
+Specifies the degree to which multiple transport connections to the
+same peer are accepted by the service.</p>
+
+<p>
+If type <c>[node()]</c> then a connection is rejected if another already
+exists on any of the specified nodes.
+Values of type <c>false</c>, <c>node</c>, <c>nodes</c> or
+<c>diameter:evaluable()</c> are equivalent to values <c>[]</c>,
+<c>[node()]</c>, <c>[node()|nodes()]</c> and the evaluated value,
+respectively, evaluation of each expression taking place whenever a
+new connection is to be established.
+Note that <c>false</c> allows an unlimited number of connections to be
+established with the same peer.</p>
+
+<p>
+Multiple connections are independent and governed
+by their own peer and watchdog state machines.</p>
+
+<p>
+Defaults to <c>nodes</c>.</p>
+</item>
+
<tag><c>{sequence, {H,N} | <seealso
marker="diameter#evaluable">diameter:evaluable()</seealso>}</c></tag>
<item>
@@ -879,7 +913,7 @@ reconnection attempts, as required by RFC 3539.</p>
For a listening transport, the timer specifies the time after which a
previously connected peer will be forgotten: a connection after this time is
regarded as an initial connection rather than a reestablishment,
-causing the RFC 3539 state machine to pass to state OPEN rather than
+causing the RFC 3539 state machine to pass to state OKAY rather than
REOPEN.
Note that these semantics are not goverened by the RFC and
that a listening transport's <c>reconnect_timer</c> should be greater
@@ -44,6 +44,7 @@
stop/0]).
-export_type([evaluable/0,
+ restriction/0,
sequence/0,
app_alias/0,
service_name/0,
@@ -284,11 +285,19 @@ call(SvcName, App, Message) ->
-type sequence()
:: {'Unsigned32'(), 0..32}.
+-type restriction()
+ :: false
+ | node
+ | nodes
+ | [node()]
+ | evaluable().
+
%% Options passed to start_service/2
-type service_opt()
:: capability()
| {application, [application_opt()]}
+ | {restrict_connections, restriction()}
| {sequence, sequence() | evaluable()}.
-type application_opt()
@@ -555,7 +555,8 @@ make_config(SvcName, Opts) ->
Os = split(Opts, fun opt/2, [{false, share_peers},
{false, use_shared_peers},
{false, monitor},
- {?NOMASK, sequence}]),
+ {?NOMASK, sequence},
+ {nodes, restrict_connections}]),
%% share_peers and use_shared_peers are currently undocumented.
#service{name = SvcName,
@@ -579,15 +580,33 @@ opt(monitor, P)
when is_pid(P) ->
P;
+opt(restrict_connections, T)
+ when T == node;
+ T == nodes;
+ T == [];
+ is_atom(hd(T)) ->
+ T;
+
+opt(restrict_connections = K, F) ->
+ try diameter_lib:eval(F) of %% no guarantee that it won't fail later
+ Nodes when is_list(Nodes) ->
+ F;
+ V ->
+ ?THROW({value, {K,V}})
+ catch
+ E:R ->
+ ?THROW({value, {K, E, R, ?STACK}})
+ end;
+
opt(sequence, {_,_} = T) ->
sequence(T);
-opt(sequence, F) ->
+opt(sequence = K, F) ->
try diameter_lib:eval(F) of
T -> sequence(T)
catch
E:R ->
- ?THROW({value, {sequence, E, R, ?STACK}})
+ ?THROW({value, {K, E, R, ?STACK}})
end;
opt(K, _) ->
@@ -60,6 +60,7 @@
-define(Q_KEY, q). %% transport start queue
-define(START_KEY, start). %% start of connected transport
-define(SEQUENCE_KEY, mask). %% mask for sequence numbers
+-define(RESTRICT_KEY, restrict). %% nodes for connection check
%% The default sequence mask.
-define(NOMASK, {0,32}).
@@ -126,7 +127,9 @@
%%% ---------------------------------------------------------------------------
-spec start(T, [Opt], #diameter_service{} %% from old code
- | {diameter:sequence(), #diameter_service{}})
+ | {diameter:sequence(),
+ diameter:restriction(),
+ #diameter_service{}})
-> pid()
when T :: {connect|accept, diameter:transport_ref()},
Opt :: diameter:transport_opt().
@@ -157,18 +160,19 @@ init(T) ->
gen_server:enter_loop(?MODULE, [], i(T)).
i({WPid, Type, Opts, #diameter_service{} = Svc}) -> %% from old code
- i({WPid, Type, Opts, {?NOMASK, Svc}});
+ i({WPid, Type, Opts, {?NOMASK, [node() | nodes()], Svc}});
-i({WPid, T, Opts, {Mask, #diameter_service{applications = Apps,
- capabilities = Caps}
- = Svc}}) ->
+i({WPid, T, Opts, {Mask, Nodes, #diameter_service{applications = Apps,
+ capabilities = Caps}
+ = Svc}}) ->
[] /= Apps orelse ?ERROR({no_apps, T, Opts}),
putr(?DWA_KEY, dwa(Caps)),
{M, Ref} = T,
diameter_stats:reg(Ref),
{[Ts], Rest} = proplists:split(Opts, [capabilities_cb]),
putr(?CB_KEY, {Ref, [F || {_,F} <- Ts]}),
putr(?SEQUENCE_KEY, Mask),
+ putr(?RESTRICT_KEY, Nodes),
erlang:monitor(process, WPid),
{TPid, Addrs} = start_transport(T, Rest, Svc),
#state{parent = WPid,
@@ -990,15 +994,31 @@ dpa_timer() ->
%% Register a term and ensure it's not registered elsewhere. Note that
%% two process that simultaneously register the same term may well
%% both fail to do so this isn't foolproof.
+%%
+%% Everywhere is no longer everywhere, it's where a
+%% restrict_connections service_opt() specifies.
register_everywhere(T) ->
- diameter_reg:add_new(T)
- andalso unregistered(T).
+ reg(getr(?RESTRICT_KEY), T).
+
+reg(Nodes, T) ->
+ add(lists:member(node(), Nodes), T) andalso unregistered(Nodes, T).
+
+add(true, T) ->
+ diameter_reg:add_new(T);
+add(false, T) ->
+ diameter_reg:add(T).
-unregistered(T) ->
- {ResL, _} = rpc:multicall(?MODULE, match, [{node(), T}]),
+%% unregistered
+%%
+%% Ensure that the term in question isn't registered on other nodes.
+
+unregistered(Nodes, T) ->
+ {ResL, _} = rpc:multicall(Nodes, ?MODULE, match, [{node(), T}]),
lists:all(fun(L) -> [] == L end, ResL).
+%% match/1
+
match({Node, _})
when Node == node() ->
[];
@@ -110,6 +110,9 @@
%% The default sequence mask.
-define(NOMASK, {0,32}).
+%% The default restrict_connections.
+-define(RESTRICT, nodes).
+
%% Workaround for dialyzer's lack of understanding of match specs.
-type match(T)
:: T | '_' | '$1' | '$2' | '$3' | '$4'.
@@ -126,6 +129,7 @@
monitor = false :: false | pid(), %% process to die with
options
:: [{sequence, diameter:sequence()} %% sequence mask
+ | {restrict_connections, diameter:restriction()}
| {share_peers, boolean()} %% broadcast peers to remote nodes?
| {use_shared_peers, boolean()}]}).%% use broadcasted peers?
%% shared_peers reflects the peers broadcast from remote nodes. Note
@@ -500,6 +504,10 @@ handle_call(stop, _From, S) ->
handle_call(sequence, _From, #state{options = [{_, Mask} | _]} = S) ->
{reply, Mask, S};
+%% Watchdog is asking for the nodes restriction.
+handle_call(restriction, _From, #state{options = [_,_,_,{_,R} | _]} = S) ->
+ {reply, R, S};
+
handle_call(Req, From, S) ->
unexpected(handle_call, [Req, From], S),
{reply, nok, S}.
@@ -656,7 +664,8 @@ upgrade({state, Id, Svc, Name, Svc, PT, CT, SB, UB, SD, LD, MPid}) ->
monitor = MPid,
options = [{sequence, ?NOMASK},
{share_peers, SB},
- {use_shared_peers, UB}]},
+ {use_shared_peers, UB},
+ {restrict_connections, ?RESTRICT}]},
upgrade_insert(S),
S.
@@ -867,7 +876,10 @@ cfg_acc({_Ref, Type, _Opts} = T, {S, Acc})
service_options(Opts) ->
[{sequence, proplists:get_value(sequence, Opts, ?NOMASK)},
{share_peers, get_value(share_peers, Opts)},
- {use_shared_peers, get_value(use_shared_peers, Opts)}].
+ {use_shared_peers, get_value(use_shared_peers, Opts)},
+ {restrict_connections, proplists:get_value(restrict_connections,
+ Opts,
+ ?RESTRICT)}].
%% The order of options is significant since we match against the list.
mref(false = No) ->
Oops, something went wrong.

0 comments on commit cedda10

Please sign in to comment.