21 use Psr\Log\LoggerAwareInterface;
22 use Psr\Log\LoggerInterface;
23 use Wikimedia\ObjectFactory;
24 use Wikimedia\WaitConditionLoop;
78 $this->protocol =
$params[
'protocol'];
80 $this->encoding =
$params[
'encoding'];
81 $this->skewCacheTTL =
$params[
'skewTTL'];
82 $this->baseCacheTTL = max(
$params[
'cacheTTL'] - $this->skewCacheTTL, 0 );
83 $this->timeout =
$params[
'timeout'];
85 if ( !isset(
$params[
'cache'] ) ) {
88 $this->srvCache =
$params[
'cache'];
90 $this->srvCache = ObjectFactory::getObjectFromSpec(
$params[
'cache'] );
93 $this->logger =
new Psr\Log\NullLogger();
95 'connTimeout' => $this->timeout,
96 'reqTimeout' => $this->timeout,
97 'logger' => $this->logger
103 $this->
http->setLogger( $logger );
109 return array_key_exists(
$name, $this->procCache[
'config'] );
115 if ( !array_key_exists(
$name, $this->procCache[
'config'] ) ) {
119 return $this->procCache[
'config'][
$name];
124 return $this->procCache[
'modifiedIndex'];
131 if ( $this->procCache !==
null ) {
135 $now = microtime(
true );
136 $key = $this->srvCache->makeGlobalKey(
145 $loop =
new WaitConditionLoop(
146 function ()
use ( $key, $now, &$data, &$error ) {
148 $data = $this->srvCache->get( $key );
149 if ( is_array( $data ) && $data[
'expires'] > $now ) {
150 $this->logger->debug(
"Found up-to-date etcd configuration cache." );
152 return WaitConditionLoop::CONDITION_REACHED;
157 if ( $this->srvCache->lock( $key, 0, $this->baseCacheTTL ) ) {
160 $error = $etcdResponse[
'error'];
161 if ( is_array( $etcdResponse[
'config'] ) ) {
166 'config' => $etcdResponse[
'config'],
167 'expires' => $expiry,
168 'modifiedIndex' => $etcdResponse[
'modifiedIndex']
172 $this->logger->info(
"Refreshed stale etcd configuration cache." );
174 return WaitConditionLoop::CONDITION_REACHED;
176 $this->logger->error(
"Failed to fetch configuration: $error" );
177 if ( !$etcdResponse[
'retry'] ) {
179 return WaitConditionLoop::CONDITION_FAILED;
183 $this->srvCache->unlock( $key );
187 if ( is_array( $data ) ) {
188 $this->logger->info(
"Using stale etcd configuration cache." );
190 return WaitConditionLoop::CONDITION_REACHED;
193 return WaitConditionLoop::CONDITION_CONTINUE;
198 if ( $loop->invoke() !== WaitConditionLoop::CONDITION_REACHED ) {
200 throw new ConfigException(
"Failed to load configuration from etcd: $error" );
203 $this->procCache = $data;
212 $servers = $dsd->getServers();
219 $server = $dsd->pickServer( $servers );
228 $servers = $dsd->removeServer( $server, $servers );
229 }
while ( $servers );
240 list( $rcode, $rdesc, , $rbody, $rerr ) = $this->
http->run( [
242 'url' =>
"{$this->protocol}://{$address}/v2/keys/{$this->directory}/?recursive=true",
243 'headers' => [
'content-type' =>
'application/json' ]
246 $response = [
'config' =>
null,
'error' =>
null,
'retry' =>
false,
'modifiedIndex' => 0 ];
248 static $terminalCodes = [ 404 =>
true ];
249 if ( $rcode < 200 || $rcode > 399 ) {
250 $response[
'error'] = strlen( $rerr ) ? $rerr :
"HTTP $rcode ($rdesc)";
251 $response[
'retry'] = empty( $terminalCodes[$rcode] );
258 $parsedResponse = [
'error' =>
$e->getMessage() ];
260 return array_merge(
$response, $parsedResponse );
270 $info = json_decode( $rbody,
true );
271 if ( $info ===
null ) {
274 if ( !isset( $info[
'node'] ) || !is_array( $info[
'node'] ) ) {
276 "Unexpected JSON response: Missing or invalid node at top level." );
279 $lastModifiedIndex = $this->
parseDirectory(
'', $info[
'node'], $config );
280 return [
'modifiedIndex' => $lastModifiedIndex,
'config' => $config ];
293 $lastModifiedIndex = 0;
294 if ( !isset( $dirNode[
'nodes'] ) ) {
296 "Unexpected JSON response in dir '$dirName'; missing 'nodes' list." );
298 if ( !is_array( $dirNode[
'nodes'] ) ) {
300 "Unexpected JSON response in dir '$dirName'; 'nodes' is not an array." );
303 foreach ( $dirNode[
'nodes']
as $node ) {
304 $baseName = basename( $node[
'key'] );
305 $fullName = $dirName ===
'' ? $baseName :
"$dirName/$baseName";
306 if ( !empty( $node[
'dir'] ) ) {
307 $lastModifiedIndex = max(
309 $lastModifiedIndex );
312 if ( !is_array(
$value ) || !array_key_exists(
'val',
$value ) ) {
315 $lastModifiedIndex = max( $node[
'modifiedIndex'], $lastModifiedIndex );
316 $config[$fullName] =
$value[
'val'];
319 return $lastModifiedIndex;
327 if ( $this->encoding ===
'YAML' ) {
328 return yaml_parse( $string );
330 return json_decode( $string,
true );