Forráskód Böngészése

Implement ability to fetch an updated list of sentinels from active sentinel.

This can be optionally done automatically but is disabled by default, just use
SentinelReplication::setUpdateSentinels() accordingly to enable the automatic
fetching of an updated list of sentinels.
Daniele Alessandri 9 éve
szülő
commit
24fb72a732
1 módosított fájl, 49 hozzáadás és 0 törlés
  1. 49 0
      src/Connection/Aggregate/SentinelReplication.php

+ 49 - 0
src/Connection/Aggregate/SentinelReplication.php

@@ -60,6 +60,11 @@ class SentinelReplication extends MasterSlaveReplication
      */
     protected $autoRetry = true;
 
+    /**
+     * Flag for automatic fetching of available sentinels.
+     */
+    protected $updateSentinels = false;
+
     /**
      * @param array                      $sentinels         Sentinel servers connection parameters.
      * @param string                     $service           Name of the service for autodiscovery.
@@ -102,6 +107,16 @@ class SentinelReplication extends MasterSlaveReplication
         $this->autoRetry = (bool) $retry;
     }
 
+    /**
+     * Set automatic fetching of available sentinels.
+     *
+     * @param bool $update Enable or disable automatic updates.
+     */
+    public function setUpdateSentinels($update)
+    {
+        $this->updateSentinels = (bool) $update;
+    }
+
     /**
      * {@inheritdoc}
      */
@@ -236,6 +251,10 @@ class SentinelReplication extends MasterSlaveReplication
      */
     public function querySentinel()
     {
+        if ($this->updateSentinels) {
+            $this->updateSentinels();
+        }
+
         $this->wipeServerList();
 
         SENTINEL_QUERY: {
@@ -262,6 +281,36 @@ class SentinelReplication extends MasterSlaveReplication
         }
     }
 
+    /**
+     * Updates the full list of sentinels by asking to a sentinel server.
+     */
+    public function updateSentinels()
+    {
+        SENTINEL_QUERY: {
+            $sentinel = $this->getSentinelConnection();
+
+            try {
+                $payload = $sentinel->executeCommand(
+                    RawCommand::create('SENTINEL', 'sentinels', $this->service)
+                );
+
+                $this->sentinels = array();
+                $this->sentinels[] = $sentinel->getParameters()->toArray();
+
+                foreach ($payload as $sentinel) {
+                    $this->sentinels[] = array(
+                        'host' => $sentinel[3],
+                        'port' => $sentinel[5],
+                    );
+                }
+            } catch (ConnectionException $exception) {
+                $this->sentinelConnection = null;
+
+                goto SENTINEL_QUERY;
+            }
+        }
+    }
+
     /**
      * Retries the execution of a command upon server failure after asking a new
      * configuration to one of the sentinels.