Add ExternalStoreMedium::isReadOnly() method
authorAaron Schulz <aschulz@wikimedia.org>
Thu, 22 Feb 2018 08:27:14 +0000 (00:27 -0800)
committerAaron Schulz <aschulz@wikimedia.org>
Thu, 22 Feb 2018 16:15:20 +0000 (08:15 -0800)
Use this to abort out of store() calls early

Bug: T187942
Change-Id: I9334d36e8bc3e4589775471eee03be4f4a3119a3

includes/externalstore/ExternalStore.php
includes/externalstore/ExternalStoreDB.php
includes/externalstore/ExternalStoreHttp.php
includes/externalstore/ExternalStoreMedium.php
includes/externalstore/ExternalStoreMwstore.php

index 3beab29..2ac6f7e 100644 (file)
@@ -92,6 +92,7 @@ class ExternalStore {
         * @param array $urls The URLs of the text to get
         * @return array Map from url to its data.  Data is either string when found
         *     or false on failure.
+        * @throws MWException
         */
        public static function batchFetchFromURLs( array $urls ) {
                $batches = [];
@@ -190,19 +191,25 @@ class ExternalStore {
                        if ( $store === false ) {
                                throw new MWException( "Invalid external storage protocol - $storeUrl" );
                        }
+
                        try {
-                               $url = $store->store( $path, $data ); // Try to save the object
+                               if ( $store->isReadOnly( $path ) ) {
+                                       $msg = 'read only';
+                               } else {
+                                       $url = $store->store( $path, $data );
+                                       if ( strlen( $url ) ) {
+                                               return $url; // a store accepted the write; done!
+                                       }
+                                       $msg = 'operation failed';
+                               }
                        } catch ( Exception $error ) {
-                               $url = false;
-                       }
-                       if ( strlen( $url ) ) {
-                               return $url; // Done!
-                       } else {
-                               unset( $tryStores[$index] ); // Don't try this one again!
-                               $tryStores = array_values( $tryStores ); // Must have consecutive keys
-                               wfDebugLog( 'ExternalStorage',
-                                       "Unable to store text to external storage $storeUrl" );
+                               $msg = 'caught exception';
                        }
+
+                       unset( $tryStores[$index] ); // Don't try this one again!
+                       $tryStores = array_values( $tryStores ); // Must have consecutive keys
+                       wfDebugLog( 'ExternalStorage',
+                               "Unable to store text to external storage $storeUrl ($msg)" );
                }
                // All stores failed
                if ( $error ) {
index e5d36e1..b43bd21 100644 (file)
@@ -103,6 +103,10 @@ class ExternalStoreDB extends ExternalStoreMedium {
                return "DB://$location/$id";
        }
 
+       public function isReadOnly( $location ) {
+               return ( $this->getLoadBalancer( $location )->getReadOnlyReason() !== false );
+       }
+
        /**
         * Get a LoadBalancer for the specified cluster
         *
index 8e1e49f..3d812c9 100644 (file)
  * @ingroup ExternalStorage
  */
 class ExternalStoreHttp extends ExternalStoreMedium {
-       /**
-        * @see ExternalStoreMedium::fetchFromURL()
-        * @param string $url
-        * @return string|bool
-        * @throws MWException
-        */
        public function fetchFromURL( $url ) {
                return Http::get( $url, [], __METHOD__ );
        }
 
-       /**
-        * @see ExternalStoreMedium::store()
-        * @param string $cluster
-        * @param string $data
-        * @return string|bool
-        * @throws MWException
-        */
-       public function store( $cluster, $data ) {
+       public function store( $location, $data ) {
                throw new MWException( "ExternalStoreHttp is read-only and does not support store()." );
        }
+
+       public function isReadOnly( $location ) {
+               return true;
+       }
 }
index 6cfa083..f1eaa93 100644 (file)
@@ -76,4 +76,15 @@ abstract class ExternalStoreMedium {
         * @throws MWException
         */
        abstract public function store( $location, $data );
+
+       /**
+        * Check if a given location is read-only
+        *
+        * @param string $location The location name
+        * @return bool Whether this location is read-only
+        * @since 1.31
+        */
+       public function isReadOnly( $location ) {
+               return false;
+       }
 }
index 5395f56..0c6d022 100644 (file)
@@ -73,13 +73,6 @@ class ExternalStoreMwstore extends ExternalStoreMedium {
                return $blobs;
        }
 
-       /**
-        * @see ExternalStoreMedium::store()
-        * @param string $backend
-        * @param string $data
-        * @return string|bool
-        * @throws MWException
-        */
        public function store( $backend, $data ) {
                $be = FileBackendGroup::singleton()->get( $backend );
                if ( $be instanceof FileBackend ) {
@@ -103,4 +96,10 @@ class ExternalStoreMwstore extends ExternalStoreMedium {
 
                return false;
        }
+
+       public function isReadOnly( $backend ) {
+               $be = FileBackendGroup::singleton()->get( $backend );
+
+               return $be ? $be->isReadOnly() : false;
+       }
 }