1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
|
#!/usr/bin/env perl
use strict;
use warnings;
use Test::More;
use FindBin qw($Bin);
use lib "$Bin/lib";
use Carp qw(croak);
use MemcachedTest;
use IO::Select;
use IO::Socket qw(AF_INET SOCK_STREAM);
if (!supports_proxy()) {
plan skip_all => 'proxy not enabled';
exit 0;
}
# Set up some server sockets.
sub mock_server {
my $port = shift;
my $srv = IO::Socket->new(
Domain => AF_INET,
Type => SOCK_STREAM,
Proto => 'tcp',
LocalHost => '127.0.0.1',
LocalPort => $port,
ReusePort => 1,
Listen => 5) || die "IO::Socket: $@";
return $srv;
}
sub accept_backend {
my $srv = shift;
my $be = $srv->accept();
$be->autoflush(1);
ok(defined $be, "mock backend created");
like(<$be>, qr/version/, "received version command");
print $be "VERSION 1.0.0-mock\r\n";
return $be;
}
# Put a version command down the pipe to ensure the socket is clear.
# client version commands skip the proxy code
sub check_version {
my $ps = shift;
print $ps "version\r\n";
like(<$ps>, qr/VERSION /, "version received");
}
sub wait_reload {
my $w = shift;
like(<$w>, qr/ts=(\S+) gid=\d+ type=proxy_conf status=start/, "reload started");
like(<$w>, qr/ts=(\S+) gid=\d+ type=proxy_conf status=done/, "reload completed");
}
my @mocksrvs = ();
note "making mock servers";
for my $port (11711, 11712, 11713) {
my $srv = mock_server($port);
ok(defined $srv, "mock server created");
push(@mocksrvs, $srv);
}
# Start up a clean server.
# Since limits are per worker thread, cut the worker threads down to 1 to ease
# testing.
my $p_srv = new_memcached('-o proxy_config=./t/proxylimits.lua -t 1');
my $ps = $p_srv->sock;
$ps->autoflush(1);
my @mbe = ();
my $watcher;
subtest 'active request limit' => sub {
for my $msrv ($mocksrvs[0], $mocksrvs[1], $mocksrvs[2]) {
my $be = accept_backend($msrv);
push(@mbe, $be);
}
my $stats = mem_stats($ps, 'proxy');
isnt($stats->{active_req_limit}, 0, "active request limit is set");
# active request limit is 4, pipeline 6 requests and ensure the last two
# get junked
my $cmd = '';
for ('a', 'b', 'c', 'd', 'e', 'f') {
$cmd .= "mg /test/$_\r\n";
}
print $ps $cmd;
# Lua config only sends commands to the first backend for this test.
my $be = $mbe[0];
for (1 .. 4) {
like(<$be>, qr/^mg \/test\/\w\r\n$/, "backend received mg");
print $be "EN\r\n";
}
my $s = IO::Select->new();
$s->add($be);
my @readable = $s->can_read(0.25);
is(scalar @readable, 0, "no more pending reads on backend");
for (1 .. 4) {
is(scalar <$ps>, "EN\r\n", "received miss from backend");
}
is(scalar <$ps>, "SERVER_ERROR active request limit reached\r\n", "got error back");
is(scalar <$ps>, "SERVER_ERROR active request limit reached\r\n", "got two limit errors");
# Test turning the limit back off.
$watcher = $p_srv->new_sock;
print $watcher "watch proxyevents\n";
is(<$watcher>, "OK\r\n", "watcher enabled");
$p_srv->reload();
wait_reload($watcher);
$stats = mem_stats($ps, 'proxy');
is($stats->{active_req_limit}, 0, "active request limit unset");
$cmd = '';
for ('a', 'b', 'c', 'd', 'e', 'f') {
$cmd .= "mg /test/$_\r\n";
}
print $ps $cmd;
for (1 .. 6) {
like(<$be>, qr/^mg \/test\/\w\r\n$/, "backend received mg");
print $be "EN\r\n";
}
for (1 .. 6) {
is(scalar <$ps>, "EN\r\n", "received miss from backend");
}
};
subtest 'buffer memory limit' => sub {
# Test the buffer memory limiter.
# - limit per worker will be 1/t global limit
$p_srv->reload();
wait_reload($watcher);
# Get a secondary client to trample limit.
my $sps = $p_srv->new_sock;
my $stats = mem_stats($ps, 'proxy');
isnt($stats->{buffer_memory_limit}, 0, "buf mem limit is set");
# - test SET commands with values, but nothing being read on backend
my $data = 'x' x 30000;
my $cmd = "ms foo 30000 T30\r\n" . $data . "\r\n";
print $ps $cmd;
my $be = $mbe[0];
my $s = IO::Select->new;
$s->add($be);
# Wait until the backend has the request queued, then send the second one.
my @readable = $s->can_read(1);
print $sps $cmd;
my $res;
is(scalar <$be>, "ms foo 30000 T30\r\n", "received first ms");
$res = scalar <$be>;
print $be "HD\r\n";
# The second request should have been caught by the memory limiter
is(scalar <$sps>, "SERVER_ERROR out of memory\r\n", "got server error");
# FIXME: The original response cannot succeed because we cannot allocate
# enough memory to read the response from the backend.
# This is conveniently testing both paths right here but I would prefer
# something better.
# TODO: need to see if it's possible to surface an OOM from the backend
# handler, but that requires more rewiring.
is(scalar <$ps>, "SERVER_ERROR backend failure\r\n", "first request succeeded");
# Backend gets killed from a read OOM, so we need to re-establish.
$be = $mbe[0] = accept_backend($mocksrvs[0]);
like(<$watcher>, qr/error=outofmemory/, "OOM log line");
# Memory limits won't drop until the garbage collectors run, which
# requires a bit more push, so instead we raise the limits here so we can
# retry from the pre-existing connections to test swallow mode.
$p_srv->reload();
wait_reload($watcher);
# Test sending another request down both pipes to ensure they still work.
$cmd = "ms foo 2 T30\r\nhi\r\n";
print $ps $cmd;
is(scalar <$be>, "ms foo 2 T30\r\n", "client works after oom");
is(scalar <$be>, "hi\r\n", "client works after oom");
print $be "HD\r\n";
is(scalar <$ps>, "HD\r\n", "client received resp after oom");
print $sps $cmd;
is(scalar <$be>, "ms foo 2 T30\r\n", "client works after oom");
is(scalar <$be>, "hi\r\n", "client works after oom");
print $be "HD\r\n";
is(scalar <$sps>, "HD\r\n", "client received resp after oom");
# - test disabling the limiter
$stats = mem_stats($ps, 'proxy');
isnt($stats->{buffer_memory_limit}, 0, "buf mem limit is set");
$p_srv->reload();
wait_reload($watcher);
$stats = mem_stats($ps, 'proxy');
is($stats->{buffer_memory_limit}, 0, "buf mem limit is not set");
# - test GET commands but don't read back, large backend values
# extended testing:
# - create errors while holding the buffers?
};
check_version($ps);
subtest 'memory used counter' => sub {
my $be = $mbe[0];
my $stats = mem_stats($ps, 'proxy');
my $used = $stats->{buffer_memory_used};
# check a very high number at first. The next batch of requests should
# kick the GC enough times to free memory from the previous set of tests.
# The rest should be much lower.
cmp_ok($used, '<', 1000000, "pre: buffer memory usage not inflated: $used");
my $cmd = "get foo\r\n";
for (1 .. 100) {
print $ps $cmd;
{
my $res = scalar <$be>;
print $be "VALUE foo 0 2\r\nhi\r\n";
print $be "END\r\n";
}
my $res = scalar <$ps>;
$res = scalar <$ps>;
$res = scalar <$ps>;
}
$stats = mem_stats($ps, 'proxy');
$used = $stats->{buffer_memory_used};
cmp_ok($used, '<', 1000, "mid: buffer memory usage not inflated: $used");
$cmd = "get foo foo foo foo\r\n";
for (1 .. 50) {
print $ps $cmd;
for (1 .. 4) {
my $res = scalar <$be>;
print $be "VALUE foo 0 2\r\nhi\r\n";
print $be "END\r\n";
}
for (1 .. 4) {
my $res = scalar <$ps>;
$res = scalar <$ps>;
}
# END
my $res = scalar <$ps>;
}
$stats = mem_stats($ps, 'proxy');
$used = $stats->{buffer_memory_used};
cmp_ok($used, '<', 1000, "multiget: buffer memory usage not inflated: $used");
$cmd = "get foo\r\n";
for (1 .. 200) {
print $ps $cmd;
{
my $res = scalar <$be>;
print $be "VALUE foo 0 2\r\nhi\r\n";
print $be "END\r\n";
}
my $res = scalar <$ps>;
$res = scalar <$ps>;
$res = scalar <$ps>;
}
$stats = mem_stats($ps, 'proxy');
$used = $stats->{buffer_memory_used};
cmp_ok($used, '<', 1000, "post: buffer memory usage not inflated: $used");
};
# TODO:
# check reqlimit/bwlimit counters
done_testing();
|