FOSElasticaBundle/Index/AliasProcessor.php

198 lines
6.1 KiB
PHP
Raw Permalink Normal View History

2014-06-18 09:13:29 +02:00
<?php
/**
* This file is part of the FOSElasticaBundle project.
*
* (c) Infinite Networks Pty Ltd <http://www.infinite.net.au>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace FOS\ElasticaBundle\Index;
2014-09-04 01:37:27 +02:00
use Elastica\Client;
2014-06-18 09:13:29 +02:00
use Elastica\Exception\ExceptionInterface;
use Elastica\Request;
2014-06-18 09:13:29 +02:00
use FOS\ElasticaBundle\Configuration\IndexConfig;
use FOS\ElasticaBundle\Elastica\Index;
use FOS\ElasticaBundle\Exception\AliasIsIndexException;
2014-06-18 09:13:29 +02:00
class AliasProcessor
{
/**
* Sets the randomised root name for an index.
*
* @param IndexConfig $indexConfig
2015-03-12 11:20:00 +01:00
* @param Index $index
2014-06-18 09:13:29 +02:00
*/
public function setRootName(IndexConfig $indexConfig, Index $index)
{
$index->overrideName(
sprintf('%s_%s',
$indexConfig->getElasticSearchName(),
date('Y-m-d-His')
)
);
2014-06-18 09:13:29 +02:00
}
/**
* Switches an index to become the new target for an alias. Only applies for
* indexes that are set to use aliases.
*
* $force will delete an index encountered where an alias is expected.
*
2014-06-18 09:13:29 +02:00
* @param IndexConfig $indexConfig
2015-03-12 11:20:00 +01:00
* @param Index $index
* @param bool $force
*
* @throws AliasIsIndexException
2014-06-18 09:13:29 +02:00
* @throws \RuntimeException
*/
public function switchIndexAlias(IndexConfig $indexConfig, Index $index, $force = false)
2014-06-18 09:13:29 +02:00
{
$client = $index->getClient();
2014-06-18 09:13:29 +02:00
$aliasName = $indexConfig->getElasticSearchName();
2015-03-13 14:51:07 +01:00
$oldIndexName = null;
2014-06-18 09:13:29 +02:00
$newIndexName = $index->getName();
try {
2015-03-13 14:51:07 +01:00
$oldIndexName = $this->getAliasedIndex($client, $aliasName);
2015-03-12 11:20:00 +01:00
} catch (AliasIsIndexException $e) {
2014-09-04 01:37:27 +02:00
if (!$force) {
throw $e;
}
2014-09-04 01:37:27 +02:00
$this->deleteIndex($client, $aliasName);
}
2014-06-18 09:13:29 +02:00
2015-03-13 14:51:07 +01:00
try {
$aliasUpdateRequest = $this->buildAliasUpdateRequest($oldIndexName, $aliasName, $newIndexName);
$client->request('_aliases', 'POST', $aliasUpdateRequest);
} catch (ExceptionInterface $e) {
$this->cleanupRenameFailure($client, $newIndexName, $e);
}
// Delete the old index after the alias has been switched
if (null !== $oldIndexName) {
$this->deleteIndex($client, $oldIndexName);
2014-06-18 09:13:29 +02:00
}
2015-03-13 14:51:07 +01:00
}
2014-06-18 09:13:29 +02:00
2015-03-13 14:51:07 +01:00
/**
* Builds an ElasticSearch request to rename or create an alias.
*
* @param string|null $aliasedIndex
* @param string $aliasName
* @param string $newIndexName
* @return array
*/
private function buildAliasUpdateRequest($aliasedIndex, $aliasName, $newIndexName)
{
2014-06-18 09:13:29 +02:00
$aliasUpdateRequest = array('actions' => array());
2015-03-13 14:51:07 +01:00
if (null !== $aliasedIndex) {
2014-06-18 09:13:29 +02:00
// if the alias is set - add an action to remove it
$aliasUpdateRequest['actions'][] = array(
2015-03-13 14:51:07 +01:00
'remove' => array('index' => $aliasedIndex, 'alias' => $aliasName),
2014-06-18 09:13:29 +02:00
);
}
// add an action to point the alias to the new index
$aliasUpdateRequest['actions'][] = array(
2015-03-12 11:20:00 +01:00
'add' => array('index' => $newIndexName, 'alias' => $aliasName),
2014-06-18 09:13:29 +02:00
);
2015-03-13 14:51:07 +01:00
return $aliasUpdateRequest;
}
2014-06-18 09:13:29 +02:00
2015-03-13 14:51:07 +01:00
/**
* Cleans up an index when we encounter a failure to rename the alias.
*
* @param Client $client
2015-03-14 06:08:50 +01:00
* @param string $indexName
2015-03-13 14:51:07 +01:00
* @param \Exception $renameAliasException
*/
private function cleanupRenameFailure(Client $client, $indexName, \Exception $renameAliasException)
{
$additionalError = '';
try {
$this->deleteIndex($client, $indexName);
} catch (ExceptionInterface $deleteNewIndexException) {
$additionalError = sprintf(
'Tried to delete newly built index %s, but also failed: %s',
$indexName,
$deleteNewIndexException->getMessage()
2014-06-18 09:13:29 +02:00
);
}
2015-03-13 14:51:07 +01:00
throw new \RuntimeException(sprintf(
'Failed to updated index alias: %s. %s',
$renameAliasException->getMessage(),
$additionalError ?: sprintf('Newly built index %s was deleted', $indexName)
), 0, $renameAliasException);
}
/**
* Delete an index.
*
* @param Client $client
* @param string $indexName Index name to delete
*/
private function deleteIndex(Client $client, $indexName)
{
try {
$path = sprintf("%s", $indexName);
$client->request($path, Request::DELETE);
} catch (ExceptionInterface $deleteOldIndexException) {
throw new \RuntimeException(sprintf(
'Failed to delete index %s with message: %s',
$indexName,
$deleteOldIndexException->getMessage()
), 0, $deleteOldIndexException);
2014-06-18 09:13:29 +02:00
}
}
/**
2015-03-13 14:51:07 +01:00
* Returns the name of a single index that an alias points to or throws
* an exception if there is more than one.
2014-06-18 09:13:29 +02:00
*
* @param Client $client
2014-06-18 09:13:29 +02:00
* @param string $aliasName Alias name
*
2015-03-13 14:51:07 +01:00
* @return string|null
2015-03-12 11:20:00 +01:00
*
2014-09-04 01:37:27 +02:00
* @throws AliasIsIndexException
2014-06-18 09:13:29 +02:00
*/
2015-03-13 14:51:07 +01:00
private function getAliasedIndex(Client $client, $aliasName)
2014-06-18 09:13:29 +02:00
{
$aliasesInfo = $client->request('_aliases', 'GET')->getData();
2014-06-18 09:13:29 +02:00
$aliasedIndexes = array();
foreach ($aliasesInfo as $indexName => $indexInfo) {
if ($indexName === $aliasName) {
throw new AliasIsIndexException($indexName);
}
2014-09-04 01:37:27 +02:00
if (!isset($indexInfo['aliases'])) {
continue;
}
2014-06-18 09:13:29 +02:00
$aliases = array_keys($indexInfo['aliases']);
if (in_array($aliasName, $aliases)) {
$aliasedIndexes[] = $indexName;
}
}
2015-03-13 14:51:07 +01:00
if (count($aliasedIndexes) > 1) {
throw new \RuntimeException(sprintf(
'Alias %s is used for multiple indexes: [%s]. Make sure it\'s'.
'either not used or is assigned to one index only',
$aliasName,
implode(', ', $aliasedIndexes)
));
}
2015-03-13 14:51:07 +01:00
return array_shift($aliasedIndexes);
}
2014-06-18 09:13:29 +02:00
}