filebackend: switch callers from ProcessCacheLRU to MapCacheLRU
authorAaron Schulz <aschulz@wikimedia.org>
Wed, 11 Jul 2018 12:53:13 +0000 (13:53 +0100)
committerAaron Schulz <aschulz@wikimedia.org>
Sat, 14 Jul 2018 11:27:09 +0000 (11:27 +0000)
Change-Id: I6efe7122ff30053a85461d31be7b5ddad0a5d2af

includes/libs/filebackend/FileBackendStore.php
includes/libs/filebackend/SwiftFileBackend.php

index 118464c..e2d56b0 100644 (file)
@@ -40,9 +40,9 @@ abstract class FileBackendStore extends FileBackend {
        protected $memCache;
        /** @var BagOStuff */
        protected $srvCache;
-       /** @var ProcessCacheLRU Map of paths to small (RAM/disk) cache items */
+       /** @var MapCacheLRU Map of paths to small (RAM/disk) cache items */
        protected $cheapCache;
-       /** @var ProcessCacheLRU Map of paths to large (RAM/disk) cache items */
+       /** @var MapCacheLRU Map of paths to large (RAM/disk) cache items */
        protected $expensiveCache;
 
        /** @var array Map of container names to sharding config */
@@ -73,8 +73,8 @@ abstract class FileBackendStore extends FileBackend {
                $this->mimeCallback = $config['mimeCallback'] ?? null;
                $this->srvCache = new EmptyBagOStuff(); // disabled by default
                $this->memCache = WANObjectCache::newEmpty(); // disabled by default
-               $this->cheapCache = new ProcessCacheLRU( self::CACHE_CHEAP_SIZE );
-               $this->expensiveCache = new ProcessCacheLRU( self::CACHE_EXPENSIVE_SIZE );
+               $this->cheapCache = new MapCacheLRU( self::CACHE_CHEAP_SIZE );
+               $this->expensiveCache = new MapCacheLRU( self::CACHE_EXPENSIVE_SIZE );
        }
 
        /**
@@ -627,11 +627,11 @@ abstract class FileBackendStore extends FileBackend {
                }
                $ps = $this->scopedProfileSection( __METHOD__ . "-{$this->name}" );
                $latest = !empty( $params['latest'] ); // use latest data?
-               if ( !$latest && !$this->cheapCache->has( $path, 'stat', self::CACHE_TTL ) ) {
+               if ( !$latest && !$this->cheapCache->hasField( $path, 'stat', self::CACHE_TTL ) ) {
                        $this->primeFileCache( [ $path ] ); // check persistent cache
                }
-               if ( $this->cheapCache->has( $path, 'stat', self::CACHE_TTL ) ) {
-                       $stat = $this->cheapCache->get( $path, 'stat' );
+               if ( $this->cheapCache->hasField( $path, 'stat', self::CACHE_TTL ) ) {
+                       $stat = $this->cheapCache->getField( $path, 'stat' );
                        // If we want the latest data, check that this cached
                        // value was in fact fetched with the latest available data.
                        if ( is_array( $stat ) ) {
@@ -648,21 +648,21 @@ abstract class FileBackendStore extends FileBackend {
                if ( is_array( $stat ) ) { // file exists
                        // Strongly consistent backends can automatically set "latest"
                        $stat['latest'] = $stat['latest'] ?? $latest;
-                       $this->cheapCache->set( $path, 'stat', $stat );
+                       $this->cheapCache->setField( $path, 'stat', $stat );
                        $this->setFileCache( $path, $stat ); // update persistent cache
                        if ( isset( $stat['sha1'] ) ) { // some backends store SHA-1 as metadata
-                               $this->cheapCache->set( $path, 'sha1',
+                               $this->cheapCache->setField( $path, 'sha1',
                                        [ 'hash' => $stat['sha1'], 'latest' => $latest ] );
                        }
                        if ( isset( $stat['xattr'] ) ) { // some backends store headers/metadata
                                $stat['xattr'] = self::normalizeXAttributes( $stat['xattr'] );
-                               $this->cheapCache->set( $path, 'xattr',
+                               $this->cheapCache->setField( $path, 'xattr',
                                        [ 'map' => $stat['xattr'], 'latest' => $latest ] );
                        }
                } elseif ( $stat === false ) { // file does not exist
-                       $this->cheapCache->set( $path, 'stat', $latest ? 'NOT_EXIST_LATEST' : 'NOT_EXIST' );
-                       $this->cheapCache->set( $path, 'xattr', [ 'map' => false, 'latest' => $latest ] );
-                       $this->cheapCache->set( $path, 'sha1', [ 'hash' => false, 'latest' => $latest ] );
+                       $this->cheapCache->setField( $path, 'stat', $latest ? 'NOT_EXIST_LATEST' : 'NOT_EXIST' );
+                       $this->cheapCache->setField( $path, 'xattr', [ 'map' => false, 'latest' => $latest ] );
+                       $this->cheapCache->setField( $path, 'sha1', [ 'hash' => false, 'latest' => $latest ] );
                        $this->logger->debug( __METHOD__ . ": File $path does not exist.\n" );
                } else { // an error occurred
                        $this->logger->warning( __METHOD__ . ": Could not stat file $path.\n" );
@@ -709,8 +709,8 @@ abstract class FileBackendStore extends FileBackend {
                }
                $ps = $this->scopedProfileSection( __METHOD__ . "-{$this->name}" );
                $latest = !empty( $params['latest'] ); // use latest data?
-               if ( $this->cheapCache->has( $path, 'xattr', self::CACHE_TTL ) ) {
-                       $stat = $this->cheapCache->get( $path, 'xattr' );
+               if ( $this->cheapCache->hasField( $path, 'xattr', self::CACHE_TTL ) ) {
+                       $stat = $this->cheapCache->getField( $path, 'xattr' );
                        // If we want the latest data, check that this cached
                        // value was in fact fetched with the latest available data.
                        if ( !$latest || $stat['latest'] ) {
@@ -719,7 +719,7 @@ abstract class FileBackendStore extends FileBackend {
                }
                $fields = $this->doGetFileXAttributes( $params );
                $fields = is_array( $fields ) ? self::normalizeXAttributes( $fields ) : false;
-               $this->cheapCache->set( $path, 'xattr', [ 'map' => $fields, 'latest' => $latest ] );
+               $this->cheapCache->setField( $path, 'xattr', [ 'map' => $fields, 'latest' => $latest ] );
 
                return $fields;
        }
@@ -740,8 +740,8 @@ abstract class FileBackendStore extends FileBackend {
                }
                $ps = $this->scopedProfileSection( __METHOD__ . "-{$this->name}" );
                $latest = !empty( $params['latest'] ); // use latest data?
-               if ( $this->cheapCache->has( $path, 'sha1', self::CACHE_TTL ) ) {
-                       $stat = $this->cheapCache->get( $path, 'sha1' );
+               if ( $this->cheapCache->hasField( $path, 'sha1', self::CACHE_TTL ) ) {
+                       $stat = $this->cheapCache->getField( $path, 'sha1' );
                        // If we want the latest data, check that this cached
                        // value was in fact fetched with the latest available data.
                        if ( !$latest || $stat['latest'] ) {
@@ -749,7 +749,7 @@ abstract class FileBackendStore extends FileBackend {
                        }
                }
                $hash = $this->doGetFileSha1Base36( $params );
-               $this->cheapCache->set( $path, 'sha1', [ 'hash' => $hash, 'latest' => $latest ] );
+               $this->cheapCache->setField( $path, 'sha1', [ 'hash' => $hash, 'latest' => $latest ] );
 
                return $hash;
        }
@@ -788,8 +788,8 @@ abstract class FileBackendStore extends FileBackend {
                        $path = self::normalizeStoragePath( $src );
                        if ( $path === null ) {
                                $fsFiles[$src] = null; // invalid storage path
-                       } elseif ( $this->expensiveCache->has( $path, 'localRef' ) ) {
-                               $val = $this->expensiveCache->get( $path, 'localRef' );
+                       } elseif ( $this->expensiveCache->hasField( $path, 'localRef' ) ) {
+                               $val = $this->expensiveCache->getField( $path, 'localRef' );
                                // If we want the latest data, check that this cached
                                // value was in fact fetched with the latest available data.
                                if ( !$latest || $val['latest'] ) {
@@ -802,7 +802,7 @@ abstract class FileBackendStore extends FileBackend {
                foreach ( $this->doGetLocalReferenceMulti( $params ) as $path => $fsFile ) {
                        $fsFiles[$path] = $fsFile;
                        if ( $fsFile ) { // update the process cache...
-                               $this->expensiveCache->set( $path, 'localRef',
+                               $this->expensiveCache->setField( $path, 'localRef',
                                        [ 'object' => $fsFile, 'latest' => $latest ] );
                        }
                }
@@ -1102,7 +1102,7 @@ abstract class FileBackendStore extends FileBackend {
                }
 
                // Enlarge the cache to fit the stat entries of these files
-               $this->cheapCache->resize( max( 2 * count( $paths ), self::CACHE_CHEAP_SIZE ) );
+               $this->cheapCache->setMaxSize( max( 2 * count( $paths ), self::CACHE_CHEAP_SIZE ) );
 
                // Load from the persistent container caches
                $this->primeContainerCache( $paths );
@@ -1129,7 +1129,7 @@ abstract class FileBackendStore extends FileBackend {
                $status->success = $subStatus->success; // not done in merge()
 
                // Shrink the stat cache back to normal size
-               $this->cheapCache->resize( self::CACHE_CHEAP_SIZE );
+               $this->cheapCache->setMaxSize( self::CACHE_CHEAP_SIZE );
 
                return $status;
        }
@@ -1323,23 +1323,23 @@ abstract class FileBackendStore extends FileBackend {
                        if ( is_array( $stat ) ) { // file exists
                                // Strongly consistent backends can automatically set "latest"
                                $stat['latest'] = $stat['latest'] ?? $latest;
-                               $this->cheapCache->set( $path, 'stat', $stat );
+                               $this->cheapCache->setField( $path, 'stat', $stat );
                                $this->setFileCache( $path, $stat ); // update persistent cache
                                if ( isset( $stat['sha1'] ) ) { // some backends store SHA-1 as metadata
-                                       $this->cheapCache->set( $path, 'sha1',
+                                       $this->cheapCache->setField( $path, 'sha1',
                                                [ 'hash' => $stat['sha1'], 'latest' => $latest ] );
                                }
                                if ( isset( $stat['xattr'] ) ) { // some backends store headers/metadata
                                        $stat['xattr'] = self::normalizeXAttributes( $stat['xattr'] );
-                                       $this->cheapCache->set( $path, 'xattr',
+                                       $this->cheapCache->setField( $path, 'xattr',
                                                [ 'map' => $stat['xattr'], 'latest' => $latest ] );
                                }
                        } elseif ( $stat === false ) { // file does not exist
-                               $this->cheapCache->set( $path, 'stat',
+                               $this->cheapCache->setField( $path, 'stat',
                                        $latest ? 'NOT_EXIST_LATEST' : 'NOT_EXIST' );
-                               $this->cheapCache->set( $path, 'xattr',
+                               $this->cheapCache->setField( $path, 'xattr',
                                        [ 'map' => false, 'latest' => $latest ] );
-                               $this->cheapCache->set( $path, 'sha1',
+                               $this->cheapCache->setField( $path, 'sha1',
                                        [ 'hash' => false, 'latest' => $latest ] );
                                $this->logger->debug( __METHOD__ . ": File $path does not exist.\n" );
                        } else { // an error occurred
@@ -1769,14 +1769,14 @@ abstract class FileBackendStore extends FileBackend {
                        $path = $pathNames[$cacheKey];
                        if ( is_array( $val ) ) {
                                $val['latest'] = false; // never completely trust cache
-                               $this->cheapCache->set( $path, 'stat', $val );
+                               $this->cheapCache->setField( $path, 'stat', $val );
                                if ( isset( $val['sha1'] ) ) { // some backends store SHA-1 as metadata
-                                       $this->cheapCache->set( $path, 'sha1',
+                                       $this->cheapCache->setField( $path, 'sha1',
                                                [ 'hash' => $val['sha1'], 'latest' => false ] );
                                }
                                if ( isset( $val['xattr'] ) ) { // some backends store headers/metadata
                                        $val['xattr'] = self::normalizeXAttributes( $val['xattr'] );
-                                       $this->cheapCache->set( $path, 'xattr',
+                                       $this->cheapCache->setField( $path, 'xattr',
                                                [ 'map' => $val['xattr'], 'latest' => false ] );
                                }
                        }
index 143f854..31882de 100644 (file)
@@ -129,7 +129,7 @@ class SwiftFileBackend extends FileBackendStore {
                        $this->memCache = $config['wanCache'];
                }
                // Process cache for container info
-               $this->containerStatCache = new ProcessCacheLRU( 300 );
+               $this->containerStatCache = new MapCacheLRU( 300 );
                // Cache auth token information to avoid RTTs
                if ( !empty( $config['cacheAuthInfo'] ) && isset( $config['srvCache'] ) ) {
                        $this->srvCache = $config['srvCache'];
@@ -1033,7 +1033,7 @@ class SwiftFileBackend extends FileBackendStore {
         * @param array $val Stat value
         */
        public function loadListingStatInternal( $path, array $val ) {
-               $this->cheapCache->set( $path, 'stat', $val );
+               $this->cheapCache->setField( $path, 'stat', $val );
        }
 
        protected function doGetFileXAttributes( array $params ) {
@@ -1188,7 +1188,7 @@ class SwiftFileBackend extends FileBackendStore {
                                // Set the file stat process cache in passing
                                $stat = $this->getStatFromHeaders( $rhdrs );
                                $stat['latest'] = $isLatest;
-                               $this->cheapCache->set( $path, 'stat', $stat );
+                               $this->cheapCache->setField( $path, 'stat', $stat );
                        } elseif ( $rcode === 404 ) {
                                $tmpFiles[$path] = false;
                        } else {
@@ -1395,10 +1395,10 @@ class SwiftFileBackend extends FileBackendStore {
 
                if ( $bypassCache ) { // purge cache
                        $this->containerStatCache->clear( $container );
-               } elseif ( !$this->containerStatCache->has( $container, 'stat' ) ) {
+               } elseif ( !$this->containerStatCache->hasField( $container, 'stat' ) ) {
                        $this->primeContainerCache( [ $container ] ); // check persistent cache
                }
-               if ( !$this->containerStatCache->has( $container, 'stat' ) ) {
+               if ( !$this->containerStatCache->hasField( $container, 'stat' ) ) {
                        $auth = $this->getAuthentication();
                        if ( !$auth ) {
                                return null;
@@ -1418,7 +1418,7 @@ class SwiftFileBackend extends FileBackendStore {
                                if ( $bypassCache ) {
                                        return $stat;
                                } else {
-                                       $this->containerStatCache->set( $container, 'stat', $stat ); // cache it
+                                       $this->containerStatCache->setField( $container, 'stat', $stat ); // cache it
                                        $this->setContainerCache( $container, $stat ); // update persistent cache
                                }
                        } elseif ( $rcode === 404 ) {
@@ -1431,7 +1431,7 @@ class SwiftFileBackend extends FileBackendStore {
                        }
                }
 
-               return $this->containerStatCache->get( $container, 'stat' );
+               return $this->containerStatCache->getField( $container, 'stat' );
        }
 
        /**
@@ -1583,7 +1583,7 @@ class SwiftFileBackend extends FileBackendStore {
 
        protected function doPrimeContainerCache( array $containerInfo ) {
                foreach ( $containerInfo as $container => $info ) {
-                       $this->containerStatCache->set( $container, 'stat', $info );
+                       $this->containerStatCache->setField( $container, 'stat', $info );
                }
        }