objectcache: detect default getWithSetCallback() set options
[lhc/web/wiklou.git] / includes / libs / rdbms / loadbalancer / LoadBalancer.php
index d42fed9..61359bc 100644 (file)
@@ -94,9 +94,11 @@ class LoadBalancer implements ILoadBalancer {
        /** @var string Current server name */
        private $host;
        /** @var bool Whether this PHP instance is for a CLI script */
-       protected $cliMode;
+       private $cliMode;
        /** @var string Agent name for query profiling */
-       protected $agent;
+       private $agent;
+       /** @var bool[] Map of (section ID => true) for usage section IDs */
+       private $usageSections = [];
 
        /** @var callable Exception logger */
        private $errorLogger;
@@ -864,6 +866,10 @@ class LoadBalancer implements ILoadBalancer {
                        }
                }
 
+               foreach ( $this->usageSections as $id => $unused ) {
+                       $db->declareUsageSectionStart( $id );
+               }
+
                return $db;
        }
 
@@ -1522,6 +1528,40 @@ class LoadBalancer implements ILoadBalancer {
                } );
        }
 
+       public function declareUsageSectionStart( $id = null ) {
+               static $nextId = 1;
+               if ( $id === null ) {
+                       $id = $nextId;
+                       ++$nextId;
+               }
+               // Handle existing connections
+               $this->forEachOpenConnection( function ( IDatabase $db ) use ( $id ) {
+                       $db->declareUsageSectionStart( $id );
+               } );
+               // Remember to set this for new connections
+               $this->usageSections[$id] = true;
+
+               return $id;
+       }
+
+       public function declareUsageSectionEnd( $id ) {
+               $info = [ 'readQueries' => 0, 'writeQueries' => 0, 'cacheSetOptions' => null ];
+               $this->forEachOpenConnection( function ( IDatabase $db ) use ( $id, &$info ) {
+                       $dbInfo = $db->declareUsageSectionEnd( $id );
+                       $info['readQueries'] += $dbInfo['readQueries'];
+                       $info['writeQueries'] += $dbInfo['writeQueries'];
+                       $dbCacheOpts = $dbInfo['cacheSetOptions'];
+                       if ( $dbCacheOpts ) {
+                               $info['cacheSetOptions'] = $info['cacheSetOptions']
+                                       ? Database::mergeCacheSetOptions( $info['cacheSetOptions'], $dbCacheOpts )
+                                       : $dbCacheOpts;
+                       }
+               } );
+               unset( $this->usageSections[$id] );
+
+               return $info;
+       }
+
        /**
         * Make PHP ignore user aborts/disconnects until the returned
         * value leaves scope. This returns null and does nothing in CLI mode.