Changeset View
Changeset View
Standalone View
Standalone View
src/infrastructure/cluster/search/PhabricatorSearchService.php
<?php | <?php | ||||
class PhabricatorSearchService | class PhabricatorSearchService | ||||
extends Phobject { | extends Phobject { | ||||
const KEY_REFS = 'cluster.search.refs'; | const KEY_REFS = 'cluster.search.refs'; | ||||
protected $config; | protected $config; | ||||
protected $disabled; | protected $disabled; | ||||
epriestley: Can we remove this, too, if `getDisabled()` / `setDisabled()` were removed? | |||||
protected $engine; | protected $engine; | ||||
protected $hosts = array(); | protected $hosts = array(); | ||||
protected $hostsConfig; | protected $hostsConfig; | ||||
protected $hostType; | protected $hostType; | ||||
protected $roles = array(); | protected $roles = array(); | ||||
const STATUS_OKAY = 'okay'; | const STATUS_OKAY = 'okay'; | ||||
const STATUS_FAIL = 'fail'; | const STATUS_FAIL = 'fail'; | ||||
Show All 23 Lines | class PhabricatorSearchService | ||||
} | } | ||||
public function getStatusViewColumns() { | public function getStatusViewColumns() { | ||||
return $this->hostType->getStatusViewColumns(); | return $this->hostType->getStatusViewColumns(); | ||||
} | } | ||||
public function setConfig($config) { | public function setConfig($config) { | ||||
$this->config = $config; | $this->config = $config; | ||||
$this->setRoles(idx($config, 'roles', array())); | |||||
if (!isset($config['hosts'])) { | if (!isset($config['hosts'])) { | ||||
$config['hosts'] = array( | $config['hosts'] = array( | ||||
array( | array( | ||||
'host' => idx($config, 'host'), | 'host' => idx($config, 'host'), | ||||
'port' => idx($config, 'port'), | 'port' => idx($config, 'port'), | ||||
'protocol' => idx($config, 'protocol'), | 'protocol' => idx($config, 'protocol'), | ||||
'roles' => idx($config, 'roles'), | 'roles' => idx($config, 'roles'), | ||||
), | ), | ||||
); | ); | ||||
} | } | ||||
foreach ($config['hosts'] as $host) { | foreach ($config['hosts'] as $host) { | ||||
$this->newHost($host); | $this->newHost($host); | ||||
} | } | ||||
} | } | ||||
public function getConfig() { | public function getConfig() { | ||||
return $this->config; | return $this->config; | ||||
} | } | ||||
public function setDisabled($disabled) { | |||||
$this->disabled = $disabled; | |||||
return $this; | |||||
} | |||||
public function getDisabled() { | |||||
return $this->disabled; | |||||
} | |||||
public static function getConnectionStatusMap() { | public static function getConnectionStatusMap() { | ||||
return array( | return array( | ||||
self::STATUS_OKAY => array( | self::STATUS_OKAY => array( | ||||
'icon' => 'fa-exchange', | 'icon' => 'fa-exchange', | ||||
'color' => 'green', | 'color' => 'green', | ||||
'label' => pht('Okay'), | 'label' => pht('Okay'), | ||||
), | ), | ||||
self::STATUS_FAIL => array( | self::STATUS_FAIL => array( | ||||
'icon' => 'fa-times', | 'icon' => 'fa-times', | ||||
'color' => 'red', | 'color' => 'red', | ||||
'label' => pht('Failed'), | 'label' => pht('Failed'), | ||||
), | ), | ||||
); | ); | ||||
} | } | ||||
public function isWritable() { | public function isWritable() { | ||||
return $this->hasRole('write'); | return $this->hasRole('write'); | ||||
} | } | ||||
public function isReadable() { | public function isReadable() { | ||||
return $this->hasRole('read'); | return $this->hasRole('read'); | ||||
} | } | ||||
public function hasRole($role) { | public function hasRole($role) { | ||||
return isset($this->roles[$role]) && $this->roles[$role] === true; | return isset($this->roles[$role]) && $this->roles[$role] !== false; | ||||
} | } | ||||
public function setRoles(array $roles) { | public function setRoles(array $roles) { | ||||
foreach ($roles as $role => $val) { | foreach ($roles as $role => $val) { | ||||
if ($val === false && isset($this->roles[$role])) { | if ($val === false && isset($this->roles[$role])) { | ||||
unset($this->roles[$role]); | unset($this->roles[$role]); | ||||
} else { | } else { | ||||
$this->roles[$role] = $val; | $this->roles[$role] = $val; | ||||
▲ Show 20 Lines • Show All 43 Lines • ▼ Show 20 Lines | class PhabricatorSearchService | ||||
} | } | ||||
/** | /** | ||||
* Get all configured hosts for this service which have the specified role. | * Get all configured hosts for this service which have the specified role. | ||||
* @return PhabricatorSearchHost[] | * @return PhabricatorSearchHost[] | ||||
*/ | */ | ||||
public function getAllHostsForRole($role) { | public function getAllHostsForRole($role) { | ||||
// if the role is explicitly set to false at the top level, then all hosts | |||||
// have the role disabled. | |||||
epriestleyUnsubmitted Not Done Inline ActionsI don't think we should have this rule: I think it's more valuable for the configuration to be consistent (sub-config always overrides parent-config) than to make it easier to disable a role. epriestley: I don't think we should have this rule: I think it's more valuable for the configuration to be… | |||||
if (idx($this->config, $role) === false) { | |||||
return array(); | |||||
} | |||||
$hosts = array(); | $hosts = array(); | ||||
foreach ($this->hosts as $host) { | foreach ($this->hosts as $host) { | ||||
if ($host->hasRole($role)) { | if ($host->hasRole($role)) { | ||||
$hosts[] = $host; | $hosts[] = $host; | ||||
} | } | ||||
} | } | ||||
return $hosts; | return $hosts; | ||||
} | } | ||||
▲ Show 20 Lines • Show All 49 Lines • ▼ Show 20 Lines | class PhabricatorSearchService | ||||
* (re)index the document: attempt to pass the document to all writable | * (re)index the document: attempt to pass the document to all writable | ||||
* fulltext search hosts | * fulltext search hosts | ||||
* @throws PhabricatorClusterNoHostForRoleException | * @throws PhabricatorClusterNoHostForRoleException | ||||
*/ | */ | ||||
public static function reindexAbstractDocument( | public static function reindexAbstractDocument( | ||||
PhabricatorSearchAbstractDocument $doc) { | PhabricatorSearchAbstractDocument $doc) { | ||||
$indexed = 0; | $indexed = 0; | ||||
foreach (self::getAllServices() as $service) { | foreach (self::getAllServices() as $service) { | ||||
$hosts = $service->getAllHostsForRole('write'); | |||||
if (count($hosts)) { | |||||
epriestleyUnsubmitted Not Done Inline ActionsPrefer if ($hosts) by convention. Particularly, this at least partially sidesteps bizarre bugs where count(false) is 1, which is unintuitive and can be hard to track down. epriestley: Prefer `if ($hosts)` by convention. Particularly, this at least partially sidesteps bizarre… | |||||
$service->getEngine()->reindexAbstractDocument($doc); | $service->getEngine()->reindexAbstractDocument($doc); | ||||
$indexed++; | $indexed++; | ||||
} | } | ||||
} | |||||
if ($indexed == 0) { | if ($indexed == 0) { | ||||
throw new PhabricatorClusterNoHostForRoleException('write'); | throw new PhabricatorClusterNoHostForRoleException('write'); | ||||
} | } | ||||
} | } | ||||
/** | /** | ||||
* Execute a full-text query and return a list of PHIDs of matching objects. | * Execute a full-text query and return a list of PHIDs of matching objects. | ||||
* @return string[] | * @return string[] | ||||
Show All 21 Lines |
Can we remove this, too, if getDisabled() / setDisabled() were removed?