02972e5dc71d645b6d724f8ca5a2a69f48fd2020
[lhc/web/wiklou.git] / includes / libs / rdbms / connectionmanager / SessionConsistentConnectionManager.php
1 <?php
2
3 namespace Wikimedia\Rdbms;
4
5 use Database;
6 use DBConnRef;
7
8 /**
9 * Database connection manager.
10 *
11 * This manages access to master and slave databases. It also manages state that indicates whether
12 * the slave databases are possibly outdated after a write operation, and thus the master database
13 * should be used for subsequent read operations.
14 *
15 * @note: Services that access overlapping sets of database tables, or interact with logically
16 * related sets of data in the database, should share a SessionConsistentConnectionManager.
17 * Services accessing unrelated sets of information may prefer to not share a
18 * SessionConsistentConnectionManager, so they can still perform read operations against slave
19 * databases after a (unrelated, per the assumption) write operation to the master database.
20 * Generally, sharing a SessionConsistentConnectionManager improves consistency (by avoiding race
21 * conditions due to replication lag), but can reduce performance (by directing more read
22 * operations to the master database server).
23 *
24 * @since 1.29
25 *
26 * @license GPL-2.0+
27 * @author Daniel Kinzler
28 * @author Addshore
29 */
30 class SessionConsistentConnectionManager extends ConnectionManager {
31
32 /**
33 * @var bool
34 */
35 private $forceWriteConnection = false;
36
37 /**
38 * Forces all future calls to getReadConnection() to return a write connection.
39 * Use this before performing read operations that are critical for a future update.
40 * Calling beginAtomicSection() implies a call to prepareForUpdates().
41 *
42 * @since 1.29
43 */
44 public function prepareForUpdates() {
45 $this->forceWriteConnection = true;
46 }
47
48 /**
49 * @since 1.29
50 *
51 * @param string[]|null $groups
52 *
53 * @return Database
54 */
55 public function getReadConnection( array $groups = null ) {
56 if ( $this->forceWriteConnection ) {
57 return parent::getWriteConnection();
58 }
59
60 return parent::getReadConnection( $groups );
61 }
62
63 /**
64 * @since 1.29
65 *
66 * @return Database
67 */
68 public function getWriteConnection() {
69 $this->prepareForUpdates();
70 return parent::getWriteConnection();
71 }
72
73 /**
74 * @since 1.29
75 *
76 * @param string[]|null $groups
77 *
78 * @return DBConnRef
79 */
80 public function getReadConnectionRef( array $groups = null ) {
81 if ( $this->forceWriteConnection ) {
82 return parent::getWriteConnectionRef();
83 }
84
85 return parent::getReadConnectionRef( $groups );
86 }
87
88 /**
89 * @since 1.29
90 *
91 * @return DBConnRef
92 */
93 public function getWriteConnectionRef() {
94 $this->prepareForUpdates();
95 return parent::getWriteConnectionRef();
96 }
97
98 /**
99 * Begins an atomic section and returns a database connection to the master DB, for updating.
100 *
101 * @since 1.29
102 *
103 * @note: This causes all future calls to getReadConnection() to return a connection
104 * to the master DB, even after commitAtomicSection() or rollbackAtomicSection() have
105 * been called.
106 *
107 * @param string $fname
108 *
109 * @return Database
110 */
111 public function beginAtomicSection( $fname ) {
112 // Once we have written to master, do not read from replica.
113 $this->prepareForUpdates();
114
115 return parent::beginAtomicSection( $fname );
116 }
117
118 }