Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions lib/Reader.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,16 @@
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 {

Check warning on line 71 in lib/Reader.php

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Move this open curly brace to the beginning of the next line.

See more on https://sonarcloud.io/project/issues?id=somethingwithproof_plugin_pfs&issues=AaH3Jz5Udww5KwE407ed&open=AaH3Jz5Udww5KwE407ed&pullRequest=82
$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);
Expand Down
2 changes: 1 addition & 1 deletion poller_pfs.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Check warning on line 81 in poller_pfs.php

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Split this 154 characters long line (which is greater than 120 authorized).

See more on https://sonarcloud.io/project/issues?id=somethingwithproof_plugin_pfs&issues=AaH3Jz68dww5KwE407ee&open=AaH3Jz68dww5KwE407ee&pullRequest=82
} finally {
unregister_process('pfs', 'reader', $pfs_poller);
}
Expand Down
10 changes: 10 additions & 0 deletions tests/Integration/LifecycleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
3 changes: 2 additions & 1 deletion tests/bin/alert-flow.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion tests/bin/device-flow.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down Expand Up @@ -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() {
Expand Down
3 changes: 2 additions & 1 deletion tests/bin/device-permissions.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,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");
Expand Down Expand Up @@ -117,7 +118,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));
Expand Down
1 change: 1 addition & 0 deletions tests/bin/lifecycle.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion tests/bin/reader-flow.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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);
}
Expand Down
32 changes: 30 additions & 2 deletions tests/bin/reader-restart.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand All @@ -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();
Expand All @@ -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);
}
Expand All @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion tests/bin/restore-flow.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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. */
Expand Down
3 changes: 2 additions & 1 deletion tests/bin/scale-flow.php
Original file line number Diff line number Diff line change
Expand Up @@ -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] : '';
Expand Down Expand Up @@ -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));
}
Expand Down
3 changes: 2 additions & 1 deletion tests/bin/target-flow.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down Expand Up @@ -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;
Expand Down