public function testThatFailoverIsInitiatedAndFailingCommandsAreRetried() { // we need a factory to create the clients $clientFactory = new PredisClientCreator(); // we need an adapter for each sentinel client too! $clientAdapter = new PredisClientAdapter($clientFactory, Client::TYPE_SENTINEL); $sentinel1 = new Client('192.168.50.40', '26379', $clientAdapter); $clientAdapter = new PredisClientAdapter($clientFactory, Client::TYPE_SENTINEL); $sentinel2 = new Client('192.168.50.41', '26379', $clientAdapter); $clientAdapter = new PredisClientAdapter($clientFactory, Client::TYPE_SENTINEL); $sentinel3 = new Client('192.168.50.30', '26379', $clientAdapter); // configure how to back-off on reconnection attempts $backoffStrategy = new Incremental(500000, 2); $backoffStrategy->setMaxAttempts(10); // now we can start configuring the sentinel in the master discovery object $masterDiscovery = new MasterDiscovery('integrationtests'); $masterDiscovery->setBackoffStrategy($backoffStrategy); $masterDiscovery->addSentinel($sentinel1); $masterDiscovery->addSentinel($sentinel2); $masterDiscovery->addSentinel($sentinel3); // configure the HAClient that we'll use to talk to the redis master as a proxy $HAClient = new HAClient($masterDiscovery); // simulate a segfault in 5s $this->debugSegfaultToMaster(); for ($i = 1; $i <= 30; $i++) { $HAClient->incr(__METHOD__); } $this->assertEquals(30, $HAClient->get(__METHOD__), 'Test that all increment calls were executed'); }
public function addSentinels() { $clients = Config::get('database.redis.masters'); foreach ($clients as $client) { $sentinel = App::make('PSRedis\\Client', [$client['host'], $client['port']]); $this->masterDiscovery->addSentinel($sentinel); } }
/** * @param $psredisConfiguration */ public function __construct($psredisConfiguration) { if (!class_exists('\\PSRedis\\Client')) { throw new \LogicException("Can't find PSRedis lib"); } $this->masterDiscovery = new MasterDiscovery($psredisConfiguration['master']); foreach ($psredisConfiguration['sentinels'] as $sentinelConfiguration) { $this->masterDiscovery->addSentinel(new Client($sentinelConfiguration['host'], $sentinelConfiguration['port'])); } }
/** * @param MasterDiscovery $masterDiscovery */ private function configureSentinels(MasterDiscovery $masterDiscovery) { foreach ($this->sentinelParametersCollection as $sentinelParameters) { $sentinel = new Client($sentinelParameters['address'], $sentinelParameters['port']); $masterDiscovery->addSentinel($sentinel); } }
public function testDiscoveryWithBackoffFailsWhenSentinelsStayOffline() { $this->setExpectedException('\\PSRedis\\Exception\\ConnectionError', 'All sentinels are unreachable'); // disable sentinel on all nodes $this->disableSentinelAt('192.168.50.40'); $this->disableSentinelAt('192.168.50.41'); $this->disableSentinelAt('192.168.50.30'); // we need a factory to create the clients $clientFactory = new PredisClientCreator(); // we need an adapter for each sentinel client too! $clientAdapter = new PredisClientAdapter($clientFactory, Client::TYPE_SENTINEL); $sentinel1 = new Client('192.168.50.40', '26379', $clientAdapter); $clientAdapter = new PredisClientAdapter($clientFactory, Client::TYPE_SENTINEL); $sentinel2 = new Client('192.168.50.41', '26379', $clientAdapter); $clientAdapter = new PredisClientAdapter($clientFactory, Client::TYPE_SENTINEL); $sentinel3 = new Client('192.168.50.30', '26379', $clientAdapter); // now we can start discovering where the master is $masterDiscovery = new MasterDiscovery('integrationtests'); $masterDiscovery->addSentinel($sentinel1); $masterDiscovery->addSentinel($sentinel2); $masterDiscovery->addSentinel($sentinel3); // configure a backoff strategy $incrementalBackoff = new Incremental(500, 1.5); $incrementalBackoff->setMaxAttempts(5); $masterDiscovery->setBackoffStrategy($incrementalBackoff); // try to discover the master $master = $masterDiscovery->getMaster(); }
/** * @return MasterDiscovery */ private function masterDiscovery() { $masterDiscovery = new MasterDiscovery($this->getMasterName()); foreach ($this->getSentinels() as $sentinel) { $masterDiscovery->addSentinel($sentinel); } return $masterDiscovery; }
/** * @group regression * @group issue-9 */ public function testThatABackoffStrategyIsResetWhenStartingTheMasterDiscovery() { $backoff = new Incremental(0, 1); $backoff->setMaxAttempts(2); $sentinel1 = $this->mockOfflineSentinel(); $masterDiscovery = new MasterDiscovery('online-sentinel'); $masterDiscovery->setBackoffStrategy($backoff); $masterDiscovery->addSentinel($sentinel1); try { $masterNode = $masterDiscovery->getMaster(); } catch (ConnectionError $e) { // we expect this to fail as no sentinels are online } // add a sentinel that fails first, but succeeds after back-off (the bug, if present, will prevent reconnection of sentinels after backoff) $sentinel2 = $this->mockTemporaryOfflineSentinel(); $masterDiscovery->addSentinel($sentinel2); // try to discover the master node $masterNode = $masterDiscovery->getMaster(); $this->assertInstanceOf('\\PSRedis\\Client', $masterNode, 'When backing off is reset on each discovery, we should have received the master node here'); }