Added LoadBalancer::waitForOne method
authorAaron Schulz <aschulz@wikimedia.org>
Wed, 22 Apr 2015 06:12:59 +0000 (23:12 -0700)
committerAaron Schulz <aschulz@wikimedia.org>
Wed, 22 Apr 2015 20:49:11 +0000 (20:49 +0000)
* This waits on the generic reader (or a random slave)

Change-Id: I4d4a96806d55b119fa535556e11abd55736e9077

includes/db/LoadBalancer.php

index bc17911..624f46b 100644 (file)
@@ -358,6 +358,37 @@ class LoadBalancer {
                }
        }
 
+       /**
+        * Set the master wait position and wait for a "generic" slave to catch up to it
+        *
+        * This can be used a faster proxy for waitForAll()
+        *
+        * @param DBMasterPos $pos
+        * @param int $timeout Max seconds to wait; default is mWaitTimeout
+        * @return bool Success (able to connect and no timeouts reached)
+        * @since 1.26
+        */
+       public function waitForOne( $pos, $timeout = null ) {
+               $this->mWaitForPos = $pos;
+
+               $i = $this->mReadIndex;
+               if ( $i <= 0 ) {
+                       // Pick a generic slave if there isn't one yet
+                       $readLoads = $this->mLoads;
+                       unset( $readLoads[$this->getWriterIndex()] ); // slaves only
+                       $readLoads = array_filter( $readLoads ); // with non-zero load
+                       $i = ArrayUtils::pickRandom( $readLoads );
+               }
+
+               if ( $i > 0 ) {
+                       $ok = $this->doWait( $i, true, $timeout );
+               } else {
+                       $ok = true; // no applicable loads
+               }
+
+               return $ok;
+       }
+
        /**
         * Set the master wait position and wait for ALL slaves to catch up to it
         * @param DBMasterPos $pos