From 5f163008d10a57be0e502488975006f5523b1f6f Mon Sep 17 00:00:00 2001 From: Aaron Schulz Date: Tue, 21 Apr 2015 23:12:59 -0700 Subject: [PATCH] Added LoadBalancer::waitForOne method * This waits on the generic reader (or a random slave) Change-Id: I4d4a96806d55b119fa535556e11abd55736e9077 --- includes/db/LoadBalancer.php | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/includes/db/LoadBalancer.php b/includes/db/LoadBalancer.php index bc17911f9a..624f46bc3b 100644 --- a/includes/db/LoadBalancer.php +++ b/includes/db/LoadBalancer.php @@ -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 -- 2.20.1