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 replica DB lag as a factor in replica DB selection */ |
||
| 40 | private $mAllowLagged; |
||
| 41 | /** @var integer Seconds to spend waiting on replica DB 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) replica DB 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 replica DB */ |
||
| 64 | private $laggedReplicaMode = false; |
||
| 65 | /** @var bool Whether the generic reader fell back to a lagged replica DB */ |
||
| 66 | private $allReplicasDownMode = 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 replica DB 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 replica DB |
||
| 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 ) { |
||
| 371 | |||
| 372 | /** |
||
| 373 | * Set the master wait position |
||
| 374 | * If a DB_REPLICA 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 ) { |
||
| 388 | |||
| 389 | /** |
||
| 390 | * Set the master wait position and wait for a "generic" replica DB to catch up to it |
||
| 391 | * |
||
| 392 | * This can be used a faster proxy for waitForAll() |
||
| 393 | * |
||
| 394 | * @param DBMasterPos $pos |
||
| 395 | * @param int $timeout Max seconds to wait; default is mWaitTimeout |
||
| 396 | * @return bool Success (able to connect and no timeouts reached) |
||
| 397 | * @since 1.26 |
||
| 398 | */ |
||
| 399 | public function waitForOne( $pos, $timeout = null ) { |
||
| 419 | |||
| 420 | /** |
||
| 421 | * Set the master wait position and wait for ALL replica DBs to catch up to it |
||
| 422 | * @param DBMasterPos $pos |
||
| 423 | * @param int $timeout Max seconds to wait; default is mWaitTimeout |
||
| 424 | * @return bool Success (able to connect and no timeouts reached) |
||
| 425 | */ |
||
| 426 | public function waitForAll( $pos, $timeout = null ) { |
||
| 439 | |||
| 440 | /** |
||
| 441 | * Get any open connection to a given server index, local or foreign |
||
| 442 | * Returns false if there is no connection open |
||
| 443 | * |
||
| 444 | * @param int $i |
||
| 445 | * @return DatabaseBase|bool False on failure |
||
| 446 | */ |
||
| 447 | public function getAnyOpenConnection( $i ) { |
||
| 456 | |||
| 457 | /** |
||
| 458 | * Wait for a given replica DB to catch up to the master pos stored in $this |
||
| 459 | * @param int $index Server index |
||
| 460 | * @param bool $open Check the server even if a new connection has to be made |
||
| 461 | * @param int $timeout Max seconds to wait; default is mWaitTimeout |
||
| 462 | * @return bool |
||
| 463 | */ |
||
| 464 | protected function doWait( $index, $open = false, $timeout = null ) { |
||
| 521 | |||
| 522 | /** |
||
| 523 | * Get a connection by index |
||
| 524 | * This is the main entry point for this class. |
||
| 525 | * |
||
| 526 | * @param int $i Server index |
||
| 527 | * @param array|string|bool $groups Query group(s), or false for the generic reader |
||
| 528 | * @param string|bool $wiki Wiki ID, or false for the current wiki |
||
| 529 | * |
||
| 530 | * @throws MWException |
||
| 531 | * @return DatabaseBase |
||
| 532 | */ |
||
| 533 | public function getConnection( $i, $groups = [], $wiki = false ) { |
||
| 599 | |||
| 600 | /** |
||
| 601 | * Mark a foreign connection as being available for reuse under a different |
||
| 602 | * DB name or prefix. This mechanism is reference-counted, and must be called |
||
| 603 | * the same number of times as getConnection() to work. |
||
| 604 | * |
||
| 605 | * @param DatabaseBase $conn |
||
| 606 | * @throws MWException |
||
| 607 | */ |
||
| 608 | public function reuseConnection( $conn ) { |
||
| 645 | |||
| 646 | /** |
||
| 647 | * Get a database connection handle reference |
||
| 648 | * |
||
| 649 | * The handle's methods wrap simply wrap those of a DatabaseBase handle |
||
| 650 | * |
||
| 651 | * @see LoadBalancer::getConnection() for parameter information |
||
| 652 | * |
||
| 653 | * @param int $db |
||
| 654 | * @param array|string|bool $groups Query group(s), or false for the generic reader |
||
| 655 | * @param string|bool $wiki Wiki ID, or false for the current wiki |
||
| 656 | * @return DBConnRef |
||
| 657 | */ |
||
| 658 | public function getConnectionRef( $db, $groups = [], $wiki = false ) { |
||
| 661 | |||
| 662 | /** |
||
| 663 | * Get a database connection handle reference without connecting yet |
||
| 664 | * |
||
| 665 | * The handle's methods wrap simply wrap those of a DatabaseBase handle |
||
| 666 | * |
||
| 667 | * @see LoadBalancer::getConnection() for parameter information |
||
| 668 | * |
||
| 669 | * @param int $db |
||
| 670 | * @param array|string|bool $groups Query group(s), or false for the generic reader |
||
| 671 | * @param string|bool $wiki Wiki ID, or false for the current wiki |
||
| 672 | * @return DBConnRef |
||
| 673 | */ |
||
| 674 | public function getLazyConnectionRef( $db, $groups = [], $wiki = false ) { |
||
| 677 | |||
| 678 | /** |
||
| 679 | * Open a connection to the server given by the specified index |
||
| 680 | * Index must be an actual index into the array. |
||
| 681 | * If the server is already open, returns it. |
||
| 682 | * |
||
| 683 | * On error, returns false, and the connection which caused the |
||
| 684 | * error will be available via $this->mErrorConnection. |
||
| 685 | * |
||
| 686 | * @note If disable() was called on this LoadBalancer, this method will throw a DBAccessError. |
||
| 687 | * |
||
| 688 | * @param int $i Server index |
||
| 689 | * @param string|bool $wiki Wiki ID, or false for the current wiki |
||
| 690 | * @return DatabaseBase|bool Returns false on errors |
||
| 691 | */ |
||
| 692 | public function openConnection( $i, $wiki = false ) { |
||
| 723 | |||
| 724 | /** |
||
| 725 | * Open a connection to a foreign DB, or return one if it is already open. |
||
| 726 | * |
||
| 727 | * Increments a reference count on the returned connection which locks the |
||
| 728 | * connection to the requested wiki. This reference count can be |
||
| 729 | * decremented by calling reuseConnection(). |
||
| 730 | * |
||
| 731 | * If a connection is open to the appropriate server already, but with the wrong |
||
| 732 | * database, it will be switched to the right database and returned, as long as |
||
| 733 | * it has been freed first with reuseConnection(). |
||
| 734 | * |
||
| 735 | * On error, returns false, and the connection which caused the |
||
| 736 | * error will be available via $this->mErrorConnection. |
||
| 737 | * |
||
| 738 | * @note If disable() was called on this LoadBalancer, this method will throw a DBAccessError. |
||
| 739 | * |
||
| 740 | * @param int $i Server index |
||
| 741 | * @param string $wiki Wiki ID to open |
||
| 742 | * @return DatabaseBase |
||
| 743 | */ |
||
| 744 | private function openForeignConnection( $i, $wiki ) { |
||
| 800 | |||
| 801 | /** |
||
| 802 | * Test if the specified index represents an open connection |
||
| 803 | * |
||
| 804 | * @param int $index Server index |
||
| 805 | * @access private |
||
| 806 | * @return bool |
||
| 807 | */ |
||
| 808 | private function isOpen( $index ) { |
||
| 815 | |||
| 816 | /** |
||
| 817 | * Really opens a connection. Uncached. |
||
| 818 | * Returns a Database object whether or not the connection was successful. |
||
| 819 | * @access private |
||
| 820 | * |
||
| 821 | * @param array $server |
||
| 822 | * @param bool $dbNameOverride |
||
| 823 | * @throws MWException |
||
| 824 | * @return DatabaseBase |
||
| 825 | */ |
||
| 826 | protected function reallyOpenConnection( $server, $dbNameOverride = false ) { |
||
| 877 | |||
| 878 | /** |
||
| 879 | * @throws DBConnectionError |
||
| 880 | * @return bool |
||
| 881 | */ |
||
| 882 | private function reportConnectionError() { |
||
| 911 | |||
| 912 | /** |
||
| 913 | * @return int |
||
| 914 | * @since 1.26 |
||
| 915 | */ |
||
| 916 | public function getWriterIndex() { |
||
| 919 | |||
| 920 | /** |
||
| 921 | * Returns true if the specified index is a valid server index |
||
| 922 | * |
||
| 923 | * @param string $i |
||
| 924 | * @return bool |
||
| 925 | */ |
||
| 926 | public function haveIndex( $i ) { |
||
| 929 | |||
| 930 | /** |
||
| 931 | * Returns true if the specified index is valid and has non-zero load |
||
| 932 | * |
||
| 933 | * @param string $i |
||
| 934 | * @return bool |
||
| 935 | */ |
||
| 936 | public function isNonZeroLoad( $i ) { |
||
| 939 | |||
| 940 | /** |
||
| 941 | * Get the number of defined servers (not the number of open connections) |
||
| 942 | * |
||
| 943 | * @return int |
||
| 944 | */ |
||
| 945 | public function getServerCount() { |
||
| 948 | |||
| 949 | /** |
||
| 950 | * Get the host name or IP address of the server with the specified index |
||
| 951 | * Prefer a readable name if available. |
||
| 952 | * @param string $i |
||
| 953 | * @return string |
||
| 954 | */ |
||
| 955 | public function getServerName( $i ) { |
||
| 966 | |||
| 967 | /** |
||
| 968 | * Return the server info structure for a given index, or false if the index is invalid. |
||
| 969 | * @param int $i |
||
| 970 | * @return array|bool |
||
| 971 | */ |
||
| 972 | public function getServerInfo( $i ) { |
||
| 979 | |||
| 980 | /** |
||
| 981 | * Sets the server info structure for the given index. Entry at index $i |
||
| 982 | * is created if it doesn't exist |
||
| 983 | * @param int $i |
||
| 984 | * @param array $serverInfo |
||
| 985 | */ |
||
| 986 | public function setServerInfo( $i, array $serverInfo ) { |
||
| 989 | |||
| 990 | /** |
||
| 991 | * Get the current master position for chronology control purposes |
||
| 992 | * @return mixed |
||
| 993 | */ |
||
| 994 | public function getMasterPos() { |
||
| 1012 | |||
| 1013 | /** |
||
| 1014 | * Disable this load balancer. All connections are closed, and any attempt to |
||
| 1015 | * open a new connection will result in a DBAccessError. |
||
| 1016 | * |
||
| 1017 | * @since 1.27 |
||
| 1018 | */ |
||
| 1019 | public function disable() { |
||
| 1023 | |||
| 1024 | /** |
||
| 1025 | * Close all open connections |
||
| 1026 | */ |
||
| 1027 | public function closeAll() { |
||
| 1039 | |||
| 1040 | /** |
||
| 1041 | * Close a connection |
||
| 1042 | * Using this function makes sure the LoadBalancer knows the connection is closed. |
||
| 1043 | * If you use $conn->close() directly, the load balancer won't update its state. |
||
| 1044 | * @param DatabaseBase $conn |
||
| 1045 | */ |
||
| 1046 | public function closeConnection( $conn ) { |
||
| 1065 | |||
| 1066 | /** |
||
| 1067 | * Commit transactions on all open connections |
||
| 1068 | * @param string $fname Caller name |
||
| 1069 | * @throws DBExpectedError |
||
| 1070 | */ |
||
| 1071 | public function commitAll( $fname = __METHOD__ ) { |
||
| 1097 | |||
| 1098 | /** |
||
| 1099 | * Perform all pre-commit callbacks that remain part of the atomic transactions |
||
| 1100 | * and disable any post-commit callbacks until runMasterPostTrxCallbacks() |
||
| 1101 | * @since 1.28 |
||
| 1102 | */ |
||
| 1103 | public function finalizeMasterChanges() { |
||
| 1112 | |||
| 1113 | /** |
||
| 1114 | * Perform all pre-commit checks for things like replication safety |
||
| 1115 | * @param array $options Includes: |
||
| 1116 | * - maxWriteDuration : max write query duration time in seconds |
||
| 1117 | * @throws DBTransactionError |
||
| 1118 | * @since 1.28 |
||
| 1119 | */ |
||
| 1120 | public function approveMasterChanges( array $options ) { |
||
| 1151 | |||
| 1152 | /** |
||
| 1153 | * Flush any master transaction snapshots and set DBO_TRX (if DBO_DEFAULT is set) |
||
| 1154 | * |
||
| 1155 | * The DBO_TRX setting will be reverted to the default in each of these methods: |
||
| 1156 | * - commitMasterChanges() |
||
| 1157 | * - rollbackMasterChanges() |
||
| 1158 | * - commitAll() |
||
| 1159 | * This allows for custom transaction rounds from any outer transaction scope. |
||
| 1160 | * |
||
| 1161 | * @param string $fname |
||
| 1162 | * @throws DBExpectedError |
||
| 1163 | * @since 1.28 |
||
| 1164 | */ |
||
| 1165 | public function beginMasterChanges( $fname = __METHOD__ ) { |
||
| 1196 | |||
| 1197 | /** |
||
| 1198 | * Issue COMMIT on all master connections where writes where done |
||
| 1199 | * @param string $fname Caller name |
||
| 1200 | * @throws DBExpectedError |
||
| 1201 | */ |
||
| 1202 | public function commitMasterChanges( $fname = __METHOD__ ) { |
||
| 1232 | |||
| 1233 | /** |
||
| 1234 | * Issue all pending post-COMMIT/ROLLBACK callbacks |
||
| 1235 | * @param integer $type IDatabase::TRIGGER_* constant |
||
| 1236 | * @return Exception|null The first exception or null if there were none |
||
| 1237 | * @since 1.28 |
||
| 1238 | */ |
||
| 1239 | public function runMasterPostTrxCallbacks( $type ) { |
||
| 1259 | |||
| 1260 | /** |
||
| 1261 | * Issue ROLLBACK only on master, only if queries were done on connection |
||
| 1262 | * @param string $fname Caller name |
||
| 1263 | * @throws DBExpectedError |
||
| 1264 | * @since 1.23 |
||
| 1265 | */ |
||
| 1266 | public function rollbackMasterChanges( $fname = __METHOD__ ) { |
||
| 1280 | |||
| 1281 | /** |
||
| 1282 | * Suppress all pending post-COMMIT/ROLLBACK callbacks |
||
| 1283 | * @return Exception|null The first exception or null if there were none |
||
| 1284 | * @since 1.28 |
||
| 1285 | */ |
||
| 1286 | public function suppressTransactionEndCallbacks() { |
||
| 1291 | |||
| 1292 | /** |
||
| 1293 | * @param DatabaseBase $conn |
||
| 1294 | */ |
||
| 1295 | private function applyTransactionRoundFlags( DatabaseBase $conn ) { |
||
| 1305 | |||
| 1306 | /** |
||
| 1307 | * @param DatabaseBase $conn |
||
| 1308 | */ |
||
| 1309 | private function undoTransactionRoundFlags( DatabaseBase $conn ) { |
||
| 1314 | |||
| 1315 | /** |
||
| 1316 | * Commit all replica DB transactions so as to flush any REPEATABLE-READ or SSI snapshot |
||
| 1317 | * |
||
| 1318 | * @param string $fname Caller name |
||
| 1319 | * @since 1.28 |
||
| 1320 | */ |
||
| 1321 | public function flushReplicaSnapshots( $fname = __METHOD__ ) { |
||
| 1326 | |||
| 1327 | /** |
||
| 1328 | * @return bool Whether a master connection is already open |
||
| 1329 | * @since 1.24 |
||
| 1330 | */ |
||
| 1331 | public function hasMasterConnection() { |
||
| 1334 | |||
| 1335 | /** |
||
| 1336 | * Determine if there are pending changes in a transaction by this thread |
||
| 1337 | * @since 1.23 |
||
| 1338 | * @return bool |
||
| 1339 | */ |
||
| 1340 | public function hasMasterChanges() { |
||
| 1355 | |||
| 1356 | /** |
||
| 1357 | * Get the timestamp of the latest write query done by this thread |
||
| 1358 | * @since 1.25 |
||
| 1359 | * @return float|bool UNIX timestamp or false |
||
| 1360 | */ |
||
| 1361 | View Code Duplication | public function lastMasterChangeTimestamp() { |
|
| 1375 | |||
| 1376 | /** |
||
| 1377 | * Check if this load balancer object had any recent or still |
||
| 1378 | * pending writes issued against it by this PHP thread |
||
| 1379 | * |
||
| 1380 | * @param float $age How many seconds ago is "recent" [defaults to mWaitTimeout] |
||
| 1381 | * @return bool |
||
| 1382 | * @since 1.25 |
||
| 1383 | */ |
||
| 1384 | public function hasOrMadeRecentMasterChanges( $age = null ) { |
||
| 1390 | |||
| 1391 | /** |
||
| 1392 | * Get the list of callers that have pending master changes |
||
| 1393 | * |
||
| 1394 | * @return array |
||
| 1395 | * @since 1.27 |
||
| 1396 | */ |
||
| 1397 | View Code Duplication | public function pendingMasterChangeCallers() { |
|
| 1413 | |||
| 1414 | /** |
||
| 1415 | * @param mixed $value |
||
| 1416 | * @return mixed |
||
| 1417 | */ |
||
| 1418 | public function waitTimeout( $value = null ) { |
||
| 1421 | |||
| 1422 | /** |
||
| 1423 | * @note This method will trigger a DB connection if not yet done |
||
| 1424 | * @param string|bool $wiki Wiki ID, or false for the current wiki |
||
| 1425 | * @return bool Whether the generic connection for reads is highly "lagged" |
||
| 1426 | */ |
||
| 1427 | public function getLaggedReplicaMode( $wiki = false ) { |
||
| 1443 | |||
| 1444 | /** |
||
| 1445 | * @param bool $wiki |
||
| 1446 | * @return bool |
||
| 1447 | * @deprecated 1.28; use getLaggedReplicaMode() |
||
| 1448 | */ |
||
| 1449 | public function getLaggedSlaveMode( $wiki = false ) { |
||
| 1452 | |||
| 1453 | /** |
||
| 1454 | * @note This method will never cause a new DB connection |
||
| 1455 | * @return bool Whether any generic connection used for reads was highly "lagged" |
||
| 1456 | * @since 1.28 |
||
| 1457 | */ |
||
| 1458 | public function laggedReplicaUsed() { |
||
| 1461 | |||
| 1462 | /** |
||
| 1463 | * @return bool |
||
| 1464 | * @since 1.27 |
||
| 1465 | * @deprecated Since 1.28; use laggedReplicaUsed() |
||
| 1466 | */ |
||
| 1467 | public function laggedSlaveUsed() { |
||
| 1470 | |||
| 1471 | /** |
||
| 1472 | * @note This method may trigger a DB connection if not yet done |
||
| 1473 | * @param string|bool $wiki Wiki ID, or false for the current wiki |
||
| 1474 | * @param DatabaseBase|null DB master connection; used to avoid loops [optional] |
||
| 1475 | * @return string|bool Reason the master is read-only or false if it is not |
||
| 1476 | * @since 1.27 |
||
| 1477 | */ |
||
| 1478 | public function getReadOnlyReason( $wiki = false, DatabaseBase $conn = null ) { |
||
| 1495 | |||
| 1496 | /** |
||
| 1497 | * @param string $wiki Wiki ID, or false for the current wiki |
||
| 1498 | * @param DatabaseBase|null DB master connectionl used to avoid loops [optional] |
||
| 1499 | * @return bool |
||
| 1500 | */ |
||
| 1501 | private function masterRunningReadOnly( $wiki, DatabaseBase $conn = null ) { |
||
| 1522 | |||
| 1523 | /** |
||
| 1524 | * Disables/enables lag checks |
||
| 1525 | * @param null|bool $mode |
||
| 1526 | * @return bool |
||
| 1527 | */ |
||
| 1528 | public function allowLagged( $mode = null ) { |
||
| 1536 | |||
| 1537 | /** |
||
| 1538 | * @return bool |
||
| 1539 | */ |
||
| 1540 | public function pingAll() { |
||
| 1550 | |||
| 1551 | /** |
||
| 1552 | * Call a function with each open connection object |
||
| 1553 | * @param callable $callback |
||
| 1554 | * @param array $params |
||
| 1555 | */ |
||
| 1556 | View Code Duplication | public function forEachOpenConnection( $callback, array $params = [] ) { |
|
| 1566 | |||
| 1567 | /** |
||
| 1568 | * Call a function with each open connection object to a master |
||
| 1569 | * @param callable $callback |
||
| 1570 | * @param array $params |
||
| 1571 | * @since 1.28 |
||
| 1572 | */ |
||
| 1573 | public function forEachOpenMasterConnection( $callback, array $params = [] ) { |
||
| 1585 | |||
| 1586 | /** |
||
| 1587 | * Call a function with each open replica DB connection object |
||
| 1588 | * @param callable $callback |
||
| 1589 | * @param array $params |
||
| 1590 | * @since 1.28 |
||
| 1591 | */ |
||
| 1592 | View Code Duplication | public function forEachOpenReplicaConnection( $callback, array $params = [] ) { |
|
| 1605 | |||
| 1606 | /** |
||
| 1607 | * Get the hostname and lag time of the most-lagged replica DB |
||
| 1608 | * |
||
| 1609 | * This is useful for maintenance scripts that need to throttle their updates. |
||
| 1610 | * May attempt to open connections to replica DBs on the default DB. If there is |
||
| 1611 | * no lag, the maximum lag will be reported as -1. |
||
| 1612 | * |
||
| 1613 | * @param bool|string $wiki Wiki ID, or false for the default database |
||
| 1614 | * @return array ( host, max lag, index of max lagged host ) |
||
| 1615 | */ |
||
| 1616 | public function getMaxLag( $wiki = false ) { |
||
| 1636 | |||
| 1637 | /** |
||
| 1638 | * Get an estimate of replication lag (in seconds) for each server |
||
| 1639 | * |
||
| 1640 | * Results are cached for a short time in memcached/process cache |
||
| 1641 | * |
||
| 1642 | * Values may be "false" if replication is too broken to estimate |
||
| 1643 | * |
||
| 1644 | * @param string|bool $wiki |
||
| 1645 | * @return int[] Map of (server index => float|int|bool) |
||
| 1646 | */ |
||
| 1647 | public function getLagTimes( $wiki = false ) { |
||
| 1655 | |||
| 1656 | /** |
||
| 1657 | * Get the lag in seconds for a given connection, or zero if this load |
||
| 1658 | * balancer does not have replication enabled. |
||
| 1659 | * |
||
| 1660 | * This should be used in preference to Database::getLag() in cases where |
||
| 1661 | * replication may not be in use, since there is no way to determine if |
||
| 1662 | * replication is in use at the connection level without running |
||
| 1663 | * potentially restricted queries such as SHOW SLAVE STATUS. Using this |
||
| 1664 | * function instead of Database::getLag() avoids a fatal error in this |
||
| 1665 | * case on many installations. |
||
| 1666 | * |
||
| 1667 | * @param IDatabase $conn |
||
| 1668 | * @return int|bool Returns false on error |
||
| 1669 | */ |
||
| 1670 | public function safeGetLag( IDatabase $conn ) { |
||
| 1677 | |||
| 1678 | /** |
||
| 1679 | * Wait for a replica DB to reach a specified master position |
||
| 1680 | * |
||
| 1681 | * This will connect to the master to get an accurate position if $pos is not given |
||
| 1682 | * |
||
| 1683 | * @param IDatabase $conn Replica DB |
||
| 1684 | * @param DBMasterPos|bool $pos Master position; default: current position |
||
| 1685 | * @param integer $timeout Timeout in seconds |
||
| 1686 | * @return bool Success |
||
| 1687 | * @since 1.27 |
||
| 1688 | */ |
||
| 1689 | public function safeWaitForMasterPos( IDatabase $conn, $pos = false, $timeout = 10 ) { |
||
| 1712 | |||
| 1713 | /** |
||
| 1714 | * Clear the cache for slag lag delay times |
||
| 1715 | * |
||
| 1716 | * This is only used for testing |
||
| 1717 | */ |
||
| 1718 | public function clearLagTimeCache() { |
||
| 1721 | |||
| 1722 | /** |
||
| 1723 | * Set a callback via DatabaseBase::setTransactionListener() on |
||
| 1724 | * all current and future master connections of this load balancer |
||
| 1725 | * |
||
| 1726 | * @param string $name Callback name |
||
| 1727 | * @param callable|null $callback |
||
| 1728 | * @since 1.28 |
||
| 1729 | */ |
||
| 1730 | public function setTransactionListener( $name, callable $callback = null ) { |
||
| 1742 | } |
||
| 1743 |
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: