Use normal, more patient, connection attempts for master connection here
[lhc/web/wiklou.git] / includes / ExternalStoreDB.php
1 <?php
2
3 /**
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
7 * of the script.
8 *
9 * Associative array of LoadBalancer objects, indexed by cluster name.
10 */
11 global $wgExternalLoadBalancers;
12 $wgExternalLoadBalancers = array();
13
14 /**
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.
19 */
20 global $wgExternalBlobCache;
21 $wgExternalBlobCache = array();
22
23 /**
24 * DB accessable external objects
25 * @ingroup ExternalStorage
26 */
27 class ExternalStoreDB {
28
29 /** @todo Document.*/
30 function &getLoadBalancer( $cluster ) {
31 return wfGetLBFactory()->getExternalLB( $cluster );
32 }
33
34 /** @todo Document.*/
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 );
39 }
40
41 /** @todo Document.*/
42 function &getMaster( $cluster, $retry = true ) {
43 $lb =& $this->getLoadBalancer( $cluster );
44 return $lb->getConnection( DB_MASTER, array(), false, false, LoadBalancer::GRACEFUL );
45 }
46
47 /** @todo Document.*/
48 function getTable( &$db ) {
49 $table = $db->getLBInfo( 'blobs table' );
50 if ( is_null( $table ) ) {
51 $table = 'blobs';
52 }
53 return $table;
54 }
55
56 /**
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.
59 */
60 function fetchFromURL( $url ) {
61 $path = explode( '/', $url );
62 $cluster = $path[2];
63 $id = $path[3];
64 if ( isset( $path[4] ) ) {
65 $itemID = $path[4];
66 } else {
67 $itemID = false;
68 }
69
70 $ret =& $this->fetchBlob( $cluster, $id, $itemID );
71
72 if ( $itemID !== false && $ret !== false ) {
73 return $ret->getItem( $itemID );
74 }
75 return $ret;
76 }
77
78 /**
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.
82 * @param $cluster
83 * @param $id
84 * @param $itemID
85 * @return mixed
86 * @private
87 */
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];
94 }
95
96 wfDebug( "ExternalStoreDB::fetchBlob cache miss on $cacheID\n" );
97
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" );
102 // Try the master
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" );
107 }
108 }
109 if( $itemID !== false && $ret !== false ) {
110 // Unserialise object; caller extracts item
111 $ret = unserialize( $ret );
112 }
113
114 $wgExternalBlobCache = array( $cacheID => &$ret );
115 return $ret;
116 }
117
118 /**
119 * Insert a data item into a given cluster
120 *
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
124 * @return string URL
125 */
126 function store( $cluster, $data, $retry = true ) {
127 if( !$dbw = $this->getMaster( $cluster, $retry ) ) {
128 return false; // failed, maybe another cluster is up...
129 }
130 $id = $dbw->nextSequenceValue( 'blob_blob_id_seq' );
131 $dbw->insert( $this->getTable( $dbw ),
132 array( 'blob_id' => $id, 'blob_text' => $data ),
133 __METHOD__ );
134 $id = $dbw->insertId();
135 if ( $dbw->getFlag( DBO_TRX ) ) {
136 $dbw->immediateCommit();
137 }
138 return "DB://$cluster/$id";
139 }
140 }