Explorar el Código

Implement the concept of role for connections.

When using replication backends, now the role of a connection is not
defined by its alias but by the new connection parameter "role" that
can be set to "master", "slave" and (for redis-sentinel) "sentinel".

This also led to a redesign of how connections can be retrieved from
replication backends: the method getConnectionById() now retrieves a
connection only by its ID (ip:port pair), to get a connection by its
alias there is the new method getConnectionByAlias(). This method is
not supported by the redis-sentinel backend due to its dynamic nature
(connections are retrieved and initialized at runtime from sentinels)
but it is still possible to get a single connection from the pool by
using its ID. It is also possible to retrive a connection by its role
using the method getConnectionByRole().

NOTE: the "role" parameter is an hint for the internals of aggregate
connection backends so it is still possible that the actual role of
a connection changes during the execution of a script (e.g. a slave
gets promoted to the role of master): in this case the parameters of
that connection will not be changed as they are immutable, but the
method getConnectionByRole() will return the appropriate connection.

Predis\Client::getClientBy() has been updated with the addition of
"role" and "alias" as supported selectors.
Daniele Alessandri hace 8 años
padre
commit
184d583895

+ 11 - 0
CHANGELOG.md

@@ -25,6 +25,17 @@ v2.0.0 (201x-xx-xx)
   to configure the hash generator using the new `crc16` client option (accepted
   values `predis`, `phpiredis` or an hash generator instance).
 
+- Replication backends now use the `role` parameter instead of `alias` in order
+  to distinguish the role of a connection. Accepted values are `master`, `slave`
+  and, for redis-sentinel, `sentinel`. This led to a redesign of how connections
+  can be retrieved from replication backends: the method getConnectionById() now
+  retrieves a connection only by its ID (ip:port pair), to get a connection by
+  its alias there is the new method getConnectionByAlias(). This method is not
+  supported by the redis-sentinel backend due to its dynamic nature (connections
+  are retrieved and initialized at runtime from sentinels) but it is possible to
+  get a single connection from the pool by using its ID. It is also possible to
+  retrive a connection by role using the method getConnectionByRole().
+
 - Client option classes now live in the `Predis\Configuration\Option` namespace.
 
 - Classes for Redis commands have been moved into the new `Predis\Command\Redis`

+ 4 - 4
README.md

@@ -218,11 +218,11 @@ the value of a key. Instead of raising a connection error when a slave fails, th
 fall back to a different slave among the ones provided in the configuration.
 
 The basic configuration needed to use the client in replication mode requires one Redis server to be
-identified as the master (this can be done via connection parameters using the `alias` parameter set
-to `master`) and one or more servers acting as slaves:
+identified as the master (this can be done via connection parameters by setting the `role` parameter
+to `master`) and one or more slaves (in this case setting `role` to `slave` for slaves is optional):
 
 ```php
-$parameters = ['tcp://10.0.0.1?alias=master', 'tcp://10.0.0.2', 'tcp://10.0.0.3'];
+$parameters = ['tcp://10.0.0.1?role=master', 'tcp://10.0.0.2', 'tcp://10.0.0.3'];
 $options    = ['replication' => 'predis'];
 
 $client = new Predis\Client($parameters, $options);
@@ -264,7 +264,7 @@ when certain Lua scripts do not perform write operations it is possible to provi
 the client to stick with slaves for their execution:
 
 ```php
-$parameters = ['tcp://10.0.0.1?alias=master', 'tcp://10.0.0.2', 'tcp://10.0.0.3'];
+$parameters = ['tcp://10.0.0.1?role=master', 'tcp://10.0.0.2', 'tcp://10.0.0.3'];
 $options    = ['replication' => function () {
     // Set scripts that won't trigger a switch from a slave to the master node.
     $strategy = new Predis\Replication\ReplicationStrategy();

+ 3 - 3
examples/replication_complex.php

@@ -47,8 +47,8 @@ LUA;
 // ------------------------------------------------------------------------- //
 
 $parameters = array(
-    'tcp://127.0.0.1:6379/?alias=master',
-    'tcp://127.0.0.1:6380/?alias=slave',
+    'tcp://127.0.0.1:6381?role=master&database=15',
+    'tcp://127.0.0.1:6382?role=slave&alias=slave-01&database=15',
 );
 
 $options = array(
@@ -76,7 +76,7 @@ $client = new Predis\Client($parameters, $options);
 $hashes = $client->hmgetall('metavars', 'servers');
 
 $replication = $client->getConnection();
-$stillOnSlave = $replication->getCurrent() === $replication->getConnectionById('slave');
+$stillOnSlave = $replication->getCurrent() === $replication->getConnectionByAlias('slave-01');
 
 echo 'Is still on slave? ', $stillOnSlave ? 'YES!' : 'NO!', PHP_EOL;
 var_export($hashes);

+ 3 - 3
examples/replication_sentinel.php

@@ -39,17 +39,17 @@ $client = new Predis\Client($sentinels, array(
 // Read operation.
 $exists = $client->exists('foo') ? 'yes' : 'no';
 $current = $client->getConnection()->getCurrent()->getParameters();
-echo "Does 'foo' exist on {$current->alias}? $exists.", PHP_EOL;
+echo "Does 'foo' exist on {$current->role}? $exists.", PHP_EOL;
 
 // Write operation.
 $client->set('foo', 'bar');
 $current = $client->getConnection()->getCurrent()->getParameters();
-echo "Now 'foo' has been set to 'bar' on {$current->alias}!", PHP_EOL;
+echo "Now 'foo' has been set to 'bar' on {$current->role}!", PHP_EOL;
 
 // Read operation.
 $bar = $client->get('foo');
 $current = $client->getConnection()->getCurrent()->getParameters();
-echo "We fetched 'foo' from {$current->alias} and its value is '$bar'.", PHP_EOL;
+echo "We fetched 'foo' from {$current->role} and its value is '$bar'.", PHP_EOL;
 
 /* OUTPUT:
 Does 'foo' exist on slave-127.0.0.1:6381? yes.

+ 5 - 5
examples/replication_simple.php

@@ -22,8 +22,8 @@ require __DIR__.'/shared.php';
 //
 
 $parameters = array(
-    'tcp://127.0.0.1:6379?database=15&alias=master',
-    'tcp://127.0.0.1:6380?database=15&alias=slave',
+    'tcp://127.0.0.1:6381?role=master&database=15',
+    'tcp://127.0.0.1:6382?role=slave&database=15',
 );
 
 $options = array('replication' => 'predis');
@@ -33,17 +33,17 @@ $client = new Predis\Client($parameters, $options);
 // Read operation.
 $exists = $client->exists('foo') ? 'yes' : 'no';
 $current = $client->getConnection()->getCurrent()->getParameters();
-echo "Does 'foo' exist on {$current->alias}? $exists.", PHP_EOL;
+echo "Does 'foo' exist on {$current->role}? $exists.", PHP_EOL;
 
 // Write operation.
 $client->set('foo', 'bar');
 $current = $client->getConnection()->getCurrent()->getParameters();
-echo "Now 'foo' has been set to 'bar' on {$current->alias}!", PHP_EOL;
+echo "Now 'foo' has been set to 'bar' on {$current->role}!", PHP_EOL;
 
 // Read operation.
 $bar = $client->get('foo');
 $current = $client->getConnection()->getCurrent()->getParameters();
-echo "We fetched 'foo' from {$current->alias} and its value is '$bar'.", PHP_EOL;
+echo "We fetched 'foo' from {$current->role} and its value is '$bar'.", PHP_EOL;
 
 /* OUTPUT:
 Does 'foo' exist on slave? yes.

+ 1 - 1
src/Client.php

@@ -215,7 +215,7 @@ class Client implements ClientInterface, \IteratorAggregate
     {
         $selector = strtolower($selector);
 
-        if (!in_array($selector, array('id', 'key', 'slot', 'command'))) {
+        if (!in_array($selector, array('id', 'key', 'slot', 'role', 'alias', 'command'))) {
             throw new \InvalidArgumentException("Invalid selector type: `$selector`");
         }
 

+ 80 - 38
src/Connection/Replication/MasterSlaveReplication.php

@@ -44,6 +44,16 @@ class MasterSlaveReplication implements ReplicationInterface
      */
     protected $slaves = array();
 
+    /**
+     * @var NodeConnectionInterface[]
+     */
+    protected $pool = array();
+
+    /**
+     * @var NodeConnectionInterface[]
+     */
+    protected $aliases = array();
+
     /**
      * @var NodeConnectionInterface
      */
@@ -105,14 +115,21 @@ class MasterSlaveReplication implements ReplicationInterface
      */
     public function add(NodeConnectionInterface $connection)
     {
-        $alias = $connection->getParameters()->alias;
+        $parameters = $connection->getParameters();
 
-        if ($alias === 'master') {
+        if ('master' === $parameters->role) {
             $this->master = $connection;
         } else {
-            $this->slaves[$alias ?: "slave-$connection"] = $connection;
+            // everything else is considered a slvave.
+            $this->slaves[] = $connection;
+        }
+
+        if (isset($parameters->alias)) {
+            $this->aliases[$parameters->alias] = $connection;
         }
 
+        $this->pool[(string) $connection] = $connection;
+
         $this->reset();
     }
 
@@ -121,21 +138,23 @@ class MasterSlaveReplication implements ReplicationInterface
      */
     public function remove(NodeConnectionInterface $connection)
     {
-        if ($connection->getParameters()->alias === 'master') {
+        if ($connection === $this->master) {
             $this->master = null;
-            $this->reset();
-
-            return true;
+        } elseif (false !== $id = array_search($connection, $this->slaves, true)) {
+            unset($this->slaves[$id]);
         } else {
-            if (($id = array_search($connection, $this->slaves, true)) !== false) {
-                unset($this->slaves[$id]);
-                $this->reset();
+            return false;
+        }
 
-                return true;
-            }
+        unset($this->pool[(string) $connection]);
+
+        if ($this->aliases && $alias = $connection->getParameters()->alias) {
+            unset($this->aliases[$alias]);
         }
 
-        return false;
+        $this->reset();
+
+        return true;
     }
 
     /**
@@ -167,33 +186,52 @@ class MasterSlaveReplication implements ReplicationInterface
     /**
      * {@inheritdoc}
      */
-    public function getConnectionById($connectionId)
+    public function getConnectionById($id)
     {
-        if ($connectionId === 'master') {
-            return $this->master;
+        if (isset($this->pool[$id])) {
+            return $this->pool[$id];
         }
+    }
 
-        if (isset($this->slaves[$connectionId])) {
-            return $this->slaves[$connectionId];
+    /**
+     * Returns a connection instance by its alias.
+     *
+     * @param string $alias Connection alias.
+     *
+     * @return NodeConnectionInterface|null
+     */
+    public function getConnectionByAlias($alias)
+    {
+        if (isset($this->aliases[$alias])) {
+            return $this->aliases[$alias];
         }
-
-        return;
     }
 
     /**
-     * Switches the connection in use by the backend to the specified instance
-     * or alias of a connection.
+     * Returns a connection by its role.
+     *
+     * @param string $role Connection role (`master` or `slave`)
      *
-     * @param NodeConnectionInterface|string $connection Instance or alias of a connection.
+     * @return NodeConnectionInterface|null
      */
-    public function switchTo($connection)
+    public function getConnectionByRole($role)
     {
-        if (!$connection instanceof NodeConnectionInterface) {
-            $connection = $this->getConnectionById($connection);
+        if ($role === 'master') {
+            return $this->getMaster();
+        } elseif ($role === 'slave') {
+            return $this->pickSlave();
         }
+    }
 
-        if (!$connection) {
-            throw new \InvalidArgumentException('Invalid connection or connection not found.');
+    /**
+     * Switches the internal connection in use by the backend.
+     *
+     * @param NodeConnectionInterface $connection Connection instance in the pool.
+     */
+    public function switchTo(NodeConnectionInterface $connection)
+    {
+        if ($connection && $connection === $this->current) {
+            return;
         }
 
         if ($connection !== $this->master && !in_array($connection, $this->slaves, true)) {
@@ -208,7 +246,11 @@ class MasterSlaveReplication implements ReplicationInterface
      */
     public function switchToMaster()
     {
-        $this->switchTo('master');
+        if (!$connection = $this->getConnectionByRole('master')) {
+            throw new \InvalidArgumentException('Invalid connection or connection not found.');
+        }
+
+        $this->switchTo($connection);
     }
 
     /**
@@ -216,7 +258,10 @@ class MasterSlaveReplication implements ReplicationInterface
      */
     public function switchToSlave()
     {
-        $connection = $this->pickSlave();
+        if (!$connection = $this->getConnectionByRole('slave')) {
+            throw new \InvalidArgumentException('Invalid connection or connection not found.');
+        }
+
         $this->switchTo($connection);
     }
 
@@ -255,7 +300,7 @@ class MasterSlaveReplication implements ReplicationInterface
      */
     public function getSlaves()
     {
-        return array_values($this->slaves);
+        return $this->slaves;
     }
 
     /**
@@ -309,11 +354,7 @@ class MasterSlaveReplication implements ReplicationInterface
      */
     public function disconnect()
     {
-        if ($this->master) {
-            $this->master->disconnect();
-        }
-
-        foreach ($this->slaves as $connection) {
+        foreach ($this->pool as $connection) {
             $connection->disconnect();
         }
     }
@@ -390,6 +431,7 @@ class MasterSlaveReplication implements ReplicationInterface
                 $slaveConnection = $connectionFactory->create(array(
                     'host' => $parameters['host'],
                     'port' => $parameters['port'],
+                    'role' => 'slave',
                 ));
 
                 $this->add($slaveConnection);
@@ -415,7 +457,7 @@ class MasterSlaveReplication implements ReplicationInterface
         $masterConnection = $connectionFactory->create(array(
             'host' => $replication['master_host'],
             'port' => $replication['master_port'],
-            'alias' => 'master',
+            'role' => 'master',
         ));
 
         $this->add($masterConnection);
@@ -507,6 +549,6 @@ class MasterSlaveReplication implements ReplicationInterface
      */
     public function __sleep()
     {
-        return array('master', 'slaves', 'strategy');
+        return array('master', 'slaves', 'pool', 'aliases', 'strategy');
     }
 }

+ 60 - 34
src/Connection/Replication/SentinelReplication.php

@@ -39,6 +39,11 @@ class SentinelReplication implements ReplicationInterface
      */
     protected $slaves = array();
 
+    /**
+     * @var NodeConnectionInterface[]
+     */
+    protected $pool = array();
+
     /**
      * @var NodeConnectionInterface
      */
@@ -183,6 +188,7 @@ class SentinelReplication implements ReplicationInterface
 
         $this->master = null;
         $this->slaves = array();
+        $this->pool = array();
     }
 
     /**
@@ -190,14 +196,21 @@ class SentinelReplication implements ReplicationInterface
      */
     public function add(NodeConnectionInterface $connection)
     {
-        $alias = $connection->getParameters()->alias;
+        $parameters = $connection->getParameters();
 
-        if ($alias === 'master') {
+        if ('master' === $role = $parameters->role) {
             $this->master = $connection;
+        } elseif ('sentinel' === $role) {
+            $this->sentinels[] = $connection;
+            // sentinels are not considered part of the pool.
+            return;
         } else {
-            $this->slaves[$alias ?: count($this->slaves)] = $connection;
+            // everything else is considered a slave.
+            $this->slaves[] = $connection;
         }
 
+        $this->pool[(string) $connection] = $connection;
+
         $this->reset();
     }
 
@@ -208,19 +221,21 @@ class SentinelReplication implements ReplicationInterface
     {
         if ($connection === $this->master) {
             $this->master = null;
-            $this->reset();
+        } elseif (false !== $id = array_search($connection, $this->slaves, true)) {
+            unset($this->slaves[$id]);
+        } elseif (false !== $id = array_search($connection, $this->sentinels, true)) {
+            unset($this->sentinels[$id]);
 
             return true;
+        } else {
+            return false;
         }
 
-        if (false !== $id = array_search($connection, $this->slaves, true)) {
-            unset($this->slaves[$id]);
-            $this->reset();
+        unset($this->pool[(string) $connection]);
 
-            return true;
-        }
+        $this->reset();
 
-        return false;
+        return true;
     }
 
     /**
@@ -296,6 +311,7 @@ class SentinelReplication implements ReplicationInterface
                     $this->sentinels[] = array(
                         'host' => $sentinel[3],
                         'port' => $sentinel[5],
+                        'role' => 'sentinel',
                     );
                 }
             } catch (ConnectionException $exception) {
@@ -358,7 +374,7 @@ class SentinelReplication implements ReplicationInterface
         return array(
             'host' => $payload[0],
             'port' => $payload[1],
-            'alias' => 'master',
+            'role' => 'master',
         );
     }
 
@@ -392,7 +408,7 @@ class SentinelReplication implements ReplicationInterface
             $slaves[] = array(
                 'host' => $slave[3],
                 'port' => $slave[5],
-                'alias' => "slave-$slave[1]",
+                'role' => 'slave',
             );
         }
 
@@ -467,7 +483,7 @@ class SentinelReplication implements ReplicationInterface
             }
         }
 
-        return array_values($this->slaves ?: array());
+        return array_values($this->slaves);
     }
 
     /**
@@ -548,28 +564,41 @@ class SentinelReplication implements ReplicationInterface
     /**
      * {@inheritdoc}
      */
-    public function getConnectionById($connectionId)
+    public function getConnectionById($id)
     {
-        if ($connectionId === 'master') {
-            return $this->getMaster();
-        }
-
-        $this->getSlaves();
-
-        if (isset($this->slaves[$connectionId])) {
-            return $this->slaves[$connectionId];
+        if (isset($this->pool[$id])) {
+            return $this->pool[$id];
         }
     }
 
     /**
-     * {@inheritdoc}
+     * Returns a connection by its role.
+     *
+     * @param string $role Connection role (`master`, `slave` or `sentinel`)
+     *
+     * @return NodeConnectionInterface|null
      */
-    public function switchTo($connection)
+    public function getConnectionByRole($role)
     {
-        if (!$connection instanceof NodeConnectionInterface) {
-            $connection = $this->getConnectionById($connection);
+        if ($role === 'master') {
+            return $this->getMaster();
+        } elseif ($role === 'slave') {
+            return $this->pickSlave();
+        } elseif ($role === 'sentinel') {
+            return $this->getSentinelConnection();
         }
+    }
 
+    /**
+     * Switches the internal connection in use by the backend.
+     *
+     * Sentinel connections are not considered as part of the pool, meaning that
+     * trying to switch to a sentinel will throw an exception.
+     *
+     * @param NodeConnectionInterface $connection Connection instance in the pool.
+     */
+    public function switchTo(NodeConnectionInterface $connection)
+    {
         if ($connection && $connection === $this->current) {
             return;
         }
@@ -592,7 +621,8 @@ class SentinelReplication implements ReplicationInterface
      */
     public function switchToMaster()
     {
-        $this->switchTo('master');
+        $connection = $this->getConnectionByRole('master');
+        $this->switchTo($connection);
     }
 
     /**
@@ -600,7 +630,7 @@ class SentinelReplication implements ReplicationInterface
      */
     public function switchToSlave()
     {
-        $connection = $this->pickSlave();
+        $connection = $this->getConnectionByRole('slave');
         $this->switchTo($connection);
     }
 
@@ -631,11 +661,7 @@ class SentinelReplication implements ReplicationInterface
      */
     public function disconnect()
     {
-        if ($this->master) {
-            $this->master->disconnect();
-        }
-
-        foreach ($this->slaves as $connection) {
+        foreach ($this->pool as $connection) {
             $connection->disconnect();
         }
     }
@@ -714,7 +740,7 @@ class SentinelReplication implements ReplicationInterface
     public function __sleep()
     {
         return array(
-            'master', 'slaves', 'service', 'sentinels', 'connectionFactory', 'strategy',
+            'master', 'slaves', 'pool', 'service', 'sentinels', 'connectionFactory', 'strategy',
         );
     }
 }

+ 54 - 8
tests/Predis/ClientTest.php

@@ -273,13 +273,13 @@ class ClientTest extends PredisTestCase
      */
     public function testConstructorWithArrayAndOptionReplication()
     {
-        $arg1 = array('tcp://host1?alias=master', 'tcp://host2?alias=slave');
+        $arg1 = array('tcp://127.0.0.1:6379?role=master', 'tcp://127.0.0.1:6380?role=slave');
         $arg2 = array('replication' => 'predis');
         $client = new Client($arg1, $arg2);
 
         $this->assertInstanceOf('Predis\Connection\Replication\ReplicationInterface', $connection = $client->getConnection());
-        $this->assertSame('host1', $connection->getConnectionById('master')->getParameters()->host);
-        $this->assertSame('host2', $connection->getConnectionById('slave')->getParameters()->host);
+        $this->assertSame('127.0.0.1:6379', (string) $connection->getConnectionByRole('master'));
+        $this->assertSame('127.0.0.1:6380', (string) $connection->getConnectionByRole('slave'));
     }
 
     /**
@@ -731,11 +731,11 @@ class ClientTest extends PredisTestCase
         $aggregate
             ->expects($this->once())
             ->method('getConnectionById')
-            ->with('nodeXX')
+            ->with('127.0.0.1:6379')
             ->will($this->returnValue($connection));
 
         $client = new Client($aggregate);
-        $nodeClient = $client->getClientBy('id', 'nodeXX');
+        $nodeClient = $client->getClientBy('id', '127.0.0.1:6379');
 
         $this->assertSame($connection, $nodeClient->getConnection());
         $this->assertSame($client->getOptions(), $nodeClient->getOptions());
@@ -744,7 +744,7 @@ class ClientTest extends PredisTestCase
     /**
      * @group disconnected
      * @expectedException \InvalidArgumentException
-     * @expectedExceptionMessage Cannot find a connection by id matching `nodeXX`
+     * @expectedExceptionMessage Cannot find a connection by id matching `127.0.0.1:7000`
      */
     public function testGetClientByMethodThrowsExceptionSelectingConnectionByUnknownId()
     {
@@ -752,11 +752,34 @@ class ClientTest extends PredisTestCase
         $aggregate
             ->expects($this->once())
             ->method('getConnectionById')
-            ->with('nodeXX')
+            ->with('127.0.0.1:7000')
             ->will($this->returnValue(null));
 
         $client = new Client($aggregate);
-        $client->getClientBy('id', 'nodeXX');
+        $client->getClientBy('id', '127.0.0.1:7000');
+    }
+
+    /**
+     * @group disconnected
+     */
+    public function testGetClientByMethodSupportsSelectingConnectionByAlias()
+    {
+        $connection = $this->getMock('Predis\Connection\NodeConnectionInterface');
+
+        $aggregate = $this->getMockBuilder('Predis\Connection\AggregateConnectionInterface')
+            ->setMethods(array('getConnectionByAlias'))
+            ->getMockForAbstractClass();
+        $aggregate
+            ->expects($this->once())
+            ->method('getConnectionByAlias')
+            ->with('myalias')
+            ->will($this->returnValue($connection));
+
+        $client = new Client($aggregate);
+        $nodeClient = $client->getClientBy('alias', 'myalias');
+
+        $this->assertSame($connection, $nodeClient->getConnection());
+        $this->assertSame($client->getOptions(), $nodeClient->getOptions());
     }
 
     /**
@@ -805,6 +828,29 @@ class ClientTest extends PredisTestCase
         $this->assertSame($client->getOptions(), $nodeClient->getOptions());
     }
 
+    /**
+     * @group disconnected
+     */
+    public function testGetClientByMethodSupportsSelectingConnectionByRole()
+    {
+        $connection = $this->getMock('Predis\Connection\NodeConnectionInterface');
+
+        $aggregate = $this->getMockBuilder('Predis\Connection\AggregateConnectionInterface')
+            ->setMethods(array('getConnectionByRole'))
+            ->getMockForAbstractClass();
+        $aggregate
+            ->expects($this->once())
+            ->method('getConnectionByRole')
+            ->with('master')
+            ->will($this->returnValue($connection));
+
+        $client = new Client($aggregate);
+        $nodeClient = $client->getClientBy('role', 'master');
+
+        $this->assertSame($connection, $nodeClient->getConnection());
+        $this->assertSame($client->getOptions(), $nodeClient->getOptions());
+    }
+
     /**
      * @group disconnected
      */

+ 197 - 112
tests/Predis/Connection/Replication/MasterSlaveReplicationTest.php

@@ -27,31 +27,46 @@ class MasterSlaveReplicationTest extends PredisTestCase
      */
     public function testAddingConnectionsToReplication()
     {
-        $master = $this->getMockConnection('tcp://host1?alias=master');
-        $slave1 = $this->getMockConnection('tcp://host2?alias=slave1');
-        $slave2 = $this->getMockConnection('tcp://host3?alias=slave2');
+        $master = $this->getMockConnection('tcp://127.0.0.1:6379?role=master');
+        $slave1 = $this->getMockConnection('tcp://127.0.0.1:6380?role=slave');
+        $slave2 = $this->getMockConnection('tcp://127.0.0.1:6381?role=slave');
 
         $replication = new MasterSlaveReplication();
         $replication->add($master);
         $replication->add($slave1);
         $replication->add($slave2);
 
-        $this->assertSame($master, $replication->getConnectionById('master'));
-        $this->assertSame($slave1, $replication->getConnectionById('slave1'));
-        $this->assertSame($slave2, $replication->getConnectionById('slave2'));
+        $this->assertSame($master, $replication->getConnectionById('127.0.0.1:6379'));
+        $this->assertSame($slave1, $replication->getConnectionById('127.0.0.1:6380'));
+        $this->assertSame($slave2, $replication->getConnectionById('127.0.0.1:6381'));
 
         $this->assertSame($master, $replication->getMaster());
         $this->assertSame(array($slave1, $slave2), $replication->getSlaves());
     }
 
+    /**
+     * @group disconnected
+     */
+    public function testAddingConnectionsWithoutRoleParameterDefaultsToSlaveRole()
+    {
+        $slave1 = $this->getMockConnection('tcp://127.0.0.1:6380');
+        $slave2 = $this->getMockConnection('tcp://127.0.0.1:6381');
+
+        $replication = new MasterSlaveReplication();
+        $replication->add($slave1);
+        $replication->add($slave2);
+
+        $this->assertSame(array($slave1, $slave2), $replication->getSlaves());
+    }
+
     /**
      * @group disconnected
      */
     public function testRemovingConnectionsFromReplication()
     {
-        $master = $this->getMockConnection('tcp://host1?alias=master');
-        $slave1 = $this->getMockConnection('tcp://host2?alias=slave1');
-        $slave2 = $this->getMockConnection('tcp://host3?alias=slave2');
+        $master = $this->getMockConnection('tcp://127.0.0.1:6379?role=master');
+        $slave1 = $this->getMockConnection('tcp://127.0.0.1:6380?role=slave');
+        $slave2 = $this->getMockConnection('tcp://127.0.0.1:6381?role=slave');
 
         $replication = new MasterSlaveReplication();
         $replication->add($master);
@@ -67,17 +82,80 @@ class MasterSlaveReplicationTest extends PredisTestCase
     /**
      * @group disconnected
      */
-    public function testAddingConnectionsToReplicationWithoutAliasesResultsInCustomId()
+    public function testGetConnectionByIdOnEmptyReplication()
     {
-        $slave1 = $this->getMockConnection('tcp://host1');
-        $slave2 = $this->getMockConnection('tcp://host2:6380');
+        $replication = new MasterSlaveReplication();
+
+        $this->assertNull($replication->getConnectionById('127.0.0.1:6379'));
+    }
+
+    /**
+     * @group disconnected
+     */
+    public function testGetConnectionByAlias()
+    {
+        $slave1 = $this->getMockConnection('tcp://127.0.0.1:6379?alias=aliased');
+        $slave2 = $this->getMockConnection('tcp://127.0.0.1:6380');
 
         $replication = new MasterSlaveReplication();
         $replication->add($slave1);
         $replication->add($slave2);
 
-        $this->assertSame($slave1, $replication->getConnectionById('slave-host1:6379'));
-        $this->assertSame($slave2, $replication->getConnectionById('slave-host2:6380'));
+        $this->assertSame($slave1, $replication->getConnectionByAlias('aliased'));
+        $this->assertNull($replication->getConnectionByAlias('127.0.0.1:6380'));
+        $this->assertNull($replication->getConnectionByAlias('unkswn'));
+    }
+
+    /**
+     * @group disconnected
+     */
+    public function testGetConnectionByAliasOnEmptyReplication()
+    {
+        $replication = new MasterSlaveReplication();
+
+        $this->assertNull($replication->getConnectionByAlias('unknown'));
+    }
+
+    /**
+     * @group disconnected
+     */
+    public function testGetConnectionByRole()
+    {
+        $master = $this->getMockConnection('tcp://127.0.0.1:6379?role=master');
+        $slave1 = $this->getMockConnection('tcp://127.0.0.1:6380?role=slave');
+
+        $replication = new MasterSlaveReplication();
+        $replication->add($master);
+        $replication->add($slave1);
+
+        $this->assertSame($master, $replication->getConnectionByRole('master'));
+        $this->assertSame($slave1, $replication->getConnectionByRole('slave'));
+    }
+
+    /**
+     * @group disconnected
+     */
+    public function testGetConnectionByRoleOnEmptyReplication()
+    {
+        $replication = new MasterSlaveReplication();
+
+        $this->assertNull($replication->getConnectionByRole('master'));
+        $this->assertNull($replication->getConnectionByRole('slave'));
+    }
+
+    /**
+     * @group disconnected
+     */
+    public function testGetConnectionByRoleUnknown()
+    {
+        $master = $this->getMockConnection('tcp://127.0.0.1:6379?role=master');
+        $slave1 = $this->getMockConnection('tcp://127.0.0.1:6380?role=slave');
+
+        $replication = new MasterSlaveReplication();
+        $replication->add($master);
+        $replication->add($slave1);
+
+        $this->assertNull($replication->getConnectionByRole('unknown'));
     }
 
     /**
@@ -96,12 +174,12 @@ class MasterSlaveReplicationTest extends PredisTestCase
      */
     public function testConnectsToOneOfSlaves()
     {
-        $master = $this->getMockConnection('tcp://host1?alias=master');
+        $master = $this->getMockConnection('tcp://127.0.0.1:6379?role=master');
         $master
             ->expects($this->never())
             ->method('connect');
 
-        $slave = $this->getMockConnection('tcp://host2?alias=slave1');
+        $slave = $this->getMockConnection('tcp://127.0.0.1:6380?role=slave');
         $slave
             ->expects($this->once())
             ->method('connect');
@@ -118,7 +196,7 @@ class MasterSlaveReplicationTest extends PredisTestCase
      */
     public function testConnectsToMasterOnMissingSlaves()
     {
-        $master = $this->getMockConnection('tcp://host1?alias=master');
+        $master = $this->getMockConnection('tcp://127.0.0.1:6379?role=master');
 
         $replication = new MasterSlaveReplication();
         $replication->add($master);
@@ -132,13 +210,13 @@ class MasterSlaveReplicationTest extends PredisTestCase
      */
     public function testIsConnectedReturnsTrueIfAtLeastOneConnectionIsOpen()
     {
-        $master = $this->getMockConnection('tcp://host1?alias=master');
+        $master = $this->getMockConnection('tcp://127.0.0.1:6379?role=master');
         $master
             ->expects($this->never())
             ->method('isConnected')
             ->will($this->returnValue(false));
 
-        $slave = $this->getMockConnection('tcp://host2?alias=slave1');
+        $slave = $this->getMockConnection('tcp://127.0.0.1:6380?role=slave');
         $slave
             ->expects($this->once())
             ->method('isConnected')
@@ -157,13 +235,13 @@ class MasterSlaveReplicationTest extends PredisTestCase
      */
     public function testIsConnectedReturnsFalseIfAllConnectionsAreClosed()
     {
-        $master = $this->getMockConnection('tcp://host1?alias=master');
+        $master = $this->getMockConnection('tcp://127.0.0.1:6379?role=master');
         $master
             ->expects($this->any())
             ->method('isConnected')
             ->will($this->returnValue(false));
 
-        $slave = $this->getMockConnection('tcp://host2?alias=slave1');
+        $slave = $this->getMockConnection('tcp://127.0.0.1:6380?role=slave');
         $slave
             ->expects($this->any())
             ->method('isConnected')
@@ -186,12 +264,12 @@ class MasterSlaveReplicationTest extends PredisTestCase
      */
     public function testDisconnectForcesCurrentConnectionToDisconnect()
     {
-        $master = $this->getMockConnection('tcp://host1?alias=master');
+        $master = $this->getMockConnection('tcp://127.0.0.1:6379?role=master');
         $master
             ->expects($this->once())
             ->method('disconnect');
 
-        $slave = $this->getMockConnection('tcp://host2?alias=slave1');
+        $slave = $this->getMockConnection('tcp://127.0.0.1:6380?role=slave');
         $slave
             ->expects($this->once())
             ->method('disconnect');
@@ -206,10 +284,10 @@ class MasterSlaveReplicationTest extends PredisTestCase
     /**
      * @group disconnected
      */
-    public function testCanSwitchConnectionByAlias()
+    public function testCanSwitchConnection()
     {
-        $master = $this->getMockConnection('tcp://host1?alias=master');
-        $slave1 = $this->getMockConnection('tcp://host2?alias=slave1');
+        $master = $this->getMockConnection('tcp://127.0.0.1:6379?role=master');
+        $slave1 = $this->getMockConnection('tcp://127.0.0.1:6380?role=slave');
 
         $replication = new MasterSlaveReplication();
         $replication->add($master);
@@ -217,10 +295,10 @@ class MasterSlaveReplicationTest extends PredisTestCase
 
         $this->assertNull($replication->getCurrent());
 
-        $replication->switchTo('master');
+        $replication->switchTo($master);
         $this->assertSame($master, $replication->getCurrent());
 
-        $replication->switchTo('slave1');
+        $replication->switchTo($slave1);
         $this->assertSame($slave1, $replication->getCurrent());
     }
 
@@ -229,14 +307,16 @@ class MasterSlaveReplicationTest extends PredisTestCase
      * @expectedException \InvalidArgumentException
      * @expectedExceptionMessage Invalid connection or connection not found.
      */
-    public function testThrowsErrorWhenSwitchingToUnknownConnectionByAlias()
+    public function testThrowsErrorWhenSwitchingToConnectionNotInPool()
     {
         $replication = new MasterSlaveReplication();
 
-        $replication->add($this->getMockConnection('tcp://host1?alias=master'));
-        $replication->add($this->getMockConnection('tcp://host2?alias=slave1'));
+        $replication->add($this->getMockConnection('tcp://127.0.0.1:6379?role=master'));
+        $replication->add($this->getMockConnection('tcp://127.0.0.1:6380?role=slave'));
+
+        $unknown = $this->getMockConnection('tcp://127.0.0.1:6381');
 
-        $replication->switchTo('unknown');
+        $replication->switchTo($unknown);
     }
 
     /**
@@ -244,8 +324,8 @@ class MasterSlaveReplicationTest extends PredisTestCase
      */
     public function testCanSwitchConnectionByInstance()
     {
-        $master = $this->getMockConnection('tcp://host1?alias=master');
-        $slave1 = $this->getMockConnection('tcp://host2?alias=slave1');
+        $master = $this->getMockConnection('tcp://127.0.0.1:6379?role=master');
+        $slave1 = $this->getMockConnection('tcp://127.0.0.1:6380?role=slave');
 
         $replication = new MasterSlaveReplication();
         $replication->add($master);
@@ -269,10 +349,10 @@ class MasterSlaveReplicationTest extends PredisTestCase
     {
         $replication = new MasterSlaveReplication();
 
-        $replication->add($this->getMockConnection('tcp://host1?alias=master'));
-        $replication->add($this->getMockConnection('tcp://host2?alias=slave1'));
+        $replication->add($this->getMockConnection('tcp://127.0.0.1:6379?role=master'));
+        $replication->add($this->getMockConnection('tcp://127.0.0.1:6380?role=slave'));
 
-        $slave2 = $this->getMockConnection('tcp://host3?alias=slave2');
+        $slave2 = $this->getMockConnection('tcp://127.0.0.1:6381');
 
         $replication->switchTo($slave2);
     }
@@ -282,9 +362,9 @@ class MasterSlaveReplicationTest extends PredisTestCase
      */
     public function testCanSwitchToMaster()
     {
-        $master = $this->getMockConnection('tcp://host1?alias=master');
-        $slave1 = $this->getMockConnection('tcp://host2?alias=slave1');
-        $slave2 = $this->getMockConnection('tcp://host3?alias=slave2');
+        $master = $this->getMockConnection('tcp://127.0.0.1:6379?role=master');
+        $slave1 = $this->getMockConnection('tcp://127.0.0.1:6380?role=slave');
+        $slave2 = $this->getMockConnection('tcp://127.0.0.1:6381?role=slave');
 
         $replication = new MasterSlaveReplication();
 
@@ -305,7 +385,7 @@ class MasterSlaveReplicationTest extends PredisTestCase
      */
     public function testThrowsErrorOnSwitchToMasterWithNoMasterDefined()
     {
-        $slave1 = $this->getMockConnection('tcp://host2?alias=slave1');
+        $slave1 = $this->getMockConnection('tcp://127.0.0.1:6380?role=slave');
 
         $replication = new MasterSlaveReplication();
 
@@ -321,8 +401,8 @@ class MasterSlaveReplicationTest extends PredisTestCase
      */
     public function testCanSwitchToRandomSlave()
     {
-        $master = $this->getMockConnection('tcp://host1?alias=master');
-        $slave1 = $this->getMockConnection('tcp://host2?alias=slave1');
+        $master = $this->getMockConnection('tcp://127.0.0.1:6379?role=master');
+        $slave1 = $this->getMockConnection('tcp://127.0.0.1:6380?role=slave');
 
         $replication = new MasterSlaveReplication();
 
@@ -342,7 +422,7 @@ class MasterSlaveReplicationTest extends PredisTestCase
      */
     public function testThrowsErrorOnSwitchToRandomSlaveWithNoSlavesDefined()
     {
-        $master = $this->getMockConnection('tcp://host1?alias=master');
+        $master = $this->getMockConnection('tcp://127.0.0.1:6379?role=master');
 
         $replication = new MasterSlaveReplication();
 
@@ -358,8 +438,8 @@ class MasterSlaveReplicationTest extends PredisTestCase
     {
         $commands = $this->getCommandFactory();
 
-        $master = $this->getMockConnection('tcp://host1?alias=master');
-        $slave1 = $this->getMockConnection('tcp://host2?alias=slave1');
+        $master = $this->getMockConnection('tcp://127.0.0.1:6379?role=master');
+        $slave1 = $this->getMockConnection('tcp://127.0.0.1:6380?role=slave');
 
         $replication = new MasterSlaveReplication();
 
@@ -380,8 +460,8 @@ class MasterSlaveReplicationTest extends PredisTestCase
     {
         $commands = $this->getCommandFactory();
 
-        $master = $this->getMockConnection('tcp://host1?alias=master');
-        $slave1 = $this->getMockConnection('tcp://host2?alias=slave1');
+        $master = $this->getMockConnection('tcp://127.0.0.1:6379?role=master');
+        $slave1 = $this->getMockConnection('tcp://127.0.0.1:6380?role=slave');
 
         $replication = new MasterSlaveReplication();
 
@@ -402,7 +482,7 @@ class MasterSlaveReplicationTest extends PredisTestCase
     {
         $commands = $this->getCommandFactory();
 
-        $master = $this->getMockConnection('tcp://host1?alias=master');
+        $master = $this->getMockConnection('tcp://127.0.0.1:6379?role=master');
 
         $replication = new MasterSlaveReplication();
 
@@ -418,12 +498,12 @@ class MasterSlaveReplicationTest extends PredisTestCase
     /**
      * @group disconnected
      */
-    public function testSwitchesFromSlaveToMasterOnWriteRequestss()
+    public function testSwitchesFromSlaveToMasterOnWriteRequests()
     {
         $commands = $this->getCommandFactory();
 
-        $master = $this->getMockConnection('tcp://host1?alias=master');
-        $slave1 = $this->getMockConnection('tcp://host2?alias=slave1');
+        $master = $this->getMockConnection('tcp://127.0.0.1:6379?role=master');
+        $slave1 = $this->getMockConnection('tcp://127.0.0.1:6380?role=slave1');
 
         $replication = new MasterSlaveReplication();
 
@@ -449,13 +529,13 @@ class MasterSlaveReplicationTest extends PredisTestCase
         $cmdExists = $commands->createCommand('exists', array('foo'));
         $cmdSet = $commands->createCommand('set', array('foo', 'bar'));
 
-        $master = $this->getMockConnection('tcp://host1?alias=master');
+        $master = $this->getMockConnection('tcp://127.0.0.1:6379?role=master');
         $master
             ->expects($this->once())
             ->method('writeRequest')
             ->with($cmdSet);
 
-        $slave1 = $this->getMockConnection('tcp://host2?alias=slave1');
+        $slave1 = $this->getMockConnection('tcp://127.0.0.1:6380?role=slave');
         $slave1
             ->expects($this->once())
             ->method('writeRequest')
@@ -478,13 +558,13 @@ class MasterSlaveReplicationTest extends PredisTestCase
         $cmdExists = $commands->createCommand('exists', array('foo'));
         $cmdSet = $commands->createCommand('set', array('foo', 'bar'));
 
-        $master = $this->getMockConnection('tcp://host1?alias=master');
+        $master = $this->getMockConnection('tcp://127.0.0.1:6379?role=master');
         $master
             ->expects($this->once())
             ->method('readResponse')
             ->with($cmdSet);
 
-        $slave1 = $this->getMockConnection('tcp://host2?alias=slave1');
+        $slave1 = $this->getMockConnection('tcp://127.0.0.1:6380?role=slave');
         $slave1
             ->expects($this->once())
             ->method('readResponse')
@@ -508,13 +588,13 @@ class MasterSlaveReplicationTest extends PredisTestCase
         $cmdExists = $commands->createCommand('exists', array('foo'));
         $cmdSet = $commands->createCommand('set', array('foo', 'bar'));
 
-        $master = $this->getMockConnection('tcp://host1?alias=master');
+        $master = $this->getMockConnection('tcp://127.0.0.1:6379?role=master');
         $master
             ->expects($this->once())
             ->method('executeCommand')
             ->with($cmdSet);
 
-        $slave1 = $this->getMockConnection('tcp://host2?alias=slave1');
+        $slave1 = $this->getMockConnection('tcp://127.0.0.1:6380?role=slave');
         $slave1
             ->expects($this->once())
             ->method('executeCommand')
@@ -537,13 +617,13 @@ class MasterSlaveReplicationTest extends PredisTestCase
         $commands = $this->getCommandFactory();
         $cmdWatch = $commands->createCommand('watch', array('foo'));
 
-        $master = $this->getMockConnection('tcp://host1?alias=master');
+        $master = $this->getMockConnection('tcp://127.0.0.1:6379?role=master');
         $master
             ->expects($this->once())
             ->method('executeCommand')
             ->with($cmdWatch);
 
-        $slave1 = $this->getMockConnection('tcp://host2?alias=slave1');
+        $slave1 = $this->getMockConnection('tcp://127.0.0.1:6380?role=slave');
         $slave1
             ->expects($this->never())
             ->method('executeCommand');
@@ -564,13 +644,13 @@ class MasterSlaveReplicationTest extends PredisTestCase
         $commands = $this->getCommandFactory();
         $cmdMulti = $commands->createCommand('multi');
 
-        $master = $this->getMockConnection('tcp://host1?alias=master');
+        $master = $this->getMockConnection('tcp://127.0.0.1:6379?role=master');
         $master
             ->expects($this->once())
             ->method('executeCommand')
             ->with($cmdMulti);
 
-        $slave1 = $this->getMockConnection('tcp://host2?alias=slave1');
+        $slave1 = $this->getMockConnection('tcp://127.0.0.1:6380?role=slave');
         $slave1
             ->expects($this->never())
             ->method('executeCommand');
@@ -591,13 +671,13 @@ class MasterSlaveReplicationTest extends PredisTestCase
         $commands = $this->getCommandFactory();
         $cmdEval = $commands->createCommand('eval', array("return redis.call('info')"));
 
-        $master = $this->getMockConnection('tcp://host1?alias=master');
+        $master = $this->getMockConnection('tcp://127.0.0.1:6379?role=master');
         $master
             ->expects($this->once())
             ->method('executeCommand')
             ->with($cmdEval);
 
-        $slave1 = $this->getMockConnection('tcp://host2?alias=slave1');
+        $slave1 = $this->getMockConnection('tcp://127.0.0.1:6380?role=slave');
         $slave1
             ->expects($this->never())
             ->method('executeCommand');
@@ -619,13 +699,13 @@ class MasterSlaveReplicationTest extends PredisTestCase
         $cmdSortNormal = $commands->createCommand('sort', array('key'));
         $cmdSortStore = $commands->createCommand('sort', array('key', array('store' => 'key:store')));
 
-        $master = $this->getMockConnection('tcp://host1?alias=master');
+        $master = $this->getMockConnection('tcp://127.0.0.1:6379?role=master');
         $master
             ->expects($this->once())
             ->method('executeCommand')
             ->with($cmdSortStore);
 
-        $slave1 = $this->getMockConnection('tcp://host2?alias=slave1');
+        $slave1 = $this->getMockConnection('tcp://127.0.0.1:6380?role=slave');
         $slave1
             ->expects($this->once())
             ->method('executeCommand')
@@ -648,12 +728,12 @@ class MasterSlaveReplicationTest extends PredisTestCase
         $commands = $this->getCommandFactory();
         $cmdExists = $commands->createCommand('exists', array('key'));
 
-        $master = $this->getMockConnection('tcp://host1?alias=master');
+        $master = $this->getMockConnection('tcp://127.0.0.1:6379?role=master');
         $master
             ->expects($this->never())
             ->method('executeCommand');
 
-        $slave1 = $this->getMockConnection('tcp://host2?alias=slave1');
+        $slave1 = $this->getMockConnection('tcp://127.0.0.1:6380?role=slave&role=slave');
         $slave1
             ->expects($this->once())
             ->method('executeCommand')
@@ -662,7 +742,7 @@ class MasterSlaveReplicationTest extends PredisTestCase
                 new Connection\ConnectionException($slave1)
             ));
 
-        $slave2 = $this->getMockConnection('tcp://host3?alias=slave2');
+        $slave2 = $this->getMockConnection('tcp://127.0.0.1:6381?role=slave&alias=slave2');
         $slave2
             ->expects($this->once())
             ->method('executeCommand')
@@ -680,8 +760,8 @@ class MasterSlaveReplicationTest extends PredisTestCase
         $response = $replication->executeCommand($cmdExists);
 
         $this->assertSame(1, $response);
-        $this->assertNull($replication->getConnectionById('slave1'));
-        $this->assertSame($slave2, $replication->getConnectionById('slave2'));
+        $this->assertNull($replication->getConnectionByAlias('slave1'));
+        $this->assertSame($slave2, $replication->getConnectionByAlias('slave2'));
     }
 
     /**
@@ -692,21 +772,21 @@ class MasterSlaveReplicationTest extends PredisTestCase
         $commands = $this->getCommandFactory();
         $cmdExists = $commands->createCommand('exists', array('key'));
 
-        $master = $this->getMockConnection('tcp://host1?alias=master');
+        $master = $this->getMockConnection('tcp://127.0.0.1:6379?role=master');
         $master
             ->expects($this->once())
             ->method('executeCommand')
             ->with($cmdExists)
             ->will($this->returnValue(1));
 
-        $slave1 = $this->getMockConnection('tcp://host2?alias=slave1');
+        $slave1 = $this->getMockConnection('tcp://127.0.0.1:6380?role=slave');
         $slave1
             ->expects($this->once())
             ->method('executeCommand')
             ->with($cmdExists)
             ->will($this->throwException(new Connection\ConnectionException($slave1)));
 
-        $slave2 = $this->getMockConnection('tcp://host3?alias=slave2');
+        $slave2 = $this->getMockConnection('tcp://127.0.0.1:6381?role=slave');
         $slave2
             ->expects($this->once())
             ->method('executeCommand')
@@ -726,8 +806,8 @@ class MasterSlaveReplicationTest extends PredisTestCase
         $response = $replication->executeCommand($cmdExists);
 
         $this->assertSame(1, $response);
-        $this->assertNull($replication->getConnectionById('slave1'));
-        $this->assertNull($replication->getConnectionById('slave2'));
+        $this->assertNull($replication->getConnectionById('127.0.0.1:6380'));
+        $this->assertNull($replication->getConnectionById('127.0.0.1:6381'));
     }
 
     /**
@@ -738,7 +818,7 @@ class MasterSlaveReplicationTest extends PredisTestCase
         $commands = $this->getCommandFactory();
         $cmdExists = $commands->createCommand('exists', array('key'));
 
-        $slave1 = $this->getMockConnection('tcp://host1?alias=slave1');
+        $slave1 = $this->getMockConnection('tcp://127.0.0.1:6379?role=slave');
         $slave1
             ->expects($this->once())
             ->method('executeCommand')
@@ -764,7 +844,7 @@ class MasterSlaveReplicationTest extends PredisTestCase
         $commands = $this->getCommandFactory();
         $cmdSet = $commands->createCommand('set', array('key', 'value'));
 
-        $slave1 = $this->getMockConnection('tcp://host1?alias=slave1');
+        $slave1 = $this->getMockConnection('tcp://127.0.0.1:6379?role=slave');
         $slave1
             ->expects($this->never())
             ->method('executeCommand');
@@ -781,11 +861,11 @@ class MasterSlaveReplicationTest extends PredisTestCase
      */
     public function testDiscardsSlaveWhenRespondsLOADINGAndExecutesReadOnlyCommandOnNextSlave()
     {
-        $master = $this->getMockConnection('tcp://host1?alias=master');
+        $master = $this->getMockConnection('tcp://127.0.0.1:6379?role=master');
         $master->expects($this->never())
                ->method('executeCommand');
 
-        $slave1 = $this->getMockConnection('tcp://host2?alias=slave1');
+        $slave1 = $this->getMockConnection('tcp://127.0.0.1:6380?role=slave');
         $slave1
             ->expects($this->once())
             ->method('executeCommand')
@@ -796,7 +876,7 @@ class MasterSlaveReplicationTest extends PredisTestCase
                 new Response\Error('LOADING')
             ));
 
-        $slave2 = $this->getMockConnection('tcp://host3?alias=slave2');
+        $slave2 = $this->getMockConnection('tcp://127.0.0.1:6381?role=slave');
         $slave2
             ->expects($this->once())
             ->method('executeCommand')
@@ -818,8 +898,8 @@ class MasterSlaveReplicationTest extends PredisTestCase
         );
 
         $this->assertSame(1, $response);
-        $this->assertNull($replication->getConnectionById('slave1'));
-        $this->assertSame($slave2, $replication->getConnectionById('slave2'));
+        $this->assertNull($replication->getConnectionById('127.0.0.1:6380'));
+        $this->assertSame($slave2, $replication->getConnectionById('127.0.0.1:6381'));
     }
 
     /**
@@ -831,7 +911,7 @@ class MasterSlaveReplicationTest extends PredisTestCase
         $commands = $this->getCommandFactory();
         $cmdSet = $commands->createCommand('set', array('key', 'value'));
 
-        $master = $this->getMockConnection('tcp://host1?alias=master');
+        $master = $this->getMockConnection('tcp://127.0.0.1:6379?role=master');
         $master
             ->expects($this->once())
             ->method('executeCommand')
@@ -840,7 +920,7 @@ class MasterSlaveReplicationTest extends PredisTestCase
                 new Connection\ConnectionException($master)
             ));
 
-        $slave1 = $this->getMockConnection('tcp://host2?alias=slave1');
+        $slave1 = $this->getMockConnection('tcp://127.0.0.1:6380?role=slave');
         $slave1
             ->expects($this->never())
             ->method('executeCommand');
@@ -864,8 +944,8 @@ class MasterSlaveReplicationTest extends PredisTestCase
 
         $replication = new MasterSlaveReplication();
 
-        $replication->add($this->getMockConnection('tcp://host1?alias=master'));
-        $replication->add($this->getMockConnection('tcp://host2?alias=slave1'));
+        $replication->add($this->getMockConnection('tcp://127.0.0.1:6379?role=master'));
+        $replication->add($this->getMockConnection('tcp://127.0.0.1:6380?role=slave'));
 
         $replication->getConnectionByCommand($cmd);
     }
@@ -879,13 +959,13 @@ class MasterSlaveReplicationTest extends PredisTestCase
         $cmdSet = $commands->createCommand('set', array('foo', 'bar'));
         $cmdGet = $commands->createCommand('get', array('foo'));
 
-        $master = $this->getMockConnection('tcp://host1?alias=master');
+        $master = $this->getMockConnection('tcp://127.0.0.1:6379?role=master');
         $master
             ->expects($this->once())
             ->method('executeCommand')
             ->with($cmdGet);
 
-        $slave1 = $this->getMockConnection('tcp://host2?alias=slave1');
+        $slave1 = $this->getMockConnection('tcp://127.0.0.1:6380?role=slave');
         $slave1
             ->expects($this->once())
             ->method('executeCommand')
@@ -912,13 +992,13 @@ class MasterSlaveReplicationTest extends PredisTestCase
         $cmdExistsFoo = $commands->createCommand('exists', array('foo'));
         $cmdExistsBar = $commands->createCommand('exists', array('bar'));
 
-        $master = $this->getMockConnection('tcp://host1?alias=master');
+        $master = $this->getMockConnection('tcp://127.0.0.1:6379?role=master');
         $master
             ->expects($this->once())
             ->method('executeCommand')
             ->with($cmdExistsBar);
 
-        $slave1 = $this->getMockConnection('tcp://host2?alias=slave1');
+        $slave1 = $this->getMockConnection('tcp://127.0.0.1:6380?role=slave');
         $slave1
             ->expects($this->once())
             ->method('executeCommand')
@@ -951,12 +1031,12 @@ class MasterSlaveReplicationTest extends PredisTestCase
         $cmdEval = $commands->createCommand('eval', array($script = "return redis.call('info');"));
         $cmdEvalSha = $commands->createCommand('evalsha', array($scriptSHA1 = sha1($script)));
 
-        $master = $this->getMockConnection('tcp://host1?alias=master');
+        $master = $this->getMockConnection('tcp://127.0.0.1:6379?role=master');
         $master
             ->expects($this->never())
             ->method('executeCommand');
 
-        $slave1 = $this->getMockConnection('tcp://host2?alias=slave1');
+        $slave1 = $this->getMockConnection('tcp://127.0.0.1:6380?role=slave');
         $slave1
             ->expects($this->exactly(2))
             ->method('executeCommand')
@@ -986,7 +1066,7 @@ class MasterSlaveReplicationTest extends PredisTestCase
     {
         $replication = new MasterSlaveReplication();
 
-        $replication->add($this->getMockConnection('tcp://host1?alias=master'));
+        $replication->add($this->getMockConnection('tcp://127.0.0.1:6379?role=master'));
 
         $replication->discover();
     }
@@ -999,7 +1079,7 @@ class MasterSlaveReplicationTest extends PredisTestCase
         $connFactory = new Connection\Factory();
         $cmdInfo = Command\RawCommand::create('INFO', 'REPLICATION');
 
-        $master = $this->getMockConnection('tcp://127.0.0.1:6381?alias=master');
+        $master = $this->getMockConnection('tcp://127.0.0.1:6381?role=master');
         $master
             ->expects($this->once())
             ->method('executeCommand')
@@ -1040,9 +1120,9 @@ repl_backlog_histlen:12978
     {
         $cmdInfo = $command = Command\RawCommand::create('INFO', 'REPLICATION');
 
-        $master = $this->getMockConnection('tcp://127.0.0.1:6381?alias=master');
-        $slave1 = $this->getMockConnection('tcp://127.0.0.1:6382?alias=slave1');
-        $slave2 = $this->getMockConnection('tcp://127.0.0.1:6383?alias=slave2');
+        $master = $this->getMockConnection('tcp://127.0.0.1:6381?role=master');
+        $slave1 = $this->getMockConnection('tcp://127.0.0.1:6382?role=slave');
+        $slave2 = $this->getMockConnection('tcp://127.0.0.1:6383?role=slave');
 
         $connFactory = $this->getMock('Predis\Connection\Factory');
         $connFactory
@@ -1051,7 +1131,7 @@ repl_backlog_histlen:12978
             ->with(array(
                 'host' => '127.0.0.1',
                 'port' => '6381',
-                'alias' => 'master',
+                'role' => 'master',
             ))
             ->will($this->returnValue($master));
         $connFactory
@@ -1060,6 +1140,7 @@ repl_backlog_histlen:12978
             ->with(array(
                 'host' => '127.0.0.1',
                 'port' => '6382',
+                'role' => 'slave',
             ))
             ->will($this->returnValue($slave1));
         $connFactory
@@ -1068,6 +1149,7 @@ repl_backlog_histlen:12978
             ->with(array(
                 'host' => '127.0.0.1',
                 'port' => '6383',
+                'role' => 'slave',
             ))
             ->will($this->returnValue($slave2));
 
@@ -1135,10 +1217,10 @@ repl_backlog_histlen:12978
     {
         $cmdInfo = $command = Command\RawCommand::create('INFO', 'REPLICATION');
 
-        $masterKO = $this->getMockConnection('tcp://127.0.0.1:7381?alias=master');
-        $master = $this->getMockConnection('tcp://127.0.0.1:6381?alias=master');
-        $slave1 = $this->getMockConnection('tcp://127.0.0.1:6382?alias=slave1');
-        $slave2 = $this->getMockConnection('tcp://127.0.0.1:6383?alias=slave2');
+        $masterKO = $this->getMockConnection('tcp://127.0.0.1:7381?role=master');
+        $master = $this->getMockConnection('tcp://127.0.0.1:6381?role=master');
+        $slave1 = $this->getMockConnection('tcp://127.0.0.1:6382?role=slave');
+        $slave2 = $this->getMockConnection('tcp://127.0.0.1:6383?role=slave');
 
         $connFactory = $this->getMock('Predis\Connection\Factory');
         $connFactory
@@ -1147,7 +1229,7 @@ repl_backlog_histlen:12978
             ->with(array(
                 'host' => '127.0.0.1',
                 'port' => '6381',
-                'alias' => 'master',
+                'role' => 'master',
             ))
             ->will($this->returnValue($master));
         $connFactory
@@ -1156,6 +1238,7 @@ repl_backlog_histlen:12978
             ->with(array(
                 'host' => '127.0.0.1',
                 'port' => '6382',
+                'role' => 'slave',
             ))
             ->will($this->returnValue($slave1));
         $connFactory
@@ -1164,6 +1247,7 @@ repl_backlog_histlen:12978
             ->with(array(
                 'host' => '127.0.0.1',
                 'port' => '6383',
+                'role' => 'slave',
             ))
             ->will($this->returnValue($slave2));
 
@@ -1240,7 +1324,7 @@ repl_backlog_histlen:12978
      */
     public function testAutomaticDiscoveryRequiresConnectionFactory()
     {
-        $master = $this->getMockConnection('tcp://host1?alias=master');
+        $master = $this->getMockConnection('tcp://127.0.0.1:6379?role=master');
 
         $replication = new MasterSlaveReplication();
 
@@ -1257,9 +1341,9 @@ repl_backlog_histlen:12978
         $cmdInfo = $command = Command\RawCommand::create('INFO', 'REPLICATION');
         $cmdExists = $command = Command\RawCommand::create('EXISTS', 'key');
 
-        $slaveKO = $this->getMockConnection('tcp://127.0.0.1:7382?alias=slaveKO');
-        $master = $this->getMockConnection('tcp://127.0.0.1:6381?alias=master');
-        $slave1 = $this->getMockConnection('tcp://127.0.0.1:6382?alias=slave1');
+        $slaveKO = $this->getMockConnection('tcp://127.0.0.1:7382?role=slave');
+        $master = $this->getMockConnection('tcp://127.0.0.1:6381?role=master');
+        $slave1 = $this->getMockConnection('tcp://127.0.0.1:6382?role=slave');
 
         $connFactory = $this->getMock('Predis\Connection\Factory');
         $connFactory
@@ -1268,6 +1352,7 @@ repl_backlog_histlen:12978
             ->with(array(
                 'host' => '127.0.0.1',
                 'port' => '6382',
+                'role' => 'slave',
             ))
             ->will($this->returnValue($slave1));
 
@@ -1331,8 +1416,8 @@ repl_backlog_histlen:12978
      */
     public function testCanBeSerialized()
     {
-        $master = $this->getMockConnection('tcp://host1?alias=master');
-        $slave1 = $this->getMockConnection('tcp://host2?alias=slave1');
+        $master = $this->getMockConnection('tcp://127.0.0.1:6379?role=master');
+        $slave1 = $this->getMockConnection('tcp://127.0.0.1:6380?role=slave');
 
         $replication = new MasterSlaveReplication();
 
@@ -1341,7 +1426,7 @@ repl_backlog_histlen:12978
 
         $unserialized = unserialize(serialize($replication));
 
-        $this->assertEquals($master, $unserialized->getConnectionById('master'));
-        $this->assertEquals($slave1, $unserialized->getConnectionById('slave1'));
+        $this->assertEquals($master, $unserialized->getConnectionByRole('master'));
+        $this->assertEquals($slave1, $unserialized->getConnectionByRole('slave'));
     }
 }

+ 216 - 128
tests/Predis/Connection/Replication/SentinelReplicationTest.php

@@ -39,7 +39,7 @@ class SentinelReplicationTest extends PredisTestCase
     public function testParametersForSentinelConnectionShouldNotUseDatabaseAndPassword()
     {
         $replication = $this->getReplicationConnection('svc', array(
-            'tcp://127.0.0.1:5381?alias=sentinel1&database=1&password=secret',
+            'tcp://127.0.0.1:5381?role=sentinel&database=1&password=secret',
         ));
 
         $parameters = $replication->getSentinelConnection()->getParameters()->toArray();
@@ -53,7 +53,7 @@ class SentinelReplicationTest extends PredisTestCase
     public function testParametersForSentinelConnectionHaveDefaultTimeout()
     {
         $replication = $this->getReplicationConnection('svc', array(
-            'tcp://127.0.0.1:5381?alias=sentinel',
+            'tcp://127.0.0.1:5381?role=sentinel',
         ));
 
         $parameters = $replication->getSentinelConnection()->getParameters()->toArray();
@@ -68,7 +68,7 @@ class SentinelReplicationTest extends PredisTestCase
     public function testParametersForSentinelConnectionCanOverrideDefaultTimeout()
     {
         $replication = $this->getReplicationConnection('svc', array(
-            'tcp://127.0.0.1:5381?alias=sentinel&timeout=1',
+            'tcp://127.0.0.1:5381?role=sentinel&timeout=1',
         ));
 
         $parameters = $replication
@@ -86,7 +86,7 @@ class SentinelReplicationTest extends PredisTestCase
     public function testConnectionParametersInstanceForSentinelConnectionIsNotModified()
     {
         $originalParameters = Connection\Parameters::create(
-            'tcp://127.0.0.1:5381?alias=sentinel1&database=1&password=secret'
+            'tcp://127.0.0.1:5381?role=sentinel&database=1&password=secret'
         );
 
         $replication = $this->getReplicationConnection('svc', array($originalParameters));
@@ -105,9 +105,9 @@ class SentinelReplicationTest extends PredisTestCase
      */
     public function testMethodGetSentinelConnectionReturnsFirstAvailableSentinel()
     {
-        $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?alias=sentinel1');
-        $sentinel2 = $this->getMockSentinelConnection('tcp://127.0.0.1:5382?alias=sentinel2');
-        $sentinel3 = $this->getMockSentinelConnection('tcp://127.0.0.1:5383?alias=sentinel3');
+        $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?role=sentinel&alias=sentinel1');
+        $sentinel2 = $this->getMockSentinelConnection('tcp://127.0.0.1:5382?role=sentinel&alias=sentinel2');
+        $sentinel3 = $this->getMockSentinelConnection('tcp://127.0.0.1:5383?role=sentinel&alias=sentinel3');
 
         $replication = $this->getReplicationConnection('svc', array($sentinel1, $sentinel2, $sentinel3));
 
@@ -119,11 +119,11 @@ class SentinelReplicationTest extends PredisTestCase
      */
     public function testMethodAddAttachesMasterOrSlaveNodesToReplication()
     {
-        $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?alias=sentinel1');
+        $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?role=sentinel');
 
-        $master = $this->getMockConnection('tcp://127.0.0.1:6381?alias=master');
-        $slave1 = $this->getMockConnection('tcp://127.0.0.1:6382?alias=slave1');
-        $slave2 = $this->getMockConnection('tcp://127.0.0.1:6383?alias=slave2');
+        $master = $this->getMockConnection('tcp://127.0.0.1:6381?role=master');
+        $slave1 = $this->getMockConnection('tcp://127.0.0.1:6382?role=slave');
+        $slave2 = $this->getMockConnection('tcp://127.0.0.1:6383?role=slave');
 
         $replication = $this->getReplicationConnection('svc', array($sentinel1));
 
@@ -131,9 +131,9 @@ class SentinelReplicationTest extends PredisTestCase
         $replication->add($slave1);
         $replication->add($slave2);
 
-        $this->assertSame($master, $replication->getConnectionById('master'));
-        $this->assertSame($slave1, $replication->getConnectionById('slave1'));
-        $this->assertSame($slave2, $replication->getConnectionById('slave2'));
+        $this->assertSame($master, $replication->getConnectionById('127.0.0.1:6381'));
+        $this->assertSame($slave1, $replication->getConnectionById('127.0.0.1:6382'));
+        $this->assertSame($slave2, $replication->getConnectionById('127.0.0.1:6383'));
 
         $this->assertSame($master, $replication->getMaster());
         $this->assertSame(array($slave1, $slave2), $replication->getSlaves());
@@ -141,14 +141,15 @@ class SentinelReplicationTest extends PredisTestCase
 
     /**
      * @group disconnected
+     * @FIXME
      */
     public function testMethodRemoveDismissesMasterOrSlaveNodesFromReplication()
     {
-        $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?alias=sentinel1');
+        $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?role=sentinel');
 
-        $master = $this->getMockConnection('tcp://127.0.0.1:6381?alias=master');
-        $slave1 = $this->getMockConnection('tcp://127.0.0.1:6382?alias=slave1');
-        $slave2 = $this->getMockConnection('tcp://127.0.0.1:6383?alias=slave2');
+        $master = $this->getMockConnection('tcp://127.0.0.1:6381?role=master');
+        $slave1 = $this->getMockConnection('tcp://127.0.0.1:6382?role=slave');
+        $slave2 = $this->getMockConnection('tcp://127.0.0.1:6383?role=slave');
 
         $replication = $this->getReplicationConnection('svc', array($sentinel1));
 
@@ -157,19 +158,105 @@ class SentinelReplicationTest extends PredisTestCase
         $replication->add($slave2);
 
         $this->assertTrue($replication->remove($slave1));
-        $this->assertFalse($replication->remove($sentinel1));
+        $this->assertTrue($replication->remove($sentinel1));
 
         $this->assertSame('127.0.0.1:6381', (string) $replication->getMaster());
         $this->assertCount(1, $slaves = $replication->getSlaves());
         $this->assertSame('127.0.0.1:6383', (string) $slaves[0]);
     }
 
+    /**
+     * @group disconnected
+     */
+    public function testMethodGetConnectionByIdOnEmptyReplication()
+    {
+        $replication = $this->getReplicationConnection('svc', array());
+
+        $this->assertNull($replication->getConnectionById('127.0.0.1:6381'));
+    }
+
+    /**
+     * @group disconnected
+     */
+    public function testMethodGetConnectionByRole()
+    {
+        $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?role=sentinel');
+        $master = $this->getMockConnection('tcp://127.0.0.1:6381?role=master');
+        $slave1 = $this->getMockConnection('tcp://127.0.0.1:6382?role=slave');
+
+        $replication = $this->getReplicationConnection('svc', array());
+
+        $replication->add($master);
+        $replication->add($slave1);
+        $replication->add($sentinel1);
+
+        $this->assertSame($sentinel1, $replication->getConnectionByRole('sentinel'));
+        $this->assertSame($master, $replication->getConnectionByRole('master'));
+        $this->assertSame($slave1, $replication->getConnectionByRole('slave'));
+    }
+
+    /**
+     * @group disconnected
+     */
+    public function testMethodGetConnectionByRoleOnEmptyReplicationForcesSentinelQueries()
+    {
+        $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?role=sentinel');
+        $sentinel1
+            ->expects($this->exactly(2))
+            ->method('executeCommand')
+            ->withConsecutive(
+                $this->isRedisCommand('SENTINEL', array('get-master-addr-by-name', 'svc')),
+                $this->isRedisCommand('SENTINEL', array('slaves', 'svc'))
+            )
+            ->will($this->onConsecutiveCalls(
+                // SENTINEL get-master-addr-by-name svc
+                array('127.0.0.1', '6381'),
+
+                // SENTINEL slaves svc
+                array(
+                    array(
+                        'name', '127.0.0.1:6382',
+                        'ip', '127.0.0.1',
+                        'port', '6382',
+                        'runid', '112cdebd22924a7d962be496f3a1c4c7c9bad93f',
+                        'flags', 'slave',
+                        'master-host', '127.0.0.1',
+                        'master-port', '6381',
+                    ),
+                )
+            ));
+
+        $replication = $this->getReplicationConnection('svc', array($sentinel1));
+
+        $this->assertSame($sentinel1, $replication->getConnectionByRole('sentinel'));
+        $this->assertInstanceOf('Predis\Connection\NodeConnectionInterface', $replication->getConnectionByRole('master'));
+        $this->assertInstanceOf('Predis\Connection\NodeConnectionInterface', $replication->getConnectionByRole('slave'));
+    }
+
+    /**
+     * @group disconnected
+     */
+    public function testMethodGetConnectionByRoleUnknown()
+    {
+        $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?role=sentinel');
+        $master = $this->getMockConnection('tcp://127.0.0.1:6381?role=master');
+        $slave1 = $this->getMockConnection('tcp://127.0.0.1:6382?role=slave');
+
+        $replication = $this->getReplicationConnection('svc', array());
+
+        $replication->add($master);
+        $replication->add($slave1);
+        $replication->add($sentinel1);
+
+        $this->assertNull($replication->getConnectionByRole('unknown'));
+    }
+
     /**
      * @group disconnected
      */
     public function testMethodUpdateSentinelsFetchesSentinelNodes()
     {
-        $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?alias=sentinel1');
+        $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?role=sentinel');
         $sentinel1
             ->expects($this->once())
             ->method('executeCommand')
@@ -217,7 +304,7 @@ class SentinelReplicationTest extends PredisTestCase
      */
     public function testMethodUpdateSentinelsRemovesCurrentSentinelAndRetriesNextOneOnFailure()
     {
-        $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?alias=sentinel1');
+        $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?role=sentinel&alias=sentinel1');
         $sentinel1
             ->expects($this->once())
             ->method('executeCommand')
@@ -228,7 +315,7 @@ class SentinelReplicationTest extends PredisTestCase
                 new Connection\ConnectionException($sentinel1, 'Unknown connection error [127.0.0.1:5381]')
             ));
 
-        $sentinel2 = $this->getMockSentinelConnection('tcp://127.0.0.1:5382?alias=sentinel2');
+        $sentinel2 = $this->getMockSentinelConnection('tcp://127.0.0.1:5382?role=sentinel&alias=sentinel2');
         $sentinel2
             ->expects($this->once())
             ->method('executeCommand')
@@ -270,7 +357,7 @@ class SentinelReplicationTest extends PredisTestCase
      */
     public function testMethodUpdateSentinelsThrowsExceptionOnNoAvailableSentinel()
     {
-        $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?alias=sentinel1');
+        $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?role=sentinel');
         $sentinel1
             ->expects($this->once())
             ->method('executeCommand')
@@ -290,7 +377,7 @@ class SentinelReplicationTest extends PredisTestCase
      */
     public function testMethodQuerySentinelFetchesMasterNodeSlaveNodesAndSentinelNodes()
     {
-        $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?alias=sentinel1');
+        $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?role=sentinel&alias=sentinel1');
         $sentinel1
             ->expects($this->exactly(3))
             ->method('executeCommand')
@@ -337,11 +424,11 @@ class SentinelReplicationTest extends PredisTestCase
                 )
             ));
 
-        $sentinel2 = $this->getMockSentinelConnection('tcp://127.0.0.1:5382?alias=sentinel2');
+        $sentinel2 = $this->getMockSentinelConnection('tcp://127.0.0.1:5382?role=sentinel&alias=sentinel2');
 
-        $master = $this->getMockConnection('tcp://127.0.0.1:6381?alias=master');
-        $slave1 = $this->getMockConnection('tcp://127.0.0.1:6382?alias=slave1');
-        $slave2 = $this->getMockConnection('tcp://127.0.0.1:6383?alias=slave2');
+        $master = $this->getMockConnection('tcp://127.0.0.1:6381?role=master');
+        $slave1 = $this->getMockConnection('tcp://127.0.0.1:6382?role=slave');
+        $slave2 = $this->getMockConnection('tcp://127.0.0.1:6383?role=slave');
 
         $replication = $this->getReplicationConnection('svc', array($sentinel1));
         $replication->querySentinel();
@@ -373,7 +460,7 @@ class SentinelReplicationTest extends PredisTestCase
      */
     public function testMethodGetMasterAsksSentinelForMasterOnMasterNotSet()
     {
-        $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?alias=sentinel1');
+        $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?role=sentinel');
         $sentinel1
             ->expects($this->at(0))
             ->method('executeCommand')
@@ -396,7 +483,7 @@ class SentinelReplicationTest extends PredisTestCase
      */
     public function testMethodGetMasterThrowsExceptionOnNoAvailableSentinels()
     {
-        $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?alias=sentinel1');
+        $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?role=sentinel');
         $sentinel1
             ->expects($this->any())
             ->method('executeCommand')
@@ -417,7 +504,7 @@ class SentinelReplicationTest extends PredisTestCase
      */
     public function testMethodGetSlavesOnEmptySlavePoolAsksSentinelForSlaves()
     {
-        $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?alias=sentinel1');
+        $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?role=sentinel');
         $sentinel1
             ->expects($this->at(0))
             ->method('executeCommand')
@@ -462,7 +549,7 @@ class SentinelReplicationTest extends PredisTestCase
      */
     public function testMethodGetSlavesThrowsExceptionOnNoAvailableSentinels()
     {
-        $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?alias=sentinel1');
+        $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?role=sentinel');
         $sentinel1
             ->expects($this->any())
             ->method('executeCommand')
@@ -494,14 +581,14 @@ class SentinelReplicationTest extends PredisTestCase
      */
     public function testMethodConnectForcesConnectionToSlave()
     {
-        $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?alias=sentinel1');
+        $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?role=sentinel');
 
-        $master = $this->getMockConnection('tcp://127.0.0.1:6381?alias=master');
+        $master = $this->getMockConnection('tcp://127.0.0.1:6381?role=master');
         $master
             ->expects($this->never())
             ->method('connect');
 
-        $slave1 = $this->getMockConnection('tcp://127.0.0.1:6382?alias=slave1');
+        $slave1 = $this->getMockConnection('tcp://127.0.0.1:6382?role=slave');
         $slave1
             ->expects($this->once())
             ->method('connect');
@@ -519,7 +606,7 @@ class SentinelReplicationTest extends PredisTestCase
      */
     public function testMethodConnectOnEmptySlavePoolAsksSentinelForSlavesAndForcesConnectionToSlave()
     {
-        $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?alias=sentinel1');
+        $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?role=sentinel');
         $sentinel1
             ->expects($this->any())
             ->method('executeCommand')
@@ -540,12 +627,12 @@ class SentinelReplicationTest extends PredisTestCase
                 )
             ));
 
-        $master = $this->getMockConnection('tcp://127.0.0.1:6381?alias=master');
+        $master = $this->getMockConnection('tcp://127.0.0.1:6381?role=master');
         $master
             ->expects($this->never())
             ->method('connect');
 
-        $slave1 = $this->getMockConnection('tcp://127.0.0.1:6382?alias=slave1');
+        $slave1 = $this->getMockConnection('tcp://127.0.0.1:6382?role=slave');
         $slave1
             ->expects($this->once())
             ->method('connect');
@@ -557,7 +644,7 @@ class SentinelReplicationTest extends PredisTestCase
             ->with(array(
                 'host' => '127.0.0.1',
                 'port' => '6382',
-                'alias' => 'slave-127.0.0.1:6382',
+                'role' => 'slave',
             ))
            ->will($this->returnValue($slave1));
 
@@ -573,7 +660,7 @@ class SentinelReplicationTest extends PredisTestCase
      */
     public function testMethodConnectOnEmptySlavePoolAsksSentinelForSlavesAndForcesConnectionToMasterIfStillEmpty()
     {
-        $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?alias=sentinel1');
+        $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?role=sentinel');
         $sentinel1
             ->expects($this->at(0))
             ->method('executeCommand')
@@ -593,7 +680,7 @@ class SentinelReplicationTest extends PredisTestCase
                 array('127.0.0.1', '6381')
             ));
 
-        $master = $this->getMockConnection('tcp://127.0.0.1:6381?alias=master');
+        $master = $this->getMockConnection('tcp://127.0.0.1:6381?role=master');
         $master
             ->expects($this->once())
             ->method('connect');
@@ -605,7 +692,7 @@ class SentinelReplicationTest extends PredisTestCase
             ->with(array(
                 'host' => '127.0.0.1',
                 'port' => '6381',
-                'alias' => 'master',
+                'role' => 'master',
             ))
             ->will($this->returnValue($master));
 
@@ -619,22 +706,22 @@ class SentinelReplicationTest extends PredisTestCase
      */
     public function testMethodDisconnectForcesDisconnectionOnAllConnectionsInPool()
     {
-        $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?alias=sentinel1');
+        $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?role=sentinel');
         $sentinel1
             ->expects($this->never())
             ->method('disconnect');
 
-        $master = $this->getMockConnection('tcp://127.0.0.1:6381?alias=master');
+        $master = $this->getMockConnection('tcp://127.0.0.1:6381?role=master');
         $master
             ->expects($this->once())
             ->method('disconnect');
 
-        $slave1 = $this->getMockConnection('tcp://127.0.0.1:6382?alias=slave1');
+        $slave1 = $this->getMockConnection('tcp://127.0.0.1:6382?role=slave');
         $slave1
             ->expects($this->once())
             ->method('disconnect');
 
-        $slave2 = $this->getMockConnection('tcp://127.0.0.1:6383?alias=slave2');
+        $slave2 = $this->getMockConnection('tcp://127.0.0.1:6383?role=slave');
         $slave2
             ->expects($this->once())
             ->method('disconnect');
@@ -653,9 +740,9 @@ class SentinelReplicationTest extends PredisTestCase
      */
     public function testMethodIsConnectedReturnConnectionStatusOfCurrentConnection()
     {
-        $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?alias=sentinel1');
+        $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?role=sentinel');
 
-        $slave1 = $this->getMockConnection('tcp://127.0.0.1:6382?alias=slave1');
+        $slave1 = $this->getMockConnection('tcp://127.0.0.1:6382?role=slave');
         $slave1
             ->expects($this->exactly(2))
             ->method('isConnected')
@@ -668,7 +755,7 @@ class SentinelReplicationTest extends PredisTestCase
         $this->assertFalse($replication->isConnected());
         $replication->connect();
         $this->assertTrue($replication->isConnected());
-        $replication->getConnectionById('slave1')->disconnect();
+        $replication->getConnectionById('127.0.0.1:6382')->disconnect();
         $this->assertFalse($replication->isConnected());
     }
 
@@ -677,39 +764,39 @@ class SentinelReplicationTest extends PredisTestCase
      */
     public function testMethodGetConnectionByIdReturnsConnectionWhenFound()
     {
-        $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?alias=sentinel1');
+        $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?role=sentinel');
 
-        $master = $this->getMockConnection('tcp://127.0.0.1:6381?alias=master');
-        $slave1 = $this->getMockConnection('tcp://127.0.0.1:6382?alias=slave1');
+        $master = $this->getMockConnection('tcp://127.0.0.1:6381?role=master');
+        $slave1 = $this->getMockConnection('tcp://127.0.0.1:6382?role=slave');
 
         $replication = $this->getReplicationConnection('svc', array($sentinel1));
 
         $replication->add($master);
         $replication->add($slave1);
 
-        $this->assertSame($master, $replication->getConnectionById('master'));
-        $this->assertSame($slave1, $replication->getConnectionById('slave1'));
-        $this->assertNull($replication->getConnectionById('unknown'));
+        $this->assertSame($master, $replication->getConnectionById('127.0.0.1:6381'));
+        $this->assertSame($slave1, $replication->getConnectionById('127.0.0.1:6382'));
+        $this->assertNull($replication->getConnectionById('127.0.0.1:6383'));
     }
 
     /**
      * @group disconnected
      */
-    public function testMethodSwitchToSelectsCurrentConnectionByConnectionAlias()
+    public function testMethodSwitchToSelectsCurrentConnection()
     {
-        $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?alias=sentinel1');
+        $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?role=sentinel');
 
-        $master = $this->getMockConnection('tcp://127.0.0.1:6381?alias=master');
+        $master = $this->getMockConnection('tcp://127.0.0.1:6381?role=master');
         $master
             ->expects($this->once())
             ->method('connect');
 
-        $slave1 = $this->getMockConnection('tcp://127.0.0.1:6382?alias=slave1');
+        $slave1 = $this->getMockConnection('tcp://127.0.0.1:6382?role=slave');
         $slave1
             ->expects($this->never())
             ->method('connect');
 
-        $slave2 = $this->getMockConnection('tcp://127.0.0.1:6382?alias=slave2');
+        $slave2 = $this->getMockConnection('tcp://127.0.0.1:6382?role=slave');
         $slave2
             ->expects($this->once())
             ->method('connect');
@@ -720,10 +807,10 @@ class SentinelReplicationTest extends PredisTestCase
         $replication->add($slave1);
         $replication->add($slave2);
 
-        $replication->switchTo('master');
+        $replication->switchTo($master);
         $this->assertSame($master, $replication->getCurrent());
 
-        $replication->switchTo('slave2');
+        $replication->switchTo($slave2);
         $this->assertSame($slave2, $replication->getCurrent());
     }
 
@@ -734,17 +821,18 @@ class SentinelReplicationTest extends PredisTestCase
      */
     public function testMethodSwitchToThrowsExceptionOnConnectionNotFound()
     {
-        $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?alias=sentinel1');
+        $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?role=sentinel');
 
-        $master = $this->getMockConnection('tcp://127.0.0.1:6381?alias=master');
-        $slave1 = $this->getMockConnection('tcp://127.0.0.1:6382?alias=slave1');
+        $master = $this->getMockConnection('tcp://127.0.0.1:6381?role=master');
+        $slave1 = $this->getMockConnection('tcp://127.0.0.1:6382?role=slave');
+        $slave2 = $this->getMockConnection('tcp://127.0.0.1:6383?role=slave');
 
         $replication = $this->getReplicationConnection('svc', array($sentinel1));
 
         $replication->add($master);
         $replication->add($slave1);
 
-        $replication->switchTo('unknown');
+        $replication->switchTo($slave2);
     }
 
     /**
@@ -752,14 +840,14 @@ class SentinelReplicationTest extends PredisTestCase
      */
     public function testMethodSwitchToMasterSelectsCurrentConnectionToMaster()
     {
-        $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?alias=sentinel1');
+        $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?role=sentinel');
 
-        $master = $this->getMockConnection('tcp://127.0.0.1:6381?alias=master');
+        $master = $this->getMockConnection('tcp://127.0.0.1:6381?role=master');
         $master
             ->expects($this->once())
             ->method('connect');
 
-        $slave1 = $this->getMockConnection('tcp://127.0.0.1:6382?alias=slave1');
+        $slave1 = $this->getMockConnection('tcp://127.0.0.1:6382?role=slave');
         $slave1
             ->expects($this->never())
             ->method('connect');
@@ -779,14 +867,14 @@ class SentinelReplicationTest extends PredisTestCase
      */
     public function testMethodSwitchToSlaveSelectsCurrentConnectionToRandomSlave()
     {
-        $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?alias=sentinel1');
+        $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?role=sentinel');
 
-        $master = $this->getMockConnection('tcp://127.0.0.1:6381?alias=master');
+        $master = $this->getMockConnection('tcp://127.0.0.1:6381?role=master');
         $master
             ->expects($this->never())
             ->method('connect');
 
-        $slave1 = $this->getMockConnection('tcp://127.0.0.1:6382?alias=slave1');
+        $slave1 = $this->getMockConnection('tcp://127.0.0.1:6382?role=slave');
         $slave1
             ->expects($this->once())
             ->method('connect');
@@ -804,24 +892,24 @@ class SentinelReplicationTest extends PredisTestCase
     /**
      * @group disconnected
      */
-    public function testGetConnectionReturnsMasterForWriteCommands()
+    public function testGetConnectionByCommandReturnsMasterForWriteCommands()
     {
-        $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?alias=sentinel1');
+        $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?role=sentinel');
 
-        $master = $this->getMockConnection('tcp://127.0.0.1:6381?alias=master');
+        $master = $this->getMockConnection('tcp://127.0.0.1:6381?role=master');
         $master
             ->expects($this->exactly(2))
             ->method('isConnected')
             ->will($this->onConsecutiveCalls(false, true));
         $master
-            ->expects($this->at(2))
+            ->expects($this->at(3))
             ->method('executeCommand')
             ->with($this->isRedisCommand('ROLE'))
             ->will($this->returnValue(array(
                 'master', 3129659, array(array('127.0.0.1', 6382, 3129242)),
             )));
 
-        $slave1 = $this->getMockConnection('tcp://127.0.0.1:6382?alias=slave1');
+        $slave1 = $this->getMockConnection('tcp://127.0.0.1:6382?role=slave');
 
         $replication = $this->getReplicationConnection('svc', array($sentinel1));
 
@@ -840,19 +928,19 @@ class SentinelReplicationTest extends PredisTestCase
     /**
      * @group disconnected
      */
-    public function testGetConnectionReturnsSlaveForReadOnlyCommands()
+    public function testGetConnectionByCommandReturnsSlaveForReadOnlyCommands()
     {
-        $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?alias=sentinel1');
+        $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?role=sentinel');
 
-        $master = $this->getMockConnection('tcp://127.0.0.1:6381?alias=master');
+        $master = $this->getMockConnection('tcp://127.0.0.1:6381?role=master');
 
-        $slave1 = $this->getMockConnection('tcp://127.0.0.1:6382?alias=slave1');
+        $slave1 = $this->getMockConnection('tcp://127.0.0.1:6382?role=slave');
         $slave1
             ->expects($this->exactly(2))
             ->method('isConnected')
             ->will($this->onConsecutiveCalls(false, true));
         $slave1
-            ->expects($this->at(2))
+            ->expects($this->at(3))
             ->method('executeCommand')
             ->with($this->isRedisCommand('ROLE'))
             ->will($this->returnValue(array(
@@ -876,30 +964,30 @@ class SentinelReplicationTest extends PredisTestCase
     /**
      * @group disconnected
      */
-    public function testGetConnectionSwitchesToMasterAfterWriteCommand()
+    public function testGetConnectionByCommandSwitchesToMasterAfterWriteCommand()
     {
-        $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?alias=sentinel1');
+        $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?role=sentinel');
 
-        $master = $this->getMockConnection('tcp://127.0.0.1:6381?alias=master');
+        $master = $this->getMockConnection('tcp://127.0.0.1:6381?role=master');
         $master
             ->expects($this->exactly(2))
             ->method('isConnected')
             ->will($this->onConsecutiveCalls(false, true));
         $master
-            ->expects($this->at(2))
+            ->expects($this->at(3))
             ->method('executeCommand')
             ->with($this->isRedisCommand('ROLE'))
             ->will($this->returnValue(array(
                 'master', 3129659, array(array('127.0.0.1', 6382, 3129242)),
             )));
 
-        $slave1 = $this->getMockConnection('tcp://127.0.0.1:6382?alias=slave1');
+        $slave1 = $this->getMockConnection('tcp://127.0.0.1:6382?role=slave');
         $slave1
             ->expects($this->exactly(1))
             ->method('isConnected')
             ->will($this->onConsecutiveCalls(false));
         $slave1
-            ->expects($this->at(2))
+            ->expects($this->at(3))
             ->method('executeCommand')
             ->with($this->isRedisCommand('ROLE'))
             ->will($this->returnValue(array(
@@ -929,17 +1017,17 @@ class SentinelReplicationTest extends PredisTestCase
      * @expectedException Predis\Replication\RoleException
      * @expectedExceptionMessage Expected master but got slave [127.0.0.1:6381]
      */
-    public function testGetConnectionThrowsExceptionOnNodeRoleMismatch()
+    public function testGetConnectionByCommandThrowsExceptionOnNodeRoleMismatch()
     {
-        $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?alias=sentinel1');
+        $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?role=sentinel');
 
-        $master = $this->getMockConnection('tcp://127.0.0.1:6381?alias=master');
+        $master = $this->getMockConnection('tcp://127.0.0.1:6381?role=master');
         $master
             ->expects($this->once())
             ->method('isConnected')
             ->will($this->returnValue(false));
         $master
-            ->expects($this->at(2))
+            ->expects($this->at(3))
             ->method('executeCommand')
             ->with($this->isRedisCommand('ROLE'))
             ->will($this->returnValue(array(
@@ -956,9 +1044,9 @@ class SentinelReplicationTest extends PredisTestCase
     /**
      * @group disconnected
      */
-    public function testGetConnectionReturnsMasterForReadOnlyOperationsOnUnavailableSlaves()
+    public function testGetConnectionByCommandReturnsMasterForReadOnlyOperationsOnUnavailableSlaves()
     {
-        $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?alias=sentinel1');
+        $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?role=sentinel');
         $sentinel1
             ->expects($this->once())
             ->method('executeCommand')
@@ -979,13 +1067,13 @@ class SentinelReplicationTest extends PredisTestCase
                 )
             ));
 
-        $master = $this->getMockConnection('tcp://127.0.0.1:6381?alias=master');
+        $master = $this->getMockConnection('tcp://127.0.0.1:6381?role=master');
         $master
             ->expects($this->once())
             ->method('isConnected')
             ->will($this->returnValue(false));
         $master
-            ->expects($this->at(2))
+            ->expects($this->at(3))
             ->method('executeCommand')
             ->with($this->isRedisCommand('ROLE'))
             ->will($this->returnValue(array(
@@ -1004,7 +1092,7 @@ class SentinelReplicationTest extends PredisTestCase
      */
     public function testMethodExecuteCommandSendsCommandToNodeAndReturnsResponse()
     {
-        $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?alias=sentinel1');
+        $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?role=sentinel');
 
         $cmdGet = Command\RawCommand::create('get', 'key');
         $cmdGetResponse = 'value';
@@ -1012,26 +1100,26 @@ class SentinelReplicationTest extends PredisTestCase
         $cmdSet = Command\RawCommand::create('set', 'key', 'value');
         $cmdSetResponse = Response\Status::get('OK');
 
-        $master = $this->getMockConnection('tcp://127.0.0.1:6381?alias=master');
+        $master = $this->getMockConnection('tcp://127.0.0.1:6381?role=master');
         $master
             ->expects($this->any())
             ->method('isConnected')
             ->will($this->returnValue(true));
         $master
-            ->expects($this->at(2))
+            ->expects($this->at(3))
             ->method('executeCommand')
             ->with($this->isRedisCommand(
                 'SET', array('key', $cmdGetResponse)
             ))
            ->will($this->returnValue($cmdSetResponse));
 
-        $slave1 = $this->getMockConnection('tcp://127.0.0.1:6382?alias=slave1');
+        $slave1 = $this->getMockConnection('tcp://127.0.0.1:6382?role=slave');
         $slave1
             ->expects($this->any())
             ->method('isConnected')
             ->will($this->returnValue(true));
         $slave1
-            ->expects($this->at(2))
+            ->expects($this->at(3))
             ->method('executeCommand')
             ->with($this->isRedisCommand(
                 'GET', array('key')
@@ -1052,7 +1140,7 @@ class SentinelReplicationTest extends PredisTestCase
      */
     public function testMethodExecuteCommandRetriesReadOnlyCommandOnNextSlaveOnFailure()
     {
-        $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?alias=sentinel1');
+        $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?role=sentinel');
         $sentinel1
             ->expects($this->any())
             ->method('executeCommand')
@@ -1073,19 +1161,19 @@ class SentinelReplicationTest extends PredisTestCase
                 )
             ));
 
-        $master = $this->getMockConnection('tcp://127.0.0.1:6381?alias=master');
+        $master = $this->getMockConnection('tcp://127.0.0.1:6381?role=master');
         $master
             ->expects($this->any())
             ->method('isConnected')
             ->will($this->returnValue(true));
 
-        $slave1 = $this->getMockConnection('tcp://127.0.0.1:6382?alias=slave1');
+        $slave1 = $this->getMockConnection('tcp://127.0.0.1:6382?role=slave');
         $slave1
             ->expects($this->any())
             ->method('isConnected')
             ->will($this->returnValue(true));
         $slave1
-            ->expects($this->at(2))
+            ->expects($this->at(3))
             ->method('executeCommand')
             ->with($this->isRedisCommand(
                 'GET', array('key')
@@ -1094,13 +1182,13 @@ class SentinelReplicationTest extends PredisTestCase
                 new Connection\ConnectionException($slave1, 'Unknown connection error [127.0.0.1:6382]')
             ));
 
-        $slave2 = $this->getMockConnection('tcp://127.0.0.1:6383?alias=slave2');
+        $slave2 = $this->getMockConnection('tcp://127.0.0.1:6383?role=slave');
         $slave2
             ->expects($this->any())
             ->method('isConnected')
             ->will($this->returnValue(true));
         $slave2
-            ->expects($this->at(2))
+            ->expects($this->at(3))
             ->method('executeCommand')
             ->with($this->isRedisCommand(
                 'GET', array('key')
@@ -1114,7 +1202,7 @@ class SentinelReplicationTest extends PredisTestCase
             ->with(array(
                 'host' => '127.0.0.1',
                 'port' => '6383',
-                'alias' => 'slave-127.0.0.1:6383',
+                'role' => 'slave',
             ))
             ->will($this->returnValue($slave2));
 
@@ -1133,7 +1221,7 @@ class SentinelReplicationTest extends PredisTestCase
      */
     public function testMethodExecuteCommandRetriesWriteCommandOnNewMasterOnFailure()
     {
-        $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?alias=sentinel1');
+        $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?role=sentinel');
         $sentinel1
             ->expects($this->any())
             ->method('executeCommand')
@@ -1144,13 +1232,13 @@ class SentinelReplicationTest extends PredisTestCase
                 array('127.0.0.1', '6391')
             ));
 
-        $masterOld = $this->getMockConnection('tcp://127.0.0.1:6381?alias=master');
+        $masterOld = $this->getMockConnection('tcp://127.0.0.1:6381?role=master');
         $masterOld
             ->expects($this->any())
             ->method('isConnected')
             ->will($this->returnValue(true));
         $masterOld
-            ->expects($this->at(2))
+            ->expects($this->at(3))
             ->method('executeCommand')
             ->with($this->isRedisCommand(
                 'DEL', array('key')
@@ -1159,13 +1247,13 @@ class SentinelReplicationTest extends PredisTestCase
                 new Connection\ConnectionException($masterOld, 'Unknown connection error [127.0.0.1:6381]')
             ));
 
-        $masterNew = $this->getMockConnection('tcp://127.0.0.1:6391?alias=master');
+        $masterNew = $this->getMockConnection('tcp://127.0.0.1:6391?role=master');
         $masterNew
             ->expects($this->any())
             ->method('isConnected')
             ->will($this->returnValue(true));
         $masterNew
-            ->expects($this->at(2))
+            ->expects($this->at(3))
             ->method('executeCommand')
             ->with($this->isRedisCommand(
                 'DEL', array('key')
@@ -1179,7 +1267,7 @@ class SentinelReplicationTest extends PredisTestCase
             ->with(array(
                 'host' => '127.0.0.1',
                 'port' => '6391',
-                'alias' => 'master',
+                'role' => 'master',
             ))
             ->will($this->returnValue($masterNew));
 
@@ -1199,7 +1287,7 @@ class SentinelReplicationTest extends PredisTestCase
      */
     public function testMethodExecuteCommandThrowsExceptionOnUnknownServiceName()
     {
-        $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?alias=sentinel1');
+        $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?role=sentinel');
         $sentinel1
             ->expects($this->any())
             ->method('executeCommand')
@@ -1208,13 +1296,13 @@ class SentinelReplicationTest extends PredisTestCase
             ))
             ->will($this->returnValue(null));
 
-        $masterOld = $this->getMockConnection('tcp://127.0.0.1:6381?alias=master');
+        $masterOld = $this->getMockConnection('tcp://127.0.0.1:6381?role=master');
         $masterOld
             ->expects($this->any())
             ->method('isConnected')
             ->will($this->returnValue(true));
         $masterOld
-            ->expects($this->at(2))
+            ->expects($this->at(3))
             ->method('executeCommand')
             ->with($this->isRedisCommand(
                 'DEL', array('key')
@@ -1239,7 +1327,7 @@ class SentinelReplicationTest extends PredisTestCase
      */
     public function testMethodExecuteCommandThrowsExceptionOnConnectionFailureAndNoAvailableSentinels()
     {
-        $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?alias=sentinel1');
+        $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?role=sentinel');
         $sentinel1
             ->expects($this->any())
             ->method('executeCommand')
@@ -1250,13 +1338,13 @@ class SentinelReplicationTest extends PredisTestCase
                 new Connection\ConnectionException($sentinel1, 'Unknown connection error [127.0.0.1:5381]')
             ));
 
-        $master = $this->getMockConnection('tcp://127.0.0.1:6381?alias=master');
+        $master = $this->getMockConnection('tcp://127.0.0.1:6381?role=master');
         $master
             ->expects($this->any())
             ->method('isConnected')
             ->will($this->returnValue(true));
         $master
-            ->expects($this->at(2))
+            ->expects($this->at(3))
             ->method('executeCommand')
             ->with($this->isRedisCommand(
                 'DEL', array('key')
@@ -1283,7 +1371,7 @@ class SentinelReplicationTest extends PredisTestCase
         $factory = new Connection\Factory();
 
         $replication = new SentinelReplication(
-            'svc', array('tcp://127.0.0.1:5381?alias=sentinel1'), $factory, $strategy
+            'svc', array('tcp://127.0.0.1:5381?role=sentinel'), $factory, $strategy
         );
 
         $this->assertSame($strategy, $replication->getReplicationStrategy());
@@ -1294,11 +1382,11 @@ class SentinelReplicationTest extends PredisTestCase
      */
     public function testMethodSerializeCanSerializeWholeObject()
     {
-        $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?alias=sentinel1');
+        $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?role=sentinel');
 
-        $master = $this->getMockConnection('tcp://127.0.0.1:6381?alias=master');
-        $slave1 = $this->getMockConnection('tcp://127.0.0.1:6382?alias=slave1');
-        $slave2 = $this->getMockConnection('tcp://127.0.0.1:6383?alias=slave2');
+        $master = $this->getMockConnection('tcp://127.0.0.1:6381?role=master');
+        $slave1 = $this->getMockConnection('tcp://127.0.0.1:6382?role=slave');
+        $slave2 = $this->getMockConnection('tcp://127.0.0.1:6383?role=slave');
 
         $strategy = new Replication\ReplicationStrategy();
         $factory = new Connection\Factory();
@@ -1311,9 +1399,9 @@ class SentinelReplicationTest extends PredisTestCase
 
         $unserialized = unserialize(serialize($replication));
 
-        $this->assertEquals($master, $unserialized->getConnectionById('master'));
-        $this->assertEquals($slave1, $unserialized->getConnectionById('slave1'));
-        $this->assertEquals($master, $unserialized->getConnectionById('slave2'));
+        $this->assertEquals($master, $unserialized->getConnectionById('127.0.0.1:6381'));
+        $this->assertEquals($slave1, $unserialized->getConnectionById('127.0.0.1:6382'));
+        $this->assertEquals($master, $unserialized->getConnectionById('127.0.0.1:6383'));
         $this->assertEquals($strategy, $unserialized->getReplicationStrategy());
     }