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 5 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;
}
}
69 changes: 50 additions & 19 deletions src/Cache/FileHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,21 +41,65 @@ public function read(): ?CacheInterface
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);

fclose($handle);

return $cache;
}

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

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

if (getenv('PHP_CS_FIXER_EXPERIMENTAL_PARALLEL_CACHE')) {
Copy link
Member Author

Choose a reason for hiding this comment

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

idea for future (out of this PR scope) - we can detect that file was modified since it was read initially, and do the backilling based on that indicator, without env var.

Copy link
Member Author

Choose a reason for hiding this comment

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

implemented in 2aef0f8

ker@d ~/github/PHP-CS-Fixer λ ./php-cs-fixer list-files --config=.php-cs-fixer.dist.php | xargs -n 400 -P 3 ./php-cs-fixer fix --config=.php-cs-fixer.dist.php --path-mode intersection   
Loaded config default from ".php-cs-fixer.dist.php".
Using cache file ".php-cs-fixer.cache".
Loaded config default from ".php-cs-fixer.dist.php".
Using cache file ".php-cs-fixer.cache".
Loaded config default from ".php-cs-fixer.dist.php".
Using cache file ".php-cs-fixer.cache".
string(34) "
73993 !!! FRS !!! READ 1689350710"
string(34) "
73994 !!! FRS !!! READ 1689350710"
string(34) "
73995 !!! FRS !!! READ 1689350710"

Fixed 0 of 212 files in 7.380 seconds, 20.000 MB memory used
string(31) "
73995 !!! FRS !!! DECISION ???"
array(3) {
  ["property"]=>
  int(1689350710)
  ["os"]=>
  int(1689350710)
  ["should backfill"]=>
  bool(false)
}
string(28) "
73995 !!! FRS !!! SAVE FILE"
string(40) "
73995 !!! FRS !!! AFTER SAVE 1689350727"

Fixed 0 of 400 files in 23.354 seconds, 22.000 MB memory used
string(31) "
73993 !!! FRS !!! DECISION ???"
array(3) {
  ["property"]=>
  int(1689350710)
  ["os"]=>
  int(1689350727)
  ["should backfill"]=>
  bool(true)
}
string(27) "
73993 !!! FRS !!! BACKFILL"
string(28) "
73993 !!! FRS !!! SAVE FILE"
string(40) "
73993 !!! FRS !!! AFTER SAVE 1689350743"
string(31) "
73994 !!! FRS !!! DECISION ???"
array(3) {
  ["property"]=>
  int(1689350710)
  ["os"]=>
  int(1689350743)
  ["should backfill"]=>
  bool(true)
}
string(27) "
73994 !!! FRS !!! BACKFILL"
string(28) "
73994 !!! FRS !!! SAVE FILE"
string(40) "
73994 !!! FRS !!! AFTER SAVE 1689350748"

Fixed 0 of 400 files in 27.835 seconds, 22.000 MB memory used

flock($handle, LOCK_EX);

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

if ($oldCache && method_exists($cache, 'backfillHashes')) {
$cache->backfillHashes($oldCache);
}
}

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);
fclose($handle);
}

/**
* @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 +139,5 @@ public function write(CacheInterface $cache): void
@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
);
}
}
}