40 private $baseCacheTTL;
42 private $skewCacheTTL;
69 $service = $params[
'service'];
70 $this->host = $params[
'host'];
71 $this->port = $params[
'port'];
72 $this->protocol = $params[
'protocol'];
73 $this->directory = trim( $params[
'directory'],
'/' );
74 $this->skewCacheTTL = $params[
'skewTTL'];
75 $this->baseCacheTTL = max( $params[
'cacheTTL'] - $this->skewCacheTTL, 0 );
76 $this->timeout = $params[
'timeout'];
79 $hostAndPort = IPUtils::splitHostAndPort( $this->host );
82 $this->host = $hostAndPort[0];
84 if ( $hostAndPort[1] ) {
85 $this->port = $hostAndPort[1];
91 if ( preg_match(
'/^_([^\.]+)\._tcp\.(.+)$/', $this->host, $m ) ) {
96 if ( !isset( $params[
'cache'] ) ) {
98 } elseif ( $params[
'cache'] instanceof
BagOStuff ) {
99 $this->srvCache = $params[
'cache'];
101 $this->srvCache = ObjectFactory::getObjectFromSpec( $params[
'cache'] );
105 'connTimeout' => $this->timeout,
106 'reqTimeout' => $this->timeout,
112 public function has( $name ) {
115 return array_key_exists( $name, $this->procCache[
'config'] );
119 public function get( $name ) {
122 if ( !array_key_exists( $name, $this->procCache[
'config'] ) ) {
126 return $this->procCache[
'config'][$name];
131 return (
int)$this->procCache[
'modifiedIndex'];
137 private function load() {
138 if ( $this->procCache !==
null ) {
142 $now = microtime(
true );
143 $key = $this->srvCache->makeGlobalKey(
152 $loop =
new WaitConditionLoop(
153 function () use ( $key, $now, &$data, &$error ) {
155 $data = $this->srvCache->get( $key );
156 if ( is_array( $data ) && $data[
'expires'] > $now ) {
157 return WaitConditionLoop::CONDITION_REACHED;
162 if ( $this->srvCache->lock( $key, 0, $this->baseCacheTTL ) ) {
164 $etcdResponse = $this->fetchAllFromEtcd();
165 $error = $etcdResponse[
'error'];
166 if ( is_array( $etcdResponse[
'config'] ) ) {
168 $expiry = microtime( true ) + $this->baseCacheTTL;
170 $expiry += mt_rand( 0, 1e6 ) / 1e6 * $this->skewCacheTTL;
172 'config' => $etcdResponse[
'config'],
173 'expires' => $expiry,
174 'modifiedIndex' => $etcdResponse[
'modifiedIndex']
176 $this->srvCache->set( $key, $data, BagOStuff::TTL_INDEFINITE );
178 return WaitConditionLoop::CONDITION_REACHED;
180 trigger_error(
"EtcdConfig failed to fetch data: $error", E_USER_WARNING );
181 if ( !$etcdResponse[
'retry'] && !is_array( $data ) ) {
183 return WaitConditionLoop::CONDITION_FAILED;
187 $this->srvCache->unlock( $key );
190 $error =
'lost lock';
193 if ( is_array( $data ) ) {
194 trigger_error(
"EtcdConfig using stale data: $error", E_USER_NOTICE );
196 return WaitConditionLoop::CONDITION_REACHED;
199 return WaitConditionLoop::CONDITION_CONTINUE;
204 if ( $loop->invoke() !== WaitConditionLoop::CONDITION_REACHED ) {
207 throw new ConfigException(
"Failed to load configuration from etcd: $error" );
211 $this->procCache = $data;
218 $servers = $this->dsd->getServers() ?: [ [ $this->host, $this->port ] ];
220 foreach ( $servers as [ $host, $port ] ) {
222 $response = $this->fetchAllFromEtcdServer( $host, $port );
223 if ( is_array( $response[
'config'] ) || $response[
'retry'] ) {
239 if ( $port !==
null ) {
240 $host = IPUtils::combineHostAndPort( $address, $port );
244 [ $rcode, $rdesc, , $rbody, $rerr ] = $this->http->run( [
246 'url' =>
"{$this->protocol}://{$host}/v2/keys/{$this->directory}/?recursive=true",
248 'content-type' =>
'application/json',
252 $response = [
'config' =>
null,
'error' =>
null,
'retry' =>
false,
'modifiedIndex' => 0 ];
254 static $terminalCodes = [ 404 => true ];
255 if ( $rcode < 200 || $rcode > 399 ) {
256 $response[
'error'] = strlen( $rerr ??
'' ) ? $rerr :
"HTTP $rcode ($rdesc)";
257 $response[
'retry'] = empty( $terminalCodes[$rcode] );
262 $parsedResponse = $this->parseResponse( $rbody );
264 $parsedResponse = [
'error' => $e->getMessage() ];
266 return array_merge( $response, $parsedResponse );
277 $info = json_decode( $rbody,
true );
278 if ( $info ===
null ) {
281 if ( !isset( $info[
'node'] ) || !is_array( $info[
'node'] ) ) {
283 "Unexpected JSON response: Missing or invalid node at top level." );
286 $lastModifiedIndex = $this->parseDirectory(
'', $info[
'node'], $config );
287 return [
'modifiedIndex' => $lastModifiedIndex,
'config' => $config ];
301 $lastModifiedIndex = 0;
302 if ( !isset( $dirNode[
'nodes'] ) ) {
304 "Unexpected JSON response in dir '$dirName'; missing 'nodes' list." );
306 if ( !is_array( $dirNode[
'nodes'] ) ) {
308 "Unexpected JSON response in dir '$dirName'; 'nodes' is not an array." );
311 foreach ( $dirNode[
'nodes'] as $node ) {
312 '@phan-var array $node';
313 $baseName = basename( $node[
'key'] );
314 $fullName = $dirName ===
'' ? $baseName :
"$dirName/$baseName";
315 if ( !empty( $node[
'dir'] ) ) {
316 $lastModifiedIndex = max(
317 $this->parseDirectory( $fullName, $node, $config ),
318 $lastModifiedIndex );
320 $value = $this->unserialize( $node[
'value'] );
321 if ( !is_array( $value ) || !array_key_exists(
'val', $value ) ) {
324 $lastModifiedIndex = max( $node[
'modifiedIndex'], $lastModifiedIndex );
325 $config[$fullName] = $value[
'val'];
328 return $lastModifiedIndex;
335 private function unserialize( $string ) {
336 return json_decode( $string,
true );