Sentinel.php 10 KB

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