ILBFactory.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. <?php
  2. /**
  3. * Generator and manager of database load balancing objects
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. * http://www.gnu.org/copyleft/gpl.html
  19. *
  20. * @file
  21. * @ingroup Database
  22. */
  23. namespace Wikimedia\Rdbms;
  24. use InvalidArgumentException;
  25. /**
  26. * An interface for generating database load balancers
  27. * @ingroup Database
  28. * @since 1.28
  29. */
  30. interface ILBFactory {
  31. /** @var int Don't save DB positions at all */
  32. const SHUTDOWN_NO_CHRONPROT = 0; // don't save DB positions at all
  33. /** @var int Save DB positions, but don't wait on remote DCs */
  34. const SHUTDOWN_CHRONPROT_ASYNC = 1;
  35. /** @var int Save DB positions, waiting on all DCs */
  36. const SHUTDOWN_CHRONPROT_SYNC = 2;
  37. /** @var string Default main LB cluster name (do not change this) */
  38. const CLUSTER_MAIN_DEFAULT = 'DEFAULT';
  39. /**
  40. * Construct a manager of ILoadBalancer objects
  41. *
  42. * Sub-classes will extend the required keys in $conf with additional parameters
  43. *
  44. * @param array $conf Array with keys:
  45. * - localDomain: A DatabaseDomain or domain ID string.
  46. * - readOnlyReason: Reason the master DB is read-only if so [optional]
  47. * - srvCache: BagOStuff object for server cache [optional]
  48. * - memStash: BagOStuff object for cross-datacenter memory storage [optional]
  49. * - wanCache: WANObjectCache object [optional]
  50. * - hostname: The name of the current server [optional]
  51. * - cliMode: Whether the execution context is a CLI script. [optional]
  52. * - maxLag: Try to avoid DB replicas with lag above this many seconds [optional]
  53. * - profiler: Class name or instance with profileIn()/profileOut() methods. [optional]
  54. * - trxProfiler: TransactionProfiler instance. [optional]
  55. * - replLogger: PSR-3 logger instance. [optional]
  56. * - connLogger: PSR-3 logger instance. [optional]
  57. * - queryLogger: PSR-3 logger instance. [optional]
  58. * - perfLogger: PSR-3 logger instance. [optional]
  59. * - errorLogger: Callback that takes an Exception and logs it. [optional]
  60. * - deprecationLogger: Callback to log a deprecation warning. [optional]
  61. * - secret: Secret string to use for HMAC hashing [optional]
  62. * @throws InvalidArgumentException
  63. */
  64. public function __construct( array $conf );
  65. /**
  66. * Disables all load balancers. All connections are closed, and any attempt to
  67. * open a new connection will result in a DBAccessError.
  68. * @see ILoadBalancer::disable()
  69. */
  70. public function destroy();
  71. /**
  72. * Get the local (and default) database domain ID of connection handles
  73. *
  74. * @see DatabaseDomain
  75. * @return string Database domain ID; this specifies DB name, schema, and table prefix
  76. * @since 1.32
  77. */
  78. public function getLocalDomainID();
  79. /**
  80. * @param DatabaseDomain|string|bool $domain Database domain
  81. * @return string Value of $domain if provided or the local domain otherwise
  82. * @since 1.32
  83. */
  84. public function resolveDomainID( $domain );
  85. /**
  86. * Close all connection and redefine the local domain for testing or schema creation
  87. *
  88. * @param DatabaseDomain|string $domain
  89. * @since 1.33
  90. */
  91. public function redefineLocalDomain( $domain );
  92. /**
  93. * Create a new load balancer object. The resulting object will be untracked,
  94. * not chronology-protected, and the caller is responsible for cleaning it up.
  95. *
  96. * This method is for only advanced usage and callers should almost always use
  97. * getMainLB() instead. This method can be useful when a table is used as a key/value
  98. * store. In that cases, one might want to query it in autocommit mode (DBO_TRX off)
  99. * but still use DBO_TRX transaction rounds on other tables.
  100. *
  101. * @param bool|string $domain Domain ID, or false for the current domain
  102. * @param int|null $owner Owner ID of the new instance (e.g. this LBFactory ID)
  103. * @return ILoadBalancer
  104. */
  105. public function newMainLB( $domain = false, $owner = null );
  106. /**
  107. * Get a cached (tracked) load balancer object.
  108. *
  109. * @param bool|string $domain Domain ID, or false for the current domain
  110. * @return ILoadBalancer
  111. */
  112. public function getMainLB( $domain = false );
  113. /**
  114. * Create a new load balancer for external storage. The resulting object will be
  115. * untracked, not chronology-protected, and the caller is responsible for cleaning it up.
  116. *
  117. * This method is for only advanced usage and callers should almost always use
  118. * getExternalLB() instead. This method can be useful when a table is used as a
  119. * key/value store. In that cases, one might want to query it in autocommit mode
  120. * (DBO_TRX off) but still use DBO_TRX transaction rounds on other tables.
  121. *
  122. * @param string $cluster External storage cluster name
  123. * @param int|null $owner Owner ID of the new instance (e.g. this LBFactory ID)
  124. * @return ILoadBalancer
  125. */
  126. public function newExternalLB( $cluster, $owner = null );
  127. /**
  128. * Get a cached (tracked) load balancer for external storage
  129. *
  130. * @param string $cluster External storage cluster name
  131. * @return ILoadBalancer
  132. */
  133. public function getExternalLB( $cluster );
  134. /**
  135. * Get cached (tracked) load balancers for all main database clusters
  136. *
  137. * The default cluster name is ILoadBalancer::CLUSTER_MAIN_DEFAULT
  138. *
  139. * @return ILoadBalancer[] Map of (cluster name => ILoadBalancer)
  140. * @since 1.29
  141. */
  142. public function getAllMainLBs();
  143. /**
  144. * Get cached (tracked) load balancers for all external database clusters
  145. *
  146. * @return ILoadBalancer[] Map of (cluster name => ILoadBalancer)
  147. * @since 1.29
  148. */
  149. public function getAllExternalLBs();
  150. /**
  151. * Execute a function for each currently tracked (instantiated) load balancer
  152. *
  153. * The callback is called with the load balancer as the first parameter,
  154. * and $params passed as the subsequent parameters.
  155. *
  156. * @param callable $callback
  157. * @param array $params
  158. */
  159. public function forEachLB( $callback, array $params = [] );
  160. /**
  161. * Prepare all currently tracked (instantiated) load balancers for shutdown
  162. *
  163. * @param int $mode One of the class SHUTDOWN_* constants
  164. * @param callable|null $workCallback Work to mask ChronologyProtector writes
  165. * @param int|null &$cpIndex Position key write counter for ChronologyProtector
  166. * @param string|null &$cpClientId Client ID hash for ChronologyProtector
  167. */
  168. public function shutdown(
  169. $mode = self::SHUTDOWN_CHRONPROT_SYNC,
  170. callable $workCallback = null,
  171. &$cpIndex = null,
  172. &$cpClientId = null
  173. );
  174. /**
  175. * Commit all replica DB transactions so as to flush any REPEATABLE-READ or SSI snapshot
  176. *
  177. * This is useful for getting rid of stale data from an implicit transaction round
  178. *
  179. * @param string $fname Caller name
  180. */
  181. public function flushReplicaSnapshots( $fname = __METHOD__ );
  182. /**
  183. * Commit open transactions on all connections. This is useful for two main cases:
  184. * - a) To commit changes to the masters.
  185. * - b) To release the snapshot on all connections, master and replica DBs.
  186. * @param string $fname Caller name
  187. * @param array $options Options map:
  188. * - maxWriteDuration: abort if more than this much time was spent in write queries
  189. */
  190. public function commitAll( $fname = __METHOD__, array $options = [] );
  191. /**
  192. * Flush any master transaction snapshots and set DBO_TRX (if DBO_DEFAULT is set)
  193. *
  194. * The DBO_TRX setting will be reverted to the default in each of these methods:
  195. * - commitMasterChanges()
  196. * - rollbackMasterChanges()
  197. * - commitAll()
  198. *
  199. * This allows for custom transaction rounds from any outer transaction scope.
  200. *
  201. * @param string $fname
  202. * @throws DBTransactionError
  203. */
  204. public function beginMasterChanges( $fname = __METHOD__ );
  205. /**
  206. * Commit changes and clear view snapshots on all master connections
  207. * @param string $fname Caller name
  208. * @param array $options Options map:
  209. * - maxWriteDuration: abort if more than this much time was spent in write queries
  210. * @throws DBTransactionError
  211. */
  212. public function commitMasterChanges( $fname = __METHOD__, array $options = [] );
  213. /**
  214. * Rollback changes on all master connections
  215. * @param string $fname Caller name
  216. */
  217. public function rollbackMasterChanges( $fname = __METHOD__ );
  218. /**
  219. * Check if an explicit transaction round is active
  220. * @return bool
  221. * @since 1.29
  222. */
  223. public function hasTransactionRound();
  224. /**
  225. * Check if transaction rounds can be started, committed, or rolled back right now
  226. *
  227. * This can be used as a recusion guard to avoid exceptions in transaction callbacks
  228. *
  229. * @return bool
  230. * @since 1.32
  231. */
  232. public function isReadyForRoundOperations();
  233. /**
  234. * Determine if any master connection has pending changes
  235. * @return bool
  236. */
  237. public function hasMasterChanges();
  238. /**
  239. * Detemine if any lagged replica DB connection was used
  240. * @return bool
  241. */
  242. public function laggedReplicaUsed();
  243. /**
  244. * Determine if any master connection has pending/written changes from this request
  245. * @param float|null $age How many seconds ago is "recent" [defaults to LB lag wait timeout]
  246. * @return bool
  247. */
  248. public function hasOrMadeRecentMasterChanges( $age = null );
  249. /**
  250. * Waits for the replica DBs to catch up to the current master position
  251. *
  252. * Use this when updating very large numbers of rows, as in maintenance scripts,
  253. * to avoid causing too much lag. Of course, this is a no-op if there are no replica DBs.
  254. *
  255. * By default this waits on all DB clusters actually used in this request.
  256. * This makes sense when lag being waiting on is caused by the code that does this check.
  257. * In that case, setting "ifWritesSince" can avoid the overhead of waiting for clusters
  258. * that were not changed since the last wait check. To forcefully wait on a specific cluster
  259. * for a given domain, use the 'domain' parameter. To forcefully wait on an "external" cluster,
  260. * use the "cluster" parameter.
  261. *
  262. * Never call this function after a large DB write that is *still* in a transaction.
  263. * It only makes sense to call this after the possible lag inducing changes were committed.
  264. *
  265. * @param array $opts Optional fields that include:
  266. * - domain : wait on the load balancer DBs that handles the given domain ID
  267. * - cluster : wait on the given external load balancer DBs
  268. * - timeout : Max wait time. Default: 60 seconds for CLI, 1 second for web.
  269. * - ifWritesSince: Only wait if writes were done since this UNIX timestamp
  270. * @return bool True on success, false if a timeout or error occurred while waiting
  271. */
  272. public function waitForReplication( array $opts = [] );
  273. /**
  274. * Add a callback to be run in every call to waitForReplication() before waiting
  275. *
  276. * Callbacks must clear any transactions that they start
  277. *
  278. * @param string $name Callback name
  279. * @param callable|null $callback Use null to unset a callback
  280. */
  281. public function setWaitForReplicationListener( $name, callable $callback = null );
  282. /**
  283. * Get a token asserting that no transaction writes are active
  284. *
  285. * @param string $fname Caller name (e.g. __METHOD__)
  286. * @return mixed A value to pass to commitAndWaitForReplication()
  287. */
  288. public function getEmptyTransactionTicket( $fname );
  289. /**
  290. * Convenience method for safely running commitMasterChanges()/waitForReplication()
  291. *
  292. * This will commit and wait unless $ticket indicates it is unsafe to do so
  293. *
  294. * @param string $fname Caller name (e.g. __METHOD__)
  295. * @param mixed $ticket Result of getEmptyTransactionTicket()
  296. * @param array $opts Options to waitForReplication()
  297. * @return bool True if the wait was successful, false on timeout
  298. */
  299. public function commitAndWaitForReplication( $fname, $ticket, array $opts = [] );
  300. /**
  301. * @param string $dbName DB master name (e.g. "db1052")
  302. * @return float|bool UNIX timestamp when client last touched the DB or false if not recent
  303. */
  304. public function getChronologyProtectorTouched( $dbName );
  305. /**
  306. * Disable the ChronologyProtector for all load balancers
  307. *
  308. * This can be called at the start of special API entry points
  309. */
  310. public function disableChronologyProtection();
  311. /**
  312. * Set a new table prefix for the existing local domain ID for testing
  313. *
  314. * @param string $prefix
  315. * @since 1.33
  316. */
  317. public function setLocalDomainPrefix( $prefix );
  318. /**
  319. * Close all open database connections on all open load balancers.
  320. */
  321. public function closeAll();
  322. /**
  323. * @param string $agent Agent name for query profiling
  324. */
  325. public function setAgentName( $agent );
  326. /**
  327. * Append ?cpPosIndex parameter to a URL for ChronologyProtector purposes if needed
  328. *
  329. * Note that unlike cookies, this works across domains
  330. *
  331. * @param string $url
  332. * @param int $index Write counter index
  333. * @return string
  334. */
  335. public function appendShutdownCPIndexAsQuery( $url, $index );
  336. /**
  337. * Get the client ID of the ChronologyProtector instance
  338. *
  339. * @return string Client ID
  340. * @since 1.34
  341. */
  342. public function getChronologyProtectorClientId();
  343. /**
  344. * @param array $info Map of fields, including:
  345. * - IPAddress : IP address
  346. * - UserAgent : User-Agent HTTP header
  347. * - ChronologyProtection : cookie/header value specifying ChronologyProtector usage
  348. * - ChronologyPositionIndex: timestamp used to get up-to-date DB positions for the agent
  349. */
  350. public function setRequestInfo( array $info );
  351. /**
  352. * Make certain table names use their own database, schema, and table prefix
  353. * when passed into SQL queries pre-escaped and without a qualified database name
  354. *
  355. * For example, "user" can be converted to "myschema.mydbname.user" for convenience.
  356. * Appearances like `user`, somedb.user, somedb.someschema.user will used literally.
  357. *
  358. * Calling this twice will completely clear any old table aliases. Also, note that
  359. * callers are responsible for making sure the schemas and databases actually exist.
  360. *
  361. * @param array[] $aliases Map of (table => (dbname, schema, prefix) map)
  362. * @since 1.31
  363. */
  364. public function setTableAliases( array $aliases );
  365. /**
  366. * Convert certain index names to alternative names before querying the DB
  367. *
  368. * Note that this applies to indexes regardless of the table they belong to.
  369. *
  370. * This can be employed when an index was renamed X => Y in code, but the new Y-named
  371. * indexes were not yet built on all DBs. After all the Y-named ones are added by the DBA,
  372. * the aliases can be removed, and then the old X-named indexes dropped.
  373. *
  374. * @param string[] $aliases
  375. * @since 1.31
  376. */
  377. public function setIndexAliases( array $aliases );
  378. }