| 1 |
# We need to wait until the plugin becomes responsive. |
| 2 |
# We achieve that by using memcached command "stats" |
| 3 |
# which returns an hash containing many fields, among them |
| 4 |
# "hosts", but only if the plugin is up and running. |
| 5 |
# |
| 6 |
# Example of usage: |
| 7 |
# INSTALL PLUGIN daemon_memcached SONAME 'libmemcached.so'; |
| 8 |
# # host and port on which the memcached should operate |
| 9 |
# # ususally host is 127.0.0.1 and |
| 10 |
# # port is in -master.opt file as --loose-daemon_memcached_option |
| 11 |
# --let $memcached_address=127.0.0.1:11227 |
| 12 |
# # each attempt consists of reconnecting and trying to do 'stats' |
| 13 |
# # which may take up to 20 seconds, but actually takes more like 1s |
| 14 |
# # because determining that local port is closed or that server is |
| 15 |
# # not responding as it should is quick, and we do 1s sleep in between. |
| 16 |
# --let $memcached_check_attempts=20 |
| 17 |
# # Either you expect 'success' or 'error' |
| 18 |
# --let $memcached_expect=success |
| 19 |
# --source ../include/check_daemon_memcached.inc |
| 20 |
# |
| 21 |
# ..or even better use: |
| 22 |
# --let $memcached_address=127.0.0.1:11227 |
| 23 |
# --source ../include/load_daemon_memcached_expecting_success.inc |
| 24 |
# ..or.. |
| 25 |
# --let $memcached_address=127.0.0.1:11227 |
| 26 |
# --source ../include/load_daemon_memcached_expecting_error.inc |
| 27 |
|
| 28 |
if(!$memcached_address){ |
| 29 |
--die You need to specify $memcached_address variable |
| 30 |
} |
| 31 |
--let $memcached_expect_is_valid=0 |
| 32 |
if($memcached_expect=="error"){ |
| 33 |
--let $memcached_expect_is_valid=1 |
| 34 |
} |
| 35 |
if($memcached_expect=="success"){ |
| 36 |
--let $memcached_expect_is_valid=1 |
| 37 |
} |
| 38 |
if(!$memcached_expect_is_valid){ |
| 39 |
--die You need to specify $memcached_expect either 'success' or 'error' |
| 40 |
} |
| 41 |
if(!$memcached_check_attempts){ |
| 42 |
--die You need to specify a non-zero number of $memcached_check_attempts |
| 43 |
} |
| 44 |
# For some reason the ENV variable can not be named the same |
| 45 |
# as the regular variable even if case of letters is different, |
| 46 |
# hance we use ENV_ prefix. |
| 47 |
--let ENV_MEMCACHED_ADDRESS= $memcached_address |
| 48 |
--let ENV_MEMCACHED_CHECK_ATTEMPTS= $memcached_check_attempts |
| 49 |
--let ENV_MEMCACHED_EXPECT= $memcached_expect |
| 50 |
|
| 51 |
perl; |
| 52 |
use DBI; |
| 53 |
use Cache::Memcached; |
| 54 |
my $memd = new Cache::Memcached { |
| 55 |
'servers' => [ $ENV{'ENV_MEMCACHED_ADDRESS'} ], |
| 56 |
'connect_timeout' => 20, |
| 57 |
'select_timeout' => 20 |
| 58 |
}; |
| 59 |
my $max_wait = $ENV{'ENV_MEMCACHED_CHECK_ATTEMPTS'}; |
| 60 |
my $wait_sec = $max_wait; |
| 61 |
my $success = 0; |
| 62 |
while (1) { |
| 63 |
# The undocumented ->forget_dead_hosts() clears a global %host_dead |
| 64 |
# which is crucial, as otherwise failed attempts of ->stats() |
| 65 |
# combined with later attempt to reconnect, will mark the |
| 66 |
# server as dead for 20+rand(10) seconds. |
| 67 |
# By the way there is no connection between all occurences of |
| 68 |
# number 20 in this file. |
| 69 |
$memd->forget_dead_hosts(); |
| 70 |
my $hash = $memd->stats(); |
| 71 |
if(exists $hash->{'hosts'}){ |
| 72 |
$success = 1; |
| 73 |
last; |
| 74 |
} |
| 75 |
if ($wait_sec-->0) { |
| 76 |
# Sleep 1 second and try again |
| 77 |
sleep 1; |
| 78 |
} else { |
| 79 |
last; |
| 80 |
} |
| 81 |
} |
| 82 |
$memd->disconnect_all(); |
| 83 |
if (($ENV{'ENV_MEMCACHED_EXPECT'} eq 'success') && !$success) { |
| 84 |
die "Waited for " . $max_wait . " sec. but " . $ENV{'ENV_MEMCACHED_ADDRESS'} . " did not respond with stats\n"; |
| 85 |
} |
| 86 |
if (($ENV{'ENV_MEMCACHED_EXPECT'} eq 'error') && $success) { |
| 87 |
die "The memcached plugin at " . $ENV{'ENV_MEMCACHED_ADDRESS'} . " was supposed to fail, but it works"; |
| 88 |
} |
| 89 |
EOF |