Repository navigation
Expand file tree
/
Copy pathParallelFileProcessor.php
More file actions
428 lines (367 loc) · 15.9 KB
/
Copy pathParallelFileProcessor.php
File metadata and controls
428 lines (367 loc) · 15.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
<?php
declare(strict_types=1);
namespace Rector\Parallel\Application;
use Clue\React\NDJson\Decoder;
use Clue\React\NDJson\Encoder;
use Nette\Utils\FileSystem;
use Nette\Utils\Random;
use PHPStan\Turbo\TurboExtensionEnabler;
use PHPStan\Turbo\TurboExtensionSelector;
use React\EventLoop\StreamSelectLoop;
use React\Socket\ConnectionInterface;
use React\Socket\TcpServer;
use Rector\Configuration\Option;
use Rector\Configuration\Parameter\SimpleParameterProvider;
use Rector\Console\Command\ProcessCommand;
use Rector\Console\Command\WorkerCommand;
use Rector\Parallel\Command\WorkerCommandLineFactory;
use Rector\Parallel\Enum\Action;
use Rector\Parallel\Enum\Content;
use Rector\Parallel\Enum\ReactCommand;
use Rector\Parallel\Enum\ReactEvent;
use Rector\Parallel\Enum\StreamFormat;
use Rector\Parallel\ValueObject\Bridge;
use Rector\Parallel\ValueObject\ParallelProcess;
use Rector\Parallel\ValueObject\ProcessPool;
use Rector\Parallel\ValueObject\Schedule;
use Rector\Testing\PHPUnit\StaticPHPUnitEnvironment;
use Rector\ValueObject\Error\SystemError;
use Rector\ValueObject\ProcessResult;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\ConsoleOutput;
use Throwable;
/**
* Workers process their own balanced bucket first, and are replaced by a fresh one bound to the same bucket
* after a few chunks to free memory. Idle workers take pending chunks from the largest remaining queue.
* Each chunk advances progress and re-arms the per-job timeout.
*
* Where possible a worker is forked from the main process, so it starts with the container the main process
* already booted; otherwise it is spawned as a new process that boots on its own.
*/
final class ParallelFileProcessor
{
private const int SYSTEM_ERROR_LIMIT = 50;
/**
* The number of chunks a worker can process before getting killed.
* In contrast the jobSize defines the maximum size of a chunk, a worker process at a time.
*/
private const int MAX_CHUNKS_PER_WORKER = 8;
private ProcessPool $processPool;
/**
* @var callable(): WorkerCommand
*/
private $workerCommandResolver;
/**
* @param callable(): WorkerCommand $workerCommandResolver Lazy, as the worker command itself depends on this class
*/
public function __construct(
private readonly WorkerCommandLineFactory $workerCommandLineFactory,
callable $workerCommandResolver,
) {
$this->workerCommandResolver = $workerCommandResolver;
}
/**
* @param callable(int $stepCount): void $postFileCallback Used for progress bar jump
*/
public function process(
Schedule $schedule,
string $mainScript,
callable $postFileCallback,
InputInterface $input
): ProcessResult {
// reversed, so that array_pop() hands out the jobs in bucket order
$jobsPerWorker = array_map(
array_reverse(...),
$schedule->getJobsPerWorker()
);
/** @var array<string, int> $bucketKeyByIdentifier */
$bucketKeyByIdentifier = [];
$streamSelectLoop = new StreamSelectLoop();
$parallelResultCollector = new ParallelResultCollector();
$tcpServer = new TcpServer('127.0.0.1:0', $streamSelectLoop);
$this->processPool = new ProcessPool($tcpServer);
$tcpServer->on(ReactEvent::CONNECTION, function (ConnectionInterface $connection) use (
&$jobsPerWorker,
&$bucketKeyByIdentifier
): void {
$inDecoder = new Decoder($connection, true, StreamFormat::DEPTH, 0, StreamFormat::MAX_LENGTH);
$outEncoder = new Encoder($connection);
$inDecoder->on(ReactEvent::DATA, function (array $data) use (
&$jobsPerWorker,
&$bucketKeyByIdentifier,
$inDecoder,
$outEncoder
): void {
$action = $data[ReactCommand::ACTION];
if ($action !== Action::HELLO) {
return;
}
$processIdentifier = $data[Option::PARALLEL_IDENTIFIER];
$parallelProcess = $this->processPool->getProcess($processIdentifier);
$parallelProcess->bindConnection($inDecoder, $outEncoder);
$jobsChunk = $this->popJobChunk($processIdentifier, $jobsPerWorker, $bucketKeyByIdentifier);
if ($jobsChunk === null) {
$this->processPool->quitProcess($processIdentifier);
return;
}
$parallelProcess->request([
ReactCommand::ACTION => Action::MAIN,
Content::FILES => $jobsChunk,
]);
});
});
/** @var string $serverAddress */
$serverAddress = $tcpServer->getAddress();
/** @var int $serverPort */
$serverPort = parse_url($serverAddress, PHP_URL_PORT);
$scheduledFilesCount = 0;
foreach ($jobsPerWorker as $jobs) {
$scheduledFilesCount += array_sum(array_map(count(...), $jobs));
}
$processedFilesCount = 0;
$systemErrorsCount = 0;
$reachedSystemErrorsCountLimit = false;
$handleErrorCallable = function (Throwable $throwable) use (
$parallelResultCollector,
&$systemErrorsCount,
&$reachedSystemErrorsCountLimit
): void {
$parallelResultCollector->collectSystemError(new SystemError(
$throwable->getMessage(),
$throwable->getFile(),
$throwable->getLine(),
));
++$systemErrorsCount;
$reachedSystemErrorsCountLimit = $systemErrorsCount >= self::SYSTEM_ERROR_LIMIT;
$this->processPool->quitAll();
// give the child processes time to actually die, they can still write to cache meanwhile
// @see https://github.com/rectorphp/rector-src/pull/3834/files#r1231696531
sleep(1);
};
$timeoutInSeconds = SimpleParameterProvider::provideIntParameter(Option::PARALLEL_JOB_TIMEOUT_IN_SECONDS);
$fileChunksBudgetPerProcess = [];
$canForkWorkers = $this->canForkWorkers();
// forked workers log their PHP errors next to the cache
$cacheDirectory = SimpleParameterProvider::provideStringParameter(Option::CACHE_DIR);
if ($canForkWorkers) {
FileSystem::createDir($cacheDirectory);
}
$processSpawner = function (int $bucketKey) use (
$parallelResultCollector,
&$jobsPerWorker,
&$bucketKeyByIdentifier,
$postFileCallback,
&$systemErrorsCount,
&$reachedSystemErrorsCountLimit,
$mainScript,
$input,
$serverPort,
$streamSelectLoop,
$timeoutInSeconds,
$handleErrorCallable,
$canForkWorkers,
$cacheDirectory,
&$fileChunksBudgetPerProcess,
&$processSpawner,
&$processedFilesCount
): void {
$processIdentifier = Random::generate();
$bucketKeyByIdentifier[$processIdentifier] = $bucketKey;
$fileChunksBudgetPerProcess[$processIdentifier] = self::MAX_CHUNKS_PER_WORKER;
if ($canForkWorkers) {
$parallelProcess = new ParallelProcess(
'',
$streamSelectLoop,
$timeoutInSeconds,
$cacheDirectory,
$this->createForkedWorker($input, $processIdentifier, $serverPort)
);
} else {
$workerCommandLine = $this->workerCommandLineFactory->create(
$mainScript,
ProcessCommand::class,
'worker',
$input,
$processIdentifier,
$serverPort,
);
$parallelProcess = new ParallelProcess(
$workerCommandLine,
$streamSelectLoop,
$timeoutInSeconds,
$cacheDirectory
);
}
$parallelProcess->start(
// 1. callable on data
function (array $json) use (
$parallelProcess,
$parallelResultCollector,
&$jobsPerWorker,
&$bucketKeyByIdentifier,
$postFileCallback,
&$systemErrorsCount,
&$reachedSystemErrorsCountLimit,
$processIdentifier,
&$fileChunksBudgetPerProcess,
&$processSpawner,
&$processedFilesCount
): void {
/** @var array{
* total_changed: int,
* system_errors: mixed[],
* file_diffs: array<string, mixed>,
* files_count: int,
* system_errors_count: int,
* used_skips: array<string, string[]>
* } $json */
$filesCount = $parallelResultCollector->collectWorkerResult($json);
$postFileCallback($filesCount);
$processedFilesCount += $filesCount;
$systemErrorsCount += $json[Bridge::SYSTEM_ERRORS_COUNT];
if ($systemErrorsCount >= self::SYSTEM_ERROR_LIMIT) {
$reachedSystemErrorsCountLimit = true;
$this->processPool->quitAll();
return;
}
$hasPendingJobs = array_any($jobsPerWorker, static fn (array $jobs): bool => $jobs !== []);
if ($fileChunksBudgetPerProcess[$processIdentifier] <= 0 && $hasPendingJobs) {
// replace the current worker with a fresh one on the same bucket to free memory; spawn the
// replacement first, as quitting the last worker in the pool closes the server it connects to
($processSpawner)($bucketKeyByIdentifier[$processIdentifier]);
$this->processPool->quitProcess($processIdentifier);
return;
}
$jobsChunk = $this->popJobChunk($processIdentifier, $jobsPerWorker, $bucketKeyByIdentifier);
if ($jobsChunk === null) {
// bucket done, this worker has nothing left to do
$this->processPool->quitProcess($processIdentifier);
return;
}
$parallelProcess->request([
ReactCommand::ACTION => Action::MAIN,
Content::FILES => $jobsChunk,
]);
--$fileChunksBudgetPerProcess[$processIdentifier];
},
// 2. callable on error
$handleErrorCallable,
// 3. callable on exit
function ($exitCode, string $stdErr) use ($parallelResultCollector, $processIdentifier): void {
$this->processPool->tryQuitProcess($processIdentifier);
if ($exitCode === Command::SUCCESS) {
return;
}
if ($exitCode === null) {
return;
}
$parallelResultCollector->collectSystemError(new SystemError(
'Child process error: ' . $stdErr
));
}
);
$this->processPool->attachProcess($processIdentifier, $parallelProcess);
};
foreach (array_keys($jobsPerWorker) as $bucketKey) {
($processSpawner)($bucketKey);
}
$streamSelectLoop->run();
if ($reachedSystemErrorsCountLimit) {
$parallelResultCollector->collectSystemError(new SystemError(sprintf(
'Reached system errors count limit of %d, exiting...',
self::SYSTEM_ERROR_LIMIT
)));
}
// Workers may exit without reporting their assigned files.
if ($processedFilesCount < $scheduledFilesCount) {
$parallelResultCollector->collectSystemError(new SystemError(sprintf(
'Some parallel jobs have not finished, results for %d of %d files are missing',
$scheduledFilesCount - $processedFilesCount,
$scheduledFilesCount
)));
}
return $parallelResultCollector->createProcessResult();
}
/**
* Forking skips starting PHP and booting the container again for every worker, including the ones that
* replace a recycled worker. A fork runs on what the main process has loaded, so it is only used when a
* spawned worker would not get more than that - PHPStan Turbo on its command line.
*/
private function canForkWorkers(): bool
{
if (StaticPHPUnitEnvironment::isPHPUnitRun()) {
return false;
}
if (! extension_loaded('pcntl') || ! extension_loaded('posix')) {
return false;
}
$disabledFunctions = (string) ini_get('disable_functions');
if (str_contains($disabledFunctions, 'pcntl_') || str_contains($disabledFunctions, 'posix_kill')) {
return false;
}
return TurboExtensionEnabler::isLoaded() || TurboExtensionSelector::findExtensionForWorkers() === null;
}
/**
* @return callable(): int
*/
private function createForkedWorker(InputInterface $input, string $processIdentifier, int $serverPort): callable
{
$workerInput = new ArrayInput($this->workerCommandLineFactory->createInputParameters(
ProcessCommand::class,
$input,
$processIdentifier,
$serverPort
));
return function () use ($workerInput): int {
// the main process collects PHP errors on its own while it waits for the workers;
// a worker leaves them to PHP, the same as a spawned one
restore_error_handler();
$workerCommand = ($this->workerCommandResolver)();
return $workerCommand->run($workerInput, new ConsoleOutput());
};
}
/**
* @param array<int, array<int, array<string>>> $jobsPerWorker
* @param array<string, int> $bucketKeyByIdentifier
* @return array<string>|null
*/
private function popJobChunk(
string $processIdentifier,
array &$jobsPerWorker,
array $bucketKeyByIdentifier
): ?array {
$bucketKey = $bucketKeyByIdentifier[$processIdentifier] ?? null;
if ($bucketKey !== null && ($jobsPerWorker[$bucketKey] ?? []) !== []) {
return array_pop($jobsPerWorker[$bucketKey]);
}
return $this->stealJobChunk($jobsPerWorker);
}
/**
* Own bucket drained while others still have work. Byte size only estimates the cost of a file, and
* not every core is equally fast - on a big.LITTLE CPU (Apple silicon, recent Intel) a bucket handed
* to an efficiency core takes several times longer. Take over a chunk from whoever has the most left,
* so the run is not held up by one straggler.
*
* @param array<int, array<int, array<string>>> $jobsPerWorker
* @return array<string>|null
*/
private function stealJobChunk(array &$jobsPerWorker): ?array
{
$fattestBucketKey = null;
$fattestJobCount = 0;
foreach ($jobsPerWorker as $currentBucketKey => $jobs) {
$jobCount = count($jobs);
if ($jobCount <= $fattestJobCount) {
continue;
}
$fattestJobCount = $jobCount;
$fattestBucketKey = $currentBucketKey;
}
if ($fattestBucketKey === null) {
return null;
}
return array_pop($jobsPerWorker[$fattestBucketKey]);
}
}