Skip to content

Add an option to use index swap in import command #354

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 52 additions & 1 deletion src/Command/MeilisearchImportCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

final class MeilisearchImportCommand extends IndexCommand
{
private const TEMP_INDEX_PREFIX = '_tmp_';

private Client $searchClient;
private ManagerRegistry $managerRegistry;
private SettingsUpdater $settingsUpdater;
Expand Down Expand Up @@ -73,6 +75,12 @@ protected function configure(): void
'Timeout (in ms) to get response from the search engine',
self::DEFAULT_RESPONSE_TIMEOUT
)
->addOption(
'swap-indices',
null,
InputOption::VALUE_NONE,
'Import to temporary indices and use index swap to prevent downtime'
)
;
}

Expand All @@ -83,6 +91,15 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$indexes = $this->getEntitiesFromArgs($input, $output);
$entitiesToIndex = $this->entitiesToIndex($indexes);
$config = $this->searchService->getConfiguration();
$swapIndices = $input->getOption('swap-indices');
$initialPrefix = $config['prefix'] ?? '';
$prefix = null;

if ($swapIndices) {
$prefix = self::TEMP_INDEX_PREFIX;
$config['prefix'] = $prefix.($config['prefix'] ?? '');
}

$updateSettings = $input->getOption('update-settings');
$batchSize = $input->getOption('batch-size') ?? '';
$batchSize = ctype_digit($batchSize) ? (int) $batchSize : $config->get('batchSize');
Expand Down Expand Up @@ -149,10 +166,16 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$manager->clear();

if ($updateSettings) {
$this->settingsUpdater->update($index['prefixed_name'], $responseTimeout);
$this->settingsUpdater->update($index['prefixed_name'], $responseTimeout, $prefix ? $prefix.$index['prefixed_name'] : null);
}
}

if ($swapIndices) {
$this->swapIndices($indexes, $prefix, $output);

$config['prefix'] = $initialPrefix;
}

$output->writeln('<info>Done!</info>');

return 0;
Expand Down Expand Up @@ -210,4 +233,32 @@ private function entitiesToIndex($indexes): array

return array_unique($indexes->all(), SORT_REGULAR);
}

private function swapIndices(Collection $indexes, string $prefix, OutputInterface $output): void
{
$indexPairs = [];

foreach ($indexes as $index) {
$tempIndex = $index;
$tempIndex['name'] = $prefix.$tempIndex['name'];
$pair = [$tempIndex['name'], $index['name']];

// Indexes must be declared only once during a swap
if (!\in_array($pair, $indexPairs, true)) {
$indexPairs[] = $pair;
}
}

// swap indexes
$output->writeln('<info>Swapping indices...</info>');
$this->searchClient->swapIndexes($indexPairs);
$output->writeln('<info>Indices swapped.</info>');
$output->writeln('<info>Deleting temporary indices...</info>');

// delete temp indexes
foreach ($indexPairs as $pair) {
$this->searchService->deleteByIndexName($pair[0]);
$output->writeln('<info>Deleted '.$pair[0].'</info>');
}
}
}
4 changes: 2 additions & 2 deletions src/Services/SettingsUpdater.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function __construct(SearchService $searchService, Client $searchClient,
* @param non-empty-string $indice
* @param positive-int|null $responseTimeout
*/
public function update(string $indice, ?int $responseTimeout = null): void
public function update(string $indice, ?int $responseTimeout = null, ?string $prefixedName = null): void
{
$index = (new Collection($this->configuration->get('indices')))->firstWhere('prefixed_name', $indice);

Expand All @@ -45,7 +45,7 @@ public function update(string $indice, ?int $responseTimeout = null): void
return;
}

$indexName = $index['prefixed_name'];
$indexName = $prefixedName ?? $index['prefixed_name'];
$indexInstance = $this->searchClient->index($indexName);
$responseTimeout = $responseTimeout ?? self::DEFAULT_RESPONSE_TIMEOUT;

Expand Down
23 changes: 23 additions & 0 deletions tests/Integration/Command/MeilisearchImportCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -391,4 +391,27 @@ public function testAlias(): void

self::assertSame(['meili:import'], $command->getAliases());
}

public function testImportingIndexWithSwap(): void
{
for ($i = 0; $i <= 5; ++$i) {
$this->createPost();
}

$command = $this->application->find('meilisearch:import');
$commandTester = new CommandTester($command);
$return = $commandTester->execute([
'--swap-indices' => true,
]);

$output = $commandTester->getDisplay();
$this->assertStringContainsString('Importing for index Meilisearch\Bundle\Tests\Entity\Post', $output);
$this->assertStringContainsString('Indexed a batch of 6 / 6 Meilisearch\Bundle\Tests\Entity\Post entities into _tmp_sf_phpunit__'.self::$indexName.' index (6 indexed since start)', $output);
$this->assertStringContainsString('Swapping indices...', $output);
$this->assertStringContainsString('Indices swapped.', $output);
$this->assertStringContainsString('Deleting temporary indices...', $output);
$this->assertStringContainsString('Deleted _tmp_sf_phpunit__'.self::$indexName, $output);
$this->assertStringContainsString('Done!', $output);
$this->assertSame(0, $return);
}
}
Loading