From 629a05f703ec474169aa0e7ddd60f86abd3c2703 Mon Sep 17 00:00:00 2001 From: Thomas Vincent Date: Thu, 19 Nov 2026 20:11:15 -0800 Subject: [PATCH] fix(reader): hold the lease for the budget plus the read timeout The reader took its lease for the budget plus 60 seconds, but the budget only stops new reads from starting. A read started near the deadline may run for the whole read timeout, so a run could outlive its lease and have every result discarded, possibly on every cycle. Reader::run() now takes the read timeout and holds the lease for budget + read timeout + 60, in line with the process registration in poller_pfs.php. A lifecycle scenario runs a read from the budget's end to a 120 second timeout and checks that its results are written. Fixes #79 Signed-off-by: Thomas Vincent --- lib/Reader.php | 10 +++++++-- poller_pfs.php | 2 +- tests/Integration/LifecycleTest.php | 10 +++++++++ tests/bin/alert-flow.php | 3 ++- tests/bin/device-flow.php | 3 ++- tests/bin/device-permissions.php | 3 ++- tests/bin/lifecycle.php | 1 + tests/bin/reader-flow.php | 3 ++- tests/bin/reader-restart.php | 32 +++++++++++++++++++++++++++-- tests/bin/restore-flow.php | 3 ++- tests/bin/scale-flow.php | 3 ++- tests/bin/target-flow.php | 3 ++- 12 files changed, 64 insertions(+), 12 deletions(-) diff --git a/lib/Reader.php b/lib/Reader.php index 2b7a2cf..c279fd2 100644 --- a/lib/Reader.php +++ b/lib/Reader.php @@ -62,10 +62,16 @@ public static function dueAfter(int $interval): int { return $interval - min(self::DUE_SLACK, intdiv($interval, 10)); } - public function run(int $pollerId, int $budgetSeconds, int $concurrency): ReaderRun { + /** + * The budget only stops new reads from starting; a read started just + * before it may run for the whole read timeout. The lease covers that, + * plus time to write, so a slow last read does not cost the run its + * results. + */ + public function run(int $pollerId, int $budgetSeconds, int $concurrency, int $readTimeout): ReaderRun { $started = $this->clock->now(); - if (!$this->runs->acquire($pollerId, $this->holder, $budgetSeconds + 60)) { + if (!$this->runs->acquire($pollerId, $this->holder, $budgetSeconds + $readTimeout + 60)) { Log::info(sprintf('Reader on poller %d skipped: another run holds the lease', $pollerId)); return new ReaderRun($pollerId); diff --git a/poller_pfs.php b/poller_pfs.php index ad03a8d..6d99020 100644 --- a/poller_pfs.php +++ b/poller_pfs.php @@ -78,7 +78,7 @@ } try { - $pfs_run = pfs_reader($pfs_poller, $pfs_settings)->run($pfs_poller, $pfs_settings->readerBudget, $pfs_settings->concurrency); + $pfs_run = pfs_reader($pfs_poller, $pfs_settings)->run($pfs_poller, $pfs_settings->readerBudget, $pfs_settings->concurrency, $pfs_settings->readTimeout); } finally { unregister_process('pfs', 'reader', $pfs_poller); } diff --git a/tests/Integration/LifecycleTest.php b/tests/Integration/LifecycleTest.php index 0c8f0c6..4db9d5e 100644 --- a/tests/Integration/LifecycleTest.php +++ b/tests/Integration/LifecycleTest.php @@ -403,6 +403,16 @@ ->and($r['final'])->toMatchArray(['health' => 1, 'targets' => 2, 'observations' => 18]); }); +it('keeps the lease through a read that runs from the budget\'s end to the read timeout', function () { + lifecycle('install'); + + $r = lifecycle('reader_overrun')['result']; + + expect($r['run'])->toBe(['total' => 1, 'read' => 1, 'skipped' => 0]) + ->and($r['final'])->toMatchArray(['health' => 1, 'targets' => 2, 'observations' => 18]) + ->and($r['recorded'])->toBe(1); +}); + it('preserves old grants and resolves the three roles through Cacti including groups', function () { lifecycle('install'); lifecycle('enable'); diff --git a/tests/bin/alert-flow.php b/tests/bin/alert-flow.php index f50061e..c094fda 100644 --- a/tests/bin/alert-flow.php +++ b/tests/bin/alert-flow.php @@ -54,6 +54,7 @@ use Somethingwithproof\Pfs\FixedClock; use Somethingwithproof\Pfs\FixtureNodeSource; use Somethingwithproof\Pfs\NodeSources; +use Somethingwithproof\Pfs\Settings; use Somethingwithproof\Pfs\TemplateInstaller; const ALERT_STEPS = array('setup', 'warmup', 'baseline', 'burst', 'burst2', 'rated', 'reset', 'rejoined', 'resumed', 'reburst', 'reburst2', 'calm', 'imbalance', 'balanced', 'lost', 'returned', 'silent', 'answering', 'maint', 'maint2', 'unmaint', 'final'); @@ -148,7 +149,7 @@ function alert_read($oss) { // as they are when the next interval starts. db_execute('UPDATE plugin_pfs_node_health SET last_attempt = last_attempt - 3600'); - $run = pfs_reader_with(1, $sources, new FixedClock(time()))->run(1, 240, 4); + $run = pfs_reader_with(1, $sources, new FixedClock(time()))->run(1, 240, 4, Settings::DEFAULT_TIMEOUT); alert_expect($run->nodesRead + $run->nodesFailed === 2, 'the reader read both nodes (' . $run->nodesRead . ' answered, ' . $run->nodesFailed . ' failed)'); return $run; diff --git a/tests/bin/device-flow.php b/tests/bin/device-flow.php index 409a419..e25a210 100644 --- a/tests/bin/device-flow.php +++ b/tests/bin/device-flow.php @@ -61,6 +61,7 @@ use Somethingwithproof\Pfs\FixedClock; use Somethingwithproof\Pfs\FixtureNodeSource; use Somethingwithproof\Pfs\NodeSources; +use Somethingwithproof\Pfs\Settings; use Somethingwithproof\Pfs\TemplateInstaller; if (getenv('PFS_TEST_DATABASE') !== 'disposable' || !isset($argv[1]) || !in_array($argv[1], array('setup', 'check', 'later'), true)) { @@ -106,7 +107,7 @@ function flow_read(FixedClock $clock, array $answers) { $sources->register(AccessMethod::Ssh, $source); - return pfs_reader_with(1, $sources, $clock)->run(1, 240, 4); + return pfs_reader_with(1, $sources, $clock)->run(1, 240, 4, Settings::DEFAULT_TIMEOUT); } function flow_counts() { diff --git a/tests/bin/device-permissions.php b/tests/bin/device-permissions.php index 8efd8dc..8a0dbea 100644 --- a/tests/bin/device-permissions.php +++ b/tests/bin/device-permissions.php @@ -47,6 +47,7 @@ use Somethingwithproof\Pfs\FixedClock; use Somethingwithproof\Pfs\FixtureNodeSource; use Somethingwithproof\Pfs\NodeSources; +use Somethingwithproof\Pfs\Settings; if (getenv('PFS_TEST_DATABASE') !== 'disposable' || !isset($argv[1])) { fwrite(STDERR, "Set PFS_TEST_DATABASE=disposable and pass the Cacti URL\n"); @@ -116,7 +117,7 @@ function dp_login($user) { $sources = new NodeSources(); $source->answer($nodeId, (string) file_get_contents($config['base_path'] . '/plugins/pfs/tests/fixtures/lustre/oss-2.15.txt')); $sources->register(AccessMethod::Ssh, $source); -pfs_reader_with(1, $sources, new FixedClock(time()))->run(1, 240, 4); +pfs_reader_with(1, $sources, new FixedClock(time()))->run(1, 240, 4, Settings::DEFAULT_TIMEOUT); $hostId = pfs_provisioner()->create($repo->find($fsId)); $graph = (int) db_fetch_cell_prepared('SELECT id FROM graph_local WHERE host_id = ? ORDER BY id LIMIT 1', array($hostId)); diff --git a/tests/bin/lifecycle.php b/tests/bin/lifecycle.php index ca2bc23..fd5f9a7 100644 --- a/tests/bin/lifecycle.php +++ b/tests/bin/lifecycle.php @@ -203,6 +203,7 @@ case 'reader_second': case 'reader_victim': case 'reader_kill': + case 'reader_overrun': include($config['base_path'] . '/plugins/pfs/tests/bin/reader-restart.php'); break; diff --git a/tests/bin/reader-flow.php b/tests/bin/reader-flow.php index 3b01b65..965625a 100644 --- a/tests/bin/reader-flow.php +++ b/tests/bin/reader-flow.php @@ -30,6 +30,7 @@ use Somethingwithproof\Pfs\NodeRead; use Somethingwithproof\Pfs\NodeSources; use Somethingwithproof\Pfs\ReadError; +use Somethingwithproof\Pfs\Settings; include_once($config['base_path'] . '/plugins/pfs/setup.php'); pfs_boot(); @@ -63,7 +64,7 @@ function flow_run(FixedClock $clock, FixtureNodeSource $source, $budget = 240, $ $sources->register(AccessMethod::Ssh, $source); $source->reads = array(); - $run = pfs_reader_with($poller, $sources, $clock)->run($poller, $budget, 4); + $run = pfs_reader_with($poller, $sources, $clock)->run($poller, $budget, 4, Settings::DEFAULT_TIMEOUT); return array('total' => $run->nodesTotal, 'read' => $run->nodesRead, 'failed' => $run->nodesFailed, 'skipped' => $run->nodesSkipped, 'stopped' => $run->stoppedEarly, 'reads' => $source->reads); } diff --git a/tests/bin/reader-restart.php b/tests/bin/reader-restart.php index e738020..505baa8 100644 --- a/tests/bin/reader-restart.php +++ b/tests/bin/reader-restart.php @@ -24,6 +24,8 @@ * the next run continues once the lease expires * reader_victim the run that is killed (started by reader_kill) * reader_second a second reader start on the same poller exits at once + * reader_overrun a read started at the budget's end that runs to the read + * timeout still has its results written * * Sets $result. Must parse on PHP 8.0. */ @@ -33,6 +35,7 @@ use Somethingwithproof\Pfs\FixtureNodeSource; use Somethingwithproof\Pfs\NodeSource; use Somethingwithproof\Pfs\NodeSources; +use Somethingwithproof\Pfs\Settings; include_once($config['base_path'] . '/plugins/pfs/setup.php'); pfs_boot(); @@ -53,11 +56,27 @@ function_exists('posix_kill') ? posix_kill(getmypid(), 9) : exec('kill -9 ' . ge } } -function restart_reader(FixedClock $clock, NodeSource $source, $holder) { +/** + * Answers from the fixture, then moves the clock on as a read that started + * at the deadline and ran to the read timeout would. + */ +final class RestartSlowSource implements NodeSource { + public function __construct(private FixedClock $clock, private int $seconds, private FixtureNodeSource $inner) { + } + + public function readAll(array $nodes, int $concurrency, float $deadline): array { + $reads = $this->inner->readAll($nodes, $concurrency, $deadline); + $this->clock->advance($this->seconds); + + return $reads; + } +} + +function restart_reader(FixedClock $clock, NodeSource $source, $holder, $timeout = Settings::DEFAULT_TIMEOUT) { $sources = new NodeSources(); $sources->register(AccessMethod::Ssh, $source); - $run = pfs_reader_with(1, $sources, $clock, $holder)->run(1, RESTART_BUDGET, 4); + $run = pfs_reader_with(1, $sources, $clock, $holder)->run(1, RESTART_BUDGET, 4, $timeout); return array('total' => $run->nodesTotal, 'read' => $run->nodesRead, 'skipped' => $run->nodesSkipped); } @@ -80,6 +99,15 @@ function restart_seed() { } switch ($action) { + case 'reader_overrun': + $node = restart_seed(); + $timeout = 120; + $clock = new FixedClock(RESTART_TIME); + $source = new RestartSlowSource($clock, RESTART_BUDGET + $timeout, new FixtureNodeSource(array($node => (string) file_get_contents(dirname(__DIR__) . '/fixtures/lustre/oss-2.15.txt')))); + + $result = array('run' => restart_reader($clock, $source, 'slow', $timeout), 'final' => restart_state(), 'recorded' => (int) db_fetch_cell('SELECT nodes_read FROM plugin_pfs_reader WHERE poller_id = 1')); + + break; case 'reader_second': // A live process holds the reader slot for poller 1. Cacti 1.2.x // treats a slot as held only when the holder runs the same script, diff --git a/tests/bin/restore-flow.php b/tests/bin/restore-flow.php index ba4479a..58fade5 100644 --- a/tests/bin/restore-flow.php +++ b/tests/bin/restore-flow.php @@ -50,6 +50,7 @@ use Somethingwithproof\Pfs\FixedClock; use Somethingwithproof\Pfs\FixtureNodeSource; use Somethingwithproof\Pfs\NodeSources; +use Somethingwithproof\Pfs\Settings; use Somethingwithproof\Pfs\TemplateInstaller; $stateFile = (string) getenv('PFS_RESTORE_STATE'); @@ -96,7 +97,7 @@ function restore_read($fsName, $write) { db_execute('UPDATE plugin_pfs_node_health SET last_attempt = last_attempt - 3600'); - return pfs_reader_with(1, $sources, new FixedClock(time()))->run(1, 240, 4); + return pfs_reader_with(1, $sources, new FixedClock(time()))->run(1, 240, 4, Settings::DEFAULT_TIMEOUT); } /** What a backup must bring back for the device: objects by id and the RRD files. */ diff --git a/tests/bin/scale-flow.php b/tests/bin/scale-flow.php index 2d6eaff..fdee504 100644 --- a/tests/bin/scale-flow.php +++ b/tests/bin/scale-flow.php @@ -56,6 +56,7 @@ use Somethingwithproof\Pfs\FixedClock; use Somethingwithproof\Pfs\FixtureNodeSource; use Somethingwithproof\Pfs\NodeSources; +use Somethingwithproof\Pfs\Settings; use Somethingwithproof\Pfs\TemplateInstaller; $step = isset($argv[1]) ? $argv[1] : ''; @@ -108,7 +109,7 @@ function scale_read($name, array $nodes, $mds, $n, $at) { db_execute('UPDATE plugin_pfs_node_health SET last_attempt = last_attempt - 3600'); $start = hrtime(true); - $run = pfs_reader_with(1, $sources, new FixedClock($at))->run(1, 600, 8); + $run = pfs_reader_with(1, $sources, new FixedClock($at))->run(1, 600, 8, Settings::DEFAULT_TIMEOUT); return array($run, round((hrtime(true) - $start) / 1e9, 2)); } diff --git a/tests/bin/target-flow.php b/tests/bin/target-flow.php index 480edc0..947cb87 100644 --- a/tests/bin/target-flow.php +++ b/tests/bin/target-flow.php @@ -55,6 +55,7 @@ use Somethingwithproof\Pfs\FixedClock; use Somethingwithproof\Pfs\FixtureNodeSource; use Somethingwithproof\Pfs\NodeSources; +use Somethingwithproof\Pfs\Settings; use Somethingwithproof\Pfs\TemplateInstaller; if (getenv('PFS_TEST_DATABASE') !== 'disposable' || !isset($argv[1]) || !in_array($argv[1], array('setup', 'remove', 'add', 'return', 'check'), true)) { @@ -128,7 +129,7 @@ function churn_read(array $used, $at) { churn_settle(); db_execute('UPDATE plugin_pfs_node_health SET last_attempt = last_attempt - 3600'); - $run = pfs_reader_with(1, $sources, new FixedClock($at))->run(1, 240, 4); + $run = pfs_reader_with(1, $sources, new FixedClock($at))->run(1, 240, 4, Settings::DEFAULT_TIMEOUT); churn_expect($run->startedAt > 0, 'the fixture read ran, not skipped for a lease another run held'); return $run;