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 ) { |
||
| 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 ) { |
||
| 873 | |||
| 874 | /** |
||
| 875 | * @throws DBConnectionError |
||
| 876 | * @return bool |
||
| 877 | */ |
||
| 878 | private function reportConnectionError() { |
||
| 907 | |||
| 908 | /** |
||
| 909 | * @return int |
||
| 910 | * @since 1.26 |
||
| 911 | */ |
||
| 912 | public function getWriterIndex() { |
||
| 915 | |||
| 916 | /** |
||
| 917 | * Returns true if the specified index is a valid server index |
||
| 918 | * |
||
| 919 | * @param string $i |
||
| 920 | * @return bool |
||
| 921 | */ |
||
| 922 | public function haveIndex( $i ) { |
||
| 925 | |||
| 926 | /** |
||
| 927 | * Returns true if the specified index is valid and has non-zero load |
||
| 928 | * |
||
| 929 | * @param string $i |
||
| 930 | * @return bool |
||
| 931 | */ |
||
| 932 | public function isNonZeroLoad( $i ) { |
||
| 935 | |||
| 936 | /** |
||
| 937 | * Get the number of defined servers (not the number of open connections) |
||
| 938 | * |
||
| 939 | * @return int |
||
| 940 | */ |
||
| 941 | public function getServerCount() { |
||
| 944 | |||
| 945 | /** |
||
| 946 | * Get the host name or IP address of the server with the specified index |
||
| 947 | * Prefer a readable name if available. |
||
| 948 | * @param string $i |
||
| 949 | * @return string |
||
| 950 | */ |
||
| 951 | public function getServerName( $i ) { |
||
| 962 | |||
| 963 | /** |
||
| 964 | * Return the server info structure for a given index, or false if the index is invalid. |
||
| 965 | * @param int $i |
||
| 966 | * @return array|bool |
||
| 967 | */ |
||
| 968 | public function getServerInfo( $i ) { |
||
| 975 | |||
| 976 | /** |
||
| 977 | * Sets the server info structure for the given index. Entry at index $i |
||
| 978 | * is created if it doesn't exist |
||
| 979 | * @param int $i |
||
| 980 | * @param array $serverInfo |
||
| 981 | */ |
||
| 982 | public function setServerInfo( $i, array $serverInfo ) { |
||
| 985 | |||
| 986 | /** |
||
| 987 | * Get the current master position for chronology control purposes |
||
| 988 | * @return mixed |
||
| 989 | */ |
||
| 990 | public function getMasterPos() { |
||
| 1008 | |||
| 1009 | /** |
||
| 1010 | * Disable this load balancer. All connections are closed, and any attempt to |
||
| 1011 | * open a new connection will result in a DBAccessError. |
||
| 1012 | * |
||
| 1013 | * @since 1.27 |
||
| 1014 | */ |
||
| 1015 | public function disable() { |
||
| 1019 | |||
| 1020 | /** |
||
| 1021 | * Close all open connections |
||
| 1022 | */ |
||
| 1023 | public function closeAll() { |
||
| 1035 | |||
| 1036 | /** |
||
| 1037 | * Close a connection |
||
| 1038 | * Using this function makes sure the LoadBalancer knows the connection is closed. |
||
| 1039 | * If you use $conn->close() directly, the load balancer won't update its state. |
||
| 1040 | * @param DatabaseBase $conn |
||
| 1041 | */ |
||
| 1042 | public function closeConnection( $conn ) { |
||
| 1061 | |||
| 1062 | /** |
||
| 1063 | * Commit transactions on all open connections |
||
| 1064 | * @param string $fname Caller name |
||
| 1065 | */ |
||
| 1066 | public function commitAll( $fname = __METHOD__ ) { |
||
| 1071 | |||
| 1072 | /** |
||
| 1073 | * Perform all pre-commit callbacks that remain part of the atomic transactions |
||
| 1074 | * and disable any post-commit callbacks until runMasterPostCommitCallbacks() |
||
| 1075 | * @since 1.28 |
||
| 1076 | */ |
||
| 1077 | public function runMasterPreCommitCallbacks() { |
||
| 1085 | |||
| 1086 | /** |
||
| 1087 | * Perform all pre-commit checks for things like replication safety |
||
| 1088 | * @param array $options Includes: |
||
| 1089 | * - maxWriteDuration : max write query duration time in seconds |
||
| 1090 | * @throws DBTransactionError |
||
| 1091 | * @since 1.28 |
||
| 1092 | */ |
||
| 1093 | public function approveMasterChanges( array $options ) { |
||
| 1107 | |||
| 1108 | /** |
||
| 1109 | * Issue COMMIT on all master connections where writes where done |
||
| 1110 | * @param string $fname Caller name |
||
| 1111 | */ |
||
| 1112 | public function commitMasterChanges( $fname = __METHOD__ ) { |
||
| 1119 | |||
| 1120 | /** |
||
| 1121 | * Issue all pending post-commit callbacks |
||
| 1122 | * @since 1.28 |
||
| 1123 | */ |
||
| 1124 | public function runMasterPostCommitCallbacks() { |
||
| 1130 | |||
| 1131 | /** |
||
| 1132 | * Issue ROLLBACK only on master, only if queries were done on connection |
||
| 1133 | * @param string $fname Caller name |
||
| 1134 | * @throws DBExpectedError |
||
| 1135 | * @since 1.23 |
||
| 1136 | */ |
||
| 1137 | public function rollbackMasterChanges( $fname = __METHOD__ ) { |
||
| 1163 | |||
| 1164 | /** |
||
| 1165 | * @return bool Whether a master connection is already open |
||
| 1166 | * @since 1.24 |
||
| 1167 | */ |
||
| 1168 | public function hasMasterConnection() { |
||
| 1171 | |||
| 1172 | /** |
||
| 1173 | * Determine if there are pending changes in a transaction by this thread |
||
| 1174 | * @since 1.23 |
||
| 1175 | * @return bool |
||
| 1176 | */ |
||
| 1177 | public function hasMasterChanges() { |
||
| 1192 | |||
| 1193 | /** |
||
| 1194 | * Get the timestamp of the latest write query done by this thread |
||
| 1195 | * @since 1.25 |
||
| 1196 | * @return float|bool UNIX timestamp or false |
||
| 1197 | */ |
||
| 1198 | View Code Duplication | public function lastMasterChangeTimestamp() { |
|
| 1212 | |||
| 1213 | /** |
||
| 1214 | * Check if this load balancer object had any recent or still |
||
| 1215 | * pending writes issued against it by this PHP thread |
||
| 1216 | * |
||
| 1217 | * @param float $age How many seconds ago is "recent" [defaults to mWaitTimeout] |
||
| 1218 | * @return bool |
||
| 1219 | * @since 1.25 |
||
| 1220 | */ |
||
| 1221 | public function hasOrMadeRecentMasterChanges( $age = null ) { |
||
| 1227 | |||
| 1228 | /** |
||
| 1229 | * Get the list of callers that have pending master changes |
||
| 1230 | * |
||
| 1231 | * @return array |
||
| 1232 | * @since 1.27 |
||
| 1233 | */ |
||
| 1234 | View Code Duplication | public function pendingMasterChangeCallers() { |
|
| 1250 | |||
| 1251 | /** |
||
| 1252 | * @param mixed $value |
||
| 1253 | * @return mixed |
||
| 1254 | */ |
||
| 1255 | public function waitTimeout( $value = null ) { |
||
| 1258 | |||
| 1259 | /** |
||
| 1260 | * @note This method will trigger a DB connection if not yet done |
||
| 1261 | * |
||
| 1262 | * @param string|bool $wiki Wiki ID, or false for the current wiki |
||
| 1263 | * @return bool Whether the generic connection for reads is highly "lagged" |
||
| 1264 | */ |
||
| 1265 | public function getLaggedSlaveMode( $wiki = false ) { |
||
| 1281 | |||
| 1282 | /** |
||
| 1283 | * @note This method will never cause a new DB connection |
||
| 1284 | * @return bool Whether any generic connection used for reads was highly "lagged" |
||
| 1285 | * @since 1.27 |
||
| 1286 | */ |
||
| 1287 | public function laggedSlaveUsed() { |
||
| 1290 | |||
| 1291 | /** |
||
| 1292 | * @note This method may trigger a DB connection if not yet done |
||
| 1293 | * @param string|bool $wiki Wiki ID, or false for the current wiki |
||
| 1294 | * @param DatabaseBase|null DB master connection; used to avoid loops [optional] |
||
| 1295 | * @return string|bool Reason the master is read-only or false if it is not |
||
| 1296 | * @since 1.27 |
||
| 1297 | */ |
||
| 1298 | public function getReadOnlyReason( $wiki = false, DatabaseBase $conn = null ) { |
||
| 1315 | |||
| 1316 | /** |
||
| 1317 | * @param string $wiki Wiki ID, or false for the current wiki |
||
| 1318 | * @param DatabaseBase|null DB master connectionl used to avoid loops [optional] |
||
| 1319 | * @return bool |
||
| 1320 | */ |
||
| 1321 | private function masterRunningReadOnly( $wiki, DatabaseBase $conn = null ) { |
||
| 1342 | |||
| 1343 | /** |
||
| 1344 | * Disables/enables lag checks |
||
| 1345 | * @param null|bool $mode |
||
| 1346 | * @return bool |
||
| 1347 | */ |
||
| 1348 | public function allowLagged( $mode = null ) { |
||
| 1356 | |||
| 1357 | /** |
||
| 1358 | * @return bool |
||
| 1359 | */ |
||
| 1360 | public function pingAll() { |
||
| 1370 | |||
| 1371 | /** |
||
| 1372 | * Call a function with each open connection object |
||
| 1373 | * @param callable $callback |
||
| 1374 | * @param array $params |
||
| 1375 | */ |
||
| 1376 | public function forEachOpenConnection( $callback, array $params = [] ) { |
||
| 1386 | |||
| 1387 | /** |
||
| 1388 | * Call a function with each open connection object to a master |
||
| 1389 | * @param callable $callback |
||
| 1390 | * @param array $params |
||
| 1391 | * @since 1.28 |
||
| 1392 | */ |
||
| 1393 | public function forEachOpenMasterConnection( $callback, array $params = [] ) { |
||
| 1405 | |||
| 1406 | /** |
||
| 1407 | * Get the hostname and lag time of the most-lagged slave |
||
| 1408 | * |
||
| 1409 | * This is useful for maintenance scripts that need to throttle their updates. |
||
| 1410 | * May attempt to open connections to slaves on the default DB. If there is |
||
| 1411 | * no lag, the maximum lag will be reported as -1. |
||
| 1412 | * |
||
| 1413 | * @param bool|string $wiki Wiki ID, or false for the default database |
||
| 1414 | * @return array ( host, max lag, index of max lagged host ) |
||
| 1415 | */ |
||
| 1416 | public function getMaxLag( $wiki = false ) { |
||
| 1436 | |||
| 1437 | /** |
||
| 1438 | * Get an estimate of replication lag (in seconds) for each server |
||
| 1439 | * |
||
| 1440 | * Results are cached for a short time in memcached/process cache |
||
| 1441 | * |
||
| 1442 | * Values may be "false" if replication is too broken to estimate |
||
| 1443 | * |
||
| 1444 | * @param string|bool $wiki |
||
| 1445 | * @return int[] Map of (server index => float|int|bool) |
||
| 1446 | */ |
||
| 1447 | public function getLagTimes( $wiki = false ) { |
||
| 1455 | |||
| 1456 | /** |
||
| 1457 | * Get the lag in seconds for a given connection, or zero if this load |
||
| 1458 | * balancer does not have replication enabled. |
||
| 1459 | * |
||
| 1460 | * This should be used in preference to Database::getLag() in cases where |
||
| 1461 | * replication may not be in use, since there is no way to determine if |
||
| 1462 | * replication is in use at the connection level without running |
||
| 1463 | * potentially restricted queries such as SHOW SLAVE STATUS. Using this |
||
| 1464 | * function instead of Database::getLag() avoids a fatal error in this |
||
| 1465 | * case on many installations. |
||
| 1466 | * |
||
| 1467 | * @param IDatabase $conn |
||
| 1468 | * @return int|bool Returns false on error |
||
| 1469 | */ |
||
| 1470 | public function safeGetLag( IDatabase $conn ) { |
||
| 1477 | |||
| 1478 | /** |
||
| 1479 | * Wait for a slave DB to reach a specified master position |
||
| 1480 | * |
||
| 1481 | * This will connect to the master to get an accurate position if $pos is not given |
||
| 1482 | * |
||
| 1483 | * @param IDatabase $conn Slave DB |
||
| 1484 | * @param DBMasterPos|bool $pos Master position; default: current position |
||
| 1485 | * @param integer $timeout Timeout in seconds |
||
| 1486 | * @return bool Success |
||
| 1487 | * @since 1.27 |
||
| 1488 | */ |
||
| 1489 | public function safeWaitForMasterPos( IDatabase $conn, $pos = false, $timeout = 10 ) { |
||
| 1512 | |||
| 1513 | /** |
||
| 1514 | * Clear the cache for slag lag delay times |
||
| 1515 | * |
||
| 1516 | * This is only used for testing |
||
| 1517 | */ |
||
| 1518 | public function clearLagTimeCache() { |
||
| 1521 | } |
||
| 1522 |
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: