objectcache: detect default getWithSetCallback() set options
[lhc/web/wiklou.git] / includes / libs / rdbms / lbfactory / LBFactory.php
index 15a5c0d..70302a0 100644 (file)
@@ -59,6 +59,9 @@ abstract class LBFactory implements ILBFactory {
        /** @var array Web request information about the client */
        protected $requestInfo;
 
+       /** @var bool[] Map of (section ID => true) for usage section IDs */
+       protected $usageSections = [];
+
        /** @var mixed */
        protected $ticket;
        /** @var string|bool String if a requested DBO_TRX transaction round is active */
@@ -503,12 +506,17 @@ abstract class LBFactory implements ILBFactory {
        }
 
        /**
+        * Method called whenever a new LoadBalancer is created
+        *
         * @param ILoadBalancer $lb
         */
        protected function initLoadBalancer( ILoadBalancer $lb ) {
                if ( $this->trxRoundId !== false ) {
                        $lb->beginMasterChanges( $this->trxRoundId ); // set DBO_TRX
                }
+               foreach ( $this->usageSections as $id => $unused ) {
+                       $lb->declareUsageSectionStart( $id );
+               }
        }
 
        public function setDomainPrefix( $prefix ) {
@@ -548,6 +556,40 @@ abstract class LBFactory implements ILBFactory {
                $this->requestInfo = $info + $this->requestInfo;
        }
 
+       public function declareUsageSectionStart( $id = null ) {
+               static $nextId = 1;
+               if ( $id === null ) {
+                       $id = $nextId;
+                       ++$nextId;
+               }
+               // Handle existing load balancers
+               $this->forEachLB( function ( ILoadBalancer $lb ) use ( $id ) {
+                       $lb->declareUsageSectionStart( $id );
+               } );
+               // Remember to set this for new load balancers
+               $this->usageSections[$id] = true;
+
+               return $id;
+       }
+
+       public function declareUsageSectionEnd( $id ) {
+               $info = [ 'readQueries' => 0, 'writeQueries' => 0, 'cacheSetOptions' => null ];
+               $this->forEachLB( function ( ILoadBalancer $lb ) use ( $id, &$info ) {
+                       $lbInfo = $lb->declareUsageSectionEnd( $id );
+                       $info['readQueries'] += $lbInfo['readQueries'];
+                       $info['writeQueries'] += $lbInfo['writeQueries'];
+                       $dbCacheOpts = $lbInfo['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.