| 1 | package Test::ValkeyServer; | ||
| 2 | 4 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | 4 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 0 | ▶ | use Mouse; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 5 | |||
| 6 | our $VERSION = '0.01'; | ||
| 7 | |||
| 8 | use Carp; | ||
| 9 | use File::Temp; | ||
| 10 | use POSIX qw(SIGTERM WNOHANG); | ||
| 11 | use Time::HiRes qw(sleep); | ||
| 12 | use Errno (); | ||
| 13 | use Redis; | ||
| 14 | |||
| 15 | has auto_start => ( | ||
| 16 | is => 'rw', | ||
| 17 | default => 1, | ||
| 18 | ); | ||
| 19 | |||
| 20 | has [qw/pid _owner_pid/] => ( | ||
| 21 | is => 'rw', | ||
| 22 | ); | ||
| 23 | |||
| 24 | has conf => ( | ||
| 25 | is => 'rw', | ||
| 26 | isa => 'HashRef', | ||
| 27 | default => sub { {} }, | ||
| 28 | ); | ||
| 29 | |||
| 30 | has timeout => ( | ||
| 31 | is => 'rw', | ||
| 32 | default => 3, | ||
| 33 | ); | ||
| 34 | |||
| 35 | has tmpdir => ( | ||
| 36 | is => 'rw', | ||
| 37 | lazy_build => 1, | ||
| 38 | ); | ||
| 39 | |||
| 40 | has _redis => ( | ||
| 41 | is => 'rw', | ||
| 42 | isa => 'Redis', | ||
| 43 | ); | ||
| 44 | |||
| 45 | has cluster => ( | ||
| 46 | is => 'ro', | ||
| 47 | default => 0, | ||
| 48 | ); | ||
| 49 | |||
| 50 | no Mouse; | ||
| 51 | |||
| 52 | sub BUILD { | ||
| 53 | my ($self) = @_; | ||
| 54 | |||
| 55 | $self->_owner_pid($$); | ||
| 56 | |||
| 57 | my $tmpdir = $self->tmpdir; | ||
| 58 | if ($self->cluster) { | ||
| 59 | croak "cluster mode requires a port to be specified in conf" | ||
| 60 | unless defined $self->conf->{port} && $self->conf->{port} > 0; | ||
| 61 | croak "cluster mode does not support unixsocket" | ||
| 62 | if defined $self->conf->{unixsocket}; | ||
| 63 | $self->conf->{bind} = '127.0.0.1' | ||
| 64 | unless defined $self->conf->{bind}; | ||
| 65 | $self->conf->{'cluster-announce-ip'} = $self->conf->{bind} | ||
| 66 | unless defined $self->conf->{'cluster-announce-ip'}; | ||
| 67 | $self->conf->{'cluster-config-file'} = "$tmpdir/nodes.conf"; | ||
| 68 | $self->conf->{'cluster-enabled'} = 'yes'; | ||
| 69 | } | ||
| 70 | elsif (!defined $self->conf->{port} && !defined $self->conf->{unixsocket}) { | ||
| 71 | $self->conf->{unixsocket} = "$tmpdir/valkey.sock"; | ||
| 72 | $self->conf->{port} = '0'; | ||
| 73 | } | ||
| 74 | |||
| 75 | unless (defined $self->conf->{dir}) { | ||
| 76 | $self->conf->{dir} = "$tmpdir/"; | ||
| 77 | } | ||
| 78 | |||
| 79 | if ($self->conf->{loglevel} and $self->conf->{loglevel} eq 'warning') { | ||
| 80 | warn "Test::ValkeyServer does not support \"loglevel warning\", using \"notice\" instead.\n"; | ||
| 81 | $self->conf->{loglevel} = 'notice'; | ||
| 82 | } | ||
| 83 | |||
| 84 | if ($self->auto_start) { | ||
| 85 | $self->start; | ||
| 86 | } | ||
| 87 | } | ||
| 88 | |||
| 89 | sub DEMOLISH { | ||
| 90 | my ($self) = @_; | ||
| 91 | $self->stop if defined $self->pid && $$ == $self->_owner_pid; | ||
| 92 | } | ||
| 93 | |||
| 94 | sub start { | ||
| 95 | my ($self) = @_; | ||
| 96 | |||
| 97 | return if defined $self->pid; | ||
| 98 | |||
| 99 | my $tmpdir = $self->tmpdir; | ||
| 100 | open my $logfh, '>>', "$tmpdir/valkey-server.log" | ||
| 101 | or croak "failed to create log file: $tmpdir/valkey-server.log"; | ||
| 102 | |||
| 103 | my $pid = fork; | ||
| 104 | croak "fork(2) failed:$!" unless defined $pid; | ||
| 105 | |||
| 106 | if ($pid == 0) { | ||
| 107 | open STDOUT, '>&', $logfh or croak "dup(2) failed:$!"; | ||
| 108 | open STDERR, '>&', $logfh or croak "dup(2) failed:$!"; | ||
| 109 | $self->_exec; | ||
| 110 | } | ||
| 111 | close $logfh; | ||
| 112 | |||
| 113 | my $ready; | ||
| 114 | my $elapsed = 0; | ||
| 115 | $self->pid($pid); | ||
| 116 | |||
| 117 | while ($elapsed <= $self->timeout) { | ||
| 118 | if (waitpid($pid, WNOHANG) > 0) { | ||
| 119 | $self->pid(undef); | ||
| 120 | last; | ||
| 121 | } | ||
| 122 | else { | ||
| 123 | my $log = q[]; | ||
| 124 | if (open $logfh, '<', "$tmpdir/valkey-server.log") { | ||
| 125 | $log = do { local $/; <$logfh> }; | ||
| 126 | close $logfh; | ||
| 127 | } | ||
| 128 | |||
| 129 | if ( $log =~ /Ready to accept connections/ ) { | ||
| 130 | $ready = 1; | ||
| 131 | last; | ||
| 132 | } | ||
| 133 | } | ||
| 134 | |||
| 135 | sleep($elapsed += 0.1); | ||
| 136 | } | ||
| 137 | |||
| 138 | unless ($ready) { | ||
| 139 | if ($self->pid) { | ||
| 140 | $self->pid(undef); | ||
| 141 | kill SIGTERM, $pid; | ||
| 142 | while (waitpid($pid, WNOHANG) >= 0) { | ||
| 143 | } | ||
| 144 | } | ||
| 145 | |||
| 146 | croak "*** failed to launch valkey-server ***\n" . do { | ||
| 147 | my $log = q[]; | ||
| 148 | if (open $logfh, '<', "$tmpdir/valkey-server.log") { | ||
| 149 | $log = do { local $/; <$logfh> }; | ||
| 150 | close $logfh; | ||
| 151 | } | ||
| 152 | $log; | ||
| 153 | }; | ||
| 154 | } | ||
| 155 | |||
| 156 | if ($self->cluster) { | ||
| 157 | $self->_create_cluster($pid); | ||
| 158 | } | ||
| 159 | |||
| 160 | # This is sometimes needed to send commands to ValkeyServer during the stop process. | ||
| 161 | # Generally, we would like to generate it lazily and not have it as a property | ||
| 162 | # of the object. However, if you try to create the object at the stop, | ||
| 163 | # the object generation may fail, such as missing the socket file. Therefore, | ||
| 164 | # we will make the object and store it as property here. | ||
| 165 | $self->_redis( Redis->new($self->connect_info) ); | ||
| 166 | |||
| 167 | $self->pid($pid); | ||
| 168 | } | ||
| 169 | |||
| 170 | sub exec { | ||
| 171 | my ($self) = @_; | ||
| 172 | |||
| 173 | croak "cluster mode is not supported with exec(); use start() instead" | ||
| 174 | if $self->cluster; | ||
| 175 | |||
| 176 | $self->_exec; | ||
| 177 | } | ||
| 178 | |||
| 179 | sub _exec { | ||
| 180 | my ($self) = @_; | ||
| 181 | |||
| 182 | my $tmpdir = $self->tmpdir; | ||
| 183 | |||
| 184 | open my $conffh, '>', "$tmpdir/valkey.conf" or croak "cannot write conf: $!"; | ||
| 185 | print $conffh $self->_conf_string; | ||
| 186 | close $conffh; | ||
| 187 | |||
| 188 | CORE::exec 'valkey-server', "$tmpdir/valkey.conf" | ||
| 189 | or do { | ||
| 190 | if ($! == Errno::ENOENT) { | ||
| 191 | print STDERR "exec failed: no such file or directory\n"; | ||
| 192 | } | ||
| 193 | else { | ||
| 194 | print STDERR "exec failed: unexpected error: $!\n"; | ||
| 195 | } | ||
| 196 | exit($?); | ||
| 197 | }; | ||
| 198 | } | ||
| 199 | |||
| 200 | sub stop { | ||
| 201 | my ($self, $sig) = @_; | ||
| 202 | |||
| 203 | local $?; # waitpid may change this value :/ | ||
| 204 | return unless defined $self->pid; | ||
| 205 | |||
| 206 | # If the tmpdir has disappeared, clear the save config to prevent saving | ||
| 207 | # in the server terminating process. The newer Valkey will save on stop | ||
| 208 | # for robustness, but will keep blocking if the directory is missing. | ||
| 209 | # | ||
| 210 | # It is unlikely that tmpdir will disappear first, but if both the ValkeyServer | ||
| 211 | # object and the tmpdir are defined globally, it may happen because the order | ||
| 212 | # in which they are DEMOLISHed is uncertain. | ||
| 213 | if (! -f $self->tmpdir) { | ||
| 214 | $self->_redis->config_set('appendonly', 'no'); | ||
| 215 | $self->_redis->config_set('save', ''); | ||
| 216 | } | ||
| 217 | |||
| 218 | $sig ||= SIGTERM; | ||
| 219 | |||
| 220 | kill $sig, $self->pid; | ||
| 221 | while (waitpid($self->pid, WNOHANG) >= 0) { | ||
| 222 | } | ||
| 223 | |||
| 224 | $self->pid(undef); | ||
| 225 | } | ||
| 226 | |||
| 227 | sub wait_exit { | ||
| 228 | my ($self) = @_; | ||
| 229 | |||
| 230 | local $?; | ||
| 231 | |||
| 232 | my $kid; | ||
| 233 | my $pid = $self->pid; | ||
| 234 | do { | ||
| 235 | $kid = waitpid($pid, WNOHANG); | ||
| 236 | sleep(0.1); | ||
| 237 | } while $kid >= 0; | ||
| 238 | |||
| 239 | $self->pid(undef); | ||
| 240 | } | ||
| 241 | |||
| 242 | sub connect_info { | ||
| 243 | my ($self) = @_; | ||
| 244 | |||
| 245 | my $host = $self->conf->{bind} || '0.0.0.0'; | ||
| 246 | my $port = $self->conf->{port}; | ||
| 247 | my $sock = $self->conf->{unixsocket}; | ||
| 248 | |||
| 249 | if ($port && $port > 0) { | ||
| 250 | return (server => $host . ':' . $port); | ||
| 251 | } | ||
| 252 | else { | ||
| 253 | return (sock => $sock); | ||
| 254 | } | ||
| 255 | } | ||
| 256 | |||
| 257 | sub _build_tmpdir { | ||
| 258 | File::Temp->newdir( CLEANUP => 1 ); | ||
| 259 | } | ||
| 260 | |||
| 261 | sub _conf_string { | ||
| 262 | my ($self) = @_; | ||
| 263 | |||
| 264 | my $conf = q[]; | ||
| 265 | my %conf = %{ $self->conf }; | ||
| 266 | while (my ($k, $v) = each %conf) { | ||
| 267 | next unless defined $v; | ||
| 268 | $conf .= "$k $v\n"; | ||
| 269 | } | ||
| 270 | |||
| 271 | $conf; | ||
| 272 | } | ||
| 273 | |||
| 274 | sub _create_cluster { | ||
| 275 | my ($self, $pid) = @_; | ||
| 276 | |||
| 277 | my $host = $self->conf->{bind}; | ||
| 278 | my $port = $self->conf->{port}; | ||
| 279 | |||
| 280 | my $create = $self->_run_valkey_cli( | ||
| 281 | '--cluster', 'create', "$host:$port", | ||
| 282 | '--cluster-replicas', '0', '--cluster-yes', | ||
| 283 | ); | ||
| 284 | |||
| 285 | my $elapsed = 0; | ||
| 286 | my $cluster_ok; | ||
| 287 | my $info = { output => q[], timed_out => 0 }; | ||
| 288 | while ($elapsed <= $self->timeout) { | ||
| 289 | $info = $self->_run_valkey_cli('-h', $host, '-p', $port, 'cluster', 'info'); | ||
| 290 | last if $info->{timed_out}; | ||
| 291 | |||
| 292 | if ($info->{output} =~ /cluster_state:ok/) { | ||
| 293 | $cluster_ok = 1; | ||
| 294 | last; | ||
| 295 | } | ||
| 296 | sleep(0.1); | ||
| 297 | $elapsed += 0.1; | ||
| 298 | } | ||
| 299 | |||
| 300 | unless ($cluster_ok) { | ||
| 301 | kill SIGTERM, $pid; | ||
| 302 | while (waitpid($pid, WNOHANG) >= 0) { | ||
| 303 | sleep(0.1); | ||
| 304 | } | ||
| 305 | $self->pid(undef); | ||
| 306 | my @message = ('*** failed to create valkey cluster ***'); | ||
| 307 | push @message, 'valkey-cli --cluster create timed out' | ||
| 308 | if $create->{timed_out}; | ||
| 309 | push @message, 'valkey-cli cluster info timed out' | ||
| 310 | if $info->{timed_out}; | ||
| 311 | push @message, "valkey-cli output: $create->{output}" | ||
| 312 | if length $create->{output}; | ||
| 313 | push @message, "last valkey-cli cluster info output: $info->{output}" | ||
| 314 | if length $info->{output}; | ||
| 315 | croak join("\n", @message) . "\n"; | ||
| 316 | } | ||
| 317 | } | ||
| 318 | |||
| 319 | sub _run_valkey_cli { | ||
| 320 | my ($self, @args) = @_; | ||
| 321 | |||
| 322 | my $tmpdir = $self->tmpdir; | ||
| 323 | my $logfile = "$tmpdir/valkey-cli.log"; | ||
| 324 | |||
| 325 | open my $logfh, '>>', $logfile | ||
| 326 | or croak "failed to create log file: $logfile"; | ||
| 327 | seek $logfh, 0, 2 or croak "seek(2) failed: $!"; | ||
| 328 | my $offset = tell $logfh; | ||
| 329 | croak "tell(2) failed: $!" unless defined $offset; | ||
| 330 | |||
| 331 | my $child_pid = fork; | ||
| 332 | croak "fork(2) failed: $!" unless defined $child_pid; | ||
| 333 | |||
| 334 | if ($child_pid == 0) { | ||
| 335 | open STDOUT, '>&', $logfh or croak "dup(2) failed: $!"; | ||
| 336 | open STDERR, '>&', $logfh or croak "dup(2) failed: $!"; | ||
| 337 | CORE::exec 'valkey-cli', @args | ||
| 338 | or do { print STDERR "exec valkey-cli failed: $!\n"; exit 1; }; | ||
| 339 | } | ||
| 340 | |||
| 341 | close $logfh; | ||
| 342 | |||
| 343 | my $elapsed = 0; | ||
| 344 | while ($elapsed <= $self->timeout) { | ||
| 345 | if (waitpid($child_pid, WNOHANG) > 0) { | ||
| 346 | return { | ||
| 347 | output => $self->_read_log_since($logfile, $offset), | ||
| 348 | timed_out => 0, | ||
| 349 | }; | ||
| 350 | } | ||
| 351 | |||
| 352 | sleep(0.1); | ||
| 353 | $elapsed += 0.1; | ||
| 354 | } | ||
| 355 | |||
| 356 | kill SIGTERM, $child_pid; | ||
| 357 | while (waitpid($child_pid, WNOHANG) == 0) { | ||
| 358 | sleep(0.1); | ||
| 359 | } | ||
| 360 | |||
| 361 | return { | ||
| 362 | output => $self->_read_log_since($logfile, $offset), | ||
| 363 | timed_out => 1, | ||
| 364 | }; | ||
| 365 | } | ||
| 366 | |||
| 367 | sub _read_log_since { | ||
| 368 | my ($self, $logfile, $offset) = @_; | ||
| 369 | |||
| 370 | my $output = q[]; | ||
| 371 | if (open my $logfh, '<', $logfile) { | ||
| 372 | seek $logfh, $offset, 0 or croak "seek(2) failed: $!"; | ||
| 373 | $output = do { local $/; <$logfh> }; | ||
| 374 | close $logfh; | ||
| 375 | } | ||
| 376 | |||
| 377 | return $output; | ||
| 378 | } | ||
| 379 | |||
| 380 | __PACKAGE__->meta->make_immutable; | ||
| 381 | |||
| 382 | __END__ | ||