Merge remote-tracking branch 'upstream/pr/705' into 3.0.x

Conflicts:
	Index/AliasProcessor.php
This commit is contained in:
Tim Nagel 2014-09-04 09:26:53 +10:00
commit 029ebb153a
4 changed files with 57 additions and 9 deletions

View file

@ -33,6 +33,7 @@ class ResetCommand extends ContainerAwareCommand
->setName('fos:elastica:reset')
->addOption('index', null, InputOption::VALUE_OPTIONAL, 'The index to reset')
->addOption('type', null, InputOption::VALUE_OPTIONAL, 'The type to reset')
->addOption('force', null, InputOption::VALUE_NONE, 'Force index deletion if same name as alias')
->setDescription('Reset search indexes')
;
}
@ -51,8 +52,9 @@ class ResetCommand extends ContainerAwareCommand
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$index = $input->getOption('index');
$type = $input->getOption('type');
$index = $input->getOption('index');
$type = $input->getOption('type');
$force = (bool) $input->getOption('force');
if (null === $index && null !== $type) {
throw new \InvalidArgumentException('Cannot specify type option without an index.');
@ -69,7 +71,7 @@ class ResetCommand extends ContainerAwareCommand
foreach ($indexes as $index) {
$output->writeln(sprintf('<info>Resetting</info> <comment>%s</comment>', $index));
$this->resetter->resetIndex($index);
$this->resetter->resetIndex($index, false, $force);
}
}
}

View file

@ -0,0 +1,12 @@
<?php
namespace FOS\ElasticaBundle\Exception;
class AliasIsIndexException extends \Exception
{
public function __construct($indexName)
{
parent::__construct(sprintf('Expected alias %s instead of index', $indexName));
}
}

View file

@ -12,9 +12,11 @@
namespace FOS\ElasticaBundle\Index;
use Elastica\Exception\ExceptionInterface;
use Elastica\Request;
use FOS\ElasticaBundle\Configuration\IndexConfig;
use FOS\ElasticaBundle\Elastica\Client;
use FOS\ElasticaBundle\Elastica\Index;
use FOS\ElasticaBundle\Exception\AliasIsIndexException;
class AliasProcessor
{
@ -33,11 +35,15 @@ class AliasProcessor
* 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.
*
* @param IndexConfig $indexConfig
* @param Index $index
* @param bool $force
* @throws AliasIsIndexException
* @throws \RuntimeException
*/
public function switchIndexAlias(IndexConfig $indexConfig, Index $index)
public function switchIndexAlias(IndexConfig $indexConfig, Index $index, $force = false)
{
$client = $index->getClient();
@ -45,7 +51,17 @@ class AliasProcessor
$oldIndexName = false;
$newIndexName = $index->getName();
$aliasedIndexes = $this->getAliasedIndexes($client, $aliasName);
try {
$aliasedIndexes = $this->getAliasedIndexes($client, $aliasName);
} catch(AliasIsIndexException $e) {
if ($force) {
$this->deleteIndex($client, $aliasName);
return;
}
throw $e;
}
if (count($aliasedIndexes) > 1) {
throw new \RuntimeException(
@ -116,8 +132,10 @@ class AliasProcessor
/**
* Returns array of indexes which are mapped to given alias
*
* @param Client $client
* @param string $aliasName Alias name
* @return array
* @throws \FOS\ElasticaBundle\Exception\AliasIsIndexException
*/
private function getAliasedIndexes(Client $client, $aliasName)
{
@ -125,6 +143,9 @@ class AliasProcessor
$aliasedIndexes = array();
foreach ($aliasesInfo as $indexName => $indexInfo) {
if ($indexName === $aliasName) {
throw new AliasIsIndexException($indexName);
}
if (isset($indexInfo['aliases'])) {
$aliases = array_keys($indexInfo['aliases']);
if (in_array($aliasName, $aliases)) {
@ -135,4 +156,16 @@ class AliasProcessor
return $aliasedIndexes;
}
/**
* Delete an index
*
* @param Client $client
* @param string $indexName Index name to delete
*/
private function deleteIndex(Client $client, $indexName)
{
$path = sprintf("%s", $indexName);
$client->request($path, Request::DELETE);
}
}

View file

@ -45,10 +45,10 @@ class Resetter
/**
* Deletes and recreates all indexes
*/
public function resetAllIndexes($populating = false)
public function resetAllIndexes($populating = false, $force = false)
{
foreach ($this->configManager->getIndexNames() as $name) {
$this->resetIndex($name, $populating);
$this->resetIndex($name, $populating, $force);
}
}
@ -58,9 +58,10 @@ class Resetter
*
* @param string $indexName
* @param bool $populating
* @param bool $force If index exists with same name as alias, remove it
* @throws \InvalidArgumentException if no index exists for the given name
*/
public function resetIndex($indexName, $populating = false)
public function resetIndex($indexName, $populating = false, $force = false)
{
$indexConfig = $this->configManager->getIndexConfiguration($indexName);
$index = $this->indexManager->getIndex($indexName);
@ -73,7 +74,7 @@ class Resetter
$index->create($mapping, true);
if (!$populating and $indexConfig->isUseAlias()) {
$this->aliasProcessor->switchIndexAlias($indexConfig, $index);
$this->aliasProcessor->switchIndexAlias($indexConfig, $index, $force);
}
}