-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ensure rexi_buffer metric includes the internal bufferd messages
Previously, the rexi_buffer metric reported just the mailbox size of the buffers. However rexi buffers are special as they themselves act as explicit message queues, and current length of those explicit buffers wasn't reflected anywhere in metrics. So, improve the metric usefulness and reflect both the explicit and the implicit queue lengths. There was already an existent `gen_buffered_count` gen_server call, however since the buffers could potentially be in the hotpath, avoid using and instead use a persistent term + a counter scheme similar how we do for all couch_stats metrics. In addition, noticed that rexi had no tests at all. It is of course, battle tested in production, but since we made changes to it, added some tests to cover the changed bits.
- Loading branch information
Showing
4 changed files
with
284 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
% Licensed under the Apache License, Version 2.0 (the "License"); you may not | ||
% use this file except in compliance with the License. You may obtain a copy of | ||
% the License at | ||
% | ||
% http://www.apache.org/licenses/LICENSE-2.0 | ||
% | ||
% Unless required by applicable law or agreed to in writing, software | ||
% distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
% WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
% License for the specific language governing permissions and limitations under | ||
% the License. | ||
|
||
-module(rexi_buffer_tests). | ||
|
||
-include_lib("couch/include/couch_eunit.hrl"). | ||
|
||
rexi_buffer_test_() -> | ||
{ | ||
foreach, | ||
fun setup/0, | ||
fun teardown/1, | ||
[ | ||
?TDEF_FE(t_send), | ||
?TDEF_FE(t_get_buffered_count), | ||
?TDEF_FE(t_buffer_erase), | ||
?TDEF_FE(t_terminate_clears_persistent_term) | ||
] | ||
}. | ||
|
||
setup() -> | ||
Module = atom_to_binary(?MODULE), | ||
RandSuffix = binary:encode_hex(rand:bytes(4)), | ||
ServerId = binary_to_atom(<<Module/binary, "_", RandSuffix/binary>>), | ||
{ok, Pid} = rexi_buffer:start_link(ServerId), | ||
unlink(Pid), | ||
{ServerId, Pid}. | ||
|
||
teardown({_ServerId, Pid}) -> | ||
case is_process_alive(Pid) of | ||
true -> test_util:stop_sync(Pid); | ||
false -> ok | ||
end. | ||
|
||
t_send({ServerId, Pid}) -> | ||
?assert(is_process_alive(Pid)), | ||
?assertEqual(Pid, whereis(ServerId)), | ||
{DestPid, DestRef} = spawn_monitor(fun() -> | ||
receive | ||
Msg -> exit({got, Msg}) | ||
end | ||
end), | ||
gen_server:cast(ServerId, {deliver, DestPid, potato}), | ||
ReceivedVal = | ||
receive | ||
{'DOWN', DestRef, process, DestPid, Res} -> Res | ||
end, | ||
?assertEqual({got, potato}, ReceivedVal). | ||
|
||
t_get_buffered_count({ServerId, _}) -> | ||
NonExistentDest = {foo, '[email protected]'}, | ||
?assertEqual(0, rexi_buffer:get_buffered_count('nonexistent_server_id')), | ||
?assertEqual(0, rexi_buffer:get_buffered_count(ServerId)), | ||
% Set a fake sender to make the buffer block | ||
sys:replace_state(ServerId, fun(OldSt) -> setelement(4, OldSt, {foo, bar}) end), | ||
gen_server:cast(ServerId, {deliver, NonExistentDest, potato}), | ||
test_util:wait(fun() -> | ||
case rexi_buffer:get_buffered_count(ServerId) of | ||
0 -> wait; | ||
N when is_integer(N), N > 0 -> ok | ||
end | ||
end), | ||
?assertEqual(1, rexi_buffer:get_buffered_count(ServerId)), | ||
gen_server:cast(ServerId, {deliver, NonExistentDest, tomato}), | ||
gen_server:cast(ServerId, {deliver, NonExistentDest, cabbage}), | ||
test_util:wait(fun() -> | ||
case rexi_buffer:get_buffered_count(ServerId) of | ||
N when is_integer(N), N =< 2 -> wait; | ||
N when is_integer(N), N > 2 -> ok | ||
end | ||
end), | ||
?assertEqual(3, rexi_buffer:get_buffered_count(ServerId)), | ||
% Unblock sender | ||
sys:replace_state(ServerId, fun(OldSt) -> setelement(4, OldSt, nil) end), | ||
gen_server:cast(ServerId, {deliver, NonExistentDest, cucumber}), | ||
test_util:wait(fun() -> | ||
case rexi_buffer:get_buffered_count(ServerId) of | ||
N when is_integer(N), N > 0 -> wait; | ||
0 -> ok | ||
end | ||
end), | ||
?assertEqual(ok, rexi_buffer:erase_buffer(ServerId)), | ||
?assertEqual(0, rexi_buffer:get_buffered_count(ServerId)). | ||
|
||
t_buffer_erase({ServerId, _}) -> | ||
NonExistentDest = {foo, '[email protected]'}, | ||
?assertEqual(0, rexi_buffer:get_buffered_count('nonexistent_server_id')), | ||
?assertEqual(0, rexi_buffer:get_buffered_count(ServerId)), | ||
% Set a fake sender to make the buffer block | ||
sys:replace_state(ServerId, fun(OldSt) -> setelement(4, OldSt, {foo, bar}) end), | ||
gen_server:cast(ServerId, {deliver, NonExistentDest, potato}), | ||
test_util:wait(fun() -> | ||
case rexi_buffer:get_buffered_count(ServerId) of | ||
0 -> wait; | ||
N when is_integer(N), N > 0 -> ok | ||
end | ||
end), | ||
?assertEqual(1, rexi_buffer:get_buffered_count(ServerId)), | ||
?assertEqual(ok, rexi_buffer:erase_buffer(ServerId)), | ||
?assertEqual(0, rexi_buffer:get_buffered_count(ServerId)). | ||
|
||
t_terminate_clears_persistent_term({ServerId, Pid}) -> | ||
?assertNotEqual(undefined, persistent_term:get({rexi_buffer, counter, ServerId}, undefined)), | ||
?assertEqual(ok, gen_server:stop(Pid, shutdown, infinity)), | ||
?assertEqual(undefined, persistent_term:get({rexi_buffer, counter, ServerId}, undefined)). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
% Licensed under the Apache License, Version 2.0 (the "License"); you may not | ||
% use this file except in compliance with the License. You may obtain a copy of | ||
% the License at | ||
% | ||
% http://www.apache.org/licenses/LICENSE-2.0 | ||
% | ||
% Unless required by applicable law or agreed to in writing, software | ||
% distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
% WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
% License for the specific language governing permissions and limitations under | ||
% the License. | ||
|
||
-module(rexi_tests). | ||
|
||
-export([ | ||
rpc_test_fun/1 | ||
]). | ||
|
||
-include_lib("couch/include/couch_eunit.hrl"). | ||
|
||
rexi_buffer_test_() -> | ||
{ | ||
foreach, | ||
fun setup/0, | ||
fun teardown/1, | ||
[ | ||
?TDEF_FE(t_cast), | ||
?TDEF_FE(t_sync_cast), | ||
?TDEF_FE(t_kill), | ||
?TDEF_FE(t_cast_error), | ||
?TDEF_FE(t_metrics), | ||
?TDEF_FE(t_ping) | ||
] | ||
}. | ||
|
||
setup() -> | ||
test_util:start_couch([rexi]). | ||
|
||
teardown(Ctx) -> | ||
test_util:stop_couch(Ctx). | ||
|
||
rpc_test_fun({sleep, MSec}) -> | ||
rexi:reply({sleeping, self()}), | ||
timer:sleep(MSec); | ||
rpc_test_fun({error, Error}) -> | ||
error(Error); | ||
rpc_test_fun(ping) -> | ||
rexi:ping(); | ||
rpc_test_fun(Arg) -> | ||
rexi:reply({Arg, get()}). | ||
|
||
t_cast(_) -> | ||
?assertMatch({RexiServer, node42} when is_atom(RexiServer), rexi_utils:server_pid(node42)), | ||
put(nonce, yup), | ||
Ref = rexi:cast(node(), {?MODULE, rpc_test_fun, [potato]}), | ||
{Res, Dict} = | ||
receive | ||
{Ref, {R, D}} -> {R, maps:from_list(D)} | ||
end, | ||
?assertEqual(potato, Res), | ||
?assertMatch( | ||
#{ | ||
nonce := yup, | ||
'$initial_call' := {?MODULE, rpc_test_fun, 1}, | ||
rexi_from := {_Pid, _Ref} | ||
}, | ||
Dict | ||
). | ||
|
||
t_sync_cast(_) -> | ||
?assertMatch({RexiServer, node42} when is_atom(RexiServer), rexi_utils:server_pid(node42)), | ||
put(nonce, yup), | ||
Ref = rexi:cast(node(), self(), {?MODULE, rpc_test_fun, [potato]}, [sync]), | ||
{Res, Dict} = | ||
receive | ||
{Ref, {R, D}} -> {R, maps:from_list(D)} | ||
end, | ||
?assertEqual(potato, Res), | ||
?assertMatch( | ||
#{ | ||
nonce := yup, | ||
'$initial_call' := {?MODULE, rpc_test_fun, 1}, | ||
rexi_from := {_Pid, _Ref} | ||
}, | ||
Dict | ||
). | ||
|
||
t_cast_error(_) -> | ||
?assertMatch({RexiServer, node42} when is_atom(RexiServer), rexi_utils:server_pid(node42)), | ||
Ref = rexi:cast(node(), self(), {?MODULE, rpc_test_fun, [{error, tomato}]}, []), | ||
Res = | ||
receive | ||
{Ref, RexiExit} -> RexiExit | ||
end, | ||
?assertMatch({rexi_EXIT, {tomato, [{?MODULE, rpc_test_fun, 1, _} | _]}}, Res). | ||
|
||
t_kill(_) -> | ||
Ref = rexi:cast(node(), {?MODULE, rpc_test_fun, [{sleep, 10000}]}), | ||
WorkerPid = | ||
receive | ||
{Ref, {sleeping, Pid}} -> Pid | ||
end, | ||
?assert(is_process_alive(WorkerPid)), | ||
Mon = monitor(process, WorkerPid), | ||
rexi:kill_all([{node(), Ref}]), | ||
KillReason = | ||
receive | ||
{'DOWN', Mon, _, _, Res} -> Res | ||
end, | ||
?assertEqual(killed, KillReason). | ||
|
||
t_metrics(_) -> | ||
?assertEqual(0, rexi:aggregate_buffer_queue_len()), | ||
?assertEqual(0, rexi:aggregate_server_queue_len()). | ||
|
||
t_ping(_) -> | ||
rexi:cast(node(), {?MODULE, rpc_test_fun, [ping]}), | ||
Res = | ||
receive | ||
{rexi, Ping} -> Ping | ||
end, | ||
?assertEqual('$rexi_ping', Res). |