Skip to content

Commit

Permalink
Merge pull request #307 from othillo/master
Browse files Browse the repository at this point in the history
allow sentinel replication configuration
  • Loading branch information
snc authored Mar 15, 2017
2 parents 02c808d + 409fa63 commit 2c6b4ac
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 2 deletions.
3 changes: 2 additions & 1 deletion DependencyInjection/Configuration/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'))->end()
->scalarNode('service')->defaultNull()->end()
->end()
->end()
->end()
Expand Down
18 changes: 18 additions & 0 deletions Resources/doc/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 ###

Expand Down
76 changes: 75 additions & 1 deletion Tests/DependencyInjection/SncRedisExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -308,6 +308,49 @@ 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'));
}

/**
* 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();
Expand Down Expand Up @@ -466,6 +509,37 @@ 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 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'
Expand Down

0 comments on commit 2c6b4ac

Please sign in to comment.