Преглед на файлове

Switch to master inside executor when pipelining in replication mode.

The actual code to switch to master has been implemented only in the
standard and the fire-and-forget executor classes since it does not
make sense to have it in special executors used exclusively when in
client-side clustering mode.
Daniele Alessandri преди 13 години
родител
ревизия
ca8ba35fb5
променени са 3 файла, в които са добавени 34 реда и са изтрити 8 реда
  1. 17 0
      lib/Predis/Pipeline/FireAndForgetExecutor.php
  2. 0 8
      lib/Predis/Pipeline/PipelineContext.php
  3. 17 0
      lib/Predis/Pipeline/StandardExecutor.php

+ 17 - 0
lib/Predis/Pipeline/FireAndForgetExecutor.php

@@ -12,6 +12,7 @@
 namespace Predis\Pipeline;
 namespace Predis\Pipeline;
 
 
 use Predis\Network\IConnection;
 use Predis\Network\IConnection;
+use Predis\Network\IConnectionReplication;
 
 
 /**
 /**
  * Implements a pipeline executor strategy that writes a list of commands to
  * Implements a pipeline executor strategy that writes a list of commands to
@@ -21,11 +22,27 @@ use Predis\Network\IConnection;
  */
  */
 class FireAndForgetExecutor implements IPipelineExecutor
 class FireAndForgetExecutor implements IPipelineExecutor
 {
 {
+    /**
+     * Allows the pipeline executor to perform operations on the
+     * connection before starting to execute the commands stored
+     * in the pipeline.
+     *
+     * @param IConnection Connection instance.
+     */
+    protected function checkConnection(IConnection $connection)
+    {
+        if ($connection instanceof IConnectionReplication) {
+            $connection->switchTo('master');
+        }
+    }
+
     /**
     /**
      * {@inheritdoc}
      * {@inheritdoc}
      */
      */
     public function execute(IConnection $connection, &$commands)
     public function execute(IConnection $connection, &$commands)
     {
     {
+        $this->checkConnection($connection);
+
         foreach ($commands as $command) {
         foreach ($commands as $command) {
             $connection->writeCommand($command);
             $connection->writeCommand($command);
         }
         }

+ 0 - 8
lib/Predis/Pipeline/PipelineContext.php

@@ -15,7 +15,6 @@ use Predis\Client;
 use Predis\Helpers;
 use Predis\Helpers;
 use Predis\ClientException;
 use Predis\ClientException;
 use Predis\Commands\ICommand;
 use Predis\Commands\ICommand;
-use Predis\Network\IConnectionReplication;
 
 
 /**
 /**
  * Abstraction of a pipeline context where write and read operations
  * Abstraction of a pipeline context where write and read operations
@@ -121,13 +120,6 @@ class PipelineContext
         if (count($this->pipeline) > 0) {
         if (count($this->pipeline) > 0) {
             if ($send) {
             if ($send) {
                 $connection = $this->client->getConnection();
                 $connection = $this->client->getConnection();
-
-                // TODO: it would be better to use a dedicated pipeline executor
-                //       for classes implementing master/slave replication.
-                if ($connection instanceof IConnectionReplication) {
-                    $connection->switchTo('master');
-                }
-
                 $replies = $this->executor->execute($connection, $this->pipeline);
                 $replies = $this->executor->execute($connection, $this->pipeline);
                 $this->replies = array_merge($this->replies, $replies);
                 $this->replies = array_merge($this->replies, $replies);
             }
             }

+ 17 - 0
lib/Predis/Pipeline/StandardExecutor.php

@@ -13,6 +13,7 @@ namespace Predis\Pipeline;
 
 
 use Predis\ServerException;
 use Predis\ServerException;
 use Predis\Network\IConnection;
 use Predis\Network\IConnection;
+use Predis\Network\IConnectionReplication;
 
 
 /**
 /**
  * Implements the standard pipeline executor strategy used
  * Implements the standard pipeline executor strategy used
@@ -23,6 +24,20 @@ use Predis\Network\IConnection;
  */
  */
 class StandardExecutor implements IPipelineExecutor
 class StandardExecutor implements IPipelineExecutor
 {
 {
+    /**
+     * Allows the pipeline executor to perform operations on the
+     * connection before starting to execute the commands stored
+     * in the pipeline.
+     *
+     * @param IConnection Connection instance.
+     */
+    protected function checkConnection(IConnection $connection)
+    {
+        if ($connection instanceof IConnectionReplication) {
+            $connection->switchTo('master');
+        }
+    }
+
     /**
     /**
      * {@inheritdoc}
      * {@inheritdoc}
      */
      */
@@ -31,6 +46,8 @@ class StandardExecutor implements IPipelineExecutor
         $sizeofPipe = count($commands);
         $sizeofPipe = count($commands);
         $values = array();
         $values = array();
 
 
+        $this->checkConnection($connection);
+
         foreach ($commands as $command) {
         foreach ($commands as $command) {
             $connection->writeCommand($command);
             $connection->writeCommand($command);
         }
         }