[FileBackend] Added copy() sanity checks to FSFileBackend.
[lhc/web/wiklou.git] / includes / filerepo / backend / SwiftFileBackend.php
index af5d693..41d30dd 100644 (file)
@@ -24,7 +24,7 @@ class SwiftFileBackend extends FileBackendStore {
        protected $auth; // Swift authentication handler
        protected $authTTL; // integer seconds
        protected $swiftAnonUser; // string; username to handle unauthenticated requests
-       protected $maxContCacheSize = 100; // integer; max containers with entries
+       protected $maxContCacheSize = 300; // integer; max containers with entries
 
        /** @var CF_Connection */
        protected $conn; // Swift connection handle
@@ -57,13 +57,15 @@ class SwiftFileBackend extends FileBackendStore {
                // Optional settings
                $this->authTTL = isset( $config['swiftAuthTTL'] )
                        ? $config['swiftAuthTTL']
-                       : 120; // some sane number
+                       : 5 * 60; // some sane number
                $this->swiftAnonUser = isset( $config['swiftAnonUser'] )
                        ? $config['swiftAnonUser']
                        : '';
                $this->shardViaHashLevels = isset( $config['shardViaHashLevels'] )
                        ? $config['shardViaHashLevels']
                        : '';
+               // Cache container info to mask latency
+               $this->memCache = wfGetMainCache();
        }
 
        /**
@@ -770,6 +772,14 @@ class SwiftFileBackend extends FileBackendStore {
                return $tmpFile;
        }
 
+       /**
+        * @see FileBackendStore::directoriesAreVirtual()
+        * @return bool
+        */
+       protected function directoriesAreVirtual() {
+               return true;
+       }
+
        /**
         * Get headers to send to Swift when reading a file based
         * on a FileBackend params array, e.g. that of getLocalCopy().
@@ -856,23 +866,30 @@ class SwiftFileBackend extends FileBackendStore {
         * Use $reCache if the file count or byte count is needed.
         *
         * @param $container string Container name
-        * @param $reCache bool Refresh the process cache
+        * @param $bypassCache bool Bypass all caches and load from Swift
         * @return CF_Container
+        * @throws InvalidResponseException
         */
-       protected function getContainer( $container, $reCache = false ) {
+       protected function getContainer( $container, $bypassCache = false ) {
                $conn = $this->getConnection(); // Swift proxy connection
-               if ( $reCache ) {
-                       unset( $this->connContainers[$container] ); // purge cache
+               if ( $bypassCache ) { // purge cache
+                       unset( $this->connContainers[$container] );
+               } elseif ( !isset( $this->connContainers[$container] ) ) {
+                       $this->primeContainerCache( array( $container ) ); // check persistent cache
                }
                if ( !isset( $this->connContainers[$container] ) ) {
                        $contObj = $conn->get_container( $container );
                        // NoSuchContainerException not thrown: container must exist
                        if ( count( $this->connContainers ) >= $this->maxContCacheSize ) { // trim cache?
                                reset( $this->connContainers );
-                               $key = key( $this->connContainers );
-                               unset( $this->connContainers[$key] );
+                               unset( $this->connContainers[key( $this->connContainers )] );
                        }
                        $this->connContainers[$container] = $contObj; // cache it
+                       if ( !$bypassCache ) {
+                               $this->setContainerCache( $container, // update persistent cache
+                                       array( 'bytes' => $contObj->bytes_used, 'count' => $contObj->object_count )
+                               );
+                       }
                }
                return $this->connContainers[$container];
        }
@@ -882,6 +899,7 @@ class SwiftFileBackend extends FileBackendStore {
         *
         * @param $container string Container name
         * @return CF_Container
+        * @throws InvalidResponseException
         */
        protected function createContainer( $container ) {
                $conn = $this->getConnection(); // Swift proxy connection
@@ -895,6 +913,7 @@ class SwiftFileBackend extends FileBackendStore {
         *
         * @param $container string Container name
         * @return void
+        * @throws InvalidResponseException
         */
        protected function deleteContainer( $container ) {
                $conn = $this->getConnection(); // Swift proxy connection
@@ -902,6 +921,28 @@ class SwiftFileBackend extends FileBackendStore {
                unset( $this->connContainers[$container] ); // purge cache
        }
 
+       /**
+        * @see FileBackendStore::doPrimeContainerCache()
+        * @return void
+        */
+       protected function doPrimeContainerCache( array $containerInfo ) {
+               try {
+                       $conn = $this->getConnection(); // Swift proxy connection
+                       foreach ( $containerInfo as $container => $info ) {
+                               $this->connContainers[$container] = new CF_Container(
+                                       $conn->cfs_auth,
+                                       $conn->cfs_http,
+                                       $container,
+                                       $info['count'],
+                                       $info['bytes']
+                               );
+                       }
+               } catch ( InvalidResponseException $e ) {
+               } catch ( Exception $e ) { // some other exception?
+                       $this->logException( $e, __METHOD__, array() );
+               }
+       }
+
        /**
         * Log an unexpected exception for this backend
         *
@@ -1007,7 +1048,11 @@ abstract class SwiftFileBackendList implements Iterator {
         * @return bool
         */
        public function valid() {
-               return ( current( $this->bufferIter ) !== false ); // no paths can have this value
+               if ( $this->bufferIter === null ) {
+                       return false; // some failure?
+               } else {
+                       return ( current( $this->bufferIter ) !== false ); // no paths can have this value
+               }
        }
 
        /**
@@ -1018,7 +1063,7 @@ abstract class SwiftFileBackendList implements Iterator {
         * @param $after string|null
         * @param $limit integer
         * @param $params Array
-        * @return Traversable|Array|null
+        * @return Traversable|Array|null Returns null on failure
         */
        abstract protected function pageFromList( $container, $dir, &$after, $limit, array $params );
 }
@@ -1037,7 +1082,7 @@ class SwiftFileBackendDirList extends SwiftFileBackendList {
 
        /**
         * @see SwiftFileBackendList::pageFromList()
-        * @return Array
+        * @return Array|null
         */
        protected function pageFromList( $container, $dir, &$after, $limit, array $params ) {
                return $this->backend->getDirListPageInternal( $container, $dir, $after, $limit, $params );
@@ -1058,7 +1103,7 @@ class SwiftFileBackendFileList extends SwiftFileBackendList {
 
        /**
         * @see SwiftFileBackendList::pageFromList()
-        * @return Array
+        * @return Array|null
         */
        protected function pageFromList( $container, $dir, &$after, $limit, array $params ) {
                return $this->backend->getFileListPageInternal( $container, $dir, $after, $limit, $params );