| 1 | package Daemonise::Plugin::KyotoTycoon; | ||
| 2 | |||
| 3 | use Mouse::Role; | ||
| 4 | |||
| 5 | # ABSTRACT: Daemonise KyotoTycoon plugin | ||
| 6 | |||
| 7 | use Cache::KyotoTycoon; | ||
| 8 | use Try::Tiny; | ||
| 9 | use MIME::Base64 qw(encode_base64 decode_base64); | ||
| 10 | use Storable qw/nfreeze thaw/; | ||
| 11 | use Data::MessagePack; | ||
| 12 | |||
| 13 | |||
| 14 | has 'tycoon_host' => ( | ||
| 15 | is => 'rw', | ||
| 16 | isa => 'Str', | ||
| 17 | lazy => 1, | ||
| 18 | default => sub { 'localhost' }, | ||
| 19 | ); | ||
| 20 | |||
| 21 | |||
| 22 | has 'tycoon_port' => ( | ||
| 23 | is => 'rw', | ||
| 24 | isa => 'Int', | ||
| 25 | lazy => 1, | ||
| 26 | default => sub { 1978 }, | ||
| 27 | ); | ||
| 28 | |||
| 29 | |||
| 30 | has 'tycoon_db' => ( | ||
| 31 | is => 'rw', | ||
| 32 | isa => 'Str', | ||
| 33 | lazy => 1, | ||
| 34 | default => sub { 0 }, | ||
| 35 | ); | ||
| 36 | |||
| 37 | |||
| 38 | has 'tycoon_timeout' => ( | ||
| 39 | is => 'rw', | ||
| 40 | isa => 'Int', | ||
| 41 | lazy => 1, | ||
| 42 | default => sub { 5 }, | ||
| 43 | ); | ||
| 44 | |||
| 45 | |||
| 46 | has 'cache_sync_delay' => ( | ||
| 47 | is => 'rw', | ||
| 48 | isa => 'Int', | ||
| 49 | lazy => 1, | ||
| 50 | default => sub { 0 }, | ||
| 51 | ); | ||
| 52 | |||
| 53 | |||
| 54 | has 'cache_default_expire' => ( | ||
| 55 | is => 'rw', | ||
| 56 | isa => 'Int', | ||
| 57 | lazy => 1, | ||
| 58 | default => sub { 600 }, | ||
| 59 | ); | ||
| 60 | |||
| 61 | |||
| 62 | has 'tycoon' => ( | ||
| 63 | is => 'rw', | ||
| 64 | isa => 'Cache::KyotoTycoon', | ||
| 65 | required => 1, | ||
| 66 | ); | ||
| 67 | |||
| 68 | |||
| 69 | has 'mp' => ( | ||
| 70 | is => 'rw', | ||
| 71 | isa => 'Data::MessagePack', | ||
| 72 | required => 1, | ||
| 73 | ); | ||
| 74 | |||
| 75 | |||
| 76 | after 'configure' => sub { | ||
| 77 | my ($self, $reconfig) = @_; | ||
| 78 | |||
| 79 | $self->log("configuring KyotoTycoon plugin") if $self->debug; | ||
| 80 | |||
| 81 | if (ref($self->config->{kyototycoon}) eq 'HASH') { | ||
| 82 | foreach my $conf_key ('host', 'port', 'timeout') { | ||
| 83 | my $attr = "tycoon_" . $conf_key; | ||
| 84 | $self->$attr($self->config->{kyototycoon}->{$conf_key}) | ||
| 85 | if defined $self->config->{kyototycoon}->{$conf_key}; | ||
| 86 | } | ||
| 87 | $self->cache_default_expire( | ||
| 88 | $self->config->{kyototycoon}->{default_expire}) | ||
| 89 | if defined $self->config->{kyototycoon}->{default_expire}; | ||
| 90 | } | ||
| 91 | |||
| 92 | $self->tycoon( | ||
| 93 | Cache::KyotoTycoon->new( | ||
| 94 | host => $self->tycoon_host, | ||
| 95 | port => $self->tycoon_port, | ||
| 96 | timeout => $self->tycoon_timeout, | ||
| 97 | db => $self->tycoon_db, | ||
| 98 | )); | ||
| 99 | |||
| 100 | $self->mp( | ||
| 101 | Data::MessagePack->new( | ||
| 102 | prefer_integer => 0, | ||
| 103 | utf8 => 1, | ||
| 104 | )); | ||
| 105 | |||
| 106 | # don't try to lock() again when reconfiguring | ||
| 107 | return if $reconfig; | ||
| 108 | |||
| 109 | # lock cron for 24 hours | ||
| 110 | if ($self->is_cron) { | ||
| 111 | my $expire = $self->cache_default_expire; | ||
| 112 | $self->cache_default_expire(24 * 60 * 60); | ||
| 113 | die 'locking failed' unless $self->lock; | ||
| 114 | $self->cache_default_expire($expire); | ||
| 115 | |||
| 116 | $self->graph('cron.' . $self->name, 'started', 1) | ||
| 117 | if $self->can('graph'); | ||
| 118 | } | ||
| 119 | |||
| 120 | return; | ||
| 121 | }; | ||
| 122 | |||
| 123 | |||
| 124 | sub cache_get { | ||
| 125 | my ($self, $key) = @_; | ||
| 126 | |||
| 127 | my ($value, $expire) = $self->tycoon->get($key); | ||
| 128 | |||
| 129 | return unless defined $value; | ||
| 130 | |||
| 131 | my $data = try { | ||
| 132 | |||
| 133 | # first try decode using MessagePack | ||
| 134 | $self->mp->unpack($value); | ||
| 135 | } | ||
| 136 | catch { | ||
| 137 | $self->log("unpacking using MessagePack failed: $_") if $self->debug; | ||
| 138 | |||
| 139 | # then decode and thaw if it looks like a BASE64 encoding | ||
| 140 | if ($value =~ m~^[a-zA-Z0-9+/\n]+={0,2}\n$~s) { | ||
| 141 | return thaw(decode_base64($value)); | ||
| 142 | } | ||
| 143 | |||
| 144 | # finally assume it's plain text | ||
| 145 | else { | ||
| 146 | return $value; | ||
| 147 | } | ||
| 148 | }; | ||
| 149 | |||
| 150 | return ($data, $expire) if wantarray; | ||
| 151 | return $data; | ||
| 152 | } | ||
| 153 | |||
| 154 | |||
| 155 | sub cache_set { | ||
| 156 | my ($self, $key, $data, $expire) = @_; | ||
| 157 | |||
| 158 | my $scalar = $data; | ||
| 159 | |||
| 160 | # always use MessagePack because it's faster and more compact | ||
| 161 | # but leave plain scalars as is | ||
| 162 | if (ref $data) { | ||
| 163 | try { | ||
| 164 | $scalar = $self->mp->pack($data); | ||
| 165 | } | ||
| 166 | catch { | ||
| 167 | # TODO: convert/compile/do whatever necessary to not have perl | ||
| 168 | # objects in the $data hash. | ||
| 169 | # Data::MessagePack does not allow perl objects, but we appear | ||
| 170 | # to have Types::Serialiser in JSON structures | ||
| 171 | $scalar = encode_base64(nfreeze($data)); | ||
| 172 | } | ||
| 173 | } | ||
| 174 | |||
| 175 | $expire //= $self->cache_default_expire; | ||
| 176 | $self->tycoon->set($key, $scalar, $expire); | ||
| 177 | |||
| 178 | return 1; | ||
| 179 | } | ||
| 180 | |||
| 181 | |||
| 182 | sub cache_del { | ||
| 183 | my ($self, $key) = @_; | ||
| 184 | |||
| 185 | return $self->tycoon->remove($key); | ||
| 186 | } | ||
| 187 | |||
| 188 | |||
| 189 | sub lock { ## no critic (ProhibitBuiltinHomonyms) | ||
| 190 | my ($self, $key, $lock_value) = @_; | ||
| 191 | |||
| 192 | unless (ref \$key eq 'SCALAR') { | ||
| 193 | $self->log("locking failed: first argument is not of type SCALAR"); | ||
| 194 | return; | ||
| 195 | } | ||
| 196 | |||
| 197 | if (defined $lock_value) { | ||
| 198 | unless (ref \$lock_value eq 'SCALAR') { | ||
| 199 | $self->log('locking failed: second argument is not of type SCALAR'); | ||
| 200 | return; | ||
| 201 | } | ||
| 202 | } | ||
| 203 | |||
| 204 | my $lock = 'lock:' . ($key || $self->name); | ||
| 205 | |||
| 206 | # fallback to host:PID for the lock value | ||
| 207 | $lock_value //= $self->hostname . ':' . $$; | ||
| 208 | |||
| 209 | if (my $value = $self->tycoon->get($lock)) { | ||
| 210 | if ($self->_extend_lock($value, $lock_value, $lock)) { | ||
| 211 | return 1; | ||
| 212 | } | ||
| 213 | else { | ||
| 214 | sleep($self->cache_sync_delay); | ||
| 215 | if ($self->_extend_lock($value, $lock_value, $lock)) { | ||
| 216 | return 1; | ||
| 217 | } | ||
| 218 | $self->notify("$lock cannot acquire lock hold by $value") | ||
| 219 | if $self->can('notify'); | ||
| 220 | return; | ||
| 221 | } | ||
| 222 | } | ||
| 223 | else { | ||
| 224 | $self->tycoon->set($lock, $lock_value, $self->cache_default_expire); | ||
| 225 | $self->log("$lock lock acquired") if $self->debug; | ||
| 226 | return 1; | ||
| 227 | } | ||
| 228 | } | ||
| 229 | |||
| 230 | |||
| 231 | sub _extend_lock { | ||
| 232 | my ($self, $value, $lock_value, $lock) = @_; | ||
| 233 | |||
| 234 | if ($value eq $lock_value) { | ||
| 235 | $self->tycoon->replace($lock, $lock_value, $self->cache_default_expire); | ||
| 236 | $self->log("$lock locking time extended for $value") | ||
| 237 | if $self->debug; | ||
| 238 | return 1; | ||
| 239 | } | ||
| 240 | return; | ||
| 241 | } | ||
| 242 | |||
| 243 | |||
| 244 | sub unlock { | ||
| 245 | my ($self, $key, $lock_value) = @_; | ||
| 246 | |||
| 247 | unless (ref \$key eq 'SCALAR') { | ||
| 248 | $self->log("locking failed: first argument is not of type SCALAR"); | ||
| 249 | return; | ||
| 250 | } | ||
| 251 | |||
| 252 | if (defined $lock_value) { | ||
| 253 | unless (ref \$lock_value eq 'SCALAR') { | ||
| 254 | $self->log('locking failed: second argument is not of type SCALAR'); | ||
| 255 | return; | ||
| 256 | } | ||
| 257 | } | ||
| 258 | |||
| 259 | my $lock = 'lock:' . ($key || $self->name); | ||
| 260 | |||
| 261 | # fallback to PID for the lock value | ||
| 262 | $lock_value //= $self->hostname . ':' . $$; | ||
| 263 | |||
| 264 | if (my $value = $self->tycoon->get($lock)) { | ||
| 265 | if ($value eq $lock_value) { | ||
| 266 | $self->tycoon->remove($lock); | ||
| 267 | $self->log("$lock lock released") if $self->debug; | ||
| 268 | return 1; | ||
| 269 | } | ||
| 270 | else { | ||
| 271 | $self->log("$lock lock hold by $value, permission denied"); | ||
| 272 | return; | ||
| 273 | } | ||
| 274 | } | ||
| 275 | else { | ||
| 276 | $self->log("$lock lock was already released") if $self->debug; | ||
| 277 | return 1; | ||
| 278 | } | ||
| 279 | } | ||
| 280 | |||
| 281 | |||
| 282 | before 'stop' => sub { | ||
| 283 | my ($self) = @_; | ||
| 284 | |||
| 285 | $self->unlock if $self->is_cron; | ||
| 286 | |||
| 287 | return; | ||
| 288 | }; | ||
| 289 | |||
| 290 | sub DESTROY { | ||
| 291 | my ($self) = @_; | ||
| 292 | |||
| 293 | return unless ref $self; | ||
| 294 | return unless $self->{is_cron}; | ||
| 295 | |||
| 296 | # the Cache::KyotoTycoon object was already destroyed, so we need to create | ||
| 297 | # a new one with the existing config | ||
| 298 | $self->tycoon( | ||
| 299 | Cache::KyotoTycoon->new( | ||
| 300 | host => $self->tycoon_host, | ||
| 301 | port => $self->tycoon_port, | ||
| 302 | timeout => $self->tycoon_timeout, | ||
| 303 | db => $self->tycoon_db, | ||
| 304 | )); | ||
| 305 | |||
| 306 | $self->unlock; | ||
| 307 | |||
| 308 | $self->graph('cron.' . $self->name, 'stopped', 1) | ||
| 309 | if $self->can('graph'); | ||
| 310 | |||
| 311 | return; | ||
| 312 | } | ||
| 313 | |||
| 314 | 1; | ||
| 315 | |||
| 316 | __END__ |