4 * External database storage will use one (or more) separate connection pools
5 * from what the main wiki uses. If we load many revisions, such as when doing
6 * bulk backups or maintenance, we want to keep them around over the lifetime
9 * Associative array of LoadBalancer objects, indexed by cluster name.
11 global $wgExternalLoadBalancers;
12 $wgExternalLoadBalancers = array();
15 * One-step cache variable to hold base blobs; operations that
16 * pull multiple revisions may often pull multiple times from
17 * the same blob. By keeping the last-used one open, we avoid
18 * redundant unserialization and decompression overhead.
20 global $wgExternalBlobCache;
21 $wgExternalBlobCache = array();
24 * DB accessable external objects
25 * @ingroup ExternalStorage
27 class ExternalStoreDB
{
30 function &getLoadBalancer( $cluster ) {
31 return wfGetLBFactory()->getExternalLB( $cluster );
35 function &getSlave( $cluster ) {
36 $lb =& $this->getLoadBalancer( $cluster );
37 // Make only two connection attempts, since we still have the master to try
38 return $lb->getConnection( DB_SLAVE
, array(), false, 2 );
42 function &getMaster( $cluster, $retry = true ) {
43 $lb =& $this->getLoadBalancer( $cluster );
44 return $lb->getConnection( DB_MASTER
, array(), false, false, LoadBalancer
::GRACEFUL
);
48 function getTable( &$db ) {
49 $table = $db->getLBInfo( 'blobs table' );
50 if ( is_null( $table ) ) {
57 * Fetch data from given URL
58 * @param string $url An url of the form DB://cluster/id or DB://cluster/id/itemid for concatened storage.
60 function fetchFromURL( $url ) {
61 $path = explode( '/', $url );
64 if ( isset( $path[4] ) ) {
70 $ret =& $this->fetchBlob( $cluster, $id, $itemID );
72 if ( $itemID !== false && $ret !== false ) {
73 return $ret->getItem( $itemID );
79 * Fetch a blob item out of the database; a cache of the last-loaded
80 * blob will be kept so that multiple loads out of a multi-item blob
81 * can avoid redundant database access and decompression.
88 function &fetchBlob( $cluster, $id, $itemID ) {
89 global $wgExternalBlobCache;
90 $cacheID = ( $itemID === false ) ?
"$cluster/$id" : "$cluster/$id/";
91 if( isset( $wgExternalBlobCache[$cacheID] ) ) {
92 wfDebug( "ExternalStoreDB::fetchBlob cache hit on $cacheID\n" );
93 return $wgExternalBlobCache[$cacheID];
96 wfDebug( "ExternalStoreDB::fetchBlob cache miss on $cacheID\n" );
98 $dbr =& $this->getSlave( $cluster );
99 $ret = $dbr->selectField( $this->getTable( $dbr ), 'blob_text', array( 'blob_id' => $id ) );
100 if ( $ret === false ) {
101 wfDebugLog( 'ExternalStoreDB', "ExternalStoreDB::fetchBlob master fallback on $cacheID\n" );
103 $dbw =& $this->getMaster( $cluster );
104 $ret = $dbw->selectField( $this->getTable( $dbw ), 'blob_text', array( 'blob_id' => $id ) );
105 if( $ret === false) {
106 wfDebugLog( 'ExternalStoreDB', "ExternalStoreDB::fetchBlob master failed to find $cacheID\n" );
109 if( $itemID !== false && $ret !== false ) {
110 // Unserialise object; caller extracts item
111 $ret = unserialize( $ret );
114 $wgExternalBlobCache = array( $cacheID => &$ret );
119 * Insert a data item into a given cluster
121 * @param $cluster String: the cluster name
122 * @param $data String: the data item
123 * @param $retry bool: allows an extra DB connection retry after 1 second
126 function store( $cluster, $data, $retry = true ) {
127 if( !$dbw = $this->getMaster( $cluster, $retry ) ) {
128 return false; // failed, maybe another cluster is up...
130 $id = $dbw->nextSequenceValue( 'blob_blob_id_seq' );
131 $dbw->insert( $this->getTable( $dbw ),
132 array( 'blob_id' => $id, 'blob_text' => $data ),
134 $id = $dbw->insertId();
135 if ( $dbw->getFlag( DBO_TRX
) ) {
136 $dbw->immediateCommit();
138 return "DB://$cluster/$id";