From e45d07d7fd3c06c89f1dab4005d641a423883b4d Mon Sep 17 00:00:00 2001 From: Aaron Schulz Date: Fri, 1 May 2015 14:40:26 -0700 Subject: [PATCH] Improved fail-over in ReplicatedBagOStuff for redis Bug: T96123 Change-Id: I3f37417a1a4f010b092a7718d43bfcdfe16e8f4c --- includes/objectcache/RedisBagOStuff.php | 26 +++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/includes/objectcache/RedisBagOStuff.php b/includes/objectcache/RedisBagOStuff.php index 9d5d0ef0a1..2db4e9a3bc 100644 --- a/includes/objectcache/RedisBagOStuff.php +++ b/includes/objectcache/RedisBagOStuff.php @@ -374,10 +374,32 @@ class RedisBagOStuff extends BagOStuff { foreach ( $candidates as $tag ) { $server = $this->serverTagMap[$tag]; + $conn = $this->redisPool->getConnection( $server ); - if ( $conn ) { - return array( $server, $conn ); + if ( !$conn ) { + continue; + } + + try { + $info = $conn->info(); + // Check if this server has an unreachable redis master + if ( $info['role'] === 'slave' + && $info['master_link_status'] === 'down' + && $this->automaticFailover + ) { + // If the master cannot be reached, fail-over to the next server. + // If masters are in data-center A, and slaves in data-center B, + // this helps avoid the case were fail-over happens in A but not + // to the corresponding server in B (e.g. read/write mismatch). + continue; + } + } catch ( RedisException $e ) { + // Server is not accepting commands + $this->handleException( $conn, $e ); + continue; } + + return array( $server, $conn ); } $this->setLastError( BagOStuff::ERR_UNREACHABLE ); -- 2.20.1