123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425 |
- <?php
- namespace Predis\Network;
- use Predis\Commands\ICommand;
- use Predis\NotSupportedException;
- class MasterSlaveReplication implements IConnectionReplication
- {
- private $disallowed = array();
- private $readonly = array();
- private $readonlySHA1 = array();
- private $current = null;
- private $master = null;
- private $slaves = array();
-
- public function __construct()
- {
- $this->disallowed = $this->getDisallowedOperations();
- $this->readonly = $this->getReadOnlyOperations();
- }
-
- protected function check()
- {
- if (!isset($this->master) || !$this->slaves) {
- throw new \RuntimeException('Replication needs a master and at least one slave.');
- }
- }
-
- protected function reset()
- {
- $this->current = null;
- }
-
- public function add(IConnectionSingle $connection)
- {
- $alias = $connection->getParameters()->alias;
- if ($alias === 'master') {
- $this->master = $connection;
- }
- else {
- $this->slaves[$alias ?: count($this->slaves)] = $connection;
- }
- $this->reset();
- }
-
- public function remove(IConnectionSingle $connection)
- {
- if ($connection->getParameters()->alias === 'master') {
- $this->master = null;
- $this->reset();
- return true;
- }
- else {
- if (($id = array_search($connection, $this->slaves, true)) !== false) {
- unset($this->slaves[$id]);
- $this->reset();
- return true;
- }
- }
- return false;
- }
-
- public function getConnection(ICommand $command)
- {
- if ($this->current === null) {
- $this->check();
- $this->current = $this->isReadOperation($command) ? $this->pickSlave() : $this->master;
- return $this->current;
- }
- if ($this->current === $this->master) {
- return $this->current;
- }
- if (!$this->isReadOperation($command)) {
- $this->current = $this->master;
- }
- return $this->current;
- }
-
- public function getConnectionById($connectionId)
- {
- if ($connectionId === 'master') {
- return $this->master;
- }
- if (isset($this->slaves[$connectionId])) {
- return $this->slaves[$connectionId];
- }
- return null;
- }
-
- public function switchTo($connection)
- {
- $this->check();
- if (!$connection instanceof IConnectionSingle) {
- $connection = $this->getConnectionById($connection);
- }
- if ($connection !== $this->master && !in_array($connection, $this->slaves, true)) {
- throw new \InvalidArgumentException('The specified connection is not valid.');
- }
- $this->current = $connection;
- }
-
- public function getCurrent()
- {
- return $this->current;
- }
-
- public function getMaster()
- {
- return $this->master;
- }
-
- public function getSlaves()
- {
- return array_values($this->slaves);
- }
-
- protected function pickSlave()
- {
- return $this->slaves[array_rand($this->slaves)];
- }
-
- public function isConnected()
- {
- return $this->current ? $this->current->isConnected() : false;
- }
-
- public function connect()
- {
- if ($this->current === null) {
- $this->check();
- $this->current = $this->pickSlave();
- }
- $this->current->connect();
- }
-
- public function disconnect()
- {
- if ($this->master) {
- $this->master->disconnect();
- }
- foreach ($this->slaves as $connection) {
- $connection->disconnect();
- }
- }
-
- public function writeCommand(ICommand $command)
- {
- $this->getConnection($command)->writeCommand($command);
- }
-
- public function readResponse(ICommand $command)
- {
- return $this->getConnection($command)->readResponse($command);
- }
-
- public function executeCommand(ICommand $command)
- {
- return $this->getConnection($command)->executeCommand($command);
- }
-
- protected function isReadOperation(ICommand $command)
- {
- if (isset($this->disallowed[$id = $command->getId()])) {
- throw new NotSupportedException("The command $id is not allowed in replication mode");
- }
- if (isset($this->readonly[$id])) {
- if (true === $readonly = $this->readonly[$id]) {
- return true;
- }
- return call_user_func($readonly, $command);
- }
- if (($eval = $id === 'EVAL') || $id === 'EVALSHA') {
- $sha1 = $eval ? sha1($command->getArgument(0)) : $command->getArgument(0);
- if (isset($this->readonlySHA1[$sha1])) {
- if (true === $readonly = $this->readonlySHA1[$sha1]) {
- return true;
- }
- return call_user_func($readonly, $command);
- }
- }
- return false;
- }
-
- private function isSortReadOnly(ICommand $command)
- {
- $arguments = $command->getArguments();
- return ($c = count($arguments)) === 1 ? true : $arguments[$c - 2] !== 'STORE';
- }
-
- public function setCommandReadOnly($commandID, $readonly = true)
- {
- $commandID = strtoupper($commandID);
- if ($readonly) {
- $this->readonly[$commandID] = $readonly;
- }
- else {
- unset($this->readonly[$commandID]);
- }
- }
-
- public function setScriptReadOnly($script, $readonly = true)
- {
- $sha1 = sha1($script);
- if ($readonly) {
- $this->readonlySHA1[$sha1] = $readonly;
- }
- else {
- unset($this->readonlySHA1[$sha1]);
- }
- }
-
- protected function getDisallowedOperations()
- {
- return array(
- 'SHUTDOWN' => true,
- 'INFO' => true,
- 'DBSIZE' => true,
- 'LASTSAVE' => true,
- 'CONFIG' => true,
- 'MONITOR' => true,
- 'SLAVEOF' => true,
- 'SAVE' => true,
- 'BGSAVE' => true,
- 'BGREWRITEAOF' => true,
- 'SLOWLOG' => true,
- );
- }
-
- protected function getReadOnlyOperations()
- {
- return array(
- 'EXISTS' => true,
- 'TYPE' => true,
- 'KEYS' => true,
- 'RANDOMKEY' => true,
- 'TTL' => true,
- 'GET' => true,
- 'MGET' => true,
- 'SUBSTR' => true,
- 'STRLEN' => true,
- 'GETRANGE' => true,
- 'GETBIT' => true,
- 'LLEN' => true,
- 'LRANGE' => true,
- 'LINDEX' => true,
- 'SCARD' => true,
- 'SISMEMBER' => true,
- 'SINTER' => true,
- 'SUNION' => true,
- 'SDIFF' => true,
- 'SMEMBERS' => true,
- 'SRANDMEMBER' => true,
- 'ZRANGE' => true,
- 'ZREVRANGE' => true,
- 'ZRANGEBYSCORE' => true,
- 'ZREVRANGEBYSCORE' => true,
- 'ZCARD' => true,
- 'ZSCORE' => true,
- 'ZCOUNT' => true,
- 'ZRANK' => true,
- 'ZREVRANK' => true,
- 'HGET' => true,
- 'HMGET' => true,
- 'HEXISTS' => true,
- 'HLEN' => true,
- 'HKEYS' => true,
- 'HVELS' => true,
- 'HGETALL' => true,
- 'PING' => true,
- 'AUTH' => true,
- 'SELECT' => true,
- 'ECHO' => true,
- 'QUIT' => true,
- 'OBJECT' => true,
- 'BITCOUNT' => true,
- 'TIME' => true,
- 'SORT' => array($this, 'isSortReadOnly'),
- );
- }
-
- public function __sleep()
- {
- return array('master', 'slaves', 'disallowed', 'readonly', 'readonlySHA1');
- }
- }
|