Merge "Remove unused 'XMPGetInfo' and 'XMPGetResults' hooks"
[lhc/web/wiklou.git] / includes / filebackend / SwiftFileBackend.php
index 4fde124..0dcaf2a 100644 (file)
@@ -128,7 +128,9 @@ class SwiftFileBackend extends FileBackendStore {
                // HTTP helper client
                $this->http = new MultiHttpClient( array() );
                // Cache container information to mask latency
-               $this->memCache = wfGetMainCache();
+               if ( isset( $config['wanCache'] ) && $config['wanCache'] instanceof WANObjectCache ) {
+                       $this->memCache = $config['wanCache'];
+               }
                // Process cache for container info
                $this->containerStatCache = new ProcessCacheLRU( 300 );
                // Cache auth token information to avoid RTTs
@@ -235,16 +237,16 @@ class SwiftFileBackend extends FileBackendStore {
                        'body' => $params['content']
                ) );
 
-               $be = $this;
+               $that = $this;
                $method = __METHOD__;
-               $handler = function ( array $request, Status $status ) use ( $be, $method, $params ) {
+               $handler = function ( array $request, Status $status ) use ( $that, $method, $params ) {
                        list( $rcode, $rdesc, $rhdrs, $rbody, $rerr ) = $request['response'];
                        if ( $rcode === 201 ) {
                                // good
                        } elseif ( $rcode === 412 ) {
                                $status->fatal( 'backend-fail-contenttype', $params['dst'] );
                        } else {
-                               $be->onError( $status, $method, $params, $rerr, $rcode, $rdesc );
+                               $that->onError( $status, $method, $params, $rerr, $rcode, $rdesc );
                        }
                };
 
@@ -298,16 +300,16 @@ class SwiftFileBackend extends FileBackendStore {
                        'body' => $handle // resource
                ) );
 
-               $be = $this;
+               $that = $this;
                $method = __METHOD__;
-               $handler = function ( array $request, Status $status ) use ( $be, $method, $params ) {
+               $handler = function ( array $request, Status $status ) use ( $that, $method, $params ) {
                        list( $rcode, $rdesc, $rhdrs, $rbody, $rerr ) = $request['response'];
                        if ( $rcode === 201 ) {
                                // good
                        } elseif ( $rcode === 412 ) {
                                $status->fatal( 'backend-fail-contenttype', $params['dst'] );
                        } else {
-                               $be->onError( $status, $method, $params, $rerr, $rcode, $rdesc );
+                               $that->onError( $status, $method, $params, $rerr, $rcode, $rdesc );
                        }
                };
 
@@ -347,16 +349,16 @@ class SwiftFileBackend extends FileBackendStore {
                        ) + $this->sanitizeHdrs( $params ), // extra headers merged into object
                ) );
 
-               $be = $this;
+               $that = $this;
                $method = __METHOD__;
-               $handler = function ( array $request, Status $status ) use ( $be, $method, $params ) {
+               $handler = function ( array $request, Status $status ) use ( $that, $method, $params ) {
                        list( $rcode, $rdesc, $rhdrs, $rbody, $rerr ) = $request['response'];
                        if ( $rcode === 201 ) {
                                // good
                        } elseif ( $rcode === 404 ) {
                                $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
                        } else {
-                               $be->onError( $status, $method, $params, $rerr, $rcode, $rdesc );
+                               $that->onError( $status, $method, $params, $rerr, $rcode, $rdesc );
                        }
                };
 
@@ -405,9 +407,9 @@ class SwiftFileBackend extends FileBackendStore {
                        );
                }
 
-               $be = $this;
+               $that = $this;
                $method = __METHOD__;
-               $handler = function ( array $request, Status $status ) use ( $be, $method, $params ) {
+               $handler = function ( array $request, Status $status ) use ( $that, $method, $params ) {
                        list( $rcode, $rdesc, $rhdrs, $rbody, $rerr ) = $request['response'];
                        if ( $request['method'] === 'PUT' && $rcode === 201 ) {
                                // good
@@ -416,7 +418,7 @@ class SwiftFileBackend extends FileBackendStore {
                        } elseif ( $rcode === 404 ) {
                                $status->fatal( 'backend-fail-move', $params['src'], $params['dst'] );
                        } else {
-                               $be->onError( $status, $method, $params, $rerr, $rcode, $rdesc );
+                               $that->onError( $status, $method, $params, $rerr, $rcode, $rdesc );
                        }
                };
 
@@ -446,9 +448,9 @@ class SwiftFileBackend extends FileBackendStore {
                        'headers' => array()
                ) );
 
-               $be = $this;
+               $that = $this;
                $method = __METHOD__;
-               $handler = function ( array $request, Status $status ) use ( $be, $method, $params ) {
+               $handler = function ( array $request, Status $status ) use ( $that, $method, $params ) {
                        list( $rcode, $rdesc, $rhdrs, $rbody, $rerr ) = $request['response'];
                        if ( $rcode === 204 ) {
                                // good
@@ -457,7 +459,7 @@ class SwiftFileBackend extends FileBackendStore {
                                        $status->fatal( 'backend-fail-delete', $params['src'] );
                                }
                        } else {
-                               $be->onError( $status, $method, $params, $rerr, $rcode, $rdesc );
+                               $that->onError( $status, $method, $params, $rerr, $rcode, $rdesc );
                        }
                };
 
@@ -505,16 +507,16 @@ class SwiftFileBackend extends FileBackendStore {
                        'headers' => $metaHdrs + $customHdrs
                ) );
 
-               $be = $this;
+               $that = $this;
                $method = __METHOD__;
-               $handler = function ( array $request, Status $status ) use ( $be, $method, $params ) {
+               $handler = function ( array $request, Status $status ) use ( $that, $method, $params ) {
                        list( $rcode, $rdesc, $rhdrs, $rbody, $rerr ) = $request['response'];
                        if ( $rcode === 202 ) {
                                // good
                        } elseif ( $rcode === 404 ) {
                                $status->fatal( 'backend-fail-describe', $params['src'] );
                        } else {
-                               $be->onError( $status, $method, $params, $rerr, $rcode, $rdesc );
+                               $that->onError( $status, $method, $params, $rerr, $rcode, $rdesc );
                        }
                };
 
@@ -647,7 +649,7 @@ class SwiftFileBackend extends FileBackendStore {
                        $timestamp = new MWTimestamp( $ts );
 
                        return $timestamp->getTimestamp( $format );
-               } catch ( MWException $e ) {
+               } catch ( Exception $e ) {
                        throw new FileBackendError( $e->getMessage() );
                }
        }
@@ -937,6 +939,7 @@ class SwiftFileBackend extends FileBackendStore {
                                        // Convert various random Swift dates to TS_MW
                                        'mtime'  => $this->convertSwiftDate( $object->last_modified, TS_MW ),
                                        'size'   => (int)$object->bytes,
+                                       'sha1'   => null,
                                        // Note: manifiest ETags are not an MD5 of the file
                                        'md5'    => ctype_xdigit( $object->hash ) ? $object->hash : null,
                                        'latest' => false // eventually consistent
@@ -1064,6 +1067,7 @@ class SwiftFileBackend extends FileBackendStore {
                        $tmpFiles[$path] = $tmpFile;
                }
 
+               $isLatest = ( $this->isRGW || !empty( $params['latest'] ) );
                $opts = array( 'maxConnsPerHost' => $params['concurrency'] );
                $reqs = $this->http->runMulti( $reqs, $opts );
                foreach ( $reqs as $path => $op ) {
@@ -1078,6 +1082,10 @@ class SwiftFileBackend extends FileBackendStore {
                                        $this->onError( null, __METHOD__,
                                                array( 'src' => $path ) + $ep, $rerr, $rcode, $rdesc );
                                }
+                               // Set the file stat process cache in passing
+                               $stat = $this->getStatFromHeaders( $rhdrs );
+                               $stat['latest'] = $isLatest;
+                               $this->cheapCache->set( $path, 'stat', $stat );
                        } elseif ( $rcode === 404 ) {
                                $tmpFiles[$path] = false;
                        } else {
@@ -1165,6 +1173,11 @@ class SwiftFileBackend extends FileBackendStore {
                return $hdrs;
        }
 
+       /**
+        * @param FileBackendStoreOpHandle[] $fileOpHandles
+        *
+        * @return Status[]
+        */
        protected function doExecuteOpHandlesInternal( array $fileOpHandles ) {
                $statuses = array();
 
@@ -1510,25 +1523,8 @@ class SwiftFileBackend extends FileBackendStore {
                        if ( $rcode === 200 || $rcode === 204 ) {
                                // Update the object if it is missing some headers
                                $rhdrs = $this->addMissingMetadata( $rhdrs, $path );
-                               // Fetch all of the custom metadata headers
-                               $metadata = array();
-                               foreach ( $rhdrs as $name => $value ) {
-                                       if ( strpos( $name, 'x-object-meta-' ) === 0 ) {
-                                               $metadata[substr( $name, strlen( 'x-object-meta-' ) )] = $value;
-                                       }
-                               }
-                               // Fetch all of the custom raw HTTP headers
-                               $headers = $this->sanitizeHdrs( array( 'headers' => $rhdrs ) );
-                               $stat = array(
-                                       // Convert various random Swift dates to TS_MW
-                                       'mtime' => $this->convertSwiftDate( $rhdrs['last-modified'], TS_MW ),
-                                       // Empty objects actually return no content-length header in Ceph
-                                       'size'  => isset( $rhdrs['content-length'] ) ? (int)$rhdrs['content-length'] : 0,
-                                       'sha1'  => $rhdrs['x-object-meta-sha1base36'],
-                                       // Note: manifiest ETags are not an MD5 of the file
-                                       'md5'   => ctype_xdigit( $rhdrs['etag'] ) ? $rhdrs['etag'] : null,
-                                       'xattr' => array( 'metadata' => $metadata, 'headers' => $headers )
-                               );
+                               // Load the stat array from the headers
+                               $stat = $this->getStatFromHeaders( $rhdrs );
                                if ( $this->isRGW ) {
                                        $stat['latest'] = true; // strong consistency
                                }
@@ -1544,6 +1540,34 @@ class SwiftFileBackend extends FileBackendStore {
                return $stats;
        }
 
+       /**
+        * @param array $rhdrs
+        * @return array
+        */
+       protected function getStatFromHeaders( array $rhdrs ) {
+               // Fetch all of the custom metadata headers
+               $metadata = array();
+               foreach ( $rhdrs as $name => $value ) {
+                       if ( strpos( $name, 'x-object-meta-' ) === 0 ) {
+                               $metadata[substr( $name, strlen( 'x-object-meta-' ) )] = $value;
+                       }
+               }
+               // Fetch all of the custom raw HTTP headers
+               $headers = $this->sanitizeHdrs( array( 'headers' => $rhdrs ) );
+               return array(
+                       // Convert various random Swift dates to TS_MW
+                       'mtime' => $this->convertSwiftDate( $rhdrs['last-modified'], TS_MW ),
+                       // Empty objects actually return no content-length header in Ceph
+                       'size'  => isset( $rhdrs['content-length'] ) ? (int)$rhdrs['content-length'] : 0,
+                       'sha1'  => isset( $rhdrs['x-object-meta-sha1base36'] )
+                               ? $rhdrs['x-object-meta-sha1base36']
+                               : null,
+                       // Note: manifiest ETags are not an MD5 of the file
+                       'md5'   => ctype_xdigit( $rhdrs['etag'] ) ? $rhdrs['etag'] : null,
+                       'xattr' => array( 'metadata' => $metadata, 'headers' => $headers )
+               );
+       }
+
        /**
         * @return array|null Credential map
         */
@@ -1598,7 +1622,7 @@ class SwiftFileBackend extends FileBackendStore {
                        }
                        // Ceph RGW does not use <account> in URLs (OpenStack Swift uses "/v1/<account>")
                        if ( substr( $this->authCreds['storage_url'], -3 ) === '/v1' ) {
-                               $this->isRGW = true; // take advantage of strong consistency
+                               $this->isRGW = true; // take advantage of strong consistency in Ceph
                        }
                }