Changeset View
Changeset View
Standalone View
Standalone View
src/infrastructure/cluster/PhabricatorDatabaseRef.php
| Show First 20 Lines • Show All 440 Lines • ▼ Show 20 Lines | final class PhabricatorDatabaseRef | ||||
| public function getHealthRecord() { | public function getHealthRecord() { | ||||
| if (!$this->healthRecord) { | if (!$this->healthRecord) { | ||||
| $this->healthRecord = new PhabricatorDatabaseHealthRecord($this); | $this->healthRecord = new PhabricatorDatabaseHealthRecord($this); | ||||
| } | } | ||||
| return $this->healthRecord; | return $this->healthRecord; | ||||
| } | } | ||||
| public static function getMasterDatabaseRef() { | public static function getMasterDatabaseRefs() { | ||||
| $refs = self::getLiveRefs(); | $refs = self::getLiveRefs(); | ||||
| if (!$refs) { | if (!$refs) { | ||||
| return self::getLiveIndividualRef(); | return array(self::getLiveIndividualRef()); | ||||
| } | } | ||||
| $master = null; | $masters = array(); | ||||
| foreach ($refs as $ref) { | foreach ($refs as $ref) { | ||||
| if ($ref->getDisabled()) { | if ($ref->getDisabled()) { | ||||
| continue; | continue; | ||||
| } | } | ||||
| if ($ref->getIsMaster()) { | if ($ref->getIsMaster()) { | ||||
| return $ref; | $masters[] = $ref; | ||||
| } | } | ||||
| } | } | ||||
| return null; | return $masters; | ||||
| } | |||||
| public static function getMasterDatabaseRef() { | |||||
| // TODO: Remove this method; it no longer makes sense with application | |||||
| // partitioning. | |||||
| return head(self::getMasterDatabaseRefs()); | |||||
| } | |||||
| public static function getMasterDatabaseRefForDatabase($database) { | |||||
| $masters = self::getMasterDatabaseRefs(); | |||||
| // TODO: Actually implement this. | |||||
| return head($masters); | |||||
| } | } | ||||
| public static function newIndividualRef() { | public static function newIndividualRef() { | ||||
| $conf = PhabricatorEnv::newObjectFromConfig( | $conf = PhabricatorEnv::newObjectFromConfig( | ||||
| 'mysql.configuration-provider', | 'mysql.configuration-provider', | ||||
| array(null, 'w', null)); | array(null, 'w', null)); | ||||
| return id(new self()) | return id(new self()) | ||||
| ->setHost($conf->getHost()) | ->setHost($conf->getHost()) | ||||
| ->setPort($conf->getPort()) | ->setPort($conf->getPort()) | ||||
| ->setUser($conf->getUser()) | ->setUser($conf->getUser()) | ||||
| ->setPass($conf->getPassword()) | ->setPass($conf->getPassword()) | ||||
| ->setIsIndividual(true) | ->setIsIndividual(true) | ||||
| ->setIsMaster(true); | ->setIsMaster(true); | ||||
| } | } | ||||
| public static function getReplicaDatabaseRef() { | public static function getReplicaDatabaseRefs() { | ||||
| $refs = self::getLiveRefs(); | $refs = self::getLiveRefs(); | ||||
| if (!$refs) { | if (!$refs) { | ||||
| return null; | return array(); | ||||
| } | } | ||||
| // TODO: We may have multiple replicas to choose from, and could make | $replicas = array(); | ||||
| // more of an effort to pick the "best" one here instead of always | |||||
| // picking the first one. Once we've picked one, we should try to use | |||||
| // the same replica for the rest of the request, though. | |||||
| foreach ($refs as $ref) { | foreach ($refs as $ref) { | ||||
| if ($ref->getDisabled()) { | if ($ref->getDisabled()) { | ||||
| continue; | continue; | ||||
| } | } | ||||
| if ($ref->getIsMaster()) { | if ($ref->getIsMaster()) { | ||||
| continue; | continue; | ||||
| } | } | ||||
| return $ref; | |||||
| $replicas[] = $ref; | |||||
| } | } | ||||
| return null; | return $replicas; | ||||
| } | |||||
| public static function getReplicaDatabaseRefForDatabase($database) { | |||||
| $replicas = self::getReplicaDatabaseRefs(); | |||||
| // TODO: Actually implement this. | |||||
| // TODO: We may have multiple replicas to choose from, and could make | |||||
| // more of an effort to pick the "best" one here instead of always | |||||
| // picking the first one. Once we've picked one, we should try to use | |||||
| // the same replica for the rest of the request, though. | |||||
| return head($replicas); | |||||
| } | } | ||||
| private function newConnection(array $options) { | private function newConnection(array $options) { | ||||
| // If we believe the database is unhealthy, don't spend as much time | // If we believe the database is unhealthy, don't spend as much time | ||||
| // trying to connect to it, since it's likely to continue to fail and | // trying to connect to it, since it's likely to continue to fail and | ||||
| // hammering it can only make the problem worse. | // hammering it can only make the problem worse. | ||||
| $record = $this->getHealthRecord(); | $record = $this->getHealthRecord(); | ||||
| if ($record->getIsHealthy()) { | if ($record->getIsHealthy()) { | ||||
| Show All 25 Lines | |||||