Skip to content

Commit 6c29df0

Browse files
committed
WIP: BUGFIX: Fix alias creation
1 parent 31c66e9 commit 6c29df0

3 files changed

Lines changed: 128 additions & 2 deletions

File tree

Classes/Command/NodeIndexCommandController.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
use Doctrine\Common\Collections\ArrayCollection;
1717
use Flowpack\ElasticSearch\ContentRepositoryAdaptor\Driver\NodeTypeMappingBuilderInterface;
18+
use Flowpack\ElasticSearch\ContentRepositoryAdaptor\Exception\ConfigurationException;
1819
use Flowpack\ElasticSearch\ContentRepositoryAdaptor\Exception\RuntimeException;
1920
use Flowpack\ElasticSearch\ContentRepositoryAdaptor\Indexer\Error\ErrorInterface;
2021
use Flowpack\ElasticSearch\ContentRepositoryAdaptor\Indexer\NodeIndexer;
@@ -213,6 +214,7 @@ public function indexNodeCommand(string $identifier, string $workspace = null, s
213214
* @param string $postfix Index postfix, index with the same postfix will be deleted if exist
214215
* @return void
215216
* @throws StopCommandException
217+
* @throws \Flowpack\ElasticSearch\ContentRepositoryAdaptor\Exception
216218
*/
217219
public function buildCommand(int $limit = null, bool $update = false, string $workspace = null, string $postfix = null): void
218220
{
@@ -394,6 +396,7 @@ public function buildWorkspaceInternalCommand(string $workspace, string $dimensi
394396
* @throws \Flowpack\ElasticSearch\ContentRepositoryAdaptor\Exception
395397
* @throws \Flowpack\ElasticSearch\Exception
396398
* @throws \Neos\Flow\Http\Exception
399+
* @throws ConfigurationException
397400
* @Flow\Internal
398401
*/
399402
public function refreshInternalCommand(string $dimensionsValues, string $postfix): void
@@ -413,6 +416,7 @@ public function refreshInternalCommand(string $dimensionsValues, string $postfix
413416
* @throws \Flowpack\ElasticSearch\ContentRepositoryAdaptor\Exception
414417
* @throws \Flowpack\ElasticSearch\Exception
415418
* @throws ApiException
419+
* @throws ConfigurationException
416420
* @Flow\Internal
417421
*/
418422
public function aliasInternalCommand(string $dimensionsValues, string $postfix, bool $update = false): void

Classes/Indexer/NodeIndexer.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,7 @@ protected function bulkRequestLength(): int
434434
* @throws Exception
435435
* @throws \Flowpack\ElasticSearch\Exception
436436
* @throws FilesException
437+
* @throws Exception\ConfigurationException
437438
*/
438439
public function flush(): void
439440
{
@@ -511,11 +512,11 @@ public function updateIndexAlias(): void
511512
{
512513
$aliasName = $this->searchClient->getIndexName(); // The alias name is the unprefixed index name
513514
if ($this->getIndexName() === $aliasName) {
514-
throw new Exception('UpdateIndexAlias is only allowed to be called when $this->setIndexNamePostfix has been created.', 1383649061);
515+
throw new Exception('UpdateIndexAlias is only allowed to be called when setIndexNamePostfix() has been called.', 1383649061);
515516
}
516517

517518
if (!$this->getIndex()->exists()) {
518-
throw new Exception('The target index for updateIndexAlias does not exist. This shall never happen.', 1383649125);
519+
throw new Exception(sprintf('The target index "%s" does not exist.', $this->getIndex()->getName()), 1383649125);
519520
}
520521

521522
$aliasActions = [];
@@ -525,6 +526,7 @@ public function updateIndexAlias(): void
525526
// if there is an actual index with the name we want to use as alias, remove it now
526527
$this->indexDriver->deleteIndex($aliasName);
527528
} else {
529+
// Remove all existing aliasses
528530
foreach ($indexNames as $indexName) {
529531
$aliasActions[] = [
530532
'remove' => [
@@ -554,6 +556,7 @@ public function updateIndexAlias(): void
554556
/**
555557
* Update the main alias to allow to query all indices at once
556558
* @throws Exception
559+
* @throws Exception\ConfigurationException
557560
*/
558561
public function updateMainAlias()
559562
{
@@ -619,6 +622,7 @@ public function updateMainAlias()
619622
*
620623
* @return array<string> a list of index names which were removed
621624
* @throws Exception
625+
* @throws Exception\ConfigurationException
622626
*/
623627
public function removeOldIndices(): array
624628
{
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Flowpack\ElasticSearch\ContentRepositoryAdaptor\Tests\Functional\Indexer;
5+
6+
/*
7+
* This file is part of the Flowpack.ElasticSearch.ContentRepositoryAdaptor package.
8+
*
9+
* (c) Contributors of the Neos Project - www.neos.io
10+
*
11+
* This package is Open Source Software. For the full copyright and license
12+
* information, please view the LICENSE file which was distributed with this
13+
* source code.
14+
*/
15+
16+
use Flowpack\ElasticSearch\ContentRepositoryAdaptor\ElasticSearchClient;
17+
use Flowpack\ElasticSearch\ContentRepositoryAdaptor\Exception\ConfigurationException;
18+
use Flowpack\ElasticSearch\ContentRepositoryAdaptor\Indexer\NodeIndexer;
19+
use Flowpack\ElasticSearch\ContentRepositoryAdaptor\Service\DimensionsService;
20+
use Flowpack\ElasticSearch\Exception;
21+
use Flowpack\ElasticSearch\Transfer\Exception\ApiException;
22+
use Neos\Flow\Tests\FunctionalTestCase;
23+
24+
class NodeIndexerTest extends FunctionalTestCase
25+
{
26+
const TESTING_INDEX_PREFIX = 'neoscr_testing';
27+
28+
/**
29+
* @var NodeIndexer
30+
*/
31+
protected $nodeIndexer;
32+
33+
/**
34+
* @var DimensionsService
35+
*/
36+
protected $dimensionService;
37+
38+
/**
39+
* @var ElasticSearchClient
40+
*/
41+
protected $searchClient;
42+
43+
public function setUp(): void
44+
{
45+
parent::setUp();
46+
$this->searchClient = $this->objectManager->get(ElasticSearchClient::class);
47+
$this->nodeIndexer = $this->objectManager->get(NodeIndexer::class);
48+
$this->dimensionService = $this->objectManager->get(DimensionsService::class);
49+
}
50+
51+
protected function tearDown(): void
52+
{
53+
parent::tearDown();
54+
$this->searchClient->request('DELETE', '/' . self::TESTING_INDEX_PREFIX . '*');
55+
}
56+
57+
/**
58+
* @test
59+
*/
60+
public function getIndexWithoutDimensionConfigured(): void
61+
{
62+
$this->nodeIndexer->setDimensions([]);
63+
$index = $this->nodeIndexer->getIndex();
64+
static::assertEquals(self::TESTING_INDEX_PREFIX . '-default', $index->getName());
65+
}
66+
67+
/**
68+
* @test
69+
*
70+
* @throws \Flowpack\ElasticSearch\ContentRepositoryAdaptor\Exception
71+
* @throws ConfigurationException
72+
* @throws Exception
73+
*/
74+
public function getIndexForDimensionConfiguration(): void
75+
{
76+
$dimensionValues = ['language' => ['de']];
77+
$this->nodeIndexer->setDimensions($dimensionValues);
78+
$index = $this->nodeIndexer->getIndex();
79+
$dimesionHash = $this->dimensionService->hash($dimensionValues);
80+
81+
static::assertEquals(self::TESTING_INDEX_PREFIX . '-' . $dimesionHash, $index->getName());
82+
}
83+
84+
/**
85+
* @test
86+
*/
87+
public function updateIndexAlias(): void
88+
{
89+
$dimensionValues = ['language' => ['de']];
90+
$this->nodeIndexer->setDimensions($dimensionValues);
91+
$this->nodeIndexer->setIndexNamePostfix((string)time());
92+
$this->nodeIndexer->getIndex()->create();
93+
94+
$this->assertIndexExists($this->nodeIndexer->getIndexName());
95+
$this->nodeIndexer->updateIndexAlias();
96+
97+
$this->assertAliasesEquals(self::TESTING_INDEX_PREFIX, [$this->nodeIndexer->getIndexName()]);
98+
}
99+
100+
101+
/**
102+
* @param string $indexName
103+
* @throws \Flowpack\ElasticSearch\Transfer\Exception
104+
* @throws ApiException
105+
* @throws \Neos\Flow\Http\Exception
106+
*/
107+
protected function assertIndexExists(string $indexName): void
108+
{
109+
$response = $this->searchClient->request('HEAD', '/' . $indexName);
110+
self::assertEquals(200, $response->getStatusCode());
111+
}
112+
113+
protected function assertAliasesEquals(string $aliasPrefix, array $expectdAliases): void
114+
{
115+
$content = $this->searchClient->request('GET', '/_alias/' . $aliasPrefix . '*')->getTreatedContent();
116+
static::assertEquals($expectdAliases, array_keys($content));
117+
}
118+
}

0 commit comments

Comments
 (0)