| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
namespace Wikimedia\Rdbms; |
| 21 |
|
| 22 |
use InvalidArgumentException; |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
class LoadBalancerSingle extends LoadBalancer { |
| 30 |
|
| 31 |
private $conn; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
public function __construct( array $params ) { |
| 40 |
|
| 41 |
$conn = $params['connection'] ?? null; |
| 42 |
if ( !$conn ) { |
| 43 |
throw new InvalidArgumentException( "Missing 'connection' argument." ); |
| 44 |
} |
| 45 |
|
| 46 |
$this->conn = $conn; |
| 47 |
|
| 48 |
parent::__construct( [ |
| 49 |
'servers' => [ [ |
| 50 |
'type' => $conn->getType(), |
| 51 |
'host' => $conn->getServer(), |
| 52 |
'dbname' => $conn->getDBname(), |
| 53 |
'load' => 1, |
| 54 |
] ], |
| 55 |
'trxProfiler' => $params['trxProfiler'] ?? null, |
| 56 |
'srvCache' => $params['srvCache'] ?? null, |
| 57 |
'wanCache' => $params['wanCache'] ?? null, |
| 58 |
'localDomain' => $params['localDomain'] ?? $this->conn->getDomainID(), |
| 59 |
'readOnlyReason' => $params['readOnlyReason'] ?? false, |
| 60 |
'clusterName' => $params['clusterName'] ?? null, |
| 61 |
] ); |
| 62 |
|
| 63 |
if ( isset( $params['readOnlyReason'] ) ) { |
| 64 |
$conn->setLBInfo( $conn::LB_READ_ONLY_REASON, $params['readOnlyReason'] ); |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
public static function newFromConnection( IDatabase $db, array $params = [] ) { |
| 75 |
return new static( array_merge( |
| 76 |
[ 'localDomain' => $db->getDomainID() ], |
| 77 |
$params, |
| 78 |
[ 'connection' => $db ] |
| 79 |
) ); |
| 80 |
} |
| 81 |
|
| 82 |
protected function sanitizeConnectionFlags( $flags, $domain ) { |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
$flags &= ~self::CONN_TRX_AUTOCOMMIT; |
| 87 |
|
| 88 |
return $flags; |
| 89 |
} |
| 90 |
|
| 91 |
protected function reallyOpenConnection( $i, DatabaseDomain $domain, array $lbInfo ) { |
| 92 |
foreach ( $lbInfo as $k => $v ) { |
| 93 |
$this->conn->setLBInfo( $k, $v ); |
| 94 |
} |
| 95 |
|
| 96 |
return $this->conn; |
| 97 |
} |
| 98 |
|
| 99 |
public function __destruct() { |
| 100 |
|
| 101 |
} |
| 102 |
} |