r41235 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r41234‎ | r41235 | r41236 >
Date:18:38, 24 September 2008
Author:aaron
Status:old
Tags:
Comment:
Add insertRandom() with failover back. Currently not used just yet.
Modified paths:
  • /trunk/phase3/includes/ExternalStore.php (modified) (history)
  • /trunk/phase3/includes/ExternalStoreDB.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/ExternalStore.php
@@ -14,7 +14,7 @@
1515 */
1616 class ExternalStore {
1717 /* Fetch data from given URL */
18 - static function fetchFromURL($url) {
 18+ static function fetchFromURL( $url ) {
1919 global $wgExternalStores;
2020
2121 if( !$wgExternalStores )
@@ -44,7 +44,7 @@
4545
4646 $class = 'ExternalStore' . ucfirst( $proto );
4747 /* Any custom modules should be added to $wgAutoLoadClasses for on-demand loading */
48 - if( !class_exists( $class ) ){
 48+ if( !class_exists( $class ) ) {
4949 return false;
5050 }
5151
@@ -66,4 +66,47 @@
6767 return $store->store( $params, $data );
6868 }
6969 }
 70+
 71+ /**
 72+ * Like insert() above, but does more of the work for us.
 73+ * This function does not need a url param, it builds it by
 74+ * itself. It also fails-over to the next possible clusters.
 75+ *
 76+ * @param string $data
 77+ * Returns the URL of the stored data item, or false on error
 78+ */
 79+ public static function randomInsert( $data ) {
 80+ global $wgDefaultExternalStore;
 81+ $tryStores = (array)$wgDefaultExternalStore;
 82+ $error = false;
 83+ while ( count( $tryStores ) > 0 ) {
 84+ $index = mt_rand(0, count( $tryStores ) - 1);
 85+ $storeUrl = $tryStores[$index];
 86+ wfDebug( __METHOD__.": trying $storeUrl\n" );
 87+ list( $proto, $params ) = explode( '://', $storeUrl, 2 );
 88+ $store = self::getStoreObject( $proto );
 89+ if ( $store === false ) {
 90+ throw new MWException( "Invalid external storage protocol - $storeUrl" );
 91+ }
 92+ try {
 93+ $url = $store->store( $params, $data ); // Try to save the object
 94+ } catch ( DBConnectionError $error ) {
 95+ $url = false;
 96+ }
 97+ if ( $url ) {
 98+ return $url; // Done!
 99+ } else {
 100+ unset( $tryStores[$index] ); // Don't try this one again!
 101+ $tryStores = array_values( $tryStores ); // Must have consecutive keys
 102+ wfDebugLog( 'ExternalStorage', "Unable to store text to external storage $storeUrl" );
 103+ }
 104+ }
 105+ // All stores failed
 106+ if ( $error ) {
 107+ // Rethrow the last connection error
 108+ throw $error;
 109+ } else {
 110+ throw new MWException( "Unable to store text to external storage" );
 111+ }
 112+ }
70113 }
Index: trunk/phase3/includes/ExternalStoreDB.php
@@ -56,7 +56,7 @@
5757 * Fetch data from given URL
5858 * @param string $url An url of the form DB://cluster/id or DB://cluster/id/itemid for concatened storage.
5959 */
60 - function fetchFromURL($url) {
 60+ function fetchFromURL( $url ) {
6161 $path = explode( '/', $url );
6262 $cluster = $path[2];
6363 $id = $path[3];
@@ -122,12 +122,14 @@
123123 * @return string URL
124124 */
125125 function store( $cluster, $data ) {
126 - $fname = 'ExternalStoreDB::store';
127 -
128 - $dbw =& $this->getMaster( $cluster );
129 -
 126+ $dbw = $this->getMaster( $cluster );
 127+ if( !$dbw ) {
 128+ return false;
 129+ }
130130 $id = $dbw->nextSequenceValue( 'blob_blob_id_seq' );
131 - $dbw->insert( $this->getTable( $dbw ), array( 'blob_id' => $id, 'blob_text' => $data ), $fname );
 131+ $dbw->insert( $this->getTable( $dbw ),
 132+ array( 'blob_id' => $id, 'blob_text' => $data ),
 133+ __METHOD__ );
132134 $id = $dbw->insertId();
133135 if ( $dbw->getFlag( DBO_TRX ) ) {
134136 $dbw->immediateCommit();