Skip to content

Commit 03d027f

Browse files
committed
fix
1 parent 7ea8e71 commit 03d027f

4 files changed

Lines changed: 133 additions & 119 deletions

File tree

‎PARALLEL_LPT_POC_NOTES.md‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,12 @@ Nothing below is covered by a test yet — the user asked for a PoC first.
338338
- [ ] drop the `--lpt` flag and `RECTOR_LPT_NO_STEAL` env var, or promote them to real options
339339
- [ ] `MAX_CHUNKS_PER_WORKER` is dead in bucket mode — decide its fate (see the memory-driven
340340
recycle item above, which would give it a real job again)
341-
- [ ] fold `ExperimentalParallelFileProcessor` back into `ParallelFileProcessor` — it is a near-copy,
342-
kept separate only so the default path stays provably untouched, and it will trip the
343-
duplicate-code gate
341+
- [x] the shared result accumulation is extracted into `ParallelResultCollector` — this is what the
342+
`duplicated_code` CI gate caught (169 tokens against a 150 threshold). Default-path behaviour
343+
verified unchanged: `vendor/` before and after gives 3 338 diffs, identical sha, same 2 errors.
344+
The system-error counter stayed in the processors on purpose — three sites append an error but
345+
only two increment it, and unifying that would move when the 50-error limit trips.
346+
- [ ] fold `ExperimentalParallelFileProcessor` back into `ParallelFileProcessor` — the TCP wiring and
347+
the spawn loop are still parallel structures, kept apart only so the default path stays
348+
provably untouched
344349
- [ ] `composer check-cs`, `composer phpstan`, `vendor/bin/phpunit`

‎src/Parallel/Application/ParallelFileProcessor.php‎

Lines changed: 16 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
use Rector\Parallel\ValueObject\Schedule;
2525
use Rector\ValueObject\Error\SystemError;
2626
use Rector\ValueObject\ProcessResult;
27-
use Rector\ValueObject\Reporting\FileDiff;
2827
use Symfony\Component\Console\Command\Command;
2928
use Symfony\Component\Console\Input\InputInterface;
3029
use Throwable;
@@ -67,16 +66,7 @@ public function process(
6766
// basic properties setup
6867
$numberOfProcesses = $schedule->getNumberOfProcesses();
6968

70-
// initial counters
71-
72-
/** @var FileDiff[] $fileDiffs */
73-
$fileDiffs = [];
74-
75-
/** @var SystemError[] $systemErrors */
76-
$systemErrors = [];
77-
78-
/** @var array<string, array<string, true>> $usedSkips */
79-
$usedSkips = [];
69+
$parallelResultCollector = new ParallelResultCollector();
8070

8171
$tcpServer = new TcpServer('127.0.0.1:0', $streamSelectLoop);
8272
$this->processPool = new ProcessPool($tcpServer);
@@ -117,18 +107,17 @@ public function process(
117107

118108
$systemErrorsCount = 0;
119109
$reachedSystemErrorsCountLimit = false;
120-
$totalChanged = 0;
121110

122111
$handleErrorCallable = function (Throwable $throwable) use (
123-
&$systemErrors,
112+
$parallelResultCollector,
124113
&$systemErrorsCount,
125114
&$reachedSystemErrorsCountLimit
126115
): void {
127-
$systemErrors[] = new SystemError(
116+
$parallelResultCollector->collectSystemError(new SystemError(
128117
$throwable->getMessage(),
129118
$throwable->getFile(),
130119
$throwable->getLine(),
131-
);
120+
));
132121

133122
++$systemErrorsCount;
134123
$reachedSystemErrorsCountLimit = true;
@@ -144,9 +133,7 @@ public function process(
144133
$fileChunksBudgetPerProcess = [];
145134

146135
$processSpawner = function () use (
147-
&$systemErrors,
148-
&$fileDiffs,
149-
&$usedSkips,
136+
$parallelResultCollector,
150137
&$jobs,
151138
$postFileCallback,
152139
&$systemErrorsCount,
@@ -158,8 +145,7 @@ public function process(
158145
$timeoutInSeconds,
159146
$handleErrorCallable,
160147
&$fileChunksBudgetPerProcess,
161-
&$processSpawner,
162-
&$totalChanged
148+
&$processSpawner
163149
): void {
164150
$processIdentifier = Random::generate();
165151
$workerCommandLine = $this->workerCommandLineFactory->create(
@@ -178,17 +164,14 @@ public function process(
178164
// 1. callable on data
179165
function (array $json) use (
180166
$parallelProcess,
181-
&$systemErrors,
182-
&$fileDiffs,
183-
&$usedSkips,
167+
$parallelResultCollector,
184168
&$jobs,
185169
$postFileCallback,
186170
&$systemErrorsCount,
187171
&$reachedInternalErrorsCountLimit,
188172
$processIdentifier,
189173
&$fileChunksBudgetPerProcess,
190-
&$processSpawner,
191-
&$totalChanged
174+
&$processSpawner
192175
): void {
193176
/** @var array{
194177
* total_changed: int,
@@ -198,30 +181,7 @@ function (array $json) use (
198181
* system_errors_count: int,
199182
* used_skips: array<string, string[]>
200183
* } $json */
201-
$totalChanged += $json[Bridge::TOTAL_CHANGED];
202-
203-
foreach ($json[Bridge::USED_SKIPS] as $skip => $paths) {
204-
$usedSkips[$skip] ??= [];
205-
foreach ($paths as $path) {
206-
$usedSkips[$skip][$path] = true;
207-
}
208-
}
209-
210-
// decode arrays to objects
211-
foreach ($json[Bridge::SYSTEM_ERRORS] as $jsonError) {
212-
if (is_string($jsonError)) {
213-
$systemErrors[] = new SystemError('System error: ' . $jsonError);
214-
continue;
215-
}
216-
217-
$systemErrors[] = SystemError::decode($jsonError);
218-
}
219-
220-
foreach ($json[Bridge::FILE_DIFFS] as $jsonFileDiff) {
221-
$fileDiffs[] = FileDiff::decode($jsonFileDiff);
222-
}
223-
224-
$postFileCallback($json[Bridge::FILES_COUNT]);
184+
$postFileCallback($parallelResultCollector->collectWorkerResult($json));
225185

226186
$systemErrorsCount += $json[Bridge::SYSTEM_ERRORS_COUNT];
227187
if ($systemErrorsCount >= self::SYSTEM_ERROR_LIMIT) {
@@ -254,7 +214,7 @@ function (array $json) use (
254214
$handleErrorCallable,
255215

256216
// 3. callable on exit
257-
function ($exitCode, string $stdErr) use (&$systemErrors, $processIdentifier): void {
217+
function ($exitCode, string $stdErr) use ($parallelResultCollector, $processIdentifier): void {
258218
$this->processPool->tryQuitProcess($processIdentifier);
259219
if ($exitCode === Command::SUCCESS) {
260220
return;
@@ -264,7 +224,9 @@ function ($exitCode, string $stdErr) use (&$systemErrors, $processIdentifier): v
264224
return;
265225
}
266226

267-
$systemErrors[] = new SystemError('Child process error: ' . $stdErr);
227+
$parallelResultCollector->collectSystemError(new SystemError(
228+
'Child process error: ' . $stdErr
229+
));
268230
}
269231
);
270232

@@ -283,17 +245,12 @@ function ($exitCode, string $stdErr) use (&$systemErrors, $processIdentifier): v
283245
$streamSelectLoop->run();
284246

285247
if ($reachedSystemErrorsCountLimit) {
286-
$systemErrors[] = new SystemError(sprintf(
248+
$parallelResultCollector->collectSystemError(new SystemError(sprintf(
287249
'Reached system errors count limit of %d, exiting...',
288250
self::SYSTEM_ERROR_LIMIT
289-
));
290-
}
291-
292-
$mergedUsedSkips = [];
293-
foreach ($usedSkips as $skip => $paths) {
294-
$mergedUsedSkips[$skip] = array_keys($paths);
251+
)));
295252
}
296253

297-
return new ProcessResult($systemErrors, $fileDiffs, $totalChanged, $mergedUsedSkips);
254+
return $parallelResultCollector->createProcessResult();
298255
}
299256
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Parallel\Application;
6+
7+
use Rector\Parallel\ValueObject\Bridge;
8+
use Rector\ValueObject\Error\SystemError;
9+
use Rector\ValueObject\ProcessResult;
10+
use Rector\ValueObject\Reporting\FileDiff;
11+
12+
/**
13+
* Gathers what the workers report back over the run, so a parallel file processor only has to decide
14+
* who gets which files.
15+
*
16+
* Holds mutable per-run state - create one per process() call, never share it.
17+
*/
18+
final class ParallelResultCollector
19+
{
20+
/**
21+
* @var SystemError[]
22+
*/
23+
private array $systemErrors = [];
24+
25+
/**
26+
* @var FileDiff[]
27+
*/
28+
private array $fileDiffs = [];
29+
30+
/**
31+
* Paths are kept as keys while collecting, to deduplicate them per skip
32+
*
33+
* @var array<string, array<string, true>>
34+
*/
35+
private array $usedSkips = [];
36+
37+
private int $totalChanged = 0;
38+
39+
/**
40+
* @param array{
41+
* total_changed: int,
42+
* system_errors: mixed[],
43+
* file_diffs: array<string, mixed>,
44+
* files_count: int,
45+
* system_errors_count: int,
46+
* used_skips: array<string, string[]>
47+
* } $json
48+
* @return int files covered by this result, to advance the progress bar
49+
*/
50+
public function collectWorkerResult(array $json): int
51+
{
52+
$this->totalChanged += $json[Bridge::TOTAL_CHANGED];
53+
54+
foreach ($json[Bridge::USED_SKIPS] as $skip => $paths) {
55+
$this->usedSkips[$skip] ??= [];
56+
foreach ($paths as $path) {
57+
$this->usedSkips[$skip][$path] = true;
58+
}
59+
}
60+
61+
// decode arrays to objects
62+
foreach ($json[Bridge::SYSTEM_ERRORS] as $jsonError) {
63+
if (is_string($jsonError)) {
64+
$this->systemErrors[] = new SystemError('System error: ' . $jsonError);
65+
continue;
66+
}
67+
68+
$this->systemErrors[] = SystemError::decode($jsonError);
69+
}
70+
71+
foreach ($json[Bridge::FILE_DIFFS] as $jsonFileDiff) {
72+
$this->fileDiffs[] = FileDiff::decode($jsonFileDiff);
73+
}
74+
75+
return $json[Bridge::FILES_COUNT];
76+
}
77+
78+
public function collectSystemError(SystemError $systemError): void
79+
{
80+
$this->systemErrors[] = $systemError;
81+
}
82+
83+
public function createProcessResult(): ProcessResult
84+
{
85+
$mergedUsedSkips = [];
86+
foreach ($this->usedSkips as $skip => $paths) {
87+
$mergedUsedSkips[$skip] = array_keys($paths);
88+
}
89+
90+
return new ProcessResult($this->systemErrors, $this->fileDiffs, $this->totalChanged, $mergedUsedSkips);
91+
}
92+
}

0 commit comments

Comments
 (0)