Skip to content

Commit

Permalink
Improve performance of Status->getIndicesWithAlias
Browse files Browse the repository at this point in the history
Particularly improves performance on clusters with many indices.  Slow
to instant.

Closes ruflin#563
  • Loading branch information
Nik Everett committed Mar 3, 2014
1 parent 2cb3a2c commit e7152a1
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 12 deletions.
2 changes: 2 additions & 0 deletions changes.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
CHANGES
2014-03-04
- Improve performance of Elastica/Status->getIndicesWithAlias and aliasExists on clusters with many indices

2014-03-02
- Release v1.0.1.0
Expand Down
30 changes: 18 additions & 12 deletions lib/Elastica/Status.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

namespace Elastica;
use Elastica\Exception\ResponseException;
use Elastica\Index\Status as IndexStatus;

/**
Expand Down Expand Up @@ -105,30 +106,35 @@ public function indexExists($name)
*/
public function aliasExists($name)
{
foreach ($this->getIndexStatuses() as $status) {
if ($status->hasAlias($name)) {
return true;
}
}
return count($this->getIndicesWithAlias($name)) > 0;

return false;
}

/**
* Returns an array with all indices that the given alias name points to
*
* @param string $name Alias name
* @param string $alias Alias name
* @return array|\Elastica\Index[] List of Elastica\Index
*/
public function getIndicesWithAlias($name)
public function getIndicesWithAlias($alias)
{
$indices = array();
foreach ($this->getIndexStatuses() as $status) {
if ($status->hasAlias($name)) {
$indices[] = $status->getIndex();
$response = null;
try {
$response = $this->_client->request("/_alias/$alias");
} catch (ResponseException $e) {
$transferInfo = $e->getResponse()->getTransferInfo();
// 404 means the index alias doesn't exist which means no indexes have it.
if ($transferInfo['http_code'] === 404) {
return array();
}
// If we don't have a 404 then this is still unexpected so rethrow the exception.
throw $e;
}
$indices = array();
foreach ($response->getData() as $name => $unused) {
$indices[] = new Index($this->_client, $name);
}

return $indices;
}

Expand Down
6 changes: 6 additions & 0 deletions test/lib/Elastica/Test/StatusTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,12 @@ public function testAliasExists()
$index1->addAlias($aliasName);
$status->refresh();
$this->assertTrue($status->aliasExists($aliasName));

$indicesWithAlias = $status->getIndicesWithAlias($aliasName);
$this->assertEquals(array("elastica_$indexName"), array_map(
function($index) {
return $index->getName();
}, $indicesWithAlias));
}

public function testServerStatus()
Expand Down

0 comments on commit e7152a1

Please sign in to comment.