/** * @param PoolingShardConnection $connection * @return Success|Failure */ private function checkPoolingShardConnection(PoolingShardConnection $connection) { // TODO Check all shards, instead of just the active one. $isConnected = $connection->ping(); $data = [$connection->getActiveShardId() => $isConnected ? 'connected' : 'not connected']; if ($isConnected) { return new Success(get_class($connection), $data); } return new Failure(get_class($connection), $data); }
/** * @param string $sql * @param array $params * @param array $types * * @return array * * @throws \RuntimeException */ public function queryAll($sql, array $params, array $types) { $shards = $this->getShards(); if (!$shards) { throw new \RuntimeException("No shards found."); } $result = array(); $oldDistribution = $this->getCurrentDistributionValue(); foreach ($shards as $shard) { $this->selectShard($shard['id']); foreach ($this->conn->fetchAll($sql, $params, $types) as $row) { $result[] = $row; } } if ($oldDistribution === null) { $this->selectGlobal(); } else { $this->selectShard($oldDistribution); } return $result; }
/** * @param string $distributionValue * * @return void */ public function selectShard($distributionValue) { $shardId = $this->choser->pickShard($distributionValue, $this->conn); $this->conn->connect($shardId); $this->currentDistributionValue = $distributionValue; }
public function __construct(PoolingShardConnection $conn) { $params = $conn->getParams(); $this->conn = $conn; $this->choser = $params['shardChoser']; }
public function testCheckPoolingShardConnectionFailed() { $this->poolingShardConnection->method('ping')->willReturn(false); $actual = (new CheckConnection($this->poolingShardConnection))->check(); self::assertInstanceOf(Failure::class, $actual); }