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 | |||
| 55 | /** @var bool|DatabaseBase Database connection that caused a problem */ |
||
| 56 | private $mErrorConnection; |
||
| 57 | /** @var integer The generic (not query grouped) slave index (of $mServers) */ |
||
| 58 | private $mReadIndex; |
||
| 59 | /** @var bool|DBMasterPos False if not set */ |
||
| 60 | private $mWaitForPos; |
||
| 61 | /** @var bool Whether the generic reader fell back to a lagged slave */ |
||
| 62 | private $laggedSlaveMode = false; |
||
| 63 | /** @var bool Whether the generic reader fell back to a lagged slave */ |
||
| 64 | private $slavesDownMode = false; |
||
| 65 | /** @var string The last DB selection or connection error */ |
||
| 66 | private $mLastError = 'Unknown error'; |
||
| 67 | /** @var string|bool Reason the LB is read-only or false if not */ |
||
| 68 | private $readOnlyReason = false; |
||
| 69 | /** @var integer Total connections opened */ |
||
| 70 | private $connsOpened = 0; |
||
| 71 | |||
| 72 | /** @var TransactionProfiler */ |
||
| 73 | protected $trxProfiler; |
||
| 74 | |||
| 75 | /** @var integer Warn when this many connection are held */ |
||
| 76 | const CONN_HELD_WARN_THRESHOLD = 10; |
||
| 77 | /** @var integer Default 'max lag' when unspecified */ |
||
| 78 | const MAX_LAG = 10; |
||
| 79 | /** @var integer Max time to wait for a slave to catch up (e.g. ChronologyProtector) */ |
||
| 80 | const POS_WAIT_TIMEOUT = 10; |
||
| 81 | /** @var integer Seconds to cache master server read-only status */ |
||
| 82 | const TTL_CACHE_READONLY = 5; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @var boolean |
||
| 86 | */ |
||
| 87 | private $disabled = false; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @param array $params Array with keys: |
||
| 91 | * - servers : Required. Array of server info structures. |
||
| 92 | * - loadMonitor : Name of a class used to fetch server lag and load. |
||
| 93 | * - readOnlyReason : Reason the master DB is read-only if so [optional] |
||
| 94 | * @throws MWException |
||
| 95 | */ |
||
| 96 | public function __construct( array $params ) { |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Get a LoadMonitor instance |
||
| 153 | * |
||
| 154 | * @return LoadMonitor |
||
| 155 | */ |
||
| 156 | private function getLoadMonitor() { |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Get or set arbitrary data used by the parent object, usually an LBFactory |
||
| 167 | * @param mixed $x |
||
| 168 | * @return mixed |
||
| 169 | */ |
||
| 170 | public function parentInfo( $x = null ) { |
||
| 173 | |||
| 174 | /** |
||
| 175 | * @param array $loads |
||
| 176 | * @param bool|string $wiki Wiki to get non-lagged for |
||
| 177 | * @param int $maxLag Restrict the maximum allowed lag to this many seconds |
||
| 178 | * @return bool|int|string |
||
| 179 | */ |
||
| 180 | private function getRandomNonLagged( array $loads, $wiki = false, $maxLag = self::MAX_LAG ) { |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Get the index of the reader connection, which may be a slave |
||
| 225 | * This takes into account load ratios and lag times. It should |
||
| 226 | * always return a consistent index during a given invocation |
||
| 227 | * |
||
| 228 | * Side effect: opens connections to databases |
||
| 229 | * @param string|bool $group Query group, or false for the generic reader |
||
| 230 | * @param string|bool $wiki Wiki ID, or false for the current wiki |
||
| 231 | * @throws MWException |
||
| 232 | * @return bool|int|string |
||
| 233 | */ |
||
| 234 | public function getReaderIndex( $group = false, $wiki = false ) { |
||
| 362 | |||
| 363 | /** |
||
| 364 | * Set the master wait position |
||
| 365 | * If a DB_SLAVE connection has been opened already, waits |
||
| 366 | * Otherwise sets a variable telling it to wait if such a connection is opened |
||
| 367 | * @param DBMasterPos $pos |
||
| 368 | */ |
||
| 369 | public function waitFor( $pos ) { |
||
| 380 | |||
| 381 | /** |
||
| 382 | * Set the master wait position and wait for a "generic" slave to catch up to it |
||
| 383 | * |
||
| 384 | * This can be used a faster proxy for waitForAll() |
||
| 385 | * |
||
| 386 | * @param DBMasterPos $pos |
||
| 387 | * @param int $timeout Max seconds to wait; default is mWaitTimeout |
||
| 388 | * @return bool Success (able to connect and no timeouts reached) |
||
| 389 | * @since 1.26 |
||
| 390 | */ |
||
| 391 | public function waitForOne( $pos, $timeout = null ) { |
||
| 411 | |||
| 412 | /** |
||
| 413 | * Set the master wait position and wait for ALL slaves to catch up to it |
||
| 414 | * @param DBMasterPos $pos |
||
| 415 | * @param int $timeout Max seconds to wait; default is mWaitTimeout |
||
| 416 | * @return bool Success (able to connect and no timeouts reached) |
||
| 417 | */ |
||
| 418 | public function waitForAll( $pos, $timeout = null ) { |
||
| 431 | |||
| 432 | /** |
||
| 433 | * Get any open connection to a given server index, local or foreign |
||
| 434 | * Returns false if there is no connection open |
||
| 435 | * |
||
| 436 | * @param int $i |
||
| 437 | * @return DatabaseBase|bool False on failure |
||
| 438 | */ |
||
| 439 | public function getAnyOpenConnection( $i ) { |
||
| 448 | |||
| 449 | /** |
||
| 450 | * Wait for a given slave to catch up to the master pos stored in $this |
||
| 451 | * @param int $index Server index |
||
| 452 | * @param bool $open Check the server even if a new connection has to be made |
||
| 453 | * @param int $timeout Max seconds to wait; default is mWaitTimeout |
||
| 454 | * @return bool |
||
| 455 | */ |
||
| 456 | protected function doWait( $index, $open = false, $timeout = null ) { |
||
| 513 | |||
| 514 | /** |
||
| 515 | * Get a connection by index |
||
| 516 | * This is the main entry point for this class. |
||
| 517 | * |
||
| 518 | * @param int $i Server index |
||
| 519 | * @param array|string|bool $groups Query group(s), or false for the generic reader |
||
| 520 | * @param string|bool $wiki Wiki ID, or false for the current wiki |
||
| 521 | * |
||
| 522 | * @throws MWException |
||
| 523 | * @return DatabaseBase |
||
| 524 | */ |
||
| 525 | public function getConnection( $i, $groups = [], $wiki = false ) { |
||
| 591 | |||
| 592 | /** |
||
| 593 | * Mark a foreign connection as being available for reuse under a different |
||
| 594 | * DB name or prefix. This mechanism is reference-counted, and must be called |
||
| 595 | * the same number of times as getConnection() to work. |
||
| 596 | * |
||
| 597 | * @param DatabaseBase $conn |
||
| 598 | * @throws MWException |
||
| 599 | */ |
||
| 600 | public function reuseConnection( $conn ) { |
||
| 638 | |||
| 639 | /** |
||
| 640 | * Get a database connection handle reference |
||
| 641 | * |
||
| 642 | * The handle's methods wrap simply wrap those of a DatabaseBase handle |
||
| 643 | * |
||
| 644 | * @see LoadBalancer::getConnection() for parameter information |
||
| 645 | * |
||
| 646 | * @param int $db |
||
| 647 | * @param array|string|bool $groups Query group(s), or false for the generic reader |
||
| 648 | * @param string|bool $wiki Wiki ID, or false for the current wiki |
||
| 649 | * @return DBConnRef |
||
| 650 | */ |
||
| 651 | public function getConnectionRef( $db, $groups = [], $wiki = false ) { |
||
| 654 | |||
| 655 | /** |
||
| 656 | * Get a database connection handle reference without connecting yet |
||
| 657 | * |
||
| 658 | * The handle's methods wrap simply wrap those of a DatabaseBase handle |
||
| 659 | * |
||
| 660 | * @see LoadBalancer::getConnection() for parameter information |
||
| 661 | * |
||
| 662 | * @param int $db |
||
| 663 | * @param array|string|bool $groups Query group(s), or false for the generic reader |
||
| 664 | * @param string|bool $wiki Wiki ID, or false for the current wiki |
||
| 665 | * @return DBConnRef |
||
| 666 | */ |
||
| 667 | public function getLazyConnectionRef( $db, $groups = [], $wiki = false ) { |
||
| 670 | |||
| 671 | /** |
||
| 672 | * Open a connection to the server given by the specified index |
||
| 673 | * Index must be an actual index into the array. |
||
| 674 | * If the server is already open, returns it. |
||
| 675 | * |
||
| 676 | * On error, returns false, and the connection which caused the |
||
| 677 | * error will be available via $this->mErrorConnection. |
||
| 678 | * |
||
| 679 | * @note If disable() was called on this LoadBalancer, this method will throw a DBAccessError. |
||
| 680 | * |
||
| 681 | * @param int $i Server index |
||
| 682 | * @param string|bool $wiki Wiki ID, or false for the current wiki |
||
| 683 | * @return DatabaseBase|bool Returns false on errors |
||
| 684 | */ |
||
| 685 | public function openConnection( $i, $wiki = false ) { |
||
| 716 | |||
| 717 | /** |
||
| 718 | * Open a connection to a foreign DB, or return one if it is already open. |
||
| 719 | * |
||
| 720 | * Increments a reference count on the returned connection which locks the |
||
| 721 | * connection to the requested wiki. This reference count can be |
||
| 722 | * decremented by calling reuseConnection(). |
||
| 723 | * |
||
| 724 | * If a connection is open to the appropriate server already, but with the wrong |
||
| 725 | * database, it will be switched to the right database and returned, as long as |
||
| 726 | * it has been freed first with reuseConnection(). |
||
| 727 | * |
||
| 728 | * On error, returns false, and the connection which caused the |
||
| 729 | * error will be available via $this->mErrorConnection. |
||
| 730 | * |
||
| 731 | * @note If disable() was called on this LoadBalancer, this method will throw a DBAccessError. |
||
| 732 | * |
||
| 733 | * @param int $i Server index |
||
| 734 | * @param string $wiki Wiki ID to open |
||
| 735 | * @return DatabaseBase |
||
| 736 | */ |
||
| 737 | private function openForeignConnection( $i, $wiki ) { |
||
| 793 | |||
| 794 | /** |
||
| 795 | * Test if the specified index represents an open connection |
||
| 796 | * |
||
| 797 | * @param int $index Server index |
||
| 798 | * @access private |
||
| 799 | * @return bool |
||
| 800 | */ |
||
| 801 | private function isOpen( $index ) { |
||
| 808 | |||
| 809 | /** |
||
| 810 | * Really opens a connection. Uncached. |
||
| 811 | * Returns a Database object whether or not the connection was successful. |
||
| 812 | * @access private |
||
| 813 | * |
||
| 814 | * @param array $server |
||
| 815 | * @param bool $dbNameOverride |
||
| 816 | * @throws MWException |
||
| 817 | * @return DatabaseBase |
||
| 818 | */ |
||
| 819 | protected function reallyOpenConnection( $server, $dbNameOverride = false ) { |
||
| 861 | |||
| 862 | /** |
||
| 863 | * @throws DBConnectionError |
||
| 864 | * @return bool |
||
| 865 | */ |
||
| 866 | private function reportConnectionError() { |
||
| 895 | |||
| 896 | /** |
||
| 897 | * @return int |
||
| 898 | * @since 1.26 |
||
| 899 | */ |
||
| 900 | public function getWriterIndex() { |
||
| 903 | |||
| 904 | /** |
||
| 905 | * Returns true if the specified index is a valid server index |
||
| 906 | * |
||
| 907 | * @param string $i |
||
| 908 | * @return bool |
||
| 909 | */ |
||
| 910 | public function haveIndex( $i ) { |
||
| 913 | |||
| 914 | /** |
||
| 915 | * Returns true if the specified index is valid and has non-zero load |
||
| 916 | * |
||
| 917 | * @param string $i |
||
| 918 | * @return bool |
||
| 919 | */ |
||
| 920 | public function isNonZeroLoad( $i ) { |
||
| 923 | |||
| 924 | /** |
||
| 925 | * Get the number of defined servers (not the number of open connections) |
||
| 926 | * |
||
| 927 | * @return int |
||
| 928 | */ |
||
| 929 | public function getServerCount() { |
||
| 932 | |||
| 933 | /** |
||
| 934 | * Get the host name or IP address of the server with the specified index |
||
| 935 | * Prefer a readable name if available. |
||
| 936 | * @param string $i |
||
| 937 | * @return string |
||
| 938 | */ |
||
| 939 | public function getServerName( $i ) { |
||
| 950 | |||
| 951 | /** |
||
| 952 | * Return the server info structure for a given index, or false if the index is invalid. |
||
| 953 | * @param int $i |
||
| 954 | * @return array|bool |
||
| 955 | */ |
||
| 956 | public function getServerInfo( $i ) { |
||
| 963 | |||
| 964 | /** |
||
| 965 | * Sets the server info structure for the given index. Entry at index $i |
||
| 966 | * is created if it doesn't exist |
||
| 967 | * @param int $i |
||
| 968 | * @param array $serverInfo |
||
| 969 | */ |
||
| 970 | public function setServerInfo( $i, array $serverInfo ) { |
||
| 973 | |||
| 974 | /** |
||
| 975 | * Get the current master position for chronology control purposes |
||
| 976 | * @return mixed |
||
| 977 | */ |
||
| 978 | public function getMasterPos() { |
||
| 996 | |||
| 997 | /** |
||
| 998 | * Disable this load balancer. All connections are closed, and any attempt to |
||
| 999 | * open a new connection will result in a DBAccessError. |
||
| 1000 | * |
||
| 1001 | * @since 1.27 |
||
| 1002 | */ |
||
| 1003 | public function disable() { |
||
| 1007 | |||
| 1008 | /** |
||
| 1009 | * Close all open connections |
||
| 1010 | */ |
||
| 1011 | public function closeAll() { |
||
| 1023 | |||
| 1024 | /** |
||
| 1025 | * Close a connection |
||
| 1026 | * Using this function makes sure the LoadBalancer knows the connection is closed. |
||
| 1027 | * If you use $conn->close() directly, the load balancer won't update its state. |
||
| 1028 | * @param DatabaseBase $conn |
||
| 1029 | */ |
||
| 1030 | public function closeConnection( $conn ) { |
||
| 1049 | |||
| 1050 | /** |
||
| 1051 | * Commit transactions on all open connections |
||
| 1052 | * @param string $fname Caller name |
||
| 1053 | */ |
||
| 1054 | public function commitAll( $fname = __METHOD__ ) { |
||
| 1059 | |||
| 1060 | /** |
||
| 1061 | * Perform all pre-commit callbacks that remain part of the atomic transactions |
||
| 1062 | * and disable any post-commit callbacks until runMasterPostCommitCallbacks() |
||
| 1063 | * @since 1.28 |
||
| 1064 | */ |
||
| 1065 | public function runMasterPreCommitCallbacks() { |
||
| 1073 | |||
| 1074 | /** |
||
| 1075 | * Perform all pre-commit checks for things like replication safety |
||
| 1076 | * @param array $options Includes: |
||
| 1077 | * - maxWriteDuration : max write query duration time in seconds |
||
| 1078 | * @throws DBTransactionError |
||
| 1079 | * @since 1.28 |
||
| 1080 | */ |
||
| 1081 | public function approveMasterChanges( array $options ) { |
||
| 1095 | |||
| 1096 | /** |
||
| 1097 | * Issue COMMIT on all master connections where writes where done |
||
| 1098 | * @param string $fname Caller name |
||
| 1099 | */ |
||
| 1100 | public function commitMasterChanges( $fname = __METHOD__ ) { |
||
| 1107 | |||
| 1108 | /** |
||
| 1109 | * Issue all pending post-commit callbacks |
||
| 1110 | * @since 1.28 |
||
| 1111 | */ |
||
| 1112 | public function runMasterPostCommitCallbacks() { |
||
| 1118 | |||
| 1119 | /** |
||
| 1120 | * Issue ROLLBACK only on master, only if queries were done on connection |
||
| 1121 | * @param string $fname Caller name |
||
| 1122 | * @throws DBExpectedError |
||
| 1123 | * @since 1.23 |
||
| 1124 | */ |
||
| 1125 | public function rollbackMasterChanges( $fname = __METHOD__ ) { |
||
| 1151 | |||
| 1152 | /** |
||
| 1153 | * @return bool Whether a master connection is already open |
||
| 1154 | * @since 1.24 |
||
| 1155 | */ |
||
| 1156 | public function hasMasterConnection() { |
||
| 1159 | |||
| 1160 | /** |
||
| 1161 | * Determine if there are pending changes in a transaction by this thread |
||
| 1162 | * @since 1.23 |
||
| 1163 | * @return bool |
||
| 1164 | */ |
||
| 1165 | public function hasMasterChanges() { |
||
| 1180 | |||
| 1181 | /** |
||
| 1182 | * Get the timestamp of the latest write query done by this thread |
||
| 1183 | * @since 1.25 |
||
| 1184 | * @return float|bool UNIX timestamp or false |
||
| 1185 | */ |
||
| 1186 | View Code Duplication | public function lastMasterChangeTimestamp() { |
|
| 1200 | |||
| 1201 | /** |
||
| 1202 | * Check if this load balancer object had any recent or still |
||
| 1203 | * pending writes issued against it by this PHP thread |
||
| 1204 | * |
||
| 1205 | * @param float $age How many seconds ago is "recent" [defaults to mWaitTimeout] |
||
| 1206 | * @return bool |
||
| 1207 | * @since 1.25 |
||
| 1208 | */ |
||
| 1209 | public function hasOrMadeRecentMasterChanges( $age = null ) { |
||
| 1215 | |||
| 1216 | /** |
||
| 1217 | * Get the list of callers that have pending master changes |
||
| 1218 | * |
||
| 1219 | * @return array |
||
| 1220 | * @since 1.27 |
||
| 1221 | */ |
||
| 1222 | View Code Duplication | public function pendingMasterChangeCallers() { |
|
| 1238 | |||
| 1239 | /** |
||
| 1240 | * @param mixed $value |
||
| 1241 | * @return mixed |
||
| 1242 | */ |
||
| 1243 | public function waitTimeout( $value = null ) { |
||
| 1246 | |||
| 1247 | /** |
||
| 1248 | * @note This method will trigger a DB connection if not yet done |
||
| 1249 | * |
||
| 1250 | * @param string|bool $wiki Wiki ID, or false for the current wiki |
||
| 1251 | * @return bool Whether the generic connection for reads is highly "lagged" |
||
| 1252 | */ |
||
| 1253 | public function getLaggedSlaveMode( $wiki = false ) { |
||
| 1269 | |||
| 1270 | /** |
||
| 1271 | * @note This method will never cause a new DB connection |
||
| 1272 | * @return bool Whether any generic connection used for reads was highly "lagged" |
||
| 1273 | * @since 1.27 |
||
| 1274 | */ |
||
| 1275 | public function laggedSlaveUsed() { |
||
| 1278 | |||
| 1279 | /** |
||
| 1280 | * @note This method may trigger a DB connection if not yet done |
||
| 1281 | * @param string|bool $wiki Wiki ID, or false for the current wiki |
||
| 1282 | * @param DatabaseBase|null DB master connection; used to avoid loops [optional] |
||
| 1283 | * @return string|bool Reason the master is read-only or false if it is not |
||
| 1284 | * @since 1.27 |
||
| 1285 | */ |
||
| 1286 | public function getReadOnlyReason( $wiki = false, DatabaseBase $conn = null ) { |
||
| 1303 | |||
| 1304 | /** |
||
| 1305 | * @param string $wiki Wiki ID, or false for the current wiki |
||
| 1306 | * @param DatabaseBase|null DB master connectionl used to avoid loops [optional] |
||
| 1307 | * @return bool |
||
| 1308 | */ |
||
| 1309 | private function masterRunningReadOnly( $wiki, DatabaseBase $conn = null ) { |
||
| 1327 | |||
| 1328 | /** |
||
| 1329 | * Disables/enables lag checks |
||
| 1330 | * @param null|bool $mode |
||
| 1331 | * @return bool |
||
| 1332 | */ |
||
| 1333 | public function allowLagged( $mode = null ) { |
||
| 1341 | |||
| 1342 | /** |
||
| 1343 | * @return bool |
||
| 1344 | */ |
||
| 1345 | public function pingAll() { |
||
| 1355 | |||
| 1356 | /** |
||
| 1357 | * Call a function with each open connection object |
||
| 1358 | * @param callable $callback |
||
| 1359 | * @param array $params |
||
| 1360 | */ |
||
| 1361 | public function forEachOpenConnection( $callback, array $params = [] ) { |
||
| 1371 | |||
| 1372 | /** |
||
| 1373 | * Call a function with each open connection object to a master |
||
| 1374 | * @param callable $callback |
||
| 1375 | * @param array $params |
||
| 1376 | * @since 1.28 |
||
| 1377 | */ |
||
| 1378 | public function forEachOpenMasterConnection( $callback, array $params = [] ) { |
||
| 1390 | |||
| 1391 | /** |
||
| 1392 | * Get the hostname and lag time of the most-lagged slave |
||
| 1393 | * |
||
| 1394 | * This is useful for maintenance scripts that need to throttle their updates. |
||
| 1395 | * May attempt to open connections to slaves on the default DB. If there is |
||
| 1396 | * no lag, the maximum lag will be reported as -1. |
||
| 1397 | * |
||
| 1398 | * @param bool|string $wiki Wiki ID, or false for the default database |
||
| 1399 | * @return array ( host, max lag, index of max lagged host ) |
||
| 1400 | */ |
||
| 1401 | public function getMaxLag( $wiki = false ) { |
||
| 1421 | |||
| 1422 | /** |
||
| 1423 | * Get an estimate of replication lag (in seconds) for each server |
||
| 1424 | * |
||
| 1425 | * Results are cached for a short time in memcached/process cache |
||
| 1426 | * |
||
| 1427 | * Values may be "false" if replication is too broken to estimate |
||
| 1428 | * |
||
| 1429 | * @param string|bool $wiki |
||
| 1430 | * @return int[] Map of (server index => float|int|bool) |
||
| 1431 | */ |
||
| 1432 | public function getLagTimes( $wiki = false ) { |
||
| 1440 | |||
| 1441 | /** |
||
| 1442 | * Get the lag in seconds for a given connection, or zero if this load |
||
| 1443 | * balancer does not have replication enabled. |
||
| 1444 | * |
||
| 1445 | * This should be used in preference to Database::getLag() in cases where |
||
| 1446 | * replication may not be in use, since there is no way to determine if |
||
| 1447 | * replication is in use at the connection level without running |
||
| 1448 | * potentially restricted queries such as SHOW SLAVE STATUS. Using this |
||
| 1449 | * function instead of Database::getLag() avoids a fatal error in this |
||
| 1450 | * case on many installations. |
||
| 1451 | * |
||
| 1452 | * @param IDatabase $conn |
||
| 1453 | * @return int|bool Returns false on error |
||
| 1454 | */ |
||
| 1455 | public function safeGetLag( IDatabase $conn ) { |
||
| 1462 | |||
| 1463 | /** |
||
| 1464 | * Wait for a slave DB to reach a specified master position |
||
| 1465 | * |
||
| 1466 | * This will connect to the master to get an accurate position if $pos is not given |
||
| 1467 | * |
||
| 1468 | * @param IDatabase $conn Slave DB |
||
| 1469 | * @param DBMasterPos|bool $pos Master position; default: current position |
||
| 1470 | * @param integer $timeout Timeout in seconds |
||
| 1471 | * @return bool Success |
||
| 1472 | * @since 1.27 |
||
| 1473 | */ |
||
| 1474 | public function safeWaitForMasterPos( IDatabase $conn, $pos = false, $timeout = 10 ) { |
||
| 1497 | |||
| 1498 | /** |
||
| 1499 | * Clear the cache for slag lag delay times |
||
| 1500 | * |
||
| 1501 | * This is only used for testing |
||
| 1502 | */ |
||
| 1503 | public function clearLagTimeCache() { |
||
| 1506 | } |
||
| 1507 |
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: