59 private $baseCacheTTL;
61 private $skewCacheTTL;
88 $this->service =
$params[
'service'];
91 $this->protocol =
$params[
'protocol'];
92 $this->directory = trim(
$params[
'directory'],
'/' );
93 $this->skewCacheTTL =
$params[
'skewTTL'];
94 $this->baseCacheTTL = max(
$params[
'cacheTTL'] - $this->skewCacheTTL, 0 );
95 $this->timeout =
$params[
'timeout'];
98 $hostAndPort = IPUtils::splitHostAndPort( $this->host );
100 if ( $hostAndPort ) {
101 $this->host = $hostAndPort[0];
103 if ( $hostAndPort[1] ) {
104 $this->port = $hostAndPort[1];
110 if ( preg_match(
'/^_([^\.]+)\._tcp\.(.+)$/', $this->host, $m ) ) {
111 $this->service = $m[1];
115 if ( !isset(
$params[
'cache'] ) ) {
118 $this->srvCache =
$params[
'cache'];
120 $this->srvCache = ObjectFactory::getObjectFromSpec(
$params[
'cache'] );
124 'connTimeout' => $this->timeout,
125 'reqTimeout' => $this->timeout,
134 trigger_error( __METHOD__ .
' is deprecated since 1.41', E_USER_DEPRECATED );
137 public function has( $name ) {
140 return array_key_exists( $name, $this->procCache[
'config'] );
143 public function get( $name ) {
146 if ( !array_key_exists( $name, $this->procCache[
'config'] ) ) {
150 return $this->procCache[
'config'][$name];
155 return $this->procCache[
'modifiedIndex'];
161 private function load() {
162 if ( $this->procCache !==
null ) {
166 $now = microtime(
true );
167 $key = $this->srvCache->makeGlobalKey(
176 $loop =
new WaitConditionLoop(
177 function () use ( $key, $now, &$data, &$error ) {
179 $data = $this->srvCache->get( $key );
180 if ( is_array( $data ) && $data[
'expires'] > $now ) {
181 return WaitConditionLoop::CONDITION_REACHED;
186 if ( $this->srvCache->lock( $key, 0, $this->baseCacheTTL ) ) {
188 $etcdResponse = $this->fetchAllFromEtcd();
189 $error = $etcdResponse[
'error'];
190 if ( is_array( $etcdResponse[
'config'] ) ) {
192 $expiry = microtime( true ) + $this->baseCacheTTL;
194 $expiry += mt_rand( 0, 1e6 ) / 1e6 * $this->skewCacheTTL;
196 'config' => $etcdResponse[
'config'],
197 'expires' => $expiry,
198 'modifiedIndex' => $etcdResponse[
'modifiedIndex']
200 $this->srvCache->set( $key, $data, BagOStuff::TTL_INDEFINITE );
202 return WaitConditionLoop::CONDITION_REACHED;
204 trigger_error(
"EtcdConfig failed to fetch data: $error", E_USER_WARNING );
205 if ( !$etcdResponse[
'retry'] && !is_array( $data ) ) {
207 return WaitConditionLoop::CONDITION_FAILED;
211 $this->srvCache->unlock( $key );
214 $error =
'lost lock';
217 if ( is_array( $data ) ) {
218 trigger_error(
"EtcdConfig using stale data: $error", E_USER_NOTICE );
220 return WaitConditionLoop::CONDITION_REACHED;
223 return WaitConditionLoop::CONDITION_CONTINUE;
228 if ( $loop->invoke() !== WaitConditionLoop::CONDITION_REACHED ) {
231 throw new ConfigException(
"Failed to load configuration from etcd: $error" );
235 $this->procCache = $data;
242 $servers = $this->dsd->getServers() ?: [ [ $this->host, $this->port ] ];
244 foreach ( $servers as [ $host, $port ] ) {
246 $response = $this->fetchAllFromEtcdServer( $host, $port );
247 if ( is_array( $response[
'config'] ) || $response[
'retry'] ) {
263 if ( $port !==
null ) {
264 $host = IPUtils::combineHostAndPort( $address, $port );
268 [ $rcode, $rdesc, , $rbody, $rerr ] = $this->http->run( [
270 'url' =>
"{$this->protocol}://{$host}/v2/keys/{$this->directory}/?recursive=true",
272 'content-type' =>
'application/json',
276 $response = [
'config' =>
null,
'error' =>
null,
'retry' =>
false,
'modifiedIndex' => 0 ];
278 static $terminalCodes = [ 404 => true ];
279 if ( $rcode < 200 || $rcode > 399 ) {
280 $response[
'error'] = strlen( $rerr ??
'' ) ? $rerr :
"HTTP $rcode ($rdesc)";
281 $response[
'retry'] = empty( $terminalCodes[$rcode] );
286 $parsedResponse = $this->parseResponse( $rbody );
288 $parsedResponse = [
'error' => $e->getMessage() ];
290 return array_merge( $response, $parsedResponse );
300 $info = json_decode( $rbody,
true );
301 if ( $info ===
null ) {
304 if ( !isset( $info[
'node'] ) || !is_array( $info[
'node'] ) ) {
306 "Unexpected JSON response: Missing or invalid node at top level." );
309 $lastModifiedIndex = $this->parseDirectory(
'', $info[
'node'], $config );
310 return [
'modifiedIndex' => $lastModifiedIndex,
'config' => $config ];
323 $lastModifiedIndex = 0;
324 if ( !isset( $dirNode[
'nodes'] ) ) {
326 "Unexpected JSON response in dir '$dirName'; missing 'nodes' list." );
328 if ( !is_array( $dirNode[
'nodes'] ) ) {
330 "Unexpected JSON response in dir '$dirName'; 'nodes' is not an array." );
333 foreach ( $dirNode[
'nodes'] as $node ) {
334 '@phan-var array $node';
335 $baseName = basename( $node[
'key'] );
336 $fullName = $dirName ===
'' ? $baseName :
"$dirName/$baseName";
337 if ( !empty( $node[
'dir'] ) ) {
338 $lastModifiedIndex = max(
339 $this->parseDirectory( $fullName, $node, $config ),
340 $lastModifiedIndex );
342 $value = $this->unserialize( $node[
'value'] );
343 if ( !is_array( $value ) || !array_key_exists(
'val', $value ) ) {
346 $lastModifiedIndex = max( $node[
'modifiedIndex'], $lastModifiedIndex );
347 $config[$fullName] = $value[
'val'];
350 return $lastModifiedIndex;
357 private function unserialize( $string ) {
358 return json_decode( $string,
true );