From 0b9529f801bcf58b7da16a0a6b13afc886386028 Mon Sep 17 00:00:00 2001 From: Sander van Thillo Date: Mon, 5 Dec 2016 18:36:17 +0100 Subject: [PATCH 1/3] allow sentinel replication configuration --- .../Configuration/Configuration.php | 3 +- .../SncRedisExtensionTest.php | 40 ++++++++++++++++++- 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/DependencyInjection/Configuration/Configuration.php b/DependencyInjection/Configuration/Configuration.php index 4daa9098..86ff2f39 100644 --- a/DependencyInjection/Configuration/Configuration.php +++ b/DependencyInjection/Configuration/Configuration.php @@ -136,7 +136,8 @@ private function addClientsSection(ArrayNodeDefinition $rootNode) ->end() ->scalarNode('cluster')->defaultNull()->end() ->scalarNode('prefix')->defaultNull()->end() - ->booleanNode('replication')->defaultFalse()->end() + ->enumNode('replication')->values(array(true, false, 'sentinel'))->defaultNull()->end() + ->scalarNode('service')->defaultNull()->end() ->end() ->end() ->end() diff --git a/Tests/DependencyInjection/SncRedisExtensionTest.php b/Tests/DependencyInjection/SncRedisExtensionTest.php index 48fb25be..b7f852d1 100644 --- a/Tests/DependencyInjection/SncRedisExtensionTest.php +++ b/Tests/DependencyInjection/SncRedisExtensionTest.php @@ -170,7 +170,7 @@ public function testFullConfigLoad() $this->assertTrue($container->hasAlias('swiftmailer.spool.redis')); $this->assertInternalType('array', $container->findTaggedServiceIds('snc_redis.client')); - $this->assertGreaterThanOrEqual(4, $container->findTaggedServiceIds('snc_redis.client'), 'expected at least 4 tagged clients'); + $this->assertGreaterThanOrEqual(4, count($container->findTaggedServiceIds('snc_redis.client')), 'expected at least 4 tagged clients'); $tags = $container->findTaggedServiceIds('snc_redis.client'); $this->assertArrayHasKey('snc_redis.default', $tags); @@ -308,6 +308,28 @@ public function testClientReplicationOption() $this->assertEquals(array('snc_redis.default' => array(array('alias' => 'default'))), $container->findTaggedServiceIds('snc_redis.client')); } + /** + * Test valid config of the sentinel replication option + */ + public function testSentinelOption() + { + $extension = new SncRedisExtension(); + $config = $this->parseYaml($this->getSentinelYamlConfig()); + $extension->load(array($config), $container = $this->getContainer()); + + $options = $container->getDefinition('snc_redis.client.default_options')->getArgument(0); + $this->assertEquals('sentinel', $options['replication']); + $this->assertEquals('mymaster', $options['service']); + $parameters = $container->getDefinition('snc_redis.default')->getArgument(0); + $this->assertEquals('snc_redis.connection.master_parameters.default', (string) $parameters[0]); + $masterParameters = $container->getDefinition((string) $parameters[0])->getArgument(0); + $this->assertEquals('sentinel', $masterParameters['replication']); + $this->assertEquals('mymaster', $masterParameters['service']); + + $this->assertInternalType('array', $container->findTaggedServiceIds('snc_redis.client')); + $this->assertEquals(array('snc_redis.default' => array(array('alias' => 'default'))), $container->findTaggedServiceIds('snc_redis.client')); + } + private function parseYaml($yaml) { $parser = new Parser(); @@ -466,6 +488,22 @@ private function getReplicationYamlConfig() EOF; } + private function getSentinelYamlConfig() + { + return <<<'EOF' +clients: + default: + type: predis + alias: default + dsn: + - redis://localhost?alias=master + - redis://otherhost + options: + replication: sentinel + service: mymaster +EOF; + } + private function getMultipleReplicationYamlConfig() { return <<<'EOF' From 104df22b0391b69cef04ef69966d356ae76af0a4 Mon Sep 17 00:00:00 2001 From: Sander van Thillo Date: Wed, 25 Jan 2017 11:05:04 +0100 Subject: [PATCH 2/3] removed default value for replication, added test for cluster config --- .../Configuration/Configuration.php | 2 +- .../SncRedisExtensionTest.php | 36 +++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/DependencyInjection/Configuration/Configuration.php b/DependencyInjection/Configuration/Configuration.php index 86ff2f39..9f0d0836 100644 --- a/DependencyInjection/Configuration/Configuration.php +++ b/DependencyInjection/Configuration/Configuration.php @@ -136,7 +136,7 @@ private function addClientsSection(ArrayNodeDefinition $rootNode) ->end() ->scalarNode('cluster')->defaultNull()->end() ->scalarNode('prefix')->defaultNull()->end() - ->enumNode('replication')->values(array(true, false, 'sentinel'))->defaultNull()->end() + ->enumNode('replication')->values(array(true, false, 'sentinel'))->end() ->scalarNode('service')->defaultNull()->end() ->end() ->end() diff --git a/Tests/DependencyInjection/SncRedisExtensionTest.php b/Tests/DependencyInjection/SncRedisExtensionTest.php index b7f852d1..aed10ba3 100644 --- a/Tests/DependencyInjection/SncRedisExtensionTest.php +++ b/Tests/DependencyInjection/SncRedisExtensionTest.php @@ -330,6 +330,27 @@ public function testSentinelOption() $this->assertEquals(array('snc_redis.default' => array(array('alias' => 'default'))), $container->findTaggedServiceIds('snc_redis.client')); } + /** + * Test valid config of the cluster option + */ + public function testClusterOption() + { + $extension = new SncRedisExtension(); + $config = $this->parseYaml($this->getClusterYamlConfig()); + $extension->load(array($config), $container = $this->getContainer()); + + $options = $container->getDefinition('snc_redis.client.default_options')->getArgument(0); + $this->assertEquals('redis', $options['cluster']); + $this->assertFalse(array_key_exists('replication', $options)); + + $parameters = $container->getDefinition('snc_redis.default')->getArgument(0); + $this->assertEquals('snc_redis.connection.default1_parameters.default', (string) $parameters[0]); + $this->assertEquals('snc_redis.connection.default2_parameters.default', (string) $parameters[1]); + + $this->assertInternalType('array', $container->findTaggedServiceIds('snc_redis.client')); + $this->assertEquals(array('snc_redis.default' => array(array('alias' => 'default'))), $container->findTaggedServiceIds('snc_redis.client')); + } + private function parseYaml($yaml) { $parser = new Parser(); @@ -504,6 +525,21 @@ private function getSentinelYamlConfig() EOF; } + private function getClusterYamlConfig() + { + return <<<'EOF' +clients: + default: + type: predis + alias: default + dsn: + - redis://127.0.0.1/1 + - redis://127.0.0.2/2 + options: + cluster: "redis" +EOF; + } + private function getMultipleReplicationYamlConfig() { return <<<'EOF' From 409fa63269a196b4c8ab610f5b893ee1e1a575eb Mon Sep 17 00:00:00 2001 From: Sander van Thillo Date: Sat, 11 Mar 2017 13:01:40 +0100 Subject: [PATCH 3/3] sample config for sentinel replication --- Resources/doc/index.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/Resources/doc/index.md b/Resources/doc/index.md index 288d102a..4df38520 100644 --- a/Resources/doc/index.md +++ b/Resources/doc/index.md @@ -120,6 +120,24 @@ snc_redis: Please note that the master dsn connection needs to be tagged with the ```master``` alias. If not, `predis` will complain. +A setup using `predis` sentinel replication could look like this: + +``` yaml +snc_redis: + clients: + default: + type: predis + alias: default + dsn: + - redis://localhost + - redis://otherhost + options: + replication: sentinel + service: mymaster +``` + +The `service` is the name of the set of Redis instances. +You can find more information about this on [Configuring Sentinel](https://redis.io/topics/sentinel#configuring-sentinel). ### Sessions ###