Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like LoadBalancer often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use LoadBalancer, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
30 | class LoadBalancer { |
||
31 | /** @var array[] Map of (server index => server config array) */ |
||
32 | private $mServers; |
||
33 | /** @var array[] Map of (local/foreignUsed/foreignFree => server index => DatabaseBase array) */ |
||
34 | private $mConns; |
||
35 | /** @var array Map of (server index => weight) */ |
||
36 | private $mLoads; |
||
37 | /** @var array[] Map of (group => server index => weight) */ |
||
38 | private $mGroupLoads; |
||
39 | /** @var bool Whether to disregard slave lag as a factor in slave selection */ |
||
40 | private $mAllowLagged; |
||
41 | /** @var integer Seconds to spend waiting on slave lag to resolve */ |
||
42 | private $mWaitTimeout; |
||
43 | /** @var array LBFactory information */ |
||
44 | private $mParentInfo; |
||
45 | |||
46 | /** @var string The LoadMonitor subclass name */ |
||
47 | private $mLoadMonitorClass; |
||
48 | /** @var LoadMonitor */ |
||
49 | private $mLoadMonitor; |
||
50 | /** @var BagOStuff */ |
||
51 | private $srvCache; |
||
52 | /** @var WANObjectCache */ |
||
53 | private $wanCache; |
||
54 | /** @var TransactionProfiler */ |
||
55 | protected $trxProfiler; |
||
56 | |||
57 | /** @var bool|DatabaseBase Database connection that caused a problem */ |
||
58 | private $mErrorConnection; |
||
59 | /** @var integer The generic (not query grouped) slave index (of $mServers) */ |
||
60 | private $mReadIndex; |
||
61 | /** @var bool|DBMasterPos False if not set */ |
||
62 | private $mWaitForPos; |
||
63 | /** @var bool Whether the generic reader fell back to a lagged slave */ |
||
64 | private $laggedSlaveMode = false; |
||
65 | /** @var bool Whether the generic reader fell back to a lagged slave */ |
||
66 | private $slavesDownMode = false; |
||
67 | /** @var string The last DB selection or connection error */ |
||
68 | private $mLastError = 'Unknown error'; |
||
69 | /** @var string|bool Reason the LB is read-only or false if not */ |
||
70 | private $readOnlyReason = false; |
||
71 | /** @var integer Total connections opened */ |
||
72 | private $connsOpened = 0; |
||
73 | /** @var string|bool String if a requested DBO_TRX transaction round is active */ |
||
74 | private $trxRoundId = false; |
||
75 | /** @var array[] Map of (name => callable) */ |
||
76 | private $trxRecurringCallbacks = []; |
||
77 | |||
78 | /** @var integer Warn when this many connection are held */ |
||
79 | const CONN_HELD_WARN_THRESHOLD = 10; |
||
80 | /** @var integer Default 'max lag' when unspecified */ |
||
81 | const MAX_LAG = 10; |
||
82 | /** @var integer Max time to wait for a slave to catch up (e.g. ChronologyProtector) */ |
||
83 | const POS_WAIT_TIMEOUT = 10; |
||
84 | /** @var integer Seconds to cache master server read-only status */ |
||
85 | const TTL_CACHE_READONLY = 5; |
||
86 | |||
87 | /** |
||
88 | * @var boolean |
||
89 | */ |
||
90 | private $disabled = false; |
||
91 | |||
92 | /** |
||
93 | * @param array $params Array with keys: |
||
94 | * - servers : Required. Array of server info structures. |
||
95 | * - loadMonitor : Name of a class used to fetch server lag and load. |
||
96 | * - readOnlyReason : Reason the master DB is read-only if so [optional] |
||
97 | * - srvCache : BagOStuff object [optional] |
||
98 | * - wanCache : WANObjectCache object [optional] |
||
99 | * @throws MWException |
||
100 | */ |
||
101 | public function __construct( array $params ) { |
||
102 | if ( !isset( $params['servers'] ) ) { |
||
103 | throw new MWException( __CLASS__ . ': missing servers parameter' ); |
||
104 | } |
||
105 | $this->mServers = $params['servers']; |
||
106 | $this->mWaitTimeout = self::POS_WAIT_TIMEOUT; |
||
107 | |||
108 | $this->mReadIndex = -1; |
||
109 | $this->mWriteIndex = -1; |
||
|
|||
110 | $this->mConns = [ |
||
111 | 'local' => [], |
||
112 | 'foreignUsed' => [], |
||
113 | 'foreignFree' => [] ]; |
||
114 | $this->mLoads = []; |
||
115 | $this->mWaitForPos = false; |
||
116 | $this->mErrorConnection = false; |
||
117 | $this->mAllowLagged = false; |
||
118 | |||
119 | View Code Duplication | if ( isset( $params['readOnlyReason'] ) && is_string( $params['readOnlyReason'] ) ) { |
|
120 | $this->readOnlyReason = $params['readOnlyReason']; |
||
121 | } |
||
122 | |||
123 | if ( isset( $params['loadMonitor'] ) ) { |
||
124 | $this->mLoadMonitorClass = $params['loadMonitor']; |
||
125 | } else { |
||
126 | $master = reset( $params['servers'] ); |
||
127 | if ( isset( $master['type'] ) && $master['type'] === 'mysql' ) { |
||
128 | $this->mLoadMonitorClass = 'LoadMonitorMySQL'; |
||
129 | } else { |
||
130 | $this->mLoadMonitorClass = 'LoadMonitorNull'; |
||
131 | } |
||
132 | } |
||
133 | |||
134 | foreach ( $params['servers'] as $i => $server ) { |
||
135 | $this->mLoads[$i] = $server['load']; |
||
136 | if ( isset( $server['groupLoads'] ) ) { |
||
137 | foreach ( $server['groupLoads'] as $group => $ratio ) { |
||
138 | if ( !isset( $this->mGroupLoads[$group] ) ) { |
||
139 | $this->mGroupLoads[$group] = []; |
||
140 | } |
||
141 | $this->mGroupLoads[$group][$i] = $ratio; |
||
142 | } |
||
143 | } |
||
144 | } |
||
145 | |||
146 | if ( isset( $params['srvCache'] ) ) { |
||
147 | $this->srvCache = $params['srvCache']; |
||
148 | } else { |
||
149 | $this->srvCache = new EmptyBagOStuff(); |
||
150 | } |
||
151 | if ( isset( $params['wanCache'] ) ) { |
||
152 | $this->wanCache = $params['wanCache']; |
||
153 | } else { |
||
154 | $this->wanCache = WANObjectCache::newEmpty(); |
||
155 | } |
||
156 | if ( isset( $params['trxProfiler'] ) ) { |
||
157 | $this->trxProfiler = $params['trxProfiler']; |
||
158 | } else { |
||
159 | $this->trxProfiler = new TransactionProfiler(); |
||
160 | } |
||
161 | } |
||
162 | |||
163 | /** |
||
164 | * Get a LoadMonitor instance |
||
165 | * |
||
166 | * @return LoadMonitor |
||
167 | */ |
||
168 | private function getLoadMonitor() { |
||
176 | |||
177 | /** |
||
178 | * Get or set arbitrary data used by the parent object, usually an LBFactory |
||
179 | * @param mixed $x |
||
180 | * @return mixed |
||
181 | */ |
||
182 | public function parentInfo( $x = null ) { |
||
185 | |||
186 | /** |
||
187 | * @param array $loads |
||
188 | * @param bool|string $wiki Wiki to get non-lagged for |
||
189 | * @param int $maxLag Restrict the maximum allowed lag to this many seconds |
||
190 | * @return bool|int|string |
||
191 | */ |
||
192 | private function getRandomNonLagged( array $loads, $wiki = false, $maxLag = self::MAX_LAG ) { |
||
234 | |||
235 | /** |
||
236 | * Get the index of the reader connection, which may be a slave |
||
237 | * This takes into account load ratios and lag times. It should |
||
238 | * always return a consistent index during a given invocation |
||
239 | * |
||
240 | * Side effect: opens connections to databases |
||
241 | * @param string|bool $group Query group, or false for the generic reader |
||
242 | * @param string|bool $wiki Wiki ID, or false for the current wiki |
||
243 | * @throws MWException |
||
244 | * @return bool|int|string |
||
245 | */ |
||
246 | public function getReaderIndex( $group = false, $wiki = false ) { |
||
374 | |||
375 | /** |
||
376 | * Set the master wait position |
||
377 | * If a DB_SLAVE connection has been opened already, waits |
||
378 | * Otherwise sets a variable telling it to wait if such a connection is opened |
||
379 | * @param DBMasterPos $pos |
||
380 | */ |
||
381 | public function waitFor( $pos ) { |
||
392 | |||
393 | /** |
||
394 | * Set the master wait position and wait for a "generic" slave to catch up to it |
||
395 | * |
||
396 | * This can be used a faster proxy for waitForAll() |
||
397 | * |
||
398 | * @param DBMasterPos $pos |
||
399 | * @param int $timeout Max seconds to wait; default is mWaitTimeout |
||
400 | * @return bool Success (able to connect and no timeouts reached) |
||
401 | * @since 1.26 |
||
402 | */ |
||
403 | public function waitForOne( $pos, $timeout = null ) { |
||
423 | |||
424 | /** |
||
425 | * Set the master wait position and wait for ALL slaves to catch up to it |
||
426 | * @param DBMasterPos $pos |
||
427 | * @param int $timeout Max seconds to wait; default is mWaitTimeout |
||
428 | * @return bool Success (able to connect and no timeouts reached) |
||
429 | */ |
||
430 | public function waitForAll( $pos, $timeout = null ) { |
||
443 | |||
444 | /** |
||
445 | * Get any open connection to a given server index, local or foreign |
||
446 | * Returns false if there is no connection open |
||
447 | * |
||
448 | * @param int $i |
||
449 | * @return DatabaseBase|bool False on failure |
||
450 | */ |
||
451 | public function getAnyOpenConnection( $i ) { |
||
460 | |||
461 | /** |
||
462 | * Wait for a given slave to catch up to the master pos stored in $this |
||
463 | * @param int $index Server index |
||
464 | * @param bool $open Check the server even if a new connection has to be made |
||
465 | * @param int $timeout Max seconds to wait; default is mWaitTimeout |
||
466 | * @return bool |
||
467 | */ |
||
468 | protected function doWait( $index, $open = false, $timeout = null ) { |
||
525 | |||
526 | /** |
||
527 | * Get a connection by index |
||
528 | * This is the main entry point for this class. |
||
529 | * |
||
530 | * @param int $i Server index |
||
531 | * @param array|string|bool $groups Query group(s), or false for the generic reader |
||
532 | * @param string|bool $wiki Wiki ID, or false for the current wiki |
||
533 | * |
||
534 | * @throws MWException |
||
535 | * @return DatabaseBase |
||
536 | */ |
||
537 | public function getConnection( $i, $groups = [], $wiki = false ) { |
||
603 | |||
604 | /** |
||
605 | * Mark a foreign connection as being available for reuse under a different |
||
606 | * DB name or prefix. This mechanism is reference-counted, and must be called |
||
607 | * the same number of times as getConnection() to work. |
||
608 | * |
||
609 | * @param DatabaseBase $conn |
||
610 | * @throws MWException |
||
611 | */ |
||
612 | public function reuseConnection( $conn ) { |
||
650 | |||
651 | /** |
||
652 | * Get a database connection handle reference |
||
653 | * |
||
654 | * The handle's methods wrap simply wrap those of a DatabaseBase handle |
||
655 | * |
||
656 | * @see LoadBalancer::getConnection() for parameter information |
||
657 | * |
||
658 | * @param int $db |
||
659 | * @param array|string|bool $groups Query group(s), or false for the generic reader |
||
660 | * @param string|bool $wiki Wiki ID, or false for the current wiki |
||
661 | * @return DBConnRef |
||
662 | */ |
||
663 | public function getConnectionRef( $db, $groups = [], $wiki = false ) { |
||
666 | |||
667 | /** |
||
668 | * Get a database connection handle reference without connecting yet |
||
669 | * |
||
670 | * The handle's methods wrap simply wrap those of a DatabaseBase handle |
||
671 | * |
||
672 | * @see LoadBalancer::getConnection() for parameter information |
||
673 | * |
||
674 | * @param int $db |
||
675 | * @param array|string|bool $groups Query group(s), or false for the generic reader |
||
676 | * @param string|bool $wiki Wiki ID, or false for the current wiki |
||
677 | * @return DBConnRef |
||
678 | */ |
||
679 | public function getLazyConnectionRef( $db, $groups = [], $wiki = false ) { |
||
682 | |||
683 | /** |
||
684 | * Open a connection to the server given by the specified index |
||
685 | * Index must be an actual index into the array. |
||
686 | * If the server is already open, returns it. |
||
687 | * |
||
688 | * On error, returns false, and the connection which caused the |
||
689 | * error will be available via $this->mErrorConnection. |
||
690 | * |
||
691 | * @note If disable() was called on this LoadBalancer, this method will throw a DBAccessError. |
||
692 | * |
||
693 | * @param int $i Server index |
||
694 | * @param string|bool $wiki Wiki ID, or false for the current wiki |
||
695 | * @return DatabaseBase|bool Returns false on errors |
||
696 | */ |
||
697 | public function openConnection( $i, $wiki = false ) { |
||
728 | |||
729 | /** |
||
730 | * Open a connection to a foreign DB, or return one if it is already open. |
||
731 | * |
||
732 | * Increments a reference count on the returned connection which locks the |
||
733 | * connection to the requested wiki. This reference count can be |
||
734 | * decremented by calling reuseConnection(). |
||
735 | * |
||
736 | * If a connection is open to the appropriate server already, but with the wrong |
||
737 | * database, it will be switched to the right database and returned, as long as |
||
738 | * it has been freed first with reuseConnection(). |
||
739 | * |
||
740 | * On error, returns false, and the connection which caused the |
||
741 | * error will be available via $this->mErrorConnection. |
||
742 | * |
||
743 | * @note If disable() was called on this LoadBalancer, this method will throw a DBAccessError. |
||
744 | * |
||
745 | * @param int $i Server index |
||
746 | * @param string $wiki Wiki ID to open |
||
747 | * @return DatabaseBase |
||
748 | */ |
||
749 | private function openForeignConnection( $i, $wiki ) { |
||
805 | |||
806 | /** |
||
807 | * Test if the specified index represents an open connection |
||
808 | * |
||
809 | * @param int $index Server index |
||
810 | * @access private |
||
811 | * @return bool |
||
812 | */ |
||
813 | private function isOpen( $index ) { |
||
820 | |||
821 | /** |
||
822 | * Really opens a connection. Uncached. |
||
823 | * Returns a Database object whether or not the connection was successful. |
||
824 | * @access private |
||
825 | * |
||
826 | * @param array $server |
||
827 | * @param bool $dbNameOverride |
||
828 | * @throws MWException |
||
829 | * @return DatabaseBase |
||
830 | */ |
||
831 | protected function reallyOpenConnection( $server, $dbNameOverride = false ) { |
||
882 | |||
883 | /** |
||
884 | * @throws DBConnectionError |
||
885 | * @return bool |
||
886 | */ |
||
887 | private function reportConnectionError() { |
||
916 | |||
917 | /** |
||
918 | * @return int |
||
919 | * @since 1.26 |
||
920 | */ |
||
921 | public function getWriterIndex() { |
||
924 | |||
925 | /** |
||
926 | * Returns true if the specified index is a valid server index |
||
927 | * |
||
928 | * @param string $i |
||
929 | * @return bool |
||
930 | */ |
||
931 | public function haveIndex( $i ) { |
||
934 | |||
935 | /** |
||
936 | * Returns true if the specified index is valid and has non-zero load |
||
937 | * |
||
938 | * @param string $i |
||
939 | * @return bool |
||
940 | */ |
||
941 | public function isNonZeroLoad( $i ) { |
||
944 | |||
945 | /** |
||
946 | * Get the number of defined servers (not the number of open connections) |
||
947 | * |
||
948 | * @return int |
||
949 | */ |
||
950 | public function getServerCount() { |
||
953 | |||
954 | /** |
||
955 | * Get the host name or IP address of the server with the specified index |
||
956 | * Prefer a readable name if available. |
||
957 | * @param string $i |
||
958 | * @return string |
||
959 | */ |
||
960 | public function getServerName( $i ) { |
||
971 | |||
972 | /** |
||
973 | * Return the server info structure for a given index, or false if the index is invalid. |
||
974 | * @param int $i |
||
975 | * @return array|bool |
||
976 | */ |
||
977 | public function getServerInfo( $i ) { |
||
984 | |||
985 | /** |
||
986 | * Sets the server info structure for the given index. Entry at index $i |
||
987 | * is created if it doesn't exist |
||
988 | * @param int $i |
||
989 | * @param array $serverInfo |
||
990 | */ |
||
991 | public function setServerInfo( $i, array $serverInfo ) { |
||
994 | |||
995 | /** |
||
996 | * Get the current master position for chronology control purposes |
||
997 | * @return mixed |
||
998 | */ |
||
999 | public function getMasterPos() { |
||
1017 | |||
1018 | /** |
||
1019 | * Disable this load balancer. All connections are closed, and any attempt to |
||
1020 | * open a new connection will result in a DBAccessError. |
||
1021 | * |
||
1022 | * @since 1.27 |
||
1023 | */ |
||
1024 | public function disable() { |
||
1028 | |||
1029 | /** |
||
1030 | * Close all open connections |
||
1031 | */ |
||
1032 | public function closeAll() { |
||
1044 | |||
1045 | /** |
||
1046 | * Close a connection |
||
1047 | * Using this function makes sure the LoadBalancer knows the connection is closed. |
||
1048 | * If you use $conn->close() directly, the load balancer won't update its state. |
||
1049 | * @param DatabaseBase $conn |
||
1050 | */ |
||
1051 | public function closeConnection( $conn ) { |
||
1070 | |||
1071 | /** |
||
1072 | * Commit transactions on all open connections |
||
1073 | * @param string $fname Caller name |
||
1074 | * @throws DBExpectedError |
||
1075 | */ |
||
1076 | public function commitAll( $fname = __METHOD__ ) { |
||
1102 | |||
1103 | /** |
||
1104 | * Perform all pre-commit callbacks that remain part of the atomic transactions |
||
1105 | * and disable any post-commit callbacks until runMasterPostTrxCallbacks() |
||
1106 | * @since 1.28 |
||
1107 | */ |
||
1108 | public function finalizeMasterChanges() { |
||
1117 | |||
1118 | /** |
||
1119 | * Perform all pre-commit checks for things like replication safety |
||
1120 | * @param array $options Includes: |
||
1121 | * - maxWriteDuration : max write query duration time in seconds |
||
1122 | * @throws DBTransactionError |
||
1123 | * @since 1.28 |
||
1124 | */ |
||
1125 | public function approveMasterChanges( array $options ) { |
||
1156 | |||
1157 | /** |
||
1158 | * Flush any master transaction snapshots and set DBO_TRX (if DBO_DEFAULT is set) |
||
1159 | * |
||
1160 | * The DBO_TRX setting will be reverted to the default in each of these methods: |
||
1161 | * - commitMasterChanges() |
||
1162 | * - rollbackMasterChanges() |
||
1163 | * - commitAll() |
||
1164 | * This allows for custom transaction rounds from any outer transaction scope. |
||
1165 | * |
||
1166 | * @param string $fname |
||
1167 | * @throws DBExpectedError |
||
1168 | * @since 1.28 |
||
1169 | */ |
||
1170 | public function beginMasterChanges( $fname = __METHOD__ ) { |
||
1201 | |||
1202 | /** |
||
1203 | * Issue COMMIT on all master connections where writes where done |
||
1204 | * @param string $fname Caller name |
||
1205 | * @throws DBExpectedError |
||
1206 | */ |
||
1207 | public function commitMasterChanges( $fname = __METHOD__ ) { |
||
1237 | |||
1238 | /** |
||
1239 | * Issue all pending post-COMMIT/ROLLBACK callbacks |
||
1240 | * @param integer $type IDatabase::TRIGGER_* constant |
||
1241 | * @return Exception|null The first exception or null if there were none |
||
1242 | * @since 1.28 |
||
1243 | */ |
||
1244 | public function runMasterPostTrxCallbacks( $type ) { |
||
1264 | |||
1265 | /** |
||
1266 | * Issue ROLLBACK only on master, only if queries were done on connection |
||
1267 | * @param string $fname Caller name |
||
1268 | * @throws DBExpectedError |
||
1269 | * @since 1.23 |
||
1270 | */ |
||
1271 | public function rollbackMasterChanges( $fname = __METHOD__ ) { |
||
1285 | |||
1286 | /** |
||
1287 | * Suppress all pending post-COMMIT/ROLLBACK callbacks |
||
1288 | * @return Exception|null The first exception or null if there were none |
||
1289 | * @since 1.28 |
||
1290 | */ |
||
1291 | public function suppressTransactionEndCallbacks() { |
||
1296 | |||
1297 | /** |
||
1298 | * @param DatabaseBase $conn |
||
1299 | */ |
||
1300 | private function applyTransactionRoundFlags( DatabaseBase $conn ) { |
||
1310 | |||
1311 | /** |
||
1312 | * @param DatabaseBase $conn |
||
1313 | */ |
||
1314 | private function undoTransactionRoundFlags( DatabaseBase $conn ) { |
||
1319 | |||
1320 | /** |
||
1321 | * @return bool Whether a master connection is already open |
||
1322 | * @since 1.24 |
||
1323 | */ |
||
1324 | public function hasMasterConnection() { |
||
1327 | |||
1328 | /** |
||
1329 | * Determine if there are pending changes in a transaction by this thread |
||
1330 | * @since 1.23 |
||
1331 | * @return bool |
||
1332 | */ |
||
1333 | public function hasMasterChanges() { |
||
1348 | |||
1349 | /** |
||
1350 | * Get the timestamp of the latest write query done by this thread |
||
1351 | * @since 1.25 |
||
1352 | * @return float|bool UNIX timestamp or false |
||
1353 | */ |
||
1354 | View Code Duplication | public function lastMasterChangeTimestamp() { |
|
1368 | |||
1369 | /** |
||
1370 | * Check if this load balancer object had any recent or still |
||
1371 | * pending writes issued against it by this PHP thread |
||
1372 | * |
||
1373 | * @param float $age How many seconds ago is "recent" [defaults to mWaitTimeout] |
||
1374 | * @return bool |
||
1375 | * @since 1.25 |
||
1376 | */ |
||
1377 | public function hasOrMadeRecentMasterChanges( $age = null ) { |
||
1383 | |||
1384 | /** |
||
1385 | * Get the list of callers that have pending master changes |
||
1386 | * |
||
1387 | * @return array |
||
1388 | * @since 1.27 |
||
1389 | */ |
||
1390 | View Code Duplication | public function pendingMasterChangeCallers() { |
|
1406 | |||
1407 | /** |
||
1408 | * @param mixed $value |
||
1409 | * @return mixed |
||
1410 | */ |
||
1411 | public function waitTimeout( $value = null ) { |
||
1414 | |||
1415 | /** |
||
1416 | * @note This method will trigger a DB connection if not yet done |
||
1417 | * |
||
1418 | * @param string|bool $wiki Wiki ID, or false for the current wiki |
||
1419 | * @return bool Whether the generic connection for reads is highly "lagged" |
||
1420 | */ |
||
1421 | public function getLaggedSlaveMode( $wiki = false ) { |
||
1437 | |||
1438 | /** |
||
1439 | * @note This method will never cause a new DB connection |
||
1440 | * @return bool Whether any generic connection used for reads was highly "lagged" |
||
1441 | * @since 1.27 |
||
1442 | */ |
||
1443 | public function laggedSlaveUsed() { |
||
1446 | |||
1447 | /** |
||
1448 | * @note This method may trigger a DB connection if not yet done |
||
1449 | * @param string|bool $wiki Wiki ID, or false for the current wiki |
||
1450 | * @param DatabaseBase|null DB master connection; used to avoid loops [optional] |
||
1451 | * @return string|bool Reason the master is read-only or false if it is not |
||
1452 | * @since 1.27 |
||
1453 | */ |
||
1454 | public function getReadOnlyReason( $wiki = false, DatabaseBase $conn = null ) { |
||
1471 | |||
1472 | /** |
||
1473 | * @param string $wiki Wiki ID, or false for the current wiki |
||
1474 | * @param DatabaseBase|null DB master connectionl used to avoid loops [optional] |
||
1475 | * @return bool |
||
1476 | */ |
||
1477 | private function masterRunningReadOnly( $wiki, DatabaseBase $conn = null ) { |
||
1498 | |||
1499 | /** |
||
1500 | * Disables/enables lag checks |
||
1501 | * @param null|bool $mode |
||
1502 | * @return bool |
||
1503 | */ |
||
1504 | public function allowLagged( $mode = null ) { |
||
1512 | |||
1513 | /** |
||
1514 | * @return bool |
||
1515 | */ |
||
1516 | public function pingAll() { |
||
1526 | |||
1527 | /** |
||
1528 | * Call a function with each open connection object |
||
1529 | * @param callable $callback |
||
1530 | * @param array $params |
||
1531 | */ |
||
1532 | public function forEachOpenConnection( $callback, array $params = [] ) { |
||
1542 | |||
1543 | /** |
||
1544 | * Call a function with each open connection object to a master |
||
1545 | * @param callable $callback |
||
1546 | * @param array $params |
||
1547 | * @since 1.28 |
||
1548 | */ |
||
1549 | public function forEachOpenMasterConnection( $callback, array $params = [] ) { |
||
1561 | |||
1562 | /** |
||
1563 | * Get the hostname and lag time of the most-lagged slave |
||
1564 | * |
||
1565 | * This is useful for maintenance scripts that need to throttle their updates. |
||
1566 | * May attempt to open connections to slaves on the default DB. If there is |
||
1567 | * no lag, the maximum lag will be reported as -1. |
||
1568 | * |
||
1569 | * @param bool|string $wiki Wiki ID, or false for the default database |
||
1570 | * @return array ( host, max lag, index of max lagged host ) |
||
1571 | */ |
||
1572 | public function getMaxLag( $wiki = false ) { |
||
1592 | |||
1593 | /** |
||
1594 | * Get an estimate of replication lag (in seconds) for each server |
||
1595 | * |
||
1596 | * Results are cached for a short time in memcached/process cache |
||
1597 | * |
||
1598 | * Values may be "false" if replication is too broken to estimate |
||
1599 | * |
||
1600 | * @param string|bool $wiki |
||
1601 | * @return int[] Map of (server index => float|int|bool) |
||
1602 | */ |
||
1603 | public function getLagTimes( $wiki = false ) { |
||
1611 | |||
1612 | /** |
||
1613 | * Get the lag in seconds for a given connection, or zero if this load |
||
1614 | * balancer does not have replication enabled. |
||
1615 | * |
||
1616 | * This should be used in preference to Database::getLag() in cases where |
||
1617 | * replication may not be in use, since there is no way to determine if |
||
1618 | * replication is in use at the connection level without running |
||
1619 | * potentially restricted queries such as SHOW SLAVE STATUS. Using this |
||
1620 | * function instead of Database::getLag() avoids a fatal error in this |
||
1621 | * case on many installations. |
||
1622 | * |
||
1623 | * @param IDatabase $conn |
||
1624 | * @return int|bool Returns false on error |
||
1625 | */ |
||
1626 | public function safeGetLag( IDatabase $conn ) { |
||
1633 | |||
1634 | /** |
||
1635 | * Wait for a slave DB to reach a specified master position |
||
1636 | * |
||
1637 | * This will connect to the master to get an accurate position if $pos is not given |
||
1638 | * |
||
1639 | * @param IDatabase $conn Slave DB |
||
1640 | * @param DBMasterPos|bool $pos Master position; default: current position |
||
1641 | * @param integer $timeout Timeout in seconds |
||
1642 | * @return bool Success |
||
1643 | * @since 1.27 |
||
1644 | */ |
||
1645 | public function safeWaitForMasterPos( IDatabase $conn, $pos = false, $timeout = 10 ) { |
||
1668 | |||
1669 | /** |
||
1670 | * Clear the cache for slag lag delay times |
||
1671 | * |
||
1672 | * This is only used for testing |
||
1673 | */ |
||
1674 | public function clearLagTimeCache() { |
||
1677 | |||
1678 | /** |
||
1679 | * Set a callback via DatabaseBase::setTransactionListener() on |
||
1680 | * all current and future master connections of this load balancer |
||
1681 | * |
||
1682 | * @param string $name Callback name |
||
1683 | * @param callable|null $callback |
||
1684 | * @since 1.28 |
||
1685 | */ |
||
1686 | public function setTransactionListener( $name, callable $callback = null ) { |
||
1698 | } |
||
1699 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: