Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow sentinel replication configuration #307

Merged
merged 3 commits into from
Mar 15, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these sentinel hosts or redis hosts? What about port? Does the replication: sentinel setting automatically use port 26379 on those hostnames?

- 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