78 $this->host = $params[
'host'];
79 $this->protocol = $params[
'protocol'];
80 $this->directory = trim( $params[
'directory'],
'/' );
81 $this->encoding = $params[
'encoding'];
82 $this->skewCacheTTL = $params[
'skewTTL'];
83 $this->baseCacheTTL = max( $params[
'cacheTTL'] - $this->skewCacheTTL, 0 );
84 $this->timeout = $params[
'timeout'];
86 if ( !isset( $params[
'cache'] ) ) {
88 } elseif ( $params[
'cache'] instanceof
BagOStuff ) {
89 $this->srvCache = $params[
'cache'];
91 $this->srvCache = ObjectFactory::getObjectFromSpec( $params[
'cache'] );
94 $this->logger =
new Psr\Log\NullLogger();
96 'connTimeout' => $this->timeout,
97 'reqTimeout' => $this->timeout,
98 'logger' => $this->logger
135 if ( $this->procCache !==
null ) {
139 $now = microtime(
true );
140 $key = $this->srvCache->makeGlobalKey(
149 $loop =
new WaitConditionLoop(
150 function () use ( $key, $now, &$data, &$error ) {
152 $data = $this->srvCache->get( $key );
153 if ( is_array( $data ) && $data[
'expires'] > $now ) {
154 $this->logger->debug(
"Found up-to-date etcd configuration cache." );
156 return WaitConditionLoop::CONDITION_REACHED;
161 if ( $this->srvCache->lock( $key, 0, $this->baseCacheTTL ) ) {
163 $etcdResponse = $this->fetchAllFromEtcd();
164 $error = $etcdResponse[
'error'];
165 if ( is_array( $etcdResponse[
'config'] ) ) {
167 $expiry = microtime( true ) + $this->baseCacheTTL;
169 $expiry += mt_rand( 0, 1e6 ) / 1e6 * $this->skewCacheTTL;
171 'config' => $etcdResponse[
'config'],
172 'expires' => $expiry,
173 'modifiedIndex' => $etcdResponse[
'modifiedIndex']
175 $this->srvCache->set( $key, $data, BagOStuff::TTL_INDEFINITE );
177 $this->logger->info(
"Refreshed stale etcd configuration cache." );
179 return WaitConditionLoop::CONDITION_REACHED;
181 $this->logger->error(
"Failed to fetch configuration: $error" );
182 if ( !$etcdResponse[
'retry'] ) {
184 return WaitConditionLoop::CONDITION_FAILED;
188 $this->srvCache->unlock( $key );
192 if ( is_array( $data ) ) {
193 $this->logger->info(
"Using stale etcd configuration cache." );
195 return WaitConditionLoop::CONDITION_REACHED;
198 return WaitConditionLoop::CONDITION_CONTINUE;
203 if ( $loop->invoke() !== WaitConditionLoop::CONDITION_REACHED ) {
205 throw new ConfigException(
"Failed to load configuration from etcd: $error" );
208 $this->procCache = $data;
217 $servers = $dsd->getServers();
219 return $this->fetchAllFromEtcdServer( $this->host );
224 $server = $dsd->pickServer( $servers );
225 $host = IPUtils::combineHostAndPort( $server[
'target'], $server[
'port'] );
227 $response = $this->fetchAllFromEtcdServer( $host );
228 if ( is_array( $response[
'config'] ) || $response[
'retry'] ) {
233 $servers = $dsd->removeServer( $server, $servers );
234 }
while ( $servers );
245 list( $rcode, $rdesc, , $rbody, $rerr ) = $this->http->run( [
247 'url' =>
"{$this->protocol}://{$address}/v2/keys/{$this->directory}/?recursive=true",
248 'headers' => [
'content-type' =>
'application/json' ]
251 $response = [
'config' =>
null,
'error' =>
null,
'retry' =>
false,
'modifiedIndex' => 0 ];
253 static $terminalCodes = [ 404 =>
true ];
254 if ( $rcode < 200 || $rcode > 399 ) {
255 $response[
'error'] = strlen( $rerr ??
'' ) ? $rerr :
"HTTP $rcode ($rdesc)";
256 $response[
'retry'] = empty( $terminalCodes[$rcode] );
261 $parsedResponse = $this->parseResponse( $rbody );
263 $parsedResponse = [
'error' => $e->getMessage() ];
265 return array_merge( $response, $parsedResponse );
298 $lastModifiedIndex = 0;
299 if ( !isset( $dirNode[
'nodes'] ) ) {
301 "Unexpected JSON response in dir '$dirName'; missing 'nodes' list." );
303 if ( !is_array( $dirNode[
'nodes'] ) ) {
305 "Unexpected JSON response in dir '$dirName'; 'nodes' is not an array." );
308 foreach ( $dirNode[
'nodes'] as $node ) {
309 '@phan-var array $node';
310 $baseName = basename( $node[
'key'] );
311 $fullName = $dirName ===
'' ? $baseName :
"$dirName/$baseName";
312 if ( !empty( $node[
'dir'] ) ) {
313 $lastModifiedIndex = max(
314 $this->parseDirectory( $fullName, $node, $config ),
315 $lastModifiedIndex );
318 if ( !is_array( $value ) || !array_key_exists(
'val', $value ) ) {
321 $lastModifiedIndex = max( $node[
'modifiedIndex'], $lastModifiedIndex );
322 $config[$fullName] = $value[
'val'];
325 return $lastModifiedIndex;