| 1 | package DBIO::MySQL::NotifyTable; | ||||||||||||||||||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: LISTEN/NOTIFY emulated over a notify table for MySQL and MariaDB, without I/O | ||||||||||||||||||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 30 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 30 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 30 | our $VERSION = '0.900007'; | |||||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 30 | ▶ | use base 'Class::Accessor::Grouped'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||||||
| 11 | 30 | ▶ | use Carp 'croak'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 12 | 30 | ▶ | use Scalar::Util qw( looks_like_number refaddr ); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||||||
| 14 | 30 | my %DEFAULTS = ( | |||||||||||||||||||||||||||||||||||||||||||||
| 15 | table => 'dbio_notify', | ||||||||||||||||||||||||||||||||||||||||||||||
| 16 | poll_interval => 1, | ||||||||||||||||||||||||||||||||||||||||||||||
| 17 | lookback => 60, | ||||||||||||||||||||||||||||||||||||||||||||||
| 18 | retention => 3600, | ||||||||||||||||||||||||||||||||||||||||||||||
| 19 | cleanup_limit => 1000, | ||||||||||||||||||||||||||||||||||||||||||||||
| 20 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||||||
| 22 | # The widest channel the channel column holds, in characters. | ||||||||||||||||||||||||||||||||||||||||||||||
| 23 | 30 | my $MAX_CHANNEL = 255; | |||||||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||||||
| 25 | # The widest payload the TEXT payload column holds, in bytes. | ||||||||||||||||||||||||||||||||||||||||||||||
| 26 | 30 | my $MAX_PAYLOAD = 65535; | |||||||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||||||
| 28 | # A listener cleans up at most this often, in seconds of polling -- more often | ||||||||||||||||||||||||||||||||||||||||||||||
| 29 | # only while each run fills its whole batch. | ||||||||||||||||||||||||||||||||||||||||||||||
| 30 | 30 | my $CLEANUP_PERIOD = 60; | |||||||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||||||
| 32 | # The server-side format of a cutoff: fixed width, so string order is time | ||||||||||||||||||||||||||||||||||||||||||||||
| 33 | # order. | ||||||||||||||||||||||||||||||||||||||||||||||
| 34 | 30 | my $CUTOFF_FORMAT = '%Y-%m-%d %H:%i:%s.%f'; | |||||||||||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||||||||||||
| 36 | 30 | __PACKAGE__->mk_group_ro_accessors(simple => sort keys %DEFAULTS); | |||||||||||||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||||||||||
| 40 | 406 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=17 SCAR=28.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 41 | 406 | my ($class, %args) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||||||||||||||
| 43 | 406 | my @unknown = grep { !exists $DEFAULTS{$_} } sort keys %args; | |||||||||||||||||||||||||||||||||||||||||||||
| 44 | 406 | ▶ | croak "unknown notify table setting(s): @unknown" if @unknown; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||||||||||
| 46 | 405 | ▶ | my %cfg = map { $_ => $args{$_} // $DEFAULTS{$_} } keys %DEFAULTS; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$args{$_} // $DEFAULTS{$_}
MC/DC0%0/2
$args{$_} // $DEFAULTS{$_}
$args{$_}
$DEFAULTS{$_}
Truth table66%2/3
A // B
A= $args{$_}B= $DEFAULTS{$_}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||||||||||
| 48 | croak sprintf( | ||||||||||||||||||||||||||||||||||||||||||||||
| 49 | "notify table name '%s' is not a plain identifier -- it must match " | ||||||||||||||||||||||||||||||||||||||||||||||
| 50 | . '[A-Za-z_][A-Za-z0-9_]* and be at most 64 characters long', | ||||||||||||||||||||||||||||||||||||||||||||||
| 51 | ref $cfg{table} ? 'a reference' : $cfg{table}, | ||||||||||||||||||||||||||||||||||||||||||||||
| 52 | ) unless !ref $cfg{table} | ||||||||||||||||||||||||||||||||||||||||||||||
| 53 | && $cfg{table} =~ /\A[A-Za-z_][A-Za-z0-9_]*\z/ | ||||||||||||||||||||||||||||||||||||||||||||||
| 54 | 405 | ▶ | && length $cfg{table} <= 64; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition100%6/6
not ref $cfg{"table"} and $cfg{"table"} =~ /\A[A-Za-z_][A-Za-z0-9_]*\z/ and length $cfg{"table"} <= 64
not ref $cfg{"table"} and $cfg{"table"} =~ /\A[A-Za-z_][A-Za-z0-9_]*\z/
MC/DC100%3/3
not ref $cfg{"table"} and $cfg{"table"} =~ /\A[A-Za-z_][A-Za-z0-9_]*\z/ and length $cfg{"table"} <= 64
not ref $cfg{"table"}
$cfg{"table"} =~ /\A[A-Za-z_][A-Za-z0-9_]*\z/
length $cfg{"table"} <= 64
Truth table100%4/4
not(A) and B and C
A= not ref $cfg{"table"}B= $cfg{"table"} =~ /\A[A-Za-z_][A-Za-z0-9_]*\z/C= length $cfg{"table"} <= 64
| |||||||||||||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||||||||||||
| 56 | 395 | for my $key (qw( poll_interval lookback )) { | |||||||||||||||||||||||||||||||||||||||||||||
| 57 | croak "notify $key must be a positive number of seconds, got '$cfg{$key}'" | ||||||||||||||||||||||||||||||||||||||||||||||
| 58 | 789 | ▶ | unless looks_like_number($cfg{$key}) && $cfg{$key} > 0; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
&looks_like_number($cfg{$key}) and $cfg{$key} > 0
MC/DC100%2/2
&looks_like_number($cfg{$key}) and $cfg{$key} > 0
&looks_like_number($cfg{$key})
$cfg{$key} > 0
Truth table100%3/3
A and B
A= &looks_like_number($cfg{$key})B= $cfg{$key} > 0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 60 | croak "notify lookback must be at least one microsecond, got '$cfg{lookback}'" | ||||||||||||||||||||||||||||||||||||||||||||||
| 61 | 392 | ▶ | if _micro($cfg{lookback}) < 1; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||||||||||
| 63 | croak "notify retention must be 0 (no cleanup) or a positive number of seconds, " | ||||||||||||||||||||||||||||||||||||||||||||||
| 64 | . "got '$cfg{retention}'" | ||||||||||||||||||||||||||||||||||||||||||||||
| 65 | 392 | ▶ | unless looks_like_number($cfg{retention}) && $cfg{retention} >= 0; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
&looks_like_number($cfg{"retention"}) and $cfg{"retention"} >= 0
MC/DC50%1/2
&looks_like_number($cfg{"retention"}) and $cfg{"retention"} >= 0
&looks_like_number($cfg{"retention"})
$cfg{"retention"} >= 0
Truth table66%2/3
A and B
A= &looks_like_number($cfg{"retention"})B= $cfg{"retention"} >= 0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 66 | croak sprintf( | ||||||||||||||||||||||||||||||||||||||||||||||
| 67 | 'notify retention (%s s) must be greater than lookback plus poll interval ' | ||||||||||||||||||||||||||||||||||||||||||||||
| 68 | . '(%s s): a notification that commits late must still be there when the ' | ||||||||||||||||||||||||||||||||||||||||||||||
| 69 | . 'next poll looks for it', | ||||||||||||||||||||||||||||||||||||||||||||||
| 70 | $cfg{retention}, $cfg{lookback} + $cfg{poll_interval}, | ||||||||||||||||||||||||||||||||||||||||||||||
| 71 | 391 | ▶ | ) if $cfg{retention} > 0 && $cfg{retention} <= $cfg{lookback} + $cfg{poll_interval}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$cfg{"retention"} > 0 and $cfg{"retention"} <= $cfg{"lookback"} + $cfg{"poll_interval"}
MC/DC100%2/2
$cfg{"retention"} > 0 and $cfg{"retention"} <= $cfg{"lookback"} + $cfg{"poll_interval"}
$cfg{"retention"} > 0
$cfg{"retention"} <= $cfg{"lookback"} + $cfg{"poll_interval"}
Truth table100%3/3
A and B
A= $cfg{"retention"} > 0B= $cfg{"retention"} <= $cfg{"lookback"} + $cfg{"poll_interval"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||||||||||||||
| 73 | croak "notify cleanup_limit must be a positive integer, got '$cfg{cleanup_limit}'" | ||||||||||||||||||||||||||||||||||||||||||||||
| 74 | 390 | ▶ | unless defined $cfg{cleanup_limit} && $cfg{cleanup_limit} =~ /\A[1-9][0-9]*\z/; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $cfg{"cleanup_limit"} and $cfg{"cleanup_limit"} =~ /\A[1-9][0-9]*\z/
MC/DC50%1/2
defined $cfg{"cleanup_limit"} and $cfg{"cleanup_limit"} =~ /\A[1-9][0-9]*\z/
defined $cfg{"cleanup_limit"}
$cfg{"cleanup_limit"} =~ /\A[1-9][0-9]*\z/
Truth table66%2/3
A and B
A= defined $cfg{"cleanup_limit"}B= $cfg{"cleanup_limit"} =~ /\A[1-9][0-9]*\z/
| |||||||||||||||||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||||||||||||||
| 76 | 389 | return bless { | |||||||||||||||||||||||||||||||||||||||||||||
| 77 | %cfg, | ||||||||||||||||||||||||||||||||||||||||||||||
| 78 | channels => {}, # channel => { mark, cutoff, seen } | ||||||||||||||||||||||||||||||||||||||||||||||
| 79 | cleanup_in => 1, # polls until the next cleanup: due after the first | ||||||||||||||||||||||||||||||||||||||||||||||
| 80 | }, $class; | ||||||||||||||||||||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||||||||||||||||
| 84 | 28 | ▶ | sub defaults { return %DEFAULTS } | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
defaults: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||||||||||
| 87 | 280 | ▶ | sub quoted_table { return '`' . $_[0]->{table} . '`' } | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
quoted_table: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||||||||||
| 90 | 99 | ▶ | sub check_channel { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
check_channel: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 91 | 99 | my ($self, $channel) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 92 | 99 | ▶ | croak 'a notify channel name must be a non-empty string' | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
defined $channel and not ref $channel and length $channel
defined $channel and not ref $channel
MC/DC100%3/3
defined $channel and not ref $channel and length $channel
defined $channel
not ref $channel
length $channel
Truth table100%4/4
A and not(B) and C
A= defined $channelB= not ref $channelC= length $channel
| |||||||||||||||||||||||||||||||||||||||||||||||
| 93 | unless defined $channel && !ref $channel && length $channel; | ||||||||||||||||||||||||||||||||||||||||||||||
| 94 | 96 | ▶ | croak sprintf( | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 95 | "notify channel name '%s...' is %d characters long; the notify table holds " | ||||||||||||||||||||||||||||||||||||||||||||||
| 96 | . "at most $MAX_CHANNEL -- refused rather than truncated", | ||||||||||||||||||||||||||||||||||||||||||||||
| 97 | substr($channel, 0, 20), length $channel, | ||||||||||||||||||||||||||||||||||||||||||||||
| 98 | ) if length $channel > $MAX_CHANNEL; | ||||||||||||||||||||||||||||||||||||||||||||||
| 99 | 94 | return $channel; | |||||||||||||||||||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||||||||||||||
| 103 | 5 | ▶ | sub create_table_sql { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
create_table_sql: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 104 | 5 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 105 | 5 | my $t = $self->quoted_table; | |||||||||||||||||||||||||||||||||||||||||||||
| 106 | 5 | return <<"SQL"; | |||||||||||||||||||||||||||||||||||||||||||||
| 107 | CREATE TABLE IF NOT EXISTS $t ( | ||||||||||||||||||||||||||||||||||||||||||||||
| 108 | `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, | ||||||||||||||||||||||||||||||||||||||||||||||
| 109 | `channel` VARCHAR($MAX_CHANNEL) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL, | ||||||||||||||||||||||||||||||||||||||||||||||
| 110 | `payload` TEXT CHARACTER SET utf8mb4 NOT NULL, | ||||||||||||||||||||||||||||||||||||||||||||||
| 111 | `pid` BIGINT UNSIGNED NOT NULL, | ||||||||||||||||||||||||||||||||||||||||||||||
| 112 | `created_at` DATETIME(6) NOT NULL, | ||||||||||||||||||||||||||||||||||||||||||||||
| 113 | PRIMARY KEY (`id`), | ||||||||||||||||||||||||||||||||||||||||||||||
| 114 | KEY `channel_id` (`channel`, `id`), | ||||||||||||||||||||||||||||||||||||||||||||||
| 115 | KEY `channel_created_at` (`channel`, `created_at`), | ||||||||||||||||||||||||||||||||||||||||||||||
| 116 | KEY `created_at` (`created_at`) | ||||||||||||||||||||||||||||||||||||||||||||||
| 117 | ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 | ||||||||||||||||||||||||||||||||||||||||||||||
| 118 | SQL | ||||||||||||||||||||||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||||||||||||||||
| 122 | 1 | ▶ | sub drop_table_sql { return 'DROP TABLE IF EXISTS ' . $_[0]->quoted_table } | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
drop_table_sql: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||||||||||||||||
| 125 | 14 | ▶ | sub notify_sql { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
notify_sql: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 126 | 14 | my ($self, $channel, $payload) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 127 | 14 | $self->check_channel($channel); | |||||||||||||||||||||||||||||||||||||||||||||
| 128 | 10 | ▶ | $payload = defined $payload ? "$payload" : ''; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||||||||||||||
| 130 | 10 | my $bytes = $payload; | |||||||||||||||||||||||||||||||||||||||||||||
| 131 | 10 | utf8::upgrade($bytes); | |||||||||||||||||||||||||||||||||||||||||||||
| 132 | 10 | utf8::encode($bytes); | |||||||||||||||||||||||||||||||||||||||||||||
| 133 | 10 | ▶ | croak sprintf( | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 134 | 'notify payload is %d bytes long in UTF-8; the notify table holds at most ' | ||||||||||||||||||||||||||||||||||||||||||||||
| 135 | . "$MAX_PAYLOAD -- refused rather than truncated", | ||||||||||||||||||||||||||||||||||||||||||||||
| 136 | length $bytes, | ||||||||||||||||||||||||||||||||||||||||||||||
| 137 | ) if length $bytes > $MAX_PAYLOAD; | ||||||||||||||||||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||||||||||||||||
| 139 | return ( | ||||||||||||||||||||||||||||||||||||||||||||||
| 140 | 8 | 'INSERT INTO ' . $self->quoted_table | |||||||||||||||||||||||||||||||||||||||||||||
| 141 | . ' (`channel`, `payload`, `pid`, `created_at`)' | ||||||||||||||||||||||||||||||||||||||||||||||
| 142 | . ' VALUES (?, ?, CONNECTION_ID(), UTC_TIMESTAMP(6))', | ||||||||||||||||||||||||||||||||||||||||||||||
| 143 | [ $channel, $payload ], | ||||||||||||||||||||||||||||||||||||||||||||||
| 144 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||||||||||||||||
| 148 | 85 | ▶ | sub subscribe_sql { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
subscribe_sql: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 149 | 85 | my ($self, $channel) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 150 | 85 | $self->check_channel($channel); | |||||||||||||||||||||||||||||||||||||||||||||
| 151 | 84 | my $t = $self->quoted_table; | |||||||||||||||||||||||||||||||||||||||||||||
| 152 | 84 | my $us = _micro($self->{lookback}); | |||||||||||||||||||||||||||||||||||||||||||||
| 153 | return ( | ||||||||||||||||||||||||||||||||||||||||||||||
| 154 | 84 | "SELECT `id`, NULL, NULL, NULL, NULL FROM $t" | |||||||||||||||||||||||||||||||||||||||||||||
| 155 | . " WHERE `channel` = ? AND `created_at` >= UTC_TIMESTAMP(6) - INTERVAL $us MICROSECOND" | ||||||||||||||||||||||||||||||||||||||||||||||
| 156 | . " UNION ALL SELECT (SELECT COALESCE(MAX(`id`), 0) FROM $t WHERE `channel` = ?)," | ||||||||||||||||||||||||||||||||||||||||||||||
| 157 | . ' NULL, NULL, NULL, ' . _cutoff_sql($us), | ||||||||||||||||||||||||||||||||||||||||||||||
| 158 | [ $channel, $channel ], | ||||||||||||||||||||||||||||||||||||||||||||||
| 159 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||||||||||||||||
| 163 | 83 | ▶ | sub subscribed { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
subscribed: called CC=5 SCAR=16.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 164 | 83 | my ($self, $channel, $rows) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 165 | 83 | ▶ | my ($meta) = grep { ref $_ eq 'ARRAY' && defined $_->[4] } @{ $rows || [] } | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition60%3/5
ref $_ eq 'ARRAY' && defined $_->[4]
$rows || []
MC/DC50%1/2
ref $_ eq 'ARRAY' && defined $_->[4]
ref $_ eq 'ARRAY'
defined $_->[4]
MC/DC0%0/1
$rows || []
$rows
Truth table66%2/3
A && B
A= ref $_ eq 'ARRAY'B= defined $_->[4]
Truth table50%1/2
A
A= $rows
| |||||||||||||||||||||||||||||||||||||||||||||||
| 166 | or croak "the subscribe statement for channel '$channel' returned no mark row"; | ||||||||||||||||||||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||||||||||||||||||||
| 168 | $self->{channels}{$channel} = { | ||||||||||||||||||||||||||||||||||||||||||||||
| 169 | mark => defined $meta->[0] ? "$meta->[0]" : '0', | ||||||||||||||||||||||||||||||||||||||||||||||
| 170 | cutoff => "$meta->[4]", | ||||||||||||||||||||||||||||||||||||||||||||||
| 171 | 82 | ▶ | seen => { map { ("$_->[0]" => 1) } grep { !defined $_->[4] } @$rows }, | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 172 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 173 | 82 | return; | |||||||||||||||||||||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||||||||||||||||||||
| 177 | 5 | ▶ | sub unsubscribe { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
unsubscribe: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 178 | 5 | my ($self, $channel) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 179 | 5 | ▶ | delete $self->{channels}{$channel} if defined $channel; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 180 | 5 | return; | |||||||||||||||||||||||||||||||||||||||||||||
| 181 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||||||||||||||||||||
| 184 | 146 | ▶ | sub channels { return sort keys %{ $_[0]->{channels} } } | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
channels: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||||||||||||||||||||
| 187 | 145 | ▶ | sub poll_sql { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
poll_sql: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 188 | 145 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 189 | 145 | ▶ | my @channels = $self->channels or return; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||||||||||||||||||||
| 191 | 144 | my (@by_id, @by_time, @bind_id, @bind_time, %token); | |||||||||||||||||||||||||||||||||||||||||||||
| 192 | 144 | for my $channel (@channels) { | |||||||||||||||||||||||||||||||||||||||||||||
| 193 | 151 | my $state = $self->{channels}{$channel}; | |||||||||||||||||||||||||||||||||||||||||||||
| 194 | 151 | push @by_id, '(`channel` = ? AND `id` > CAST(? AS UNSIGNED))'; | |||||||||||||||||||||||||||||||||||||||||||||
| 195 | 151 | push @bind_id, $channel, $state->{mark}; | |||||||||||||||||||||||||||||||||||||||||||||
| 196 | 151 | push @by_time, '(`channel` = ? AND `created_at` >= CAST(? AS DATETIME(6)))'; | |||||||||||||||||||||||||||||||||||||||||||||
| 197 | 151 | push @bind_time, $channel, $state->{cutoff}; | |||||||||||||||||||||||||||||||||||||||||||||
| 198 | 151 | $token{$channel} = $state; | |||||||||||||||||||||||||||||||||||||||||||||
| 199 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||||||||||||||||||||
| 201 | 144 | my $t = $self->quoted_table; | |||||||||||||||||||||||||||||||||||||||||||||
| 202 | 144 | my $cols = '`id`, `channel`, `payload`, `pid`, NULL'; | |||||||||||||||||||||||||||||||||||||||||||||
| 203 | return ( | ||||||||||||||||||||||||||||||||||||||||||||||
| 204 | "SELECT $cols FROM $t WHERE " . join(' OR ', @by_id) | ||||||||||||||||||||||||||||||||||||||||||||||
| 205 | . " UNION ALL SELECT $cols FROM $t WHERE " . join(' OR ', @by_time) | ||||||||||||||||||||||||||||||||||||||||||||||
| 206 | 144 | . ' UNION ALL SELECT NULL, NULL, NULL, NULL, ' . _cutoff_sql(_micro($self->{lookback})), | |||||||||||||||||||||||||||||||||||||||||||||
| 207 | [ @bind_id, @bind_time ], | ||||||||||||||||||||||||||||||||||||||||||||||
| 208 | \%token, | ||||||||||||||||||||||||||||||||||||||||||||||
| 209 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 210 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||||||||||||||||||||
| 213 | 137 | ▶ | sub polled { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
polled: called CC=17 SCAR=28.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 214 | 137 | my ($self, $token, $rows) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 215 | 137 | ▶ | my ($cutoff) = map { "$_->[4]" } grep { ref $_ eq 'ARRAY' && defined $_->[4] } @{ $rows || [] } | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition60%3/5
ref $_ eq 'ARRAY' && defined $_->[4]
$rows || []
MC/DC50%1/2
ref $_ eq 'ARRAY' && defined $_->[4]
ref $_ eq 'ARRAY'
defined $_->[4]
MC/DC0%0/1
$rows || []
$rows
Truth table66%2/3
A && B
A= ref $_ eq 'ARRAY'B= defined $_->[4]
Truth table50%1/2
A
A= $rows
| |||||||||||||||||||||||||||||||||||||||||||||||
| 216 | or croak 'the poll statement returned no cutoff row'; | ||||||||||||||||||||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||||||||||||||||||||
| 218 | 136 | my %got; # channel => id => row | |||||||||||||||||||||||||||||||||||||||||||||
| 219 | 136 | for my $row (grep { !defined $_->[4] } @$rows) { | |||||||||||||||||||||||||||||||||||||||||||||
| 220 | 199 | my ($id, $channel) = @$row; | |||||||||||||||||||||||||||||||||||||||||||||
| 221 | 199 | ▶ | next unless defined $channel && exists $token->{$channel}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $channel and exists $token->{$channel}
MC/DC50%1/2
defined $channel and exists $token->{$channel}
defined $channel
exists $token->{$channel}
Truth table66%2/3
A and B
A= defined $channelB= exists $token->{$channel}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 222 | 197 | ▶ | $got{$channel}{"$id"} //= $row; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$got{$channel}{"$id"} //= $row
MC/DC0%0/2
$got{$channel}{"$id"} //= $row
$got{$channel}{"$id"}
$row
Truth table66%2/3
A //= B
A= $got{$channel}{"$id"}B= $row
| |||||||||||||||||||||||||||||||||||||||||||||||
| 223 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||||||||||||||||||||
| 225 | 136 | my @new; | |||||||||||||||||||||||||||||||||||||||||||||
| 226 | 136 | for my $channel (keys %$token) { | |||||||||||||||||||||||||||||||||||||||||||||
| 227 | 142 | my $state = $token->{$channel}; | |||||||||||||||||||||||||||||||||||||||||||||
| 228 | 142 | my $current = $self->{channels}{$channel}; | |||||||||||||||||||||||||||||||||||||||||||||
| 229 | 142 | ▶ | next unless $current && refaddr($current) == refaddr($state); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$current and builtin::refaddr($current) == builtin::refaddr($state)
MC/DC100%2/2
$current and builtin::refaddr($current) == builtin::refaddr($state)
$current
builtin::refaddr($current) == builtin::refaddr($state)
Truth table100%3/3
A and B
A= $currentB= builtin::refaddr($current) == builtin::refaddr($state)
| |||||||||||||||||||||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||||||||||||||||||||
| 231 | 140 | ▶ | my $rows_now = $got{$channel} || {}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$got{$channel} || {}
MC/DC100%1/1
$got{$channel} || {}
$got{$channel}
Truth table100%2/2
A
A= $got{$channel}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 232 | 140 | for my $id (keys %$rows_now) { | |||||||||||||||||||||||||||||||||||||||||||||
| 233 | 163 | ▶ | push @new, $rows_now->{$id} unless $state->{seen}{$id}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 234 | 163 | ▶ | $state->{mark} = $id if _id_cmp($id, $state->{mark}) > 0; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 235 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 236 | 140 | $state->{seen} = { map { ($_ => 1) } keys %$rows_now }; | |||||||||||||||||||||||||||||||||||||||||||||
| 237 | 140 | ▶ | $state->{cutoff} = $cutoff if $cutoff gt $state->{cutoff}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 238 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||||||||||||||||||||
| 240 | 136 | $self->{cleanup_in}--; | |||||||||||||||||||||||||||||||||||||||||||||
| 241 | return [ | ||||||||||||||||||||||||||||||||||||||||||||||
| 242 | 35 | ▶ | map { [ "$_->[1]", $_->[2] // '', $_->[3] ] } | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$_->[2] // ""
MC/DC0%0/1
$_->[2] // ""
$_->[2]
Truth table50%1/2
A
A= $_->[2]
| |||||||||||||||||||||||||||||||||||||||||||||||
| 243 | 136 | sort { _id_cmp("$a->[0]", "$b->[0]") } @new | |||||||||||||||||||||||||||||||||||||||||||||
| 244 | ]; | ||||||||||||||||||||||||||||||||||||||||||||||
| 245 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||||||||||||||||||||
| 248 | 76 | ▶ | sub cleanup_sql { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
cleanup_sql: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 249 | 76 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 250 | 76 | ▶ | return unless $self->{retention} > 0 && $self->{cleanup_in} <= 0; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$self->{"retention"} > 0 and $self->{"cleanup_in"} <= 0
MC/DC100%2/2
$self->{"retention"} > 0 and $self->{"cleanup_in"} <= 0
$self->{"retention"} > 0
$self->{"cleanup_in"} <= 0
Truth table100%3/3
A and B
A= $self->{"retention"} > 0B= $self->{"cleanup_in"} <= 0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 251 | 31 | my $us = _micro($self->{retention}); | |||||||||||||||||||||||||||||||||||||||||||||
| 252 | return ( | ||||||||||||||||||||||||||||||||||||||||||||||
| 253 | 31 | 'DELETE FROM ' . $self->quoted_table | |||||||||||||||||||||||||||||||||||||||||||||
| 254 | . " WHERE `created_at` < UTC_TIMESTAMP(6) - INTERVAL $us MICROSECOND" | ||||||||||||||||||||||||||||||||||||||||||||||
| 255 | . " LIMIT $self->{cleanup_limit}", | ||||||||||||||||||||||||||||||||||||||||||||||
| 256 | [], | ||||||||||||||||||||||||||||||||||||||||||||||
| 257 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 258 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 259 | |||||||||||||||||||||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||||||||||||||||||||
| 261 | 17 | ▶ | sub cleaned { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
cleaned: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 262 | 17 | my ($self, $affected) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 263 | $self->{cleanup_in} = | ||||||||||||||||||||||||||||||||||||||||||||||
| 264 | 17 | ▶ | (defined $affected && $affected >= $self->{cleanup_limit}) ? 0 : $self->_cleanup_every; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $affected and $affected >= $self->{"cleanup_limit"}
MC/DC100%2/2
defined $affected and $affected >= $self->{"cleanup_limit"}
defined $affected
$affected >= $self->{"cleanup_limit"}
Truth table100%3/3
A and B
A= defined $affectedB= $affected >= $self->{"cleanup_limit"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 265 | 17 | return; | |||||||||||||||||||||||||||||||||||||||||||||
| 266 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||||||||||||||||||||
| 268 | 15 | ▶ | sub _cleanup_every { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_cleanup_every: called CC=3 SCAR=11.4
| |||||||||||||||||||||||||||||||||||||||||||||||
| 269 | 15 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 270 | 15 | my $polls = int($CLEANUP_PERIOD / $self->{poll_interval}); | |||||||||||||||||||||||||||||||||||||||||||||
| 271 | 15 | ▶ | $polls++ if $polls * $self->{poll_interval} < $CLEANUP_PERIOD; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 272 | 15 | ▶ | return $polls > 1 ? $polls : 1; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 273 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||||||||||||||||||||
| 275 | # Seconds to whole microseconds, for INTERVAL ... MICROSECOND. | ||||||||||||||||||||||||||||||||||||||||||||||
| 276 | 651 | ▶ | sub _micro { return int($_[0] * 1_000_000 + 0.5) } | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_micro: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||||||||||||||||||||
| 278 | # The next cutoff, formatted by the server: its clock, fixed width. | ||||||||||||||||||||||||||||||||||||||||||||||
| 279 | 228 | ▶ | sub _cutoff_sql { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_cutoff_sql: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 280 | 228 | my ($us) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 281 | 228 | return "DATE_FORMAT(UTC_TIMESTAMP(6) - INTERVAL $us MICROSECOND, '$CUTOFF_FORMAT')"; | |||||||||||||||||||||||||||||||||||||||||||||
| 282 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||||||||||||||||||||
| 284 | # Compare two BIGINT UNSIGNED ids exactly, as the decimal strings they are. | ||||||||||||||||||||||||||||||||||||||||||||||
| 285 | 173 | ▶ | sub _id_cmp { return length($_[0]) <=> length($_[1]) || $_[0] cmp $_[1] } | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_id_cmp: called CC=2 SCAR=6.9
Condition100%3/3
length $_[0] <=> length $_[1] || $_[0] cmp $_[1]
MC/DC100%2/2
length $_[0] <=> length $_[1] || $_[0] cmp $_[1]
length $_[0] <=> length $_[1]
$_[0] cmp $_[1]
Truth table100%3/3
A || B
A= length $_[0] <=> length $_[1]B= $_[0] cmp $_[1]
| |||||||||||||||||||||||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||||||||||||||||||||
| 287 | 30 | 1; | |||||||||||||||||||||||||||||||||||||||||||||
| 288 | |||||||||||||||||||||||||||||||||||||||||||||||
| 289 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||