Skip to content

Commit

Permalink
memcached-tool: Add 'limit' parameter to 'dump'.
Browse files Browse the repository at this point in the history
  • Loading branch information
calin-iorgulescu authored and dormando committed Mar 23, 2018
1 parent f34375b commit fde4f46
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 24 deletions.
69 changes: 47 additions & 22 deletions scripts/memcached-tool
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use IO::Socket::INET;
my $addr = shift;
my $mode = shift || "display";
my ($from, $to);
my $limit;

if ($mode eq "display") {
undef $mode if @ARGV;
Expand All @@ -30,7 +31,11 @@ if ($mode eq "display") {
undef $mode if $to < 6 || $to > 17;
print STDERR "ERROR: parameters out of range\n\n" unless $mode;
} elsif ($mode eq 'dump') {
;
if (@ARGV) {
$limit = shift;
undef $mode if $limit < 1;
print STDERR "ERROR: invalid limit (should be a positive number)\n\n" unless $mode;
}
} elsif ($mode eq 'stats') {
;
} elsif ($mode eq 'settings') {
Expand All @@ -45,12 +50,12 @@ undef $mode if @ARGV;

die
"Usage: memcached-tool <host[:port] | /path/to/socket> [mode]\n
memcached-tool 10.0.0.5:11211 display # shows slabs
memcached-tool 10.0.0.5:11211 # same. (default is display)
memcached-tool 10.0.0.5:11211 stats # shows general stats
memcached-tool 10.0.0.5:11211 settings # shows settings stats
memcached-tool 10.0.0.5:11211 sizes # shows sizes stats
memcached-tool 10.0.0.5:11211 dump # dumps keys and values
memcached-tool 10.0.0.5:11211 display # shows slabs
memcached-tool 10.0.0.5:11211 # same. (default is display)
memcached-tool 10.0.0.5:11211 stats # shows general stats
memcached-tool 10.0.0.5:11211 settings # shows settings stats
memcached-tool 10.0.0.5:11211 sizes # shows sizes stats
memcached-tool 10.0.0.5:11211 dump [limit] # dumps keys and values
WARNING! sizes is a development command.
As of 1.4 it is still the only command which will lock your memcached instance for some time.
Expand All @@ -60,28 +65,36 @@ or at least speed it up.
" unless $addr && $mode;


my $sock;
if ($addr =~ m:/:) {
$sock = IO::Socket::UNIX->new(
Peer => $addr,
);
}
else {
$addr .= ':11211' unless $addr =~ /:\d+$/;
sub server_connect {
my $sock;
if ($addr =~ m:/:) {
$sock = IO::Socket::UNIX->new(
Peer => $addr,
);
}
else {
$addr .= ':11211' unless $addr =~ /:\d+$/;

$sock = IO::Socket::INET->new(
PeerAddr => $addr,
Proto => 'tcp',
);
$sock = IO::Socket::INET->new(
PeerAddr => $addr,
Proto => 'tcp',
);
}
die "Couldn't connect to $addr\n" unless $sock;
return $sock;
}
die "Couldn't connect to $addr\n" unless $sock;

my $sock = server_connect();

if ($mode eq 'dump') {
print STDERR "Dumping memcache contents\n";
print STDERR "Dumping memcache contents";
print STDERR " (limiting to $limit keys)" unless !$limit;
print STDERR "\n";
print $sock "lru_crawler metadump all\r\n";
my %keyexp;
my $keycount = 0;
while (<$sock>) {
last if /^END/;
last if /^END/ or ($limit and $keycount == $limit);
# return format looks like this
# key=foo exp=2147483647 la=1521046038 cas=717111 fetch=no cls=13 size=1232
if (/^key=(\S+) exp=(-?\d+) .*/) {
Expand All @@ -91,6 +104,18 @@ if ($mode eq 'dump') {
$keyexp{$1} = $2;
}
}
$keycount++;
}

if ($limit) {
# Need to reopen the connection here to stop the metadump in
# case the key limit was reached.
#
# XXX: Once a limit on # of keys returned is introduced in
# `lru_crawler metadump`, this should be removed and the proper
# parameter passed in the query above.
close($sock);
$sock = server_connect();
}

foreach my $k (keys(%keyexp)) {
Expand Down
7 changes: 5 additions & 2 deletions scripts/memcached-tool.1
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,12 @@ Number of times the underlying slab class was unable to store a new item.
Print general-purpose statistics of the daemon. Each line contains the name of
the statistic and its value.
.TP
.B dump
.B dump [limit]
Make a partial dump of the cache written in the add statements of the
memcached protocol.
memcached protocol. If
.B limit
is given and is a strictly positive
integer, then the dump is limited to that number of items.

.SH SEE ALSO
.BR memcached (1),
Expand Down

0 comments on commit fde4f46

Please sign in to comment.