-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathNodeIndexCommandController.php
More file actions
643 lines (555 loc) · 23.1 KB
/
NodeIndexCommandController.php
File metadata and controls
643 lines (555 loc) · 23.1 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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
<?php
declare(strict_types=1);
namespace Flowpack\ElasticSearch\ContentRepositoryAdaptor\Command;
/*
* This file is part of the Flowpack.ElasticSearch.ContentRepositoryAdaptor package.
*
* (c) Contributors of the Neos Project - www.neos.io
*
* This package is Open Source Software. For the full copyright and license
* information, please view the LICENSE file which was distributed with this
* source code.
*/
use Doctrine\Common\Collections\ArrayCollection;
use Flowpack\ElasticSearch\ContentRepositoryAdaptor\Driver\IndexDriverInterface;
use Flowpack\ElasticSearch\ContentRepositoryAdaptor\Driver\NodeTypeMappingBuilderInterface;
use Flowpack\ElasticSearch\ContentRepositoryAdaptor\ElasticSearchClient;
use Flowpack\ElasticSearch\ContentRepositoryAdaptor\ErrorHandling\ErrorHandlingService;
use Flowpack\ElasticSearch\ContentRepositoryAdaptor\Exception;
use Flowpack\ElasticSearch\ContentRepositoryAdaptor\Exception\ConfigurationException;
use Flowpack\ElasticSearch\ContentRepositoryAdaptor\Exception\RuntimeException;
use Flowpack\ElasticSearch\ContentRepositoryAdaptor\Indexer\NodeIndexer;
use Flowpack\ElasticSearch\ContentRepositoryAdaptor\Indexer\WorkspaceIndexer;
use Neos\ContentRepository\Search\Indexer\NodeIndexerInterface;
use Flowpack\ElasticSearch\Domain\Model\Mapping;
use Flowpack\ElasticSearch\Transfer\Exception\ApiException;
use Neos\ContentRepository\Domain\Model\Workspace;
use Neos\ContentRepository\Domain\Repository\NodeDataRepository;
use Neos\ContentRepository\Domain\Repository\WorkspaceRepository;
use Neos\ContentRepository\Domain\Service\ContentDimensionCombinator;
use Neos\ContentRepository\Domain\Service\ContentDimensionPresetSourceInterface;
use Neos\ContentRepository\Domain\Service\Context;
use Neos\ContentRepository\Domain\Service\ContextFactoryInterface;
use Neos\Flow\Annotations as Flow;
use Neos\Flow\Cli\CommandController;
use Neos\Flow\Cli\Exception\StopCommandException;
use Neos\Flow\Configuration\ConfigurationManager;
use Neos\Flow\Core\Booting\Exception\SubProcessException;
use Neos\Flow\Core\Booting\Scripts;
use Neos\Flow\Log\Utility\LogEnvironment;
use Neos\Utility\Files;
use Psr\Log\LoggerInterface;
/**
* Provides CLI features for index handling
*
* @Flow\Scope("singleton")
*/
class NodeIndexCommandController extends CommandController
{
/**
* @Flow\InjectConfiguration(package="Neos.Flow")
* @var array
*/
protected $flowSettings;
/**
* @var array
* @Flow\InjectConfiguration(package="Neos.ContentRepository.Search")
*/
protected $settings;
/**
* @var bool
* @Flow\InjectConfiguration(path="command.useSubProcesses")
*/
protected $useSubProcesses = true;
/**
* @Flow\Inject
* @var ErrorHandlingService
*/
protected $errorHandlingService;
/**
* @Flow\Inject
* @var NodeIndexer
*/
protected $nodeIndexer;
/**
* @Flow\Inject
* @var WorkspaceRepository
*/
protected $workspaceRepository;
/**
* @Flow\Inject
* @var NodeDataRepository
*/
protected $nodeDataRepository;
/**
* @Flow\Inject
* @var ContentDimensionPresetSourceInterface
*/
protected $contentDimensionPresetSource;
/**
* @Flow\Inject
* @var NodeTypeMappingBuilderInterface
*/
protected $nodeTypeMappingBuilder;
/**
* @Flow\Inject
* @var LoggerInterface
*/
protected $logger;
/**
* @Flow\Inject
* @var ConfigurationManager
*/
protected $configurationManager;
/**
* @Flow\Inject
* @var ContextFactoryInterface
*/
protected $contextFactory;
/**
* @Flow\Inject
* @var ContentDimensionCombinator
*/
protected $contentDimensionCombinator;
/**
* @Flow\Inject
* @var WorkspaceIndexer
*/
protected $workspaceIndexer;
/**
* @Flow\Inject
* @var ElasticSearchClient
*/
protected $searchClient;
/**
* @var IndexDriverInterface
* @Flow\Inject
*/
protected $indexDriver;
/**
* @Flow\Inject
* @var NodeIndexerInterface
*/
protected $nodeIndexerInterface;
/**
* Index a single node by the given identifier and workspace name
*
* @param string $identifier
* @param string|null $workspace
* @param string|null $postfix
* @return void
* @throws ApiException
* @throws ConfigurationException
* @throws Exception
* @throws RuntimeException
* @throws SubProcessException
* @throws \Flowpack\ElasticSearch\Exception
*/
public function indexNodeCommand(string $identifier, ?string $workspace = null, ?string $postfix = null): void
{
if ($workspace === null && $this->settings['indexAllWorkspaces'] === false) {
$workspace = 'live';
}
$updateAliases = false;
if ($postfix !== null) {
$this->nodeIndexer->setIndexNamePostfix($postfix);
} elseif ($this->aliasesExist() === false) {
$postfix = (string)time();
$updateAliases = true;
$this->nodeIndexer->setIndexNamePostfix($postfix);
}
$indexNode = function ($identifier, Workspace $workspace, array $dimensions) {
$context = $this->createContentContext($workspace->getName(), $dimensions);
$node = $context->getNodeByIdentifier($identifier);
if ($node === null) {
return [$workspace->getName(), '-', json_encode($dimensions), 'not found'];
}
$this->nodeIndexer->setDimensions($dimensions);
$this->nodeIndexer->indexNode($node);
return [$workspace->getName(), $node->getNodeType()->getName(), json_encode($dimensions), '<success>indexed</success>'];
};
$indexInWorkspace = function ($identifier, Workspace $workspace) use ($indexNode) {
$combinations = $this->contentDimensionCombinator->getAllAllowedCombinations();
$results = [];
if ($combinations === []) {
$results[] = $indexNode($identifier, $workspace, []);
} else {
foreach ($combinations as $combination) {
$results[] = $indexNode($identifier, $workspace, $combination);
}
}
$this->output->outputTable($results, ['Workspace', 'NodeType', 'Dimensions', 'State']);
};
if ($workspace === null) {
/** @var Workspace $iteratedWorkspace */
foreach ($this->workspaceRepository->findAll() as $iteratedWorkspace) {
$indexInWorkspace($identifier, $iteratedWorkspace);
}
} else {
$workspaceInstance = $this->workspaceRepository->findByIdentifier($workspace);
if ($workspaceInstance === null) {
$this->outputLine('<error>Error: The given workspace (%s) does not exist.</error>', [$workspace]);
$this->quit(1);
}
$indexInWorkspace($identifier, $workspaceInstance);
}
$this->nodeIndexer->flush();
if ($updateAliases) {
$combinations = $this->contentDimensionCombinator->getAllAllowedCombinations();
$combinations = $combinations === [] ? [[]] : $combinations;
foreach ($combinations as $combination) {
$this->executeInternalCommand('aliasInternal', [
'dimensionsValues' => json_encode($combination),
'postfix' => $postfix,
'update' => false
]);
}
$this->nodeIndexer->updateMainAlias();
}
$this->outputErrorHandling();
}
/**
* Index all nodes by creating a new index and when everything was completed, switch the index alias.
*
* This command (re-)indexes all nodes contained in the content repository and sets the schema beforehand.
*
* @param int|null $limit Amount of nodes to index at maximum
* @param bool $update if TRUE, do not throw away the index at the start. Should *only be used for development*.
* @param string|null $workspace name of the workspace which should be indexed
* @param string|null $postfix Index postfix, index with the same postfix will be deleted if exist
* @return void
* @throws StopCommandException
* @throws Exception
* @throws ConfigurationException
* @throws ApiException
*/
public function buildCommand(?int $limit = null, bool $update = false, ?string $workspace = null, ?string $postfix = null): void
{
$this->logger->info(sprintf('Starting elasticsearch indexing %s sub processes', $this->useSubProcesses ? 'with' : 'without'), LogEnvironment::fromMethodName(__METHOD__));
if ($workspace !== null && $this->workspaceRepository->findByIdentifier($workspace) === null) {
$this->logger->error('The given workspace (' . $workspace . ') does not exist.', LogEnvironment::fromMethodName(__METHOD__));
$this->quit(1);
}
$postfix = (string)($postfix ?: time());
$this->nodeIndexer->setIndexNamePostfix($postfix);
$createIndicesAndApplyMapping = function (array $dimensionsValues) use ($update, $postfix) {
$this->executeInternalCommand('createInternal', [
'dimensionsValues' => json_encode($dimensionsValues),
'update' => $update,
'postfix' => $postfix,
]);
};
$buildIndex = function (array $dimensionsValues) use ($workspace, $limit, $postfix) {
$this->build($dimensionsValues, $workspace, $postfix, $limit);
};
$refresh = function (array $dimensionsValues) use ($postfix) {
$this->executeInternalCommand('refreshInternal', [
'dimensionsValues' => json_encode($dimensionsValues),
'postfix' => $postfix,
]);
};
$updateAliases = function (array $dimensionsValues) use ($update, $postfix) {
$this->executeInternalCommand('aliasInternal', [
'dimensionsValues' => json_encode($dimensionsValues),
'postfix' => $postfix,
'update' => $update,
]);
};
$combinations = new ArrayCollection($this->contentDimensionCombinator->getAllAllowedCombinations());
$runAndLog = function ($command, string $stepInfo) use ($combinations) {
$timeStart = microtime(true);
$this->output(str_pad($stepInfo . '... ', 20));
$combinations->map($command);
$this->outputLine('<success>Done</success> (took %s seconds)', [number_format(microtime(true) - $timeStart, 2)]);
};
$runAndLog($createIndicesAndApplyMapping, 'Creating indices and apply mapping');
if ($this->aliasesExist() === false) {
$runAndLog($updateAliases, 'Set up aliases');
}
$runAndLog($buildIndex, 'Indexing nodes');
$runAndLog($refresh, 'Refresh indicies');
$runAndLog($updateAliases, 'Update aliases');
$this->outputLine('Update main alias');
$this->nodeIndexer->updateMainAlias();
$this->outputLine();
$this->outputMemoryUsage();
}
/**
* @return bool
* @throws ApiException
* @throws ConfigurationException
* @throws Exception
*/
private function aliasesExist(): bool
{
$aliasName = $this->searchClient->getIndexName();
$aliasesExist = false;
try {
$aliasesExist = $this->indexDriver->getIndexNamesByAlias($aliasName) !== [];
} catch (ApiException $exception) {
// in case of 404, do not throw an error...
if ($exception->getResponse()->getStatusCode() !== 404) {
throw $exception;
}
}
return $aliasesExist;
}
/**
* Build up the node index
*
* @param array $dimensionsValues
* @param string|null $workspace
* @param string|null $postfix
* @param int|null $limit
* @throws ConfigurationException
* @throws Exception
* @throws RuntimeException
* @throws SubProcessException
*/
private function build(array $dimensionsValues, ?string $workspace = null, ?string $postfix = null, ?int $limit = null): void
{
$dimensionsValues = $this->configureNodeIndexer($dimensionsValues, $postfix);
$this->logger->info(vsprintf('Indexing %s nodes to %s', [($limit !== null ? 'the first ' . $limit . ' ' : ''), $this->nodeIndexer->getIndexName()]), LogEnvironment::fromMethodName(__METHOD__));
if ($workspace === null && $this->settings['indexAllWorkspaces'] === false) {
$workspace = 'live';
}
$buildWorkspaceCommandOptions = static function ($workspace, array $dimensionsValues, ?int $limit, ?string $postfix) {
return [
'workspace' => $workspace instanceof Workspace ? $workspace->getName() : $workspace,
'dimensionsValues' => json_encode($dimensionsValues),
'postfix' => $postfix,
'limit' => $limit,
];
};
$output = '';
if ($workspace === null) {
foreach ($this->workspaceRepository->findAll() as $iteratedWorkspace) {
$output .= $this->executeInternalCommand('buildWorkspaceInternal', $buildWorkspaceCommandOptions($iteratedWorkspace, $dimensionsValues, $limit, $postfix));
}
} else {
$output = $this->executeInternalCommand('buildWorkspaceInternal', $buildWorkspaceCommandOptions($workspace, $dimensionsValues, $limit, $postfix));
}
$outputArray = explode(PHP_EOL, $output);
if (count($outputArray) > 0) {
foreach ($outputArray as $line) {
$line = trim($line);
if ($line === '') {
continue;
}
$this->outputLine('<info>+</info> %s', [$line]);
}
}
$this->outputErrorHandling();
}
/**
* Internal sub-command to create an index and apply the mapping
*
* @param string $dimensionsValues
* @param bool $update
* @param string|null $postfix
* @throws Exception
* @throws \Flowpack\ElasticSearch\Exception
* @throws \Neos\Flow\Http\Exception
* @throws \Exception
* @Flow\Internal
*/
public function createInternalCommand(string $dimensionsValues, bool $update = false, ?string $postfix = null): void
{
if ($update === true) {
$this->logger->warning('!!! Update Mode (Development) active!', LogEnvironment::fromMethodName(__METHOD__));
} else {
$dimensionsValuesArray = $this->configureNodeIndexer(json_decode($dimensionsValues, true), $postfix);
if ($this->nodeIndexer->getIndex()->exists() === true) {
$this->logger->warning(sprintf('Deleted index with the same postfix (%s)!', $postfix), LogEnvironment::fromMethodName(__METHOD__));
$this->nodeIndexer->getIndex()->delete();
}
$this->nodeIndexer->getIndex()->create();
$this->logger->info('Created index ' . $this->nodeIndexer->getIndexName() . ' with dimensions ' . json_encode($dimensionsValuesArray), LogEnvironment::fromMethodName(__METHOD__));
}
$this->applyMapping();
$this->outputErrorHandling();
}
/**
* @param string $workspace
* @param string $dimensionsValues
* @param string $postfix
* @param int|null $limit
* @return void
* @Flow\Internal
*/
public function buildWorkspaceInternalCommand(string $workspace, string $dimensionsValues, string $postfix, ?int $limit = null): void
{
$dimensionsValuesArray = $this->configureNodeIndexer(json_decode($dimensionsValues, true), $postfix);
$workspaceLogger = function ($workspaceName, $indexedNodes, $dimensions) {
if ($dimensions === []) {
$message = 'Workspace "' . $workspaceName . '" without dimensions done. (Indexed ' . $indexedNodes . ' nodes)';
} else {
$message = 'Workspace "' . $workspaceName . '" and dimensions "' . json_encode($dimensions) . '" done. (Indexed ' . $indexedNodes . ' nodes)';
}
$this->outputLine($message);
};
$this->workspaceIndexer->indexWithDimensions($workspace, $dimensionsValuesArray, $limit, $workspaceLogger);
$this->outputErrorHandling();
}
/**
* Internal subcommand to refresh the index
*
* @param string $dimensionsValues
* @param string $postfix
* @throws Exception
* @throws \Flowpack\ElasticSearch\Exception
* @throws \Neos\Flow\Http\Exception
* @throws ConfigurationException
* @Flow\Internal
*/
public function refreshInternalCommand(string $dimensionsValues, string $postfix): void
{
$this->configureNodeIndexer(json_decode($dimensionsValues, true), $postfix);
$this->logger->info(vsprintf('Refreshing index %s', [$this->nodeIndexer->getIndexName()]), LogEnvironment::fromMethodName(__METHOD__));
$this->nodeIndexer->getIndex()->refresh();
$this->outputErrorHandling();
}
/**
* @param string $dimensionsValues
* @param string $postfix
* @param bool $update
* @throws Exception
* @throws \Flowpack\ElasticSearch\Exception
* @throws ApiException
* @throws ConfigurationException
* @Flow\Internal
*/
public function aliasInternalCommand(string $dimensionsValues, string $postfix, bool $update = false): void
{
if ($update === true) {
return;
}
$this->configureNodeIndexer(json_decode($dimensionsValues, true), $postfix);
$this->logger->info(vsprintf('Update alias for index %s', [$this->nodeIndexer->getIndexName()]), LogEnvironment::fromMethodName(__METHOD__));
$this->nodeIndexer->updateIndexAlias();
$this->outputErrorHandling();
}
/**
* @param array $dimensionsValues
* @param string $postfix
* @return array
*/
private function configureNodeIndexer(array $dimensionsValues, string $postfix): array
{
$this->nodeIndexer->setIndexNamePostfix($postfix);
$this->nodeIndexer->setDimensions($dimensionsValues);
// Also configure the interface instance (separate singleton when QueueIndexer is installed)
if ($this->nodeIndexerInterface instanceof NodeIndexer && $this->nodeIndexerInterface !== $this->nodeIndexer) {
$this->nodeIndexerInterface->setIndexNamePostfix($postfix);
$this->nodeIndexerInterface->setDimensions($dimensionsValues);
}
return $dimensionsValues;
}
/**
* Clean up old indexes (i.e. all but the current one)
*
* @return void
* @throws ConfigurationException
* @throws Exception
*/
public function cleanupCommand(): void
{
$removed = false;
$combinations = $this->contentDimensionCombinator->getAllAllowedCombinations();
foreach ($combinations as $dimensionsValues) {
try {
$this->nodeIndexer->setDimensions($dimensionsValues);
$removedIndices = $this->nodeIndexer->removeOldIndices();
foreach ($removedIndices as $indexToBeRemoved) {
$removed = true;
$this->logger->info('Removing old index ' . $indexToBeRemoved, LogEnvironment::fromMethodName(__METHOD__));
}
} catch (ApiException $exception) {
$exception->getResponse()->getBody()->rewind();
$response = json_decode($exception->getResponse()->getBody()->getContents(), false);
$message = sprintf('Nothing removed. ElasticSearch responded with status %s', $response->status);
if (isset($response->error->type)) {
$this->logger->error(sprintf('%s, saying "%s: %s"', $message, $response->error->type, $response->error->reason), LogEnvironment::fromMethodName(__METHOD__));
} else {
$this->logger->error(sprintf('%s, saying "%s"', $message, $response->error), LogEnvironment::fromMethodName(__METHOD__));
}
}
}
if ($removed === false) {
$this->logger->info('Nothing to remove.', LogEnvironment::fromMethodName(__METHOD__));
}
}
private function outputErrorHandling(): void
{
if ($this->errorHandlingService->hasError() === false) {
return;
}
$this->outputLine();
$this->outputLine('<error>%s Errors where returned while indexing. Check your logs for more information.</error>', [$this->errorHandlingService->getErrorCount()]);
}
/**
* @param string $command
* @param array $arguments
* @return string
* @throws RuntimeException
* @throws SubProcessException
*/
private function executeInternalCommand(string $command, array $arguments): string
{
ob_start(null, 1 << 20);
if ($this->useSubProcesses) {
$commandIdentifier = 'flowpack.elasticsearch.contentrepositoryadaptor:nodeindex:' . $command;
$status = Scripts::executeCommand($commandIdentifier, $this->flowSettings, true, array_filter($arguments));
if ($status !== true) {
throw new RuntimeException(vsprintf('Command: %s with parameters: %s', [$commandIdentifier, json_encode($arguments)]), 1426767159);
}
} else {
$commandIdentifier = $command . 'Command';
call_user_func_array([self::class, $commandIdentifier], $arguments);
}
return ob_get_clean();
}
/**
* Create a ContentContext based on the given workspace name
*
* @param string $workspaceName Name of the workspace to set for the context
* @param array $dimensions Optional list of dimensions and their values which should be set
* @return Context
*/
private function createContentContext(string $workspaceName, array $dimensions = []): Context
{
$contextProperties = [
'workspaceName' => $workspaceName,
'invisibleContentShown' => true,
'inaccessibleContentShown' => true
];
if ($dimensions !== []) {
$contextProperties['dimensions'] = $dimensions;
$contextProperties['targetDimensions'] = array_map(static function ($dimensionValues) {
return array_shift($dimensionValues);
}, $dimensions);
}
return $this->contextFactory->create($contextProperties);
}
/**
* Apply the mapping to the current index.
*
* @return void
* @throws Exception
* @throws \Flowpack\ElasticSearch\Exception
* @throws ConfigurationException
* @throws \Neos\Flow\Http\Exception
*/
private function applyMapping(): void
{
$nodeTypeMappingCollection = $this->nodeTypeMappingBuilder->buildMappingInformation($this->nodeIndexer->getIndex());
foreach ($nodeTypeMappingCollection as $mapping) {
/** @var Mapping $mapping */
$mapping->apply();
}
}
private function outputMemoryUsage(): void
{
$this->outputLine('! Memory usage %s', [Files::bytesToSizeString(memory_get_usage(true))]);
}
}