Skip to content

Commit 5f822d9

Browse files
committed
BUGFIX: Filter indexes with dash in prefix correctly
1 parent ea4cf7f commit 5f822d9

5 files changed

Lines changed: 110 additions & 25 deletions

File tree

Classes/Driver/IndexDriverInterface.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,22 @@
1919
*/
2020
interface IndexDriverInterface
2121
{
22+
2223
/**
2324
* Get the list of Indexes attached to the given alias
2425
*
2526
* @param string $alias
2627
* @return array
2728
*/
28-
public function indexesByAlias(string $alias): array;
29+
public function getIndexeNamesByAlias(string $alias): array;
2930

3031
/**
3132
* Get the list of Indexes attached to the given alias prefix
3233
*
3334
* @param string $prefix
3435
* @return array
3536
*/
36-
public function indexesByPrefix(string $prefix): array;
37+
public function getIndexeNamesByPrefix(string $prefix): array;
3738

3839
/**
3940
* Remove alias by name

Classes/Driver/Version5/IndexDriver.php

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
use Flowpack\ElasticSearch\ContentRepositoryAdaptor\Driver\AbstractDriver;
1717
use Flowpack\ElasticSearch\ContentRepositoryAdaptor\Driver\IndexDriverInterface;
1818
use Flowpack\ElasticSearch\ContentRepositoryAdaptor\Exception;
19+
use Flowpack\ElasticSearch\ContentRepositoryAdaptor\Service\IndexNameService;
20+
use Flowpack\ElasticSearch\Transfer\Exception as TransferException;
21+
use Flowpack\ElasticSearch\Transfer\Exception\ApiException;
1922
use Neos\Flow\Annotations as Flow;
2023

2124
/**
@@ -36,8 +39,8 @@ public function aliasActions(array $actions)
3639
/**
3740
* @param string $index
3841
* @throws Exception
39-
* @throws \Flowpack\ElasticSearch\Transfer\Exception
40-
* @throws \Flowpack\ElasticSearch\Transfer\Exception\ApiException
42+
* @throws TransferException
43+
* @throws ApiException
4144
* @throws \Neos\Flow\Http\Exception
4245
*/
4346
public function deleteIndex(string $index): void
@@ -55,11 +58,11 @@ public function deleteIndex(string $index): void
5558
* @param string $alias
5659
* @return array
5760
* @throws Exception
58-
* @throws \Flowpack\ElasticSearch\Transfer\Exception
59-
* @throws \Flowpack\ElasticSearch\Transfer\Exception\ApiException
61+
* @throws TransferException
62+
* @throws ApiException
6063
* @throws \Neos\Flow\Http\Exception
6164
*/
62-
public function indexesByAlias(string $alias): array
65+
public function getIndexeNamesByAlias(string $alias): array
6366
{
6467
$response = $this->searchClient->request('GET', '/_alias/' . $alias);
6568
$statusCode = $response->getStatusCode();
@@ -76,21 +79,21 @@ public function indexesByAlias(string $alias): array
7679
/**
7780
* @param string $prefix
7881
* @return array
79-
* @throws \Flowpack\ElasticSearch\Transfer\Exception
80-
* @throws \Flowpack\ElasticSearch\Transfer\Exception\ApiException
82+
* @throws TransferException
83+
* @throws ApiException
8184
* @throws \Neos\Flow\Http\Exception
8285
*/
83-
public function indexesByPrefix(string $prefix): array
86+
public function getIndexeNamesByPrefix(string $prefix): array
8487
{
85-
$response = $this->searchClient->request('GET', '/_alias/');
88+
$treatedContent = $this->searchClient->request('GET', '/_alias/')->getTreatedContent();
8689

8790
// return empty array if content from response cannot be read as an array
88-
$treatedContent = $response->getTreatedContent();
8991
if (!\is_array($treatedContent)) {
9092
return [];
9193
}
94+
9295
return \array_filter(\array_keys($treatedContent), static function ($indexName) use ($prefix) {
93-
$prefix .= '-';
96+
$prefix .= IndexNameService::INDEX_PART_SEPARATOR;
9497
return strpos($indexName, $prefix) === 0;
9598
});
9699
}

Classes/Indexer/NodeIndexer.php

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
use Flowpack\ElasticSearch\ContentRepositoryAdaptor\Indexer\Error\MalformedBulkRequestError;
2727
use Flowpack\ElasticSearch\ContentRepositoryAdaptor\Service\DimensionsService;
2828
use Flowpack\ElasticSearch\ContentRepositoryAdaptor\Service\ErrorHandlingService;
29+
use Flowpack\ElasticSearch\ContentRepositoryAdaptor\Service\IndexNameService;
2930
use Flowpack\ElasticSearch\ContentRepositoryAdaptor\Service\NodeTypeIndexingConfiguration;
3031
use Flowpack\ElasticSearch\Domain\Model\Document as ElasticSearchDocument;
3132
use Flowpack\ElasticSearch\Domain\Model\Index;
@@ -169,7 +170,7 @@ public function getIndexName(): string
169170
{
170171
$indexName = $this->searchClient->getIndexName();
171172
if ($this->indexNamePostfix !== '') {
172-
$indexName .= '-' . $this->indexNamePostfix;
173+
$indexName .= IndexNameService::INDEX_PART_SEPARATOR . $this->indexNamePostfix;
173174
}
174175

175176
return $indexName;
@@ -506,7 +507,7 @@ public function updateIndexAlias(): void
506507

507508
$aliasActions = [];
508509
try {
509-
$indexNames = $this->indexDriver->indexesByAlias($aliasName);
510+
$indexNames = $this->indexDriver->getIndexeNamesByAlias($aliasName);
510511
if ($indexNames === []) {
511512
// if there is an actual index with the name we want to use as alias, remove it now
512513
$this->indexDriver->deleteIndex($aliasName);
@@ -539,22 +540,18 @@ public function updateIndexAlias(): void
539540

540541
/**
541542
* Update the main alias to allow to query all indices at once
543+
* @throws Exception
542544
*/
543545
public function updateMainAlias()
544546
{
545547
$aliasActions = [];
546548
$aliasNamePrefix = $this->searchClient->getIndexNamePrefix(); // The alias name is the unprefixed index name
547549

548-
$indexNames = $this->indexDriver->indexesByPrefix($aliasNamePrefix);
549-
$indexNames = \array_values(\array_filter($indexNames, function ($indexName) {
550-
$suffix = '-' . $this->indexNamePostfix;
551-
$indexNameParts = \explode('-', $indexName);
552-
return substr($indexName, 0 - strlen($suffix)) === $suffix && count($indexNameParts) === 3;
553-
}));
550+
$indexNames = IndexNameService::filterIndexNamesByPostfix($this->indexDriver->getIndexeNamesByPrefix($aliasNamePrefix), $this->indexNamePostfix);
554551

555552
$cleanupAlias = function ($alias) use (&$aliasActions) {
556553
try {
557-
$indexNames = $this->indexDriver->indexesByAlias($alias);
554+
$indexNames = $this->indexDriver->getIndexeNamesByAlias($alias);
558555
if ($indexNames === []) {
559556
// if there is an actual index with the name we want to use as alias, remove it now
560557
$this->indexDriver->deleteIndex($alias);
@@ -577,7 +574,7 @@ public function updateMainAlias()
577574
};
578575

579576
$postfix = function ($alias) {
580-
return $alias . '-' . $this->indexNamePostfix;
577+
return $alias . IndexNameService::INDEX_PART_SEPARATOR . $this->indexNamePostfix;
581578
};
582579

583580
if (\count($indexNames) > 0) {
@@ -614,15 +611,15 @@ public function removeOldIndices(): array
614611
{
615612
$aliasName = $this->searchClient->getIndexName(); // The alias name is the unprefixed index name
616613

617-
$currentlyLiveIndices = $this->indexDriver->indexesByAlias($aliasName);
614+
$currentlyLiveIndices = $this->indexDriver->getIndexeNamesByAlias($aliasName);
618615

619616
$indexStatus = $this->systemDriver->status();
620617
$allIndices = array_keys($indexStatus['indices']);
621618

622619
$indicesToBeRemoved = [];
623620

624621
foreach ($allIndices as $indexName) {
625-
if (strpos($indexName, $aliasName . '-') !== 0) {
622+
if (strpos($indexName, $aliasName . IndexNameService::INDEX_PART_SEPARATOR) !== 0) {
626623
// filter out all indices not starting with the alias-name, as they are unrelated to our application
627624
continue;
628625
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Flowpack\ElasticSearch\ContentRepositoryAdaptor\Service;
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+
class IndexNameService
17+
{
18+
19+
public CONST INDEX_PART_SEPARATOR = '-';
20+
21+
/**
22+
* @param array $indexNames
23+
* @param string $postfix
24+
* @return array
25+
*/
26+
public static function filterIndexNamesByPostfix(array $indexNames, string $postfix): array
27+
{
28+
return array_values(array_filter($indexNames, static function ($indexName) use ($postfix) {
29+
$postfixWithSeparator = self::INDEX_PART_SEPARATOR . $postfix;
30+
return substr($indexName, -strlen($postfixWithSeparator)) === $postfixWithSeparator;
31+
}));
32+
}
33+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Flowpack\ElasticSearch\ContentRepositoryAdaptor\Tests\Unit\Service;
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\Service\IndexNameService;
17+
use Neos\Flow\Tests\UnitTestCase;
18+
19+
class IndexNameServiceTest extends UnitTestCase
20+
{
21+
22+
public function indexNameDataProvider(): array
23+
{
24+
return [
25+
'simple' => [
26+
'indexNames' => ['neoscr-4f534b1eb0c1a785da31e681fb5e91ff-1582128256', 'neoscr-4f534b1eb0c1a785da31e681fb5e91ff-1582128111'],
27+
'postfix' => '1582128111',
28+
'expected' => ['neoscr-4f534b1eb0c1a785da31e681fb5e91ff-1582128111'],
29+
],
30+
'prefixUsesDash' => [
31+
'indexNames' => ['neos-cr-4f534b1eb0c1a785da31e681fb5e91ff-1582128256', 'neos-cr-4f534b1eb0c1a785da31e681fb5e91ff-1582128111'],
32+
'postfix' => '1582128111',
33+
'expected' => ['neos-cr-4f534b1eb0c1a785da31e681fb5e91ff-1582128111'],
34+
]
35+
];
36+
}
37+
38+
/**
39+
* @test
40+
* @dataProvider indexNameDataProvider
41+
*
42+
* @param array $indexNames
43+
* @param string $postfix
44+
* @param array $expected
45+
*/
46+
public function filterIndexNamesByPostfix(array $indexNames, string $postfix, array $expected): void
47+
{
48+
self::assertEquals($expected, IndexNameService::filterIndexNamesByPostfix($indexNames, $postfix));
49+
}
50+
51+
}

0 commit comments

Comments
 (0)