Files
zoneminder/scripts/ZoneMinder/t/storage_diskspace.t
T
Isaac Connor 3b130e4c94 fix: adjust Storage.DiskSpace with a relative UPDATE instead of read-modify-write
lock_and_load + save is a read-modify-write, and outside a transaction the
SELECT ... FOR UPDATE autocommits and drops its lock immediately. The absolute
value written afterwards then clobbers any adjustment zmc, zmaudit or another
filter made in between.

Replace both call sites with ZoneMinder::Storage::adjust_diskspace, a single
relative UPDATE that is atomic on its own and holds the row lock only for the
length of that statement. It reads the new total back into the object rather
than adding the delta locally, because ZoneMinder::Object caches objects for
the life of the process and its accessors never re-load, so the cached value
can be arbitrarily stale and adjusting it locally would preserve the error.

Keeping the adjustment to one statement also keeps Storage out of the lock
chain that deleting an event walks:

  Events[Id] -> Events_Hour/Day/Week/Month[EventId] -> Event_Summaries[MonitorId]

Adds t/storage_diskspace.t.
2026-08-19 22:04:37 -05:00

69 lines
2.9 KiB
Perl

use strict;
use warnings;
use Test::More;
eval { require ZoneMinder::Storage; 1 } or plan skip_all => "cannot load ZoneMinder::Storage: $@";
plan tests => 12;
# Capture what would go to the database instead of talking to one. Logging is
# stubbed out as well: ZoneMinder::Logger writes to the Logs table, which would
# pull in a real connection.
my @sql;
my @fetched;
my $stored = 0;
{
no warnings 'redefine', 'once';
*ZoneMinder::Database::zmDbDo = sub { push @sql, [@_]; return 1; };
*ZoneMinder::Database::zmDbFetchOne = sub {
push @fetched, [@_];
return {DiskSpace => $stored};
};
*ZoneMinder::Storage::Debug = sub { };
*ZoneMinder::Storage::Warning = sub { };
}
sub reset_state { @sql = (); @fetched = (); }
my $Storage = bless {Id => 4, DiskSpace => 3000}, 'ZoneMinder::Storage';
# --- One relative statement, no read-modify-write ---------------------------
reset_state();
$Storage->adjust_diskspace(-1000);
is(scalar @sql, 1, 'an adjustment is a single statement');
like($sql[0][0], qr/^UPDATE Storage SET DiskSpace=/, 'it is an UPDATE of Storage.DiskSpace');
unlike($sql[0][0], qr/FOR UPDATE/i,
'no SELECT ... FOR UPDATE: the row lock must not outlive the statement');
like($sql[0][0], qr/GREATEST\(COALESCE\(DiskSpace,0\)\+\?,0\)/,
'the new value is computed relative to the stored one, so no lost update');
is_deeply([@{$sql[0]}[1,2]], [-1000, 4], 'bound with the signed delta and the storage id');
# --- Refreshing the object -------------------------------------------------
# ZoneMinder::Object caches objects for the life of the process and its
# accessors never re-load, so a cached DiskSpace can be arbitrarily stale.
# Adding the delta to it locally would preserve the error; read the new total.
reset_state();
$stored = 7500;
$Storage = bless {Id => 4, DiskSpace => 3000}, 'ZoneMinder::Storage';
$Storage->adjust_diskspace(-1000);
is(scalar @fetched, 1, 'the new total is read back');
like($fetched[0][0], qr/^SELECT DiskSpace FROM Storage WHERE Id=\?$/, 'by primary key');
is($fetched[0][1], 4, 'for the storage area we just adjusted');
is($$Storage{DiskSpace}, 7500,
'the object takes the stored value, not the stale cached value plus the delta');
# --- No-ops ----------------------------------------------------------------
reset_state();
$Storage->adjust_diskspace(0);
is(scalar @sql + scalar @fetched, 0, 'a zero delta touches nothing');
# Event::Storage() hands back a blank object when the event has no StorageId.
(bless {}, 'ZoneMinder::Storage')->adjust_diskspace(-1000);
is(scalar @sql + scalar @fetched, 0, 'a storage area with no Id touches nothing');
# Deleting an event adjusts Storage on its own, outside any transaction the
# caller might want. Nothing here may open one.
reset_state();
$Storage->adjust_diskspace(-1000);
unlike(join(' ', map { $$_[0] } (@sql, @fetched)), qr/\b(BEGIN|START TRANSACTION|COMMIT)\b/i,
'no transaction is opened around the adjustment');