Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add parallel cache support #7131

Merged
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ Note: You need to pass the config to the ``fix`` command, in order to make it wo

keradus marked this conversation as resolved.
Show resolved Hide resolved
.. code-block:: console

export PHP_CS_FIXER_EXPERIMENTAL_PARALLEL_CACHE=1
php php-cs-fixer.phar list-files --config=.php-cs-fixer.dist.php | xargs -n 10 -P 8 php php-cs-fixer.phar fix --config=.php-cs-fixer.dist.php --path-mode intersection -v
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
export PHP_CS_FIXER_EXPERIMENTAL_PARALLEL_CACHE=1
php php-cs-fixer.phar list-files --config=.php-cs-fixer.dist.php | xargs -n 10 -P 8 php php-cs-fixer.phar fix --config=.php-cs-fixer.dist.php --path-mode intersection -v
PHP_CS_FIXER_EXPERIMENTAL_PARALLEL_CACHE=1 php php-cs-fixer.phar list-files --config=.php-cs-fixer.dist.php | xargs -n 10 -P 8 php php-cs-fixer.phar fix --config=.php-cs-fixer.dist.php --path-mode intersection -v

IMHO it shouldn't be exported, but defined only for this particular run, that is supported by external parallelisation. Exporting will keep that value and will use that feature also for regular run (without xargs) and I am not sure if it should be like this.

Copy link
Member Author

@keradus keradus Jul 10, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

var_dump(">>>", getenv('PHP_CS_FIXER_EXPERIMENTAL_PARALLEL_CACHE')); die;

your proposal is returning

string(3) ">>>"
bool(false)

some playground

ker@d ~/github/PHP-CS-Fixer λ cat x.php 
<?php

echo json_encode([basename(__FILE__) => getenv('ENV_FOO')]);
ker@d ~/github/PHP-CS-Fixer λ cat y.php 
<?php

echo json_encode([basename(__FILE__) => getenv('ENV_FOO')]);
ker@d ~/github/PHP-CS-Fixer λ ENV_FOO=TEST php x.php            
{"x.php":"TEST"}
ker@d ~/github/PHP-CS-Fixer λ ENV_FOO=TEST php x.php | php y.php
{"y.php":false}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did not thought about pipes and variable visibility. It should be something like FOO=bar bash -c 'somecommand someargs | somecommand2' (source), in this case variable is available on each level of the pipeline. It makes it a little bit more complicated, but enables feature for single run instead of doing it globally. I won't fight for it, just a suggestion 🙂.

Copy link
Member Author

@keradus keradus Jul 10, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think it makes it overcomplex and I'm ok to have env exported. I assume that one either will want it each time, or never - and if they need sometimes, they will figure out the way.

bash is also less portable

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

solved in #7131 (comment)


* ``-n`` defines how many files a single subprocess process
Expand Down
14 changes: 14 additions & 0 deletions src/Cache/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,4 +136,18 @@ public static function fromJson(string $json): self

return $cache;
}

/**
* @internal
*/
public function backfillHashes(self $oldCache): bool
{
if (!$this->getSignature()->equals($oldCache->getSignature())) {
return false;
}

$this->hashes = array_merge($oldCache->hashes, $this->hashes);

return true;
}
}
92 changes: 73 additions & 19 deletions src/Cache/FileHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
{
private string $file;

private int $fileLastModification = 0;

public function __construct(string $file)
{
$this->file = $file;
Expand All @@ -41,21 +43,86 @@
return null;
}

$content = file_get_contents($this->file);

try {
$cache = Cache::fromJson($content);
} catch (\InvalidArgumentException $exception) {
$handle = fopen($this->file, 'r');
if (false === $handle) {
return null;
}

$cache = $this->readFromHandle($handle);
$this->fileLastModification = $this->getFileLastUpdate($handle);
var_dump("\n".getmypid().' !!! FRS !!! READ '.$this->fileLastModification);
fclose($handle);

return $cache;
}

public function write(CacheInterface $cache): void
{
$content = $cache->toJson();
$this->ensureFileIsWriteable();

$handle = fopen($this->file, 'r+');
if (false === $handle) {
return;
}

$actualLastModification = $this->getFileLastUpdate($handle);

var_dump("\n".getmypid().' !!! FRS !!! DECISION ???');
var_dump([
'property' => $this->fileLastModification,
'os' => $actualLastModification,
'should backfill' => $this->fileLastModification < $actualLastModification,
]);

if ($this->fileLastModification < $actualLastModification) {
flock($handle, LOCK_EX);

$oldCache = $this->readFromHandle($handle);
rewind($handle);

if ($oldCache && method_exists($cache, 'backfillHashes')) {

Check failure on line 83 in src/Cache/FileHandler.php

View workflow job for this annotation

GitHub Actions / Static Code Analysis (ubuntu-20.04, 8.1)

Only booleans are allowed in &&, PhpCsFixer\Cache\CacheInterface|null given on the left side.
var_dump("\n".getmypid().' !!! FRS !!! BACKFILL');
$cache->backfillHashes($oldCache);
}
}
var_dump("\n".getmypid().' !!! FRS !!! SAVE FILE');
ftruncate($handle, 0);
fwrite($handle, $cache->toJson());
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder about putting @ to silence warning and check !== false for every method here... any better suggestion?

fflush($handle);
fsync($handle);

var_dump("\n".getmypid().' !!! FRS !!! AFTER SAVE '.$this->getFileLastUpdate($handle));
fclose($handle);
}

private function getFileLastUpdate($handle): int

Check failure on line 98 in src/Cache/FileHandler.php

View workflow job for this annotation

GitHub Actions / Static Code Analysis (ubuntu-20.04, 8.1)

Method PhpCsFixer\Cache\FileHandler::getFileLastUpdate() has parameter $handle with no type specified.
{
clearstatcache(true, $this->file);

return fstat($handle)['mtime'] ?: 0;

Check failure on line 102 in src/Cache/FileHandler.php

View workflow job for this annotation

GitHub Actions / Static Code Analysis (ubuntu-20.04, 8.1)

Short ternary operator is not allowed. Use null coalesce operator if applicable or consider using long ternary.
}

/**
* @param resource $handle
*/
private function readFromHandle($handle): ?CacheInterface
{
try {
$size = filesize($this->file);
if (!$size) {
return null;
}

$content = fread($handle, $size);

return Cache::fromJson($content);
} catch (\InvalidArgumentException $exception) {
return null;
}
}

private function ensureFileIsWriteable(): void
{
if (file_exists($this->file)) {
if (is_dir($this->file)) {
throw new IOException(
Expand Down Expand Up @@ -95,18 +162,5 @@
@touch($this->file);
@chmod($this->file, 0666);
}

$bytesWritten = @file_put_contents($this->file, $content);

if (false === $bytesWritten) {
$error = error_get_last();

throw new IOException(
sprintf('Failed to write file "%s", "%s".', $this->file, $error['message'] ?? 'no reason available'),
0,
null,
$this->file
);
}
}
}