forked from ezsystems/ezpublish-kernel
/
ReindexCommand.php
443 lines (389 loc) · 15.2 KB
/
ReindexCommand.php
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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
<?php
/**
* This file is part of the eZ Publish Kernel package.
*
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
namespace eZ\Bundle\EzPublishCoreBundle\Command;
use eZ\Publish\SPI\Persistence\Content\ContentInfo;
use eZ\Publish\Core\Search\Common\Indexer;
use eZ\Publish\Core\Search\Common\IncrementalIndexer;
use Doctrine\DBAL\Driver\Statement;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Process\PhpExecutableFinder;
use Symfony\Component\Process\ProcessBuilder;
use RuntimeException;
use DateTime;
use PDO;
class ReindexCommand extends ContainerAwareCommand
{
/**
* @var \eZ\Publish\Core\Search\Common\Indexer|\eZ\Publish\Core\Search\Common\IncrementalIndexer
*/
private $searchIndexer;
/**
* @var \Doctrine\DBAL\Connection
*/
private $connection;
/**
* @var string
*/
private $phpPath;
/**
* @var \Psr\Log\LoggerInterface
*/
private $logger;
/**
* @var string
*/
private $siteaccess;
/**
* Initialize objects required by {@see execute()}.
*
* @param InputInterface $input
* @param OutputInterface $output
*/
public function initialize(InputInterface $input, OutputInterface $output)
{
parent::initialize($input, $output);
$this->searchIndexer = $this->getContainer()->get('ezpublish.spi.search.indexer');
$this->connection = $this->getContainer()->get('ezpublish.api.storage_engine.legacy.connection');
$this->logger = $this->getContainer()->get('logger');
if (!$this->searchIndexer instanceof Indexer) {
throw new RuntimeException(
sprintf(
'Expected to find Search Engine Indexer but found "%s" instead',
get_parent_class($this->searchIndexer)
)
);
}
}
/**
* {@inheritdoc}
*/
protected function configure()
{
$this
->setName('ezplatform:reindex')
->setDescription('Recreate or Refresh search engine index')
->addOption(
'iteration-count',
'c',
InputOption::VALUE_OPTIONAL,
'Number of objects to be indexed in a single iteration, for avoiding using too much memory',
50
)->addOption(
'no-commit',
null,
InputOption::VALUE_NONE,
'Do not commit after each iteration'
)->addOption(
'no-purge',
null,
InputOption::VALUE_NONE,
'Do not purge before indexing'
)->addOption(
'since',
null,
InputOption::VALUE_OPTIONAL,
'Refresh changes since a given time, any format understood by DateTime. Implies "no-purge", can not be combined with "content-ids" or "subtree"'
)->addOption(
'content-ids',
null,
InputOption::VALUE_OPTIONAL,
'Comma separated list of content id\'s to refresh (deleted/updated/added). Implies "no-purge", can not be combined with "since" or "subtree"'
)->addOption(
'subtree',
null,
InputOption::VALUE_OPTIONAL,
'Location Id to index subtree of (incl self). Implies "no-purge", can not be combined with "since" or "content-ids"'
)->addOption(
'processes',
null,
InputOption::VALUE_OPTIONAL,
'Number of child processes to run in parallel for iterations, if set to "auto" it will set to number of CPU cores -1, set to "1" or "0" to disable',
'auto'
)->setHelp(
<<<EOT
The command <info>%command.name%</info> indexes current configured database in configured search engine index.
Example usage:
- Refresh (add/update) index changes since yesterday:
<comment>ezplatform:reindex --since=yesterday</comment>
See: http://php.net/manual/en/datetime.formats.php
- Refresh (add/update/remove) index on a set of content id's:
<comment>ezplatform:reindex --content-ids=2,34,68</comment>
- Refresh (add/update) index of a subtree:
<comment>ezplatform:reindex --subtree=45</comment>
- Refresh (add/update) index disabling use of child proccesses and initial purging,
& let search engine handle commits using auto commit:
<comment>ezplatform:reindex --no-purge --no-commit --processes=0</comment>
EOT
);
}
/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$commit = !$input->getOption('no-commit');
$iterationCount = $input->getOption('iteration-count');
$this->siteaccess = $input->getOption('siteaccess');
if (!is_numeric($iterationCount) || (int) $iterationCount < 1) {
throw new RuntimeException("'--iteration-count' option should be > 0, got '{$iterationCount}'");
}
if (!$this->searchIndexer instanceof IncrementalIndexer) {
$output->writeln(<<<EOT
DEPRECATED:
Running indexing against an Indexer that has not been updated to use IncrementalIndexer abstract.
Options that won't be taken into account:
- since
- content-ids
- subtree
- processes
- no-purge
EOT
);
$this->searchIndexer->createSearchIndex($output, (int) $iterationCount, !$commit);
} else {
$output->writeln('Re-indexing started for search engine: ' . $this->searchIndexer->getName());
$output->writeln('');
$return = $this->indexIncrementally($input, $output, $iterationCount, $commit);
$output->writeln('');
$output->writeln('Finished re-indexing');
return $return;
}
}
protected function indexIncrementally(InputInterface $input, OutputInterface $output, $iterationCount, $commit)
{
if ($contentIds = $input->getOption('content-ids')) {
$contentIds = explode(',', $contentIds);
$output->writeln(sprintf(
'Indexing list of content id\'s (%s)' . ($commit ? ', with commit' : ''),
count($contentIds)
));
return $this->searchIndexer->updateSearchIndex($contentIds, $commit);
}
if ($since = $input->getOption('since')) {
$stmt = $this->getStatementContentSince(new DateTime($since));
$count = (int)$this->getStatementContentSince(new DateTime($since), true)->fetchColumn();
$purge = false;
} elseif ($locationId = (int) $input->getOption('subtree')) {
$stmt = $this->getStatementSubtree($locationId);
$count = (int) $this->getStatementSubtree($locationId, true)->fetchColumn();
$purge = false;
} else {
$stmt = $this->getStatementContentAll();
$count = (int) $this->getStatementContentAll(true)->fetchColumn();
$purge = !$input->getOption('no-purge');
}
if (!$count) {
$output->writeln('<error>Could not find any items to index, aborting.</error>');
return 1;
}
$iterations = ceil($count / $iterationCount);
$processes = $input->getOption('processes');
$processCount = $processes === 'auto' ? $this->getNumberOfCPUCores() - 1 : (int) $processes;
$processCount = min($iterations, $processCount);
$processMessage = $processCount > 1 ? "using $processCount parallel child processes" : 'using single (current) process';
if ($purge) {
$output->writeln('Purging index...');
$this->searchIndexer->purge();
$output->writeln(
"<info>Re-Creating index for {$count} items across $iterations iteration(s), $processMessage:</info>"
);
} else {
$output->writeln(
"<info>Refreshing index for {$count} items across $iterations iteration(s), $processMessage:</info>"
);
}
$progress = new ProgressBar($output);
$progress->start($iterations);
if ($processCount > 1) {
$this->runParallelProcess($progress, $stmt, (int) $processCount, (int) $iterationCount, $commit);
} else {
// if we only have one process, or less iterations to warrant running several, we index it all inline
foreach ($this->fetchIteration($stmt, $iterationCount) as $contentIds) {
$this->searchIndexer->updateSearchIndex($contentIds, $commit);
$progress->advance(1);
}
}
$progress->finish();
}
private function runParallelProcess(ProgressBar $progress, Statement $stmt, $processCount, $iterationCount, $commit)
{
/**
* @var \Symfony\Component\Process\Process[]|null[]
*/
$processes = array_fill(0, $processCount, null);
$generator = $this->fetchIteration($stmt, $iterationCount);
do {
foreach ($processes as $key => $process) {
if ($process !== null && $process->isRunning()) {
continue;
}
if ($process !== null) {
// One of the processes just finished, so we increment progress bar
$progress->advance(1);
if (!$process->isSuccessful()) {
$this->logger->error('Child indexer process returned: ' . $process->getExitCodeText());
}
}
if (!$generator->valid()) {
unset($processes[$key]);
continue;
}
$processes[$key] = $this->getPhpProcess($generator->current(), $commit);
$processes[$key]->start();
$generator->next();
}
if (!empty($processes)) {
sleep(1);
}
} while (!empty($processes));
}
/**
* @param DateTime $since
* @param bool $count
*
* @return \Doctrine\DBAL\Driver\Statement
*/
private function getStatementContentSince(DateTime $since, $count = false)
{
$q = $this->connection->createQueryBuilder()
->select($count ? 'count(c.id)' : 'c.id')
->from('ezcontentobject', 'c')
->where('c.status = :status')->andWhere('c.modified >= :since')
->orderBy('c.modified')
->setParameter('status', ContentInfo::STATUS_PUBLISHED, PDO::PARAM_INT)
->setParameter('since', $since->getTimestamp(), PDO::PARAM_INT);
return $q->execute();
}
/**
* @param mixed $locationId
* @param bool $count
*
* @return \Doctrine\DBAL\Driver\Statement
*/
private function getStatementSubtree($locationId, $count = false)
{
/**
* @var \eZ\Publish\SPI\Persistence\Content\Location\Handler
*/
$locationHandler = $this->getContainer()->get('ezpublish.spi.persistence.location_handler');
$location = $locationHandler->load($locationId);
$q = $this->connection->createQueryBuilder()
->select($count ? 'count(DISTINCT c.id)' : 'DISTINCT c.id')
->from('ezcontentobject', 'c')
->innerJoin('c', 'ezcontentobject_tree', 't', 't.contentobject_id = c.id')
->where('c.status = :status')
->andWhere('t.path_string LIKE :path')
->setParameter('status', ContentInfo::STATUS_PUBLISHED, PDO::PARAM_INT)
->setParameter('path', $location->pathString . '%', PDO::PARAM_STR);
return $q->execute();
}
/**
* @param bool $count
*
* @return \Doctrine\DBAL\Driver\Statement
*/
private function getStatementContentAll($count = false)
{
$q = $this->connection->createQueryBuilder()
->select($count ? 'count(c.id)' : 'c.id')
->from('ezcontentobject', 'c')
->where('c.status = :status')
->setParameter('status', ContentInfo::STATUS_PUBLISHED, PDO::PARAM_INT);
return $q->execute();
}
/**
* @param \Doctrine\DBAL\Driver\Statement $stmt
* @param int $iterationCount
*
* @return \Generator Return an array of arrays, each array contains content id's of $iterationCount.
*/
private function fetchIteration(Statement $stmt, $iterationCount)
{
do {
$contentIds = [];
for ($i = 0; $i < $iterationCount; ++$i) {
if ($contentId = $stmt->fetch(PDO::FETCH_COLUMN)) {
$contentIds[] = $contentId;
} else {
break;
}
}
yield $contentIds;
} while (!empty($contentId));
}
/**
* @param array $contentIds
* @param bool $commit
*
* @return \Symfony\Component\Process\Process
*/
private function getPhpProcess(array $contentIds, $commit)
{
$process = new ProcessBuilder(array_filter([
file_exists('bin/console') ? 'bin/console' : 'app/console',
$this->siteaccess ? '--siteaccess=' . $this->siteaccess : null,
'ezplatform:reindex',
'--content-ids=' . implode(',', $contentIds),
]));
$process->setTimeout(null);
$process->setPrefix($this->getPhpPath());
if (!$commit) {
$process->add('--no-commit');
}
return $process->getProcess();
}
/**
* @return string
*/
private function getPhpPath()
{
if ($this->phpPath) {
return $this->phpPath;
}
$phpFinder = new PhpExecutableFinder();
$this->phpPath = $phpFinder->find();
if (!$this->phpPath) {
throw new \RuntimeException(
'The php executable could not be found, it\'s needed for executing parable sub processes, so add it to your PATH environment variable and try again'
);
}
return $this->phpPath;
}
/**
* @return int
*/
private function getNumberOfCPUCores()
{
$cores = 1;
if (is_file('/proc/cpuinfo')) {
// Linux (and potentially Windows with linux sub systems)
$cpuinfo = file_get_contents('/proc/cpuinfo');
preg_match_all('/^processor/m', $cpuinfo, $matches);
$cores = count($matches[0]);
} elseif (DIRECTORY_SEPARATOR === '\\') {
// Windows
if (($process = @popen('wmic cpu get NumberOfCores', 'rb')) !== false) {
fgets($process);
$cores = (int) fgets($process);
pclose($process);
}
} elseif (($process = @popen('sysctl -a', 'rb')) !== false) {
// *nix (Linux, BSD and Mac)
$output = stream_get_contents($process);
if (preg_match('/hw.ncpu: (\d+)/', $output, $matches)) {
$cores = (int) $matches[1][0];
}
pclose($process);
}
return $cores;
}
}