| 1 |
<?php |
| 2 |
|
| 3 |
namespace Wikimedia\Rdbms; |
| 4 |
|
| 5 |
use InvalidArgumentException; |
| 6 |
use UnexpectedValueException; |
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
class ServerInfo { |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
public const MAX_LAG_DEFAULT = 6; |
| 20 |
|
| 21 |
public const WRITER_INDEX = 0; |
| 22 |
|
| 23 |
|
| 24 |
private $servers; |
| 25 |
|
| 26 |
public function addServer( $i, $server ) { |
| 27 |
$this->servers[$i] = $server; |
| 28 |
} |
| 29 |
|
| 30 |
public function getServerMaxLag( $i ) { |
| 31 |
return $this->servers[$i]['max lag'] ?? self::MAX_LAG_DEFAULT; |
| 32 |
} |
| 33 |
|
| 34 |
public function getServerDriver( $i ) { |
| 35 |
return $this->servers[$i]['driver'] ?? null; |
| 36 |
} |
| 37 |
|
| 38 |
public function getServerType( $i ) { |
| 39 |
return $this->servers[$i]['type'] ?? 'unknown'; |
| 40 |
} |
| 41 |
|
| 42 |
public function getServerName( $i ): string { |
| 43 |
return $this->servers[$i]['serverName'] ?? 'localhost'; |
| 44 |
} |
| 45 |
|
| 46 |
public function getServerInfo( $i ) { |
| 47 |
return $this->servers[$i] ?? false; |
| 48 |
} |
| 49 |
|
| 50 |
public function getServerCount() { |
| 51 |
return count( $this->servers ); |
| 52 |
} |
| 53 |
|
| 54 |
public function hasServerIndex( $i ) { |
| 55 |
return isset( $this->servers[$i] ); |
| 56 |
} |
| 57 |
|
| 58 |
public function getLagTimes() { |
| 59 |
$knownLagTimes = []; |
| 60 |
$indexesWithLag = []; |
| 61 |
foreach ( $this->servers as $i => $server ) { |
| 62 |
if ( empty( $server['is static'] ) ) { |
| 63 |
$indexesWithLag[] = $i; |
| 64 |
} else { |
| 65 |
$knownLagTimes[$i] = 0; |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
return [ $indexesWithLag, $knownLagTimes ]; |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
public function getServerInfoStrict( $i, $field = null ) { |
| 79 |
if ( !isset( $this->servers[$i] ) || !is_array( $this->servers[$i] ) ) { |
| 80 |
throw new InvalidArgumentException( "No server with index '$i'" ); |
| 81 |
} |
| 82 |
|
| 83 |
if ( $field !== null ) { |
| 84 |
if ( !array_key_exists( $field, $this->servers[$i] ) ) { |
| 85 |
throw new InvalidArgumentException( "No field '$field' in server index '$i'" ); |
| 86 |
} |
| 87 |
|
| 88 |
return $this->servers[$i][$field]; |
| 89 |
} |
| 90 |
|
| 91 |
return $this->servers[$i]; |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
public function getStreamingReplicaIndexes() { |
| 98 |
$indexes = []; |
| 99 |
foreach ( $this->servers as $i => $server ) { |
| 100 |
if ( $i !== self::WRITER_INDEX && empty( $server['is static'] ) ) { |
| 101 |
$indexes[] = $i; |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
return $indexes; |
| 106 |
} |
| 107 |
|
| 108 |
public function hasStreamingReplicaServers() { |
| 109 |
return (bool)$this->getStreamingReplicaIndexes(); |
| 110 |
} |
| 111 |
|
| 112 |
public function reconfigureServers( $paramServers ) { |
| 113 |
$newIndexBySrvName = []; |
| 114 |
$this->normalizeServerMaps( $paramServers, $newIndexBySrvName ); |
| 115 |
|
| 116 |
|
| 117 |
$newIndexByServerIndex = []; |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
foreach ( $this->servers as $i => $server ) { |
| 125 |
$srvName = $this->getServerName( $i ); |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
$newIndex = $newIndexByServerIndex[$i] = $newIndexBySrvName[$srvName] ?? null; |
| 131 |
if ( $newIndex === null ) { |
| 132 |
unset( $this->servers[$i] ); |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
return $newIndexByServerIndex; |
| 137 |
} |
| 138 |
|
| 139 |
public function normalizeServerMaps( array $servers, ?array &$indexBySrvName = null ) { |
| 140 |
if ( !$servers ) { |
| 141 |
throw new InvalidArgumentException( 'Missing or empty "servers" parameter' ); |
| 142 |
} |
| 143 |
|
| 144 |
$listKey = -1; |
| 145 |
$indexBySrvName = []; |
| 146 |
foreach ( $servers as $i => $server ) { |
| 147 |
if ( ++$listKey !== $i ) { |
| 148 |
throw new UnexpectedValueException( 'List expected for "servers" parameter' ); |
| 149 |
} |
| 150 |
$srvName = $server['serverName'] ?? $server['host'] ?? ''; |
| 151 |
$srvName = ( $srvName !== '' ) ? $srvName : 'localhost'; |
| 152 |
if ( isset( $indexBySrvName[$srvName] ) ) { |
| 153 |
|
| 154 |
throw new UnexpectedValueException( 'Duplicate server name "' . $srvName . '"' ); |
| 155 |
} |
| 156 |
$indexBySrvName[$srvName] = $i; |
| 157 |
$servers[$i]['serverName'] = $srvName; |
| 158 |
$servers[$i]['groupLoads'] ??= []; |
| 159 |
} |
| 160 |
return $servers; |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
public function getPrimaryServerName() { |
| 167 |
return $this->getServerName( self::WRITER_INDEX ); |
| 168 |
} |
| 169 |
|
| 170 |
public function hasReplicaServers() { |
| 171 |
return ( $this->getServerCount() > 1 ); |
| 172 |
} |
| 173 |
} |