Sentinel.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. <?php
  2. namespace Qii\Cache\Redis;
  3. /**
  4. * Credis_Sentinel
  5. *
  6. * Implements the Sentinel API as mentioned on http://redis.io/topics/sentinel.
  7. * Sentinel is aware of master and slave nodes in a cluster and returns instances of Credis_Client accordingly.
  8. *
  9. * The complexity of read/write splitting can also be abstract by calling the createCluster() method which returns a
  10. * Credis_Cluster object that contains both the master server and a random slave. Credis_Cluster takes care of the
  11. * read/write splitting
  12. *
  13. * @author Thijs Feryn <thijs@feryn.eu>
  14. * @license http://www.opensource.org/licenses/mit-license.php The MIT License
  15. * @package Credis_Sentinel
  16. */
  17. class Sentinel
  18. {
  19. /**
  20. * Contains a client that connects to a Sentinel node.
  21. * Sentinel uses the same protocol as Redis which makes using Credis_Client convenient.
  22. * @var Credis_Client
  23. */
  24. protected $_client;
  25. /**
  26. * Contains an active instance of Credis_Cluster per master pool
  27. * @var array
  28. */
  29. protected $_cluster = array();
  30. /**
  31. * Contains an active instance of Credis_Client representing a master
  32. * @var array
  33. */
  34. protected $_master = array();
  35. /**
  36. * Contains an array Credis_Client objects representing all slaves per master pool
  37. * @var array
  38. */
  39. protected $_slaves = array();
  40. /**
  41. * Use the phpredis extension or the standalone implementation
  42. * @var bool
  43. * @deprecated
  44. */
  45. protected $_standAlone = false;
  46. /**
  47. * Store the AUTH password used by Credis_Client instances
  48. * @var string
  49. */
  50. protected $_password = '';
  51. /**
  52. * Connect with a Sentinel node. Sentinel will do the master and slave discovery
  53. *
  54. * @param Credis_Client $client
  55. * @param string $password (deprecated - use setClientPassword)
  56. * @throws CredisException
  57. */
  58. public function __construct(Credis_Client $client, $password = NULL)
  59. {
  60. if(!$client instanceof \Qii\Cache\Redis\Client){
  61. throw new CredisException('Sentinel client should be an instance of \Qii\Cache\Redis\Client');
  62. }
  63. $client->forceStandalone(); // SENTINEL command not currently supported by phpredis
  64. $this->_client = $client;
  65. $this->_password = $password;
  66. $this->_timeout = NULL;
  67. $this->_persistent = '';
  68. $this->_db = 0;
  69. }
  70. /**
  71. * Clean up client on destruct
  72. */
  73. public function __destruct()
  74. {
  75. $this->_client->close();
  76. }
  77. /**
  78. * @param float $timeout
  79. * @return $this
  80. */
  81. public function setClientTimeout($timeout)
  82. {
  83. $this->_timeout = $timeout;
  84. return $this;
  85. }
  86. /**
  87. * @param string $persistent
  88. * @return $this
  89. */
  90. public function setClientPersistent($persistent)
  91. {
  92. $this->_persistent = $persistent;
  93. return $this;
  94. }
  95. /**
  96. * @param int $db
  97. * @return $this
  98. */
  99. public function setClientDatabase($db)
  100. {
  101. $this->_db = $db;
  102. return $this;
  103. }
  104. /**
  105. * @param null|string $password
  106. * @return $this
  107. */
  108. public function setClientPassword($password)
  109. {
  110. $this->_password = $password;
  111. return $this;
  112. }
  113. /**
  114. * @return Credis_Sentinel
  115. * @deprecated
  116. */
  117. public function forceStandalone()
  118. {
  119. $this->_standAlone = true;
  120. return $this;
  121. }
  122. /**
  123. * Discover the master node automatically and return an instance of Credis_Client that connects to the master
  124. *
  125. * @param string $name
  126. * @return Credis_Client
  127. * @throws CredisException
  128. */
  129. public function createMasterClient($name)
  130. {
  131. $master = $this->getMasterAddressByName($name);
  132. if(!isset($master[0]) || !isset($master[1])){
  133. throw new CredisException('Master not found');
  134. }
  135. return new Credis_Client($master[0], $master[1], $this->_timeout, $this->_persistent, $this->_db, $this->_password);
  136. }
  137. /**
  138. * If a Credis_Client object exists for a master, return it. Otherwise create one and return it
  139. * @param string $name
  140. * @return Credis_Client
  141. */
  142. public function getMasterClient($name)
  143. {
  144. if(!isset($this->_master[$name])){
  145. $this->_master[$name] = $this->createMasterClient($name);
  146. }
  147. return $this->_master[$name];
  148. }
  149. /**
  150. * Discover the slave nodes automatically and return an array of Credis_Client objects
  151. *
  152. * @param string $name
  153. * @return Credis_Client[]
  154. * @throws CredisException
  155. */
  156. public function createSlaveClients($name)
  157. {
  158. $slaves = $this->slaves($name);
  159. $workingSlaves = array();
  160. foreach($slaves as $slave) {
  161. if(!isset($slave[9])){
  162. throw new CredisException('Can\' retrieve slave status');
  163. }
  164. if(!strstr($slave[9],'s_down') && !strstr($slave[9],'disconnected')) {
  165. $workingSlaves[] = new Credis_Client($slave[3], $slave[5], $this->_timeout, $this->_persistent, $this->_db, $this->_password);
  166. }
  167. }
  168. return $workingSlaves;
  169. }
  170. /**
  171. * If an array of Credis_Client objects exist for a set of slaves, return them. Otherwise create and return them
  172. * @param string $name
  173. * @return Credis_Client[]
  174. */
  175. public function getSlaveClients($name)
  176. {
  177. if(!isset($this->_slaves[$name])){
  178. $this->_slaves[$name] = $this->createSlaveClients($name);
  179. }
  180. return $this->_slaves[$name];
  181. }
  182. /**
  183. * Returns a Redis cluster object containing a random slave and the master
  184. * When $selectRandomSlave is true, only one random slave is passed.
  185. * When $selectRandomSlave is false, all clients are passed and hashing is applied in Credis_Cluster
  186. * When $writeOnly is false, the master server will also be used for read commands.
  187. *
  188. * @param string $name
  189. * @param int $db
  190. * @param int $replicas
  191. * @param bool $selectRandomSlave
  192. * @param bool $writeOnly
  193. * @return Credis_Cluster
  194. * @throws CredisException
  195. * @deprecated
  196. */
  197. public function createCluster($name, $db=0, $replicas=128, $selectRandomSlave=true, $writeOnly=false)
  198. {
  199. $clients = array();
  200. $workingClients = array();
  201. $master = $this->master($name);
  202. if(strstr($master[9],'s_down') || strstr($master[9],'disconnected')) {
  203. throw new CredisException('The master is down');
  204. }
  205. $slaves = $this->slaves($name);
  206. foreach($slaves as $slave){
  207. if(!strstr($slave[9],'s_down') && !strstr($slave[9],'disconnected')) {
  208. $workingClients[] = array('host'=>$slave[3],'port'=>$slave[5],'master'=>false,'db'=>$db,'password'=>$this->_password);
  209. }
  210. }
  211. if(count($workingClients)>0){
  212. if($selectRandomSlave){
  213. if(!$writeOnly){
  214. $workingClients[] = array('host'=>$master[3],'port'=>$master[5],'master'=>false,'db'=>$db,'password'=>$this->_password);
  215. }
  216. $clients[] = $workingClients[rand(0,count($workingClients)-1)];
  217. } else {
  218. $clients = $workingClients;
  219. }
  220. }
  221. $clients[] = array('host'=>$master[3],'port'=>$master[5], 'db'=>$db ,'master'=>true,'write_only'=>$writeOnly,'password'=>$this->_password);
  222. return new Credis_Cluster($clients,$replicas,$this->_standAlone);
  223. }
  224. /**
  225. * If a Credis_Cluster object exists, return it. Otherwise create one and return it.
  226. * @param string $name
  227. * @param int $db
  228. * @param int $replicas
  229. * @param bool $selectRandomSlave
  230. * @param bool $writeOnly
  231. * @return Credis_Cluster
  232. * @deprecated
  233. */
  234. public function getCluster($name, $db=0, $replicas=128, $selectRandomSlave=true, $writeOnly=false)
  235. {
  236. if(!isset($this->_cluster[$name])){
  237. $this->_cluster[$name] = $this->createCluster($name, $db, $replicas, $selectRandomSlave, $writeOnly);
  238. }
  239. return $this->_cluster[$name];
  240. }
  241. /**
  242. * Catch-all method
  243. * @param string $name
  244. * @param array $args
  245. * @return mixed
  246. */
  247. public function __call($name, $args)
  248. {
  249. array_unshift($args,$name);
  250. return call_user_func(array($this->_client,'sentinel'),$args);
  251. }
  252. /**
  253. * Return information about all registered master servers
  254. * @return mixed
  255. */
  256. public function masters()
  257. {
  258. return $this->_client->sentinel('masters');
  259. }
  260. /**
  261. * Return all information for slaves that are associated with a single master
  262. * @param string $name
  263. * @return mixed
  264. */
  265. public function slaves($name)
  266. {
  267. return $this->_client->sentinel('slaves',$name);
  268. }
  269. /**
  270. * Get the information for a specific master
  271. * @param string $name
  272. * @return mixed
  273. */
  274. public function master($name)
  275. {
  276. return $this->_client->sentinel('master',$name);
  277. }
  278. /**
  279. * Get the hostname and port for a specific master
  280. * @param string $name
  281. * @return mixed
  282. */
  283. public function getMasterAddressByName($name)
  284. {
  285. return $this->_client->sentinel('get-master-addr-by-name',$name);
  286. }
  287. /**
  288. * Check if the Sentinel is still responding
  289. * @param string $name
  290. * @return mixed
  291. */
  292. public function ping()
  293. {
  294. return $this->_client->ping();
  295. }
  296. /**
  297. * Perform an auto-failover which will re-elect another master and make the current master a slave
  298. * @param string $name
  299. * @return mixed
  300. */
  301. public function failover($name)
  302. {
  303. return $this->_client->sentinel('failover',$name);
  304. }
  305. /**
  306. * @return string
  307. */
  308. public function getHost()
  309. {
  310. return $this->_client->getHost();
  311. }
  312. /**
  313. * @return int
  314. */
  315. public function getPort()
  316. {
  317. return $this->_client->getPort();
  318. }
  319. }