rdbms: optimize DBConnRef::getType() to avoid connections when possible
authorAaron Schulz <aschulz@wikimedia.org>
Thu, 11 Apr 2019 04:40:16 +0000 (21:40 -0700)
committerAaron Schulz <aschulz@wikimedia.org>
Thu, 11 Apr 2019 22:09:18 +0000 (22:09 +0000)
Change-Id: Ibe7f2ab173ba1457b6b11c4fe37e599962ae51b0

includes/libs/rdbms/database/DBConnRef.php

index a06ce76..b216892 100644 (file)
@@ -211,6 +211,19 @@ class DBConnRef implements IDatabase {
        }
 
        public function getType() {
+               if ( $this->conn === null ) {
+                       // Avoid triggering a database connection
+                       if ( $this->params[self::FLD_INDEX] === ILoadBalancer::DB_MASTER ) {
+                               $index = $this->lb->getWriterIndex();
+                       } else {
+                               $index = $this->params[self::FLD_INDEX];
+                       }
+                       if ( $index >= 0 ) {
+                               // In theory, if $index is DB_REPLICA, the type could vary
+                               return $this->lb->getServerType( $index );
+                       }
+               }
+
                return $this->__call( __FUNCTION__, func_get_args() );
        }