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 | * - srvCache : BagOStuff object [optional] |
||
| 95 | * - wanCache : WANObjectCache object [optional] |
||
| 96 | * @throws MWException |
||
| 97 | */ |
||
| 98 | public function __construct( array $params ) { |
||
| 99 | if ( !isset( $params['servers'] ) ) { |
||
| 100 | throw new MWException( __CLASS__ . ': missing servers parameter' ); |
||
| 101 | } |
||
| 102 | $this->mServers = $params['servers']; |
||
| 103 | $this->mWaitTimeout = self::POS_WAIT_TIMEOUT; |
||
| 104 | |||
| 105 | $this->mReadIndex = -1; |
||
| 106 | $this->mWriteIndex = -1; |
||
|
|
|||
| 107 | $this->mConns = [ |
||
| 108 | 'local' => [], |
||
| 109 | 'foreignUsed' => [], |
||
| 110 | 'foreignFree' => [] ]; |
||
| 111 | $this->mLoads = []; |
||
| 112 | $this->mWaitForPos = false; |
||
| 113 | $this->mErrorConnection = false; |
||
| 114 | $this->mAllowLagged = false; |
||
| 115 | |||
| 116 | View Code Duplication | if ( isset( $params['readOnlyReason'] ) && is_string( $params['readOnlyReason'] ) ) { |
|
| 117 | $this->readOnlyReason = $params['readOnlyReason']; |
||
| 118 | } |
||
| 119 | |||
| 120 | if ( isset( $params['loadMonitor'] ) ) { |
||
| 121 | $this->mLoadMonitorClass = $params['loadMonitor']; |
||
| 122 | } else { |
||
| 123 | $master = reset( $params['servers'] ); |
||
| 124 | if ( isset( $master['type'] ) && $master['type'] === 'mysql' ) { |
||
| 125 | $this->mLoadMonitorClass = 'LoadMonitorMySQL'; |
||
| 126 | } else { |
||
| 127 | $this->mLoadMonitorClass = 'LoadMonitorNull'; |
||
| 128 | } |
||
| 129 | } |
||
| 130 | |||
| 131 | foreach ( $params['servers'] as $i => $server ) { |
||
| 132 | $this->mLoads[$i] = $server['load']; |
||
| 133 | if ( isset( $server['groupLoads'] ) ) { |
||
| 134 | foreach ( $server['groupLoads'] as $group => $ratio ) { |
||
| 135 | if ( !isset( $this->mGroupLoads[$group] ) ) { |
||
| 136 | $this->mGroupLoads[$group] = []; |
||
| 137 | } |
||
| 138 | $this->mGroupLoads[$group][$i] = $ratio; |
||
| 139 | } |
||
| 140 | } |
||
| 141 | } |
||
| 142 | |||
| 143 | if ( isset( $params['srvCache'] ) ) { |
||
| 144 | $this->srvCache = $params['srvCache']; |
||
| 145 | } else { |
||
| 146 | $this->srvCache = new EmptyBagOStuff(); |
||
| 147 | } |
||
| 148 | if ( isset( $params['wanCache'] ) ) { |
||
| 149 | $this->wanCache = $params['wanCache']; |
||
| 150 | } else { |
||
| 151 | $this->wanCache = WANObjectCache::newEmpty(); |
||
| 152 | } |
||
| 153 | if ( isset( $params['trxProfiler'] ) ) { |
||
| 154 | $this->trxProfiler = $params['trxProfiler']; |
||
| 155 | } else { |
||
| 156 | $this->trxProfiler = new TransactionProfiler(); |
||
| 157 | } |
||
| 158 | } |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Get a LoadMonitor instance |
||
| 162 | * |
||
| 163 | * @return LoadMonitor |
||
| 164 | */ |
||
| 165 | private function getLoadMonitor() { |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Get or set arbitrary data used by the parent object, usually an LBFactory |
||
| 176 | * @param mixed $x |
||
| 177 | * @return mixed |
||
| 178 | */ |
||
| 179 | public function parentInfo( $x = null ) { |
||
| 182 | |||
| 183 | /** |
||
| 184 | * @param array $loads |
||
| 185 | * @param bool|string $wiki Wiki to get non-lagged for |
||
| 186 | * @param int $maxLag Restrict the maximum allowed lag to this many seconds |
||
| 187 | * @return bool|int|string |
||
| 188 | */ |
||
| 189 | private function getRandomNonLagged( array $loads, $wiki = false, $maxLag = self::MAX_LAG ) { |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Get the index of the reader connection, which may be a slave |
||
| 234 | * This takes into account load ratios and lag times. It should |
||
| 235 | * always return a consistent index during a given invocation |
||
| 236 | * |
||
| 237 | * Side effect: opens connections to databases |
||
| 238 | * @param string|bool $group Query group, or false for the generic reader |
||
| 239 | * @param string|bool $wiki Wiki ID, or false for the current wiki |
||
| 240 | * @throws MWException |
||
| 241 | * @return bool|int|string |
||
| 242 | */ |
||
| 243 | public function getReaderIndex( $group = false, $wiki = false ) { |
||
| 371 | |||
| 372 | /** |
||
| 373 | * Set the master wait position |
||
| 374 | * If a DB_SLAVE connection has been opened already, waits |
||
| 375 | * Otherwise sets a variable telling it to wait if such a connection is opened |
||
| 376 | * @param DBMasterPos $pos |
||
| 377 | */ |
||
| 378 | public function waitFor( $pos ) { |
||
| 389 | |||
| 390 | /** |
||
| 391 | * Set the master wait position and wait for a "generic" slave to catch up to it |
||
| 392 | * |
||
| 393 | * This can be used a faster proxy for waitForAll() |
||
| 394 | * |
||
| 395 | * @param DBMasterPos $pos |
||
| 396 | * @param int $timeout Max seconds to wait; default is mWaitTimeout |
||
| 397 | * @return bool Success (able to connect and no timeouts reached) |
||
| 398 | * @since 1.26 |
||
| 399 | */ |
||
| 400 | public function waitForOne( $pos, $timeout = null ) { |
||
| 420 | |||
| 421 | /** |
||
| 422 | * Set the master wait position and wait for ALL slaves to catch up to it |
||
| 423 | * @param DBMasterPos $pos |
||
| 424 | * @param int $timeout Max seconds to wait; default is mWaitTimeout |
||
| 425 | * @return bool Success (able to connect and no timeouts reached) |
||
| 426 | */ |
||
| 427 | public function waitForAll( $pos, $timeout = null ) { |
||
| 440 | |||
| 441 | /** |
||
| 442 | * Get any open connection to a given server index, local or foreign |
||
| 443 | * Returns false if there is no connection open |
||
| 444 | * |
||
| 445 | * @param int $i |
||
| 446 | * @return DatabaseBase|bool False on failure |
||
| 447 | */ |
||
| 448 | public function getAnyOpenConnection( $i ) { |
||
| 457 | |||
| 458 | /** |
||
| 459 | * Wait for a given slave to catch up to the master pos stored in $this |
||
| 460 | * @param int $index Server index |
||
| 461 | * @param bool $open Check the server even if a new connection has to be made |
||
| 462 | * @param int $timeout Max seconds to wait; default is mWaitTimeout |
||
| 463 | * @return bool |
||
| 464 | */ |
||
| 465 | protected function doWait( $index, $open = false, $timeout = null ) { |
||
| 522 | |||
| 523 | /** |
||
| 524 | * Get a connection by index |
||
| 525 | * This is the main entry point for this class. |
||
| 526 | * |
||
| 527 | * @param int $i Server index |
||
| 528 | * @param array|string|bool $groups Query group(s), or false for the generic reader |
||
| 529 | * @param string|bool $wiki Wiki ID, or false for the current wiki |
||
| 530 | * |
||
| 531 | * @throws MWException |
||
| 532 | * @return DatabaseBase |
||
| 533 | */ |
||
| 534 | public function getConnection( $i, $groups = [], $wiki = false ) { |
||
| 600 | |||
| 601 | /** |
||
| 602 | * Mark a foreign connection as being available for reuse under a different |
||
| 603 | * DB name or prefix. This mechanism is reference-counted, and must be called |
||
| 604 | * the same number of times as getConnection() to work. |
||
| 605 | * |
||
| 606 | * @param DatabaseBase $conn |
||
| 607 | * @throws MWException |
||
| 608 | */ |
||
| 609 | public function reuseConnection( $conn ) { |
||
| 647 | |||
| 648 | /** |
||
| 649 | * Get a database connection handle reference |
||
| 650 | * |
||
| 651 | * The handle's methods wrap simply wrap those of a DatabaseBase handle |
||
| 652 | * |
||
| 653 | * @see LoadBalancer::getConnection() for parameter information |
||
| 654 | * |
||
| 655 | * @param int $db |
||
| 656 | * @param array|string|bool $groups Query group(s), or false for the generic reader |
||
| 657 | * @param string|bool $wiki Wiki ID, or false for the current wiki |
||
| 658 | * @return DBConnRef |
||
| 659 | */ |
||
| 660 | public function getConnectionRef( $db, $groups = [], $wiki = false ) { |
||
| 663 | |||
| 664 | /** |
||
| 665 | * Get a database connection handle reference without connecting yet |
||
| 666 | * |
||
| 667 | * The handle's methods wrap simply wrap those of a DatabaseBase handle |
||
| 668 | * |
||
| 669 | * @see LoadBalancer::getConnection() for parameter information |
||
| 670 | * |
||
| 671 | * @param int $db |
||
| 672 | * @param array|string|bool $groups Query group(s), or false for the generic reader |
||
| 673 | * @param string|bool $wiki Wiki ID, or false for the current wiki |
||
| 674 | * @return DBConnRef |
||
| 675 | */ |
||
| 676 | public function getLazyConnectionRef( $db, $groups = [], $wiki = false ) { |
||
| 679 | |||
| 680 | /** |
||
| 681 | * Open a connection to the server given by the specified index |
||
| 682 | * Index must be an actual index into the array. |
||
| 683 | * If the server is already open, returns it. |
||
| 684 | * |
||
| 685 | * On error, returns false, and the connection which caused the |
||
| 686 | * error will be available via $this->mErrorConnection. |
||
| 687 | * |
||
| 688 | * @note If disable() was called on this LoadBalancer, this method will throw a DBAccessError. |
||
| 689 | * |
||
| 690 | * @param int $i Server index |
||
| 691 | * @param string|bool $wiki Wiki ID, or false for the current wiki |
||
| 692 | * @return DatabaseBase|bool Returns false on errors |
||
| 693 | */ |
||
| 694 | public function openConnection( $i, $wiki = false ) { |
||
| 725 | |||
| 726 | /** |
||
| 727 | * Open a connection to a foreign DB, or return one if it is already open. |
||
| 728 | * |
||
| 729 | * Increments a reference count on the returned connection which locks the |
||
| 730 | * connection to the requested wiki. This reference count can be |
||
| 731 | * decremented by calling reuseConnection(). |
||
| 732 | * |
||
| 733 | * If a connection is open to the appropriate server already, but with the wrong |
||
| 734 | * database, it will be switched to the right database and returned, as long as |
||
| 735 | * it has been freed first with reuseConnection(). |
||
| 736 | * |
||
| 737 | * On error, returns false, and the connection which caused the |
||
| 738 | * error will be available via $this->mErrorConnection. |
||
| 739 | * |
||
| 740 | * @note If disable() was called on this LoadBalancer, this method will throw a DBAccessError. |
||
| 741 | * |
||
| 742 | * @param int $i Server index |
||
| 743 | * @param string $wiki Wiki ID to open |
||
| 744 | * @return DatabaseBase |
||
| 745 | */ |
||
| 746 | private function openForeignConnection( $i, $wiki ) { |
||
| 802 | |||
| 803 | /** |
||
| 804 | * Test if the specified index represents an open connection |
||
| 805 | * |
||
| 806 | * @param int $index Server index |
||
| 807 | * @access private |
||
| 808 | * @return bool |
||
| 809 | */ |
||
| 810 | private function isOpen( $index ) { |
||
| 817 | |||
| 818 | /** |
||
| 819 | * Really opens a connection. Uncached. |
||
| 820 | * Returns a Database object whether or not the connection was successful. |
||
| 821 | * @access private |
||
| 822 | * |
||
| 823 | * @param array $server |
||
| 824 | * @param bool $dbNameOverride |
||
| 825 | * @throws MWException |
||
| 826 | * @return DatabaseBase |
||
| 827 | */ |
||
| 828 | protected function reallyOpenConnection( $server, $dbNameOverride = false ) { |
||
| 870 | |||
| 871 | /** |
||
| 872 | * @throws DBConnectionError |
||
| 873 | * @return bool |
||
| 874 | */ |
||
| 875 | private function reportConnectionError() { |
||
| 904 | |||
| 905 | /** |
||
| 906 | * @return int |
||
| 907 | * @since 1.26 |
||
| 908 | */ |
||
| 909 | public function getWriterIndex() { |
||
| 912 | |||
| 913 | /** |
||
| 914 | * Returns true if the specified index is a valid server index |
||
| 915 | * |
||
| 916 | * @param string $i |
||
| 917 | * @return bool |
||
| 918 | */ |
||
| 919 | public function haveIndex( $i ) { |
||
| 922 | |||
| 923 | /** |
||
| 924 | * Returns true if the specified index is valid and has non-zero load |
||
| 925 | * |
||
| 926 | * @param string $i |
||
| 927 | * @return bool |
||
| 928 | */ |
||
| 929 | public function isNonZeroLoad( $i ) { |
||
| 932 | |||
| 933 | /** |
||
| 934 | * Get the number of defined servers (not the number of open connections) |
||
| 935 | * |
||
| 936 | * @return int |
||
| 937 | */ |
||
| 938 | public function getServerCount() { |
||
| 941 | |||
| 942 | /** |
||
| 943 | * Get the host name or IP address of the server with the specified index |
||
| 944 | * Prefer a readable name if available. |
||
| 945 | * @param string $i |
||
| 946 | * @return string |
||
| 947 | */ |
||
| 948 | public function getServerName( $i ) { |
||
| 959 | |||
| 960 | /** |
||
| 961 | * Return the server info structure for a given index, or false if the index is invalid. |
||
| 962 | * @param int $i |
||
| 963 | * @return array|bool |
||
| 964 | */ |
||
| 965 | public function getServerInfo( $i ) { |
||
| 972 | |||
| 973 | /** |
||
| 974 | * Sets the server info structure for the given index. Entry at index $i |
||
| 975 | * is created if it doesn't exist |
||
| 976 | * @param int $i |
||
| 977 | * @param array $serverInfo |
||
| 978 | */ |
||
| 979 | public function setServerInfo( $i, array $serverInfo ) { |
||
| 982 | |||
| 983 | /** |
||
| 984 | * Get the current master position for chronology control purposes |
||
| 985 | * @return mixed |
||
| 986 | */ |
||
| 987 | public function getMasterPos() { |
||
| 1005 | |||
| 1006 | /** |
||
| 1007 | * Disable this load balancer. All connections are closed, and any attempt to |
||
| 1008 | * open a new connection will result in a DBAccessError. |
||
| 1009 | * |
||
| 1010 | * @since 1.27 |
||
| 1011 | */ |
||
| 1012 | public function disable() { |
||
| 1016 | |||
| 1017 | /** |
||
| 1018 | * Close all open connections |
||
| 1019 | */ |
||
| 1020 | public function closeAll() { |
||
| 1032 | |||
| 1033 | /** |
||
| 1034 | * Close a connection |
||
| 1035 | * Using this function makes sure the LoadBalancer knows the connection is closed. |
||
| 1036 | * If you use $conn->close() directly, the load balancer won't update its state. |
||
| 1037 | * @param DatabaseBase $conn |
||
| 1038 | */ |
||
| 1039 | public function closeConnection( $conn ) { |
||
| 1058 | |||
| 1059 | /** |
||
| 1060 | * Commit transactions on all open connections |
||
| 1061 | * @param string $fname Caller name |
||
| 1062 | */ |
||
| 1063 | public function commitAll( $fname = __METHOD__ ) { |
||
| 1068 | |||
| 1069 | /** |
||
| 1070 | * Perform all pre-commit callbacks that remain part of the atomic transactions |
||
| 1071 | * and disable any post-commit callbacks until runMasterPostCommitCallbacks() |
||
| 1072 | * @since 1.28 |
||
| 1073 | */ |
||
| 1074 | public function runMasterPreCommitCallbacks() { |
||
| 1082 | |||
| 1083 | /** |
||
| 1084 | * Perform all pre-commit checks for things like replication safety |
||
| 1085 | * @param array $options Includes: |
||
| 1086 | * - maxWriteDuration : max write query duration time in seconds |
||
| 1087 | * @throws DBTransactionError |
||
| 1088 | * @since 1.28 |
||
| 1089 | */ |
||
| 1090 | public function approveMasterChanges( array $options ) { |
||
| 1113 | |||
| 1114 | /** |
||
| 1115 | * Issue COMMIT on all master connections where writes where done |
||
| 1116 | * @param string $fname Caller name |
||
| 1117 | */ |
||
| 1118 | public function commitMasterChanges( $fname = __METHOD__ ) { |
||
| 1125 | |||
| 1126 | /** |
||
| 1127 | * Issue all pending post-commit callbacks |
||
| 1128 | * @since 1.28 |
||
| 1129 | */ |
||
| 1130 | public function runMasterPostCommitCallbacks() { |
||
| 1136 | |||
| 1137 | /** |
||
| 1138 | * Issue ROLLBACK only on master, only if queries were done on connection |
||
| 1139 | * @param string $fname Caller name |
||
| 1140 | * @throws DBExpectedError |
||
| 1141 | * @since 1.23 |
||
| 1142 | */ |
||
| 1143 | public function rollbackMasterChanges( $fname = __METHOD__ ) { |
||
| 1169 | |||
| 1170 | /** |
||
| 1171 | * @return bool Whether a master connection is already open |
||
| 1172 | * @since 1.24 |
||
| 1173 | */ |
||
| 1174 | public function hasMasterConnection() { |
||
| 1177 | |||
| 1178 | /** |
||
| 1179 | * Determine if there are pending changes in a transaction by this thread |
||
| 1180 | * @since 1.23 |
||
| 1181 | * @return bool |
||
| 1182 | */ |
||
| 1183 | public function hasMasterChanges() { |
||
| 1198 | |||
| 1199 | /** |
||
| 1200 | * Get the timestamp of the latest write query done by this thread |
||
| 1201 | * @since 1.25 |
||
| 1202 | * @return float|bool UNIX timestamp or false |
||
| 1203 | */ |
||
| 1204 | View Code Duplication | public function lastMasterChangeTimestamp() { |
|
| 1218 | |||
| 1219 | /** |
||
| 1220 | * Check if this load balancer object had any recent or still |
||
| 1221 | * pending writes issued against it by this PHP thread |
||
| 1222 | * |
||
| 1223 | * @param float $age How many seconds ago is "recent" [defaults to mWaitTimeout] |
||
| 1224 | * @return bool |
||
| 1225 | * @since 1.25 |
||
| 1226 | */ |
||
| 1227 | public function hasOrMadeRecentMasterChanges( $age = null ) { |
||
| 1233 | |||
| 1234 | /** |
||
| 1235 | * Get the list of callers that have pending master changes |
||
| 1236 | * |
||
| 1237 | * @return array |
||
| 1238 | * @since 1.27 |
||
| 1239 | */ |
||
| 1240 | View Code Duplication | public function pendingMasterChangeCallers() { |
|
| 1256 | |||
| 1257 | /** |
||
| 1258 | * @param mixed $value |
||
| 1259 | * @return mixed |
||
| 1260 | */ |
||
| 1261 | public function waitTimeout( $value = null ) { |
||
| 1264 | |||
| 1265 | /** |
||
| 1266 | * @note This method will trigger a DB connection if not yet done |
||
| 1267 | * |
||
| 1268 | * @param string|bool $wiki Wiki ID, or false for the current wiki |
||
| 1269 | * @return bool Whether the generic connection for reads is highly "lagged" |
||
| 1270 | */ |
||
| 1271 | public function getLaggedSlaveMode( $wiki = false ) { |
||
| 1287 | |||
| 1288 | /** |
||
| 1289 | * @note This method will never cause a new DB connection |
||
| 1290 | * @return bool Whether any generic connection used for reads was highly "lagged" |
||
| 1291 | * @since 1.27 |
||
| 1292 | */ |
||
| 1293 | public function laggedSlaveUsed() { |
||
| 1296 | |||
| 1297 | /** |
||
| 1298 | * @note This method may trigger a DB connection if not yet done |
||
| 1299 | * @param string|bool $wiki Wiki ID, or false for the current wiki |
||
| 1300 | * @param DatabaseBase|null DB master connection; used to avoid loops [optional] |
||
| 1301 | * @return string|bool Reason the master is read-only or false if it is not |
||
| 1302 | * @since 1.27 |
||
| 1303 | */ |
||
| 1304 | public function getReadOnlyReason( $wiki = false, DatabaseBase $conn = null ) { |
||
| 1321 | |||
| 1322 | /** |
||
| 1323 | * @param string $wiki Wiki ID, or false for the current wiki |
||
| 1324 | * @param DatabaseBase|null DB master connectionl used to avoid loops [optional] |
||
| 1325 | * @return bool |
||
| 1326 | */ |
||
| 1327 | private function masterRunningReadOnly( $wiki, DatabaseBase $conn = null ) { |
||
| 1348 | |||
| 1349 | /** |
||
| 1350 | * Disables/enables lag checks |
||
| 1351 | * @param null|bool $mode |
||
| 1352 | * @return bool |
||
| 1353 | */ |
||
| 1354 | public function allowLagged( $mode = null ) { |
||
| 1362 | |||
| 1363 | /** |
||
| 1364 | * @return bool |
||
| 1365 | */ |
||
| 1366 | public function pingAll() { |
||
| 1376 | |||
| 1377 | /** |
||
| 1378 | * Call a function with each open connection object |
||
| 1379 | * @param callable $callback |
||
| 1380 | * @param array $params |
||
| 1381 | */ |
||
| 1382 | public function forEachOpenConnection( $callback, array $params = [] ) { |
||
| 1392 | |||
| 1393 | /** |
||
| 1394 | * Call a function with each open connection object to a master |
||
| 1395 | * @param callable $callback |
||
| 1396 | * @param array $params |
||
| 1397 | * @since 1.28 |
||
| 1398 | */ |
||
| 1399 | public function forEachOpenMasterConnection( $callback, array $params = [] ) { |
||
| 1411 | |||
| 1412 | /** |
||
| 1413 | * Get the hostname and lag time of the most-lagged slave |
||
| 1414 | * |
||
| 1415 | * This is useful for maintenance scripts that need to throttle their updates. |
||
| 1416 | * May attempt to open connections to slaves on the default DB. If there is |
||
| 1417 | * no lag, the maximum lag will be reported as -1. |
||
| 1418 | * |
||
| 1419 | * @param bool|string $wiki Wiki ID, or false for the default database |
||
| 1420 | * @return array ( host, max lag, index of max lagged host ) |
||
| 1421 | */ |
||
| 1422 | public function getMaxLag( $wiki = false ) { |
||
| 1442 | |||
| 1443 | /** |
||
| 1444 | * Get an estimate of replication lag (in seconds) for each server |
||
| 1445 | * |
||
| 1446 | * Results are cached for a short time in memcached/process cache |
||
| 1447 | * |
||
| 1448 | * Values may be "false" if replication is too broken to estimate |
||
| 1449 | * |
||
| 1450 | * @param string|bool $wiki |
||
| 1451 | * @return int[] Map of (server index => float|int|bool) |
||
| 1452 | */ |
||
| 1453 | public function getLagTimes( $wiki = false ) { |
||
| 1461 | |||
| 1462 | /** |
||
| 1463 | * Get the lag in seconds for a given connection, or zero if this load |
||
| 1464 | * balancer does not have replication enabled. |
||
| 1465 | * |
||
| 1466 | * This should be used in preference to Database::getLag() in cases where |
||
| 1467 | * replication may not be in use, since there is no way to determine if |
||
| 1468 | * replication is in use at the connection level without running |
||
| 1469 | * potentially restricted queries such as SHOW SLAVE STATUS. Using this |
||
| 1470 | * function instead of Database::getLag() avoids a fatal error in this |
||
| 1471 | * case on many installations. |
||
| 1472 | * |
||
| 1473 | * @param IDatabase $conn |
||
| 1474 | * @return int|bool Returns false on error |
||
| 1475 | */ |
||
| 1476 | public function safeGetLag( IDatabase $conn ) { |
||
| 1483 | |||
| 1484 | /** |
||
| 1485 | * Wait for a slave DB to reach a specified master position |
||
| 1486 | * |
||
| 1487 | * This will connect to the master to get an accurate position if $pos is not given |
||
| 1488 | * |
||
| 1489 | * @param IDatabase $conn Slave DB |
||
| 1490 | * @param DBMasterPos|bool $pos Master position; default: current position |
||
| 1491 | * @param integer $timeout Timeout in seconds |
||
| 1492 | * @return bool Success |
||
| 1493 | * @since 1.27 |
||
| 1494 | */ |
||
| 1495 | public function safeWaitForMasterPos( IDatabase $conn, $pos = false, $timeout = 10 ) { |
||
| 1518 | |||
| 1519 | /** |
||
| 1520 | * Clear the cache for slag lag delay times |
||
| 1521 | * |
||
| 1522 | * This is only used for testing |
||
| 1523 | */ |
||
| 1524 | public function clearLagTimeCache() { |
||
| 1527 | } |
||
| 1528 |
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: