use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerInterface;
+use Wikimedia\ObjectFactory;
use Wikimedia\WaitConditionLoop;
/**
return $this->procCache['config'][$name];
}
+ public function getModifiedIndex() {
+ $this->load();
+ return $this->procCache['modifiedIndex'];
+ }
+
/**
* @throws ConfigException
*/
// refresh the cache from etcd, using a mutex to reduce stampedes...
if ( $this->srvCache->lock( $key, 0, $this->baseCacheTTL ) ) {
try {
- list( $config, $error, $retry ) = $this->fetchAllFromEtcd();
- if ( is_array( $config ) ) {
+ $etcdResponse = $this->fetchAllFromEtcd();
+ $error = $etcdResponse['error'];
+ if ( is_array( $etcdResponse['config'] ) ) {
// Avoid having all servers expire cache keys at the same time
$expiry = microtime( true ) + $this->baseCacheTTL;
$expiry += mt_rand( 0, 1e6 ) / 1e6 * $this->skewCacheTTL;
-
- $data = [ 'config' => $config, 'expires' => $expiry ];
+ $data = [
+ 'config' => $etcdResponse['config'],
+ 'expires' => $expiry,
+ 'modifiedIndex' => $etcdResponse['modifiedIndex']
+ ];
$this->srvCache->set( $key, $data, BagOStuff::TTL_INDEFINITE );
$this->logger->info( "Refreshed stale etcd configuration cache." );
return WaitConditionLoop::CONDITION_REACHED;
} else {
$this->logger->error( "Failed to fetch configuration: $error" );
- if ( !$retry ) {
+ if ( !$etcdResponse['retry'] ) {
// Fail fast since the error is likely to keep happening
return WaitConditionLoop::CONDITION_FAILED;
}
}
/**
- * @return array (config array or null, error string, allow retries)
+ * @return array (containing the keys config, error, retry, modifiedIndex)
*/
public function fetchAllFromEtcd() {
+ // TODO: inject DnsSrvDiscoverer in order to be able to test this method
$dsd = new DnsSrvDiscoverer( $this->host );
$servers = $dsd->getServers();
if ( !$servers ) {
$server = $dsd->pickServer( $servers );
$host = IP::combineHostAndPort( $server['target'], $server['port'] );
// Try to load the config from this particular server
- list( $config, $error, $retry ) = $this->fetchAllFromEtcdServer( $host );
- if ( is_array( $config ) || !$retry ) {
+ $response = $this->fetchAllFromEtcdServer( $host );
+ if ( is_array( $response['config'] ) || $response['retry'] ) {
break;
}
$servers = $dsd->removeServer( $server, $servers );
} while ( $servers );
- return [ $config, $error, $retry ];
+ return $response;
}
/**
* @param string $address Host and port
- * @return array (config array or null, error string, whether to allow retries)
+ * @return array (containing the keys config, error, retry, modifiedIndex)
*/
protected function fetchAllFromEtcdServer( $address ) {
// Retrieve all the values under the MediaWiki config directory
'headers' => [ 'content-type' => 'application/json' ]
] );
+ $response = [ 'config' => null, 'error' => null, 'retry' => false, 'modifiedIndex' => 0 ];
+
static $terminalCodes = [ 404 => true ];
if ( $rcode < 200 || $rcode > 399 ) {
- return [
- null,
- strlen( $rerr ) ? $rerr : "HTTP $rcode ($rdesc)",
- empty( $terminalCodes[$rcode] )
- ];
+ $response['error'] = strlen( $rerr ) ? $rerr : "HTTP $rcode ($rdesc)";
+ $response['retry'] = empty( $terminalCodes[$rcode] );
+ return $response;
}
+
try {
- return [ $this->parseResponse( $rbody ), null, false ];
+ $parsedResponse = $this->parseResponse( $rbody );
} catch ( EtcdConfigParseError $e ) {
- return [ null, $e->getMessage(), false ];
+ $parsedResponse = [ 'error' => $e->getMessage() ];
}
+ return array_merge( $response, $parsedResponse );
}
/**
"Unexpected JSON response: Missing or invalid node at top level." );
}
$config = [];
- $this->parseDirectory( '', $info['node'], $config );
- return $config;
+ $lastModifiedIndex = $this->parseDirectory( '', $info['node'], $config );
+ return [ 'modifiedIndex' => $lastModifiedIndex, 'config' => $config ];
}
/**
* @param string $dirName The relative directory name
* @param array $dirNode The decoded directory node
* @param array &$config The output array
+ * @return int lastModifiedIndex The maximum last modified index across all keys in the directory
*/
protected function parseDirectory( $dirName, $dirNode, &$config ) {
+ $lastModifiedIndex = 0;
if ( !isset( $dirNode['nodes'] ) ) {
throw new EtcdConfigParseError(
"Unexpected JSON response in dir '$dirName'; missing 'nodes' list." );
$baseName = basename( $node['key'] );
$fullName = $dirName === '' ? $baseName : "$dirName/$baseName";
if ( !empty( $node['dir'] ) ) {
- $this->parseDirectory( $fullName, $node, $config );
+ $lastModifiedIndex = max(
+ $this->parseDirectory( $fullName, $node, $config ),
+ $lastModifiedIndex );
} else {
$value = $this->unserialize( $node['value'] );
if ( !is_array( $value ) || !array_key_exists( 'val', $value ) ) {
throw new EtcdConfigParseError( "Failed to parse value for '$fullName'." );
}
-
+ $lastModifiedIndex = max( $node['modifiedIndex'], $lastModifiedIndex );
$config[$fullName] = $value['val'];
}
}
+ return $lastModifiedIndex;
}
/**