2013-05-10 20:54:50 +00:00
|
|
|
%% -------------------------------------------------------------------
|
|
|
|
%%
|
|
|
|
%% Copyright (c) 2013 Basho Technologies, Inc.
|
|
|
|
%%
|
|
|
|
%% This file is provided to you 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(overload).
|
|
|
|
-compile(export_all).
|
|
|
|
-include_lib("eunit/include/eunit.hrl").
|
|
|
|
|
2014-01-24 20:13:24 +00:00
|
|
|
-define(NUM_REQUESTS, 200).
|
|
|
|
-define(THRESHOLD, 100).
|
2013-05-10 20:54:50 +00:00
|
|
|
-define(BUCKET, <<"test">>).
|
|
|
|
-define(KEY, <<"hotkey">>).
|
|
|
|
|
|
|
|
confirm() ->
|
|
|
|
Config = [{riak_core, [{ring_creation_size, 8},
|
|
|
|
{enable_health_checks, false},
|
|
|
|
{vnode_overload_threshold, undefined}]},
|
|
|
|
{riak_kv, [{fsm_limit, undefined},
|
|
|
|
{storage_backend, riak_kv_memory_backend},
|
|
|
|
{anti_entropy, {off, []}}]}],
|
|
|
|
Nodes = rt:build_cluster(2, Config),
|
|
|
|
[_Node1, Node2] = Nodes,
|
|
|
|
|
|
|
|
Ring = rt:get_ring(Node2),
|
|
|
|
Hash = riak_core_util:chash_std_keyfun({?BUCKET, ?KEY}),
|
|
|
|
PL = lists:sublist(riak_core_ring:preflist(Hash, Ring), 3),
|
|
|
|
Victim = hd([Idx || {Idx, Node} <- PL,
|
|
|
|
Node =:= Node2]),
|
|
|
|
RO = riak_object:new(?BUCKET, ?KEY, <<"test">>),
|
|
|
|
|
2014-01-17 02:06:26 +00:00
|
|
|
|
2014-01-24 20:13:24 +00:00
|
|
|
ok = test_no_overload_protection(Nodes, Victim, RO),
|
2014-01-17 02:06:26 +00:00
|
|
|
ok = test_vnode_protection(Nodes, Victim, RO),
|
2014-01-24 20:13:24 +00:00
|
|
|
ok = test_fsm_protection(Nodes, Victim, RO),
|
2014-01-17 02:06:26 +00:00
|
|
|
pass.
|
|
|
|
|
|
|
|
test_no_overload_protection(Nodes, Victim, RO) ->
|
2013-05-10 20:54:50 +00:00
|
|
|
lager:info("Testing with no overload protection"),
|
|
|
|
{NumProcs, QueueLen} = run_test(Nodes, Victim, RO),
|
|
|
|
?assert(NumProcs >= (2*?NUM_REQUESTS * 0.9)),
|
|
|
|
?assert(QueueLen >= (?NUM_REQUESTS * 0.9)),
|
2014-01-17 02:06:26 +00:00
|
|
|
ok.
|
2013-05-10 20:54:50 +00:00
|
|
|
|
|
|
|
test_vnode_protection(Nodes, Victim, RO) ->
|
2014-01-24 20:13:24 +00:00
|
|
|
[Node1, Node2] = Nodes,
|
2014-01-28 23:49:57 +00:00
|
|
|
|
|
|
|
%% Setting check_interval to one ensures that process_info is called
|
|
|
|
%% to check the queue length on each vnode send.
|
|
|
|
%% This allows us to artificially raise vnode queue lengths with dummy
|
|
|
|
%% messages instead of having to go through the vnode path for coverage
|
|
|
|
%% query overload testing.
|
2013-05-10 20:54:50 +00:00
|
|
|
lager:info("Testing with vnode queue protection enabled"),
|
|
|
|
lager:info("Setting vnode overload threshold to ~b", [?THRESHOLD]),
|
2014-01-28 23:49:57 +00:00
|
|
|
lager:info("Setting vnode check interval to 1"),
|
|
|
|
Config2 = [{riak_core, [{vnode_overload_threshold, ?THRESHOLD},
|
|
|
|
{vnode_check_interval, 1}]}],
|
2013-05-10 20:54:50 +00:00
|
|
|
rt:pmap(fun(Node) ->
|
|
|
|
rt:update_app_config(Node, Config2)
|
|
|
|
end, Nodes),
|
|
|
|
{NumProcs2, QueueLen2} = run_test(Nodes, Victim, RO),
|
|
|
|
?assert(NumProcs2 =< (2*?THRESHOLD * 1.5)),
|
|
|
|
?assert(QueueLen2 =< (?THRESHOLD * 1.1)),
|
|
|
|
|
|
|
|
%% This stats check often fails. Manual testing shows stats
|
|
|
|
%% always incrementing properly. Plus, if I add code to Riak
|
|
|
|
%% to log when the dropped stat is incremented I see it called
|
|
|
|
%% the correct number of times. This looks like a stats bug
|
|
|
|
%% that is outside the scope of this test. Punting for now.
|
|
|
|
%%
|
|
|
|
%% ShouldDrop = ?NUM_REQUESTS - ?THRESHOLD,
|
|
|
|
%% ok = rt:wait_until(Node2, fun(Node) ->
|
|
|
|
%% dropped_stat(Node) =:= ShouldDrop
|
|
|
|
%% end),
|
|
|
|
|
|
|
|
CheckInterval = ?THRESHOLD div 2,
|
|
|
|
Dropped = read_until_success(Node1),
|
|
|
|
lager:info("Unnecessary dropped requests: ~b", [Dropped]),
|
|
|
|
?assert(Dropped =< CheckInterval),
|
|
|
|
|
2014-01-28 23:49:57 +00:00
|
|
|
test_cover_queries_overload(Nodes),
|
2014-01-17 02:06:26 +00:00
|
|
|
|
2014-01-24 20:13:24 +00:00
|
|
|
lager:info("Suspending vnode proxy for ~b", [Victim]),
|
|
|
|
Pid = suspend_vnode_proxy(Node2, Victim),
|
|
|
|
{NumProcs3, QueueLen3} = run_test(Nodes, Victim, RO),
|
|
|
|
Pid ! resume,
|
|
|
|
?assert(NumProcs3 >= (2*?NUM_REQUESTS * 0.9)),
|
|
|
|
?assert(QueueLen3 =< (?THRESHOLD * 1.1)),
|
2013-05-10 20:54:50 +00:00
|
|
|
ok.
|
|
|
|
|
|
|
|
test_fsm_protection(Nodes, Victim, RO) ->
|
|
|
|
lager:info("Testing with coordinator protection enabled"),
|
|
|
|
lager:info("Setting FSM limit to ~b", [?THRESHOLD]),
|
|
|
|
Config3 = [{riak_kv, [{fsm_limit, ?THRESHOLD}]}],
|
|
|
|
rt:pmap(fun(Node) ->
|
|
|
|
rt:update_app_config(Node, Config3)
|
|
|
|
end, Nodes),
|
|
|
|
{NumProcs4, QueueLen4} = run_test(Nodes, Victim, RO),
|
|
|
|
?assert(NumProcs4 =< (?THRESHOLD * 1.1)),
|
|
|
|
?assert(QueueLen4 =< (?THRESHOLD * 1.1)),
|
|
|
|
ok.
|
|
|
|
|
|
|
|
run_test(Nodes, Victim, RO) ->
|
|
|
|
[Node1, Node2] = Nodes,
|
|
|
|
rt:wait_for_cluster_service(Nodes, riak_kv),
|
|
|
|
lager:info("Sleeping for 10s to let process count stablize"),
|
|
|
|
timer:sleep(10000),
|
|
|
|
rt:load_modules_on_nodes([?MODULE], Nodes),
|
|
|
|
lager:info("Suspending vnode ~p/~p", [Node2, Victim]),
|
|
|
|
Suspended = suspend_vnode(Node2, Victim),
|
|
|
|
NumProcs1 = process_count(Node1),
|
|
|
|
lager:info("Initial process count on ~p: ~b", [Node1, NumProcs1]),
|
2014-01-17 02:06:26 +00:00
|
|
|
lager:info("Sending ~b read requests", [?NUM_REQUESTS]),
|
2013-05-10 20:54:50 +00:00
|
|
|
write_once(Node1, RO),
|
2014-01-17 02:06:26 +00:00
|
|
|
Reads = spawn_reads(Node1, ?NUM_REQUESTS),
|
2013-05-10 20:54:50 +00:00
|
|
|
timer:sleep(5000),
|
|
|
|
NumProcs2 = process_count(Node1),
|
|
|
|
QueueLen = vnode_queue_len(Node2, Victim),
|
|
|
|
|
|
|
|
lager:info("Final process count on ~p: ~b", [Node1, NumProcs2]),
|
|
|
|
lager:info("Final vnode queue length: ~b", [QueueLen]),
|
|
|
|
|
|
|
|
resume_vnode(Suspended),
|
|
|
|
rt:wait_until(Node2, fun(Node) ->
|
|
|
|
vnode_queue_len(Node, Victim) =:= 0
|
|
|
|
end),
|
2014-01-17 02:06:26 +00:00
|
|
|
kill_pids(Reads),
|
2013-05-10 20:54:50 +00:00
|
|
|
{NumProcs2 - NumProcs1, QueueLen}.
|
|
|
|
|
2014-01-28 23:49:57 +00:00
|
|
|
test_cover_queries_overload(Nodes) ->
|
2014-01-17 02:06:26 +00:00
|
|
|
[Node1, Node2] = Nodes,
|
2014-01-25 02:44:22 +00:00
|
|
|
lager:info("Suspending all kv vnodes on Node2"),
|
2014-01-28 23:49:57 +00:00
|
|
|
Pid = suspend_and_overload_all_kv_vnodes(Node2),
|
2014-01-25 02:44:22 +00:00
|
|
|
|
|
|
|
lager:info("Checking Coverage queries for overload"),
|
|
|
|
|
|
|
|
Res = list_keys(Node1),
|
2014-02-13 20:51:49 +00:00
|
|
|
?assertEqual({error, <<"mailbox_overload">>}, Res),
|
2014-01-25 02:44:22 +00:00
|
|
|
lager:info("list_keys correctly handled overload"),
|
|
|
|
|
|
|
|
Res2 = list_buckets(Node1),
|
|
|
|
?assertEqual({error, mailbox_overload}, Res2),
|
|
|
|
lager:info("list_buckets correctly handled overload"),
|
|
|
|
|
|
|
|
lager:info("Resuming all kv vnodes on Node2"),
|
2014-01-17 02:06:26 +00:00
|
|
|
resume_all_vnodes(Pid),
|
2014-01-25 02:44:22 +00:00
|
|
|
|
|
|
|
lager:info("Waiting for vnode queues to empty"),
|
2014-01-28 23:49:57 +00:00
|
|
|
wait_for_all_vnode_queues_empty(Node2).
|
2014-01-17 02:06:26 +00:00
|
|
|
|
2014-01-25 02:44:22 +00:00
|
|
|
list_keys(Node) ->
|
2014-02-13 20:51:49 +00:00
|
|
|
Pid = rt:pbc(Node),
|
|
|
|
riakc_pb_socket:list_keys(Pid, ?BUCKET, 30000).
|
2014-01-25 02:44:22 +00:00
|
|
|
|
|
|
|
list_buckets(Node) ->
|
|
|
|
{ok, C} = riak:client_connect(Node),
|
|
|
|
riak_client:list_buckets(30000, C).
|
2014-01-24 20:13:24 +00:00
|
|
|
|
2014-01-17 02:06:26 +00:00
|
|
|
wait_for_all_vnode_queues_empty(Node) ->
|
|
|
|
rt:wait_until(Node, fun(N) ->
|
|
|
|
vnode_queues_empty(N)
|
|
|
|
end).
|
|
|
|
|
|
|
|
vnode_queues_empty(Node) ->
|
|
|
|
rpc:call(Node, ?MODULE, remote_vnode_queues_empty, []).
|
|
|
|
|
|
|
|
remote_vnode_queues_empty() ->
|
|
|
|
lists:all(fun({_, _, Pid}) ->
|
|
|
|
{message_queue_len, Len} =
|
|
|
|
process_info(Pid, message_queue_len),
|
|
|
|
Len =:= 0
|
|
|
|
end, riak_core_vnode_manager:all_vnodes()).
|
|
|
|
|
2013-05-10 20:54:50 +00:00
|
|
|
write_once(Node, RO) ->
|
|
|
|
{ok, C} = riak:client_connect(Node),
|
|
|
|
C:put(RO, 3).
|
|
|
|
|
|
|
|
read_until_success(Node) ->
|
|
|
|
{ok, C} = riak:client_connect(Node),
|
|
|
|
read_until_success(C, 0).
|
|
|
|
|
|
|
|
read_until_success(C, Count) ->
|
|
|
|
case C:get(?BUCKET, ?KEY) of
|
2014-02-13 20:51:49 +00:00
|
|
|
{error, mailbox_overload} ->
|
2013-05-10 20:54:50 +00:00
|
|
|
read_until_success(C, Count+1);
|
|
|
|
_ ->
|
|
|
|
Count
|
|
|
|
end.
|
|
|
|
|
|
|
|
spawn_reads(Node, Num) ->
|
|
|
|
[spawn(fun() ->
|
|
|
|
{ok, C} = riak:client_connect(Node),
|
|
|
|
riak_client:get(?BUCKET, ?KEY, C)
|
|
|
|
end) || _ <- lists:seq(1,Num)].
|
|
|
|
|
2014-01-17 02:06:26 +00:00
|
|
|
kill_pids(Pids) ->
|
2013-05-10 20:54:50 +00:00
|
|
|
[exit(Pid, kill) || Pid <- Pids].
|
|
|
|
|
2014-01-28 23:49:57 +00:00
|
|
|
suspend_and_overload_all_kv_vnodes(Node) ->
|
|
|
|
Pid = rpc:call(Node, ?MODULE, remote_suspend_and_overload, []),
|
|
|
|
Pid ! {overload, self()},
|
|
|
|
receive overloaded ->
|
|
|
|
Pid
|
|
|
|
end.
|
2014-01-17 02:06:26 +00:00
|
|
|
|
2014-01-28 23:49:57 +00:00
|
|
|
remote_suspend_and_overload() ->
|
2014-01-17 02:06:26 +00:00
|
|
|
spawn(fun() ->
|
|
|
|
Vnodes = riak_core_vnode_manager:all_vnodes(),
|
2014-01-24 20:13:24 +00:00
|
|
|
[erlang:suspend_process(Pid, []) || {riak_kv_vnode, _, Pid}
|
|
|
|
<- Vnodes],
|
2014-01-28 23:49:57 +00:00
|
|
|
receive {overload, From} ->
|
|
|
|
io:format("Overloading vnodes ~n"),
|
|
|
|
[?MODULE:overload(Pid) || {riak_kv_vnode, _, Pid}
|
|
|
|
<- Vnodes],
|
|
|
|
From ! overloaded
|
|
|
|
end,
|
2014-01-17 02:06:26 +00:00
|
|
|
receive resume ->
|
2014-01-24 20:13:24 +00:00
|
|
|
io:format("Resuming vnodes~n"),
|
|
|
|
[erlang:resume_process(Pid) || {riak_kv_vnode, _, Pid}
|
|
|
|
<- Vnodes]
|
2014-01-17 02:06:26 +00:00
|
|
|
end
|
|
|
|
end).
|
|
|
|
|
2014-01-28 23:49:57 +00:00
|
|
|
overload(Pid) ->
|
2014-02-19 19:15:51 +00:00
|
|
|
%% The actual message doesn't matter. This one just has the least side
|
|
|
|
% effects.
|
|
|
|
[Pid ! {set_concurrency_limit, some_lock, 1} || _ <- lists:seq(1, ?NUM_REQUESTS)].
|
2014-01-28 23:49:57 +00:00
|
|
|
|
2013-05-10 20:54:50 +00:00
|
|
|
suspend_vnode(Node, Idx) ->
|
|
|
|
Pid = rpc:call(Node, ?MODULE, remote_suspend_vnode, [Idx], infinity),
|
|
|
|
Pid.
|
|
|
|
|
|
|
|
remote_suspend_vnode(Idx) ->
|
|
|
|
spawn(fun() ->
|
|
|
|
{ok, Pid} = riak_core_vnode_manager:get_vnode_pid(Idx, riak_kv_vnode),
|
|
|
|
erlang:suspend_process(Pid, []),
|
|
|
|
receive resume ->
|
|
|
|
erlang:resume_process(Pid)
|
|
|
|
end
|
|
|
|
end).
|
|
|
|
|
|
|
|
suspend_vnode_proxy(Node, Idx) ->
|
|
|
|
Pid = rpc:call(Node, ?MODULE, remote_suspend_vnode_proxy, [Idx], infinity),
|
|
|
|
Pid.
|
|
|
|
|
|
|
|
remote_suspend_vnode_proxy(Idx) ->
|
|
|
|
spawn(fun() ->
|
|
|
|
Name = riak_core_vnode_proxy:reg_name(riak_kv_vnode, Idx),
|
|
|
|
Pid = whereis(Name),
|
|
|
|
erlang:suspend_process(Pid, []),
|
|
|
|
receive resume ->
|
|
|
|
erlang:resume_process(Pid)
|
|
|
|
end
|
|
|
|
end).
|
|
|
|
|
2014-01-17 02:06:26 +00:00
|
|
|
resume_all_vnodes(Pid) ->
|
|
|
|
Pid ! resume.
|
|
|
|
|
2013-05-10 20:54:50 +00:00
|
|
|
resume_vnode(Pid) ->
|
|
|
|
Pid ! resume.
|
|
|
|
|
|
|
|
process_count(Node) ->
|
|
|
|
rpc:call(Node, erlang, system_info, [process_count]).
|
|
|
|
|
|
|
|
vnode_queue_len(Node, Idx) ->
|
|
|
|
rpc:call(Node, ?MODULE, remote_vnode_queue, [Idx]).
|
|
|
|
|
|
|
|
dropped_stat(Node) ->
|
|
|
|
Stats = rpc:call(Node, riak_core_stat, get_stats, []),
|
|
|
|
proplists:get_value(dropped_vnode_requests_total, Stats).
|
|
|
|
|
|
|
|
remote_vnode_queue(Idx) ->
|
|
|
|
{ok, Pid} = riak_core_vnode_manager:get_vnode_pid(Idx, riak_kv_vnode),
|
|
|
|
{message_queue_len, Len} = process_info(Pid, message_queue_len),
|
|
|
|
Len.
|