21use Psr\Log\LoggerAwareInterface;
22use Psr\Log\LoggerInterface;
23use Wikimedia\WaitConditionLoop;
30class EtcdConfig implements Config, LoggerAwareInterface {
77 $this->protocol =
$params[
'protocol'];
79 $this->encoding =
$params[
'encoding'];
80 $this->skewCacheTTL =
$params[
'skewTTL'];
81 $this->baseCacheTTL = max(
$params[
'cacheTTL'] - $this->skewCacheTTL, 0 );
82 $this->timeout =
$params[
'timeout'];
84 if ( !isset(
$params[
'cache'] ) ) {
87 $this->srvCache =
$params[
'cache'];
89 $this->srvCache = ObjectFactory::getObjectFromSpec(
$params[
'cache'] );
92 $this->logger =
new Psr\Log\NullLogger();
94 'connTimeout' => $this->timeout,
95 'reqTimeout' => $this->timeout,
96 'logger' => $this->logger
102 $this->
http->setLogger( $logger );
105 public function has( $name ) {
108 return array_key_exists(
$name, $this->procCache[
'config'] );
114 if ( !array_key_exists(
$name, $this->procCache[
'config'] ) ) {
118 return $this->procCache[
'config'][
$name];
125 if ( $this->procCache !==
null ) {
129 $now = microtime(
true );
130 $key = $this->srvCache->makeGlobalKey(
139 $loop =
new WaitConditionLoop(
140 function ()
use ( $key, $now, &$data, &$error ) {
142 $data = $this->srvCache->get( $key );
143 if ( is_array( $data ) && $data[
'expires'] > $now ) {
144 $this->logger->debug(
"Found up-to-date etcd configuration cache." );
146 return WaitConditionLoop::CONDITION_REACHED;
151 if ( $this->srvCache->lock( $key, 0, $this->baseCacheTTL ) ) {
153 list( $config, $error, $retry ) = $this->fetchAllFromEtcd();
154 if ( is_array( $config ) ) {
156 $expiry = microtime( true ) + $this->baseCacheTTL;
157 $expiry += mt_rand( 0, 1e6 ) / 1e6 * $this->skewCacheTTL;
159 $data = [
'config' => $config,
'expires' => $expiry ];
160 $this->srvCache->set( $key, $data, BagOStuff::TTL_INDEFINITE );
162 $this->logger->info(
"Refreshed stale etcd configuration cache." );
164 return WaitConditionLoop::CONDITION_REACHED;
166 $this->logger->error(
"Failed to fetch configuration: $error" );
169 return WaitConditionLoop::CONDITION_FAILED;
173 $this->srvCache->unlock( $key );
177 if ( is_array( $data ) ) {
178 $this->logger->info(
"Using stale etcd configuration cache." );
180 return WaitConditionLoop::CONDITION_REACHED;
183 return WaitConditionLoop::CONDITION_CONTINUE;
188 if ( $loop->invoke() !== WaitConditionLoop::CONDITION_REACHED ) {
190 throw new ConfigException(
"Failed to load configuration from etcd: $error" );
193 $this->procCache = $data;
201 $servers = $dsd->getServers();
203 return $this->fetchAllFromEtcdServer( $this->host );
208 $server = $dsd->pickServer( $servers );
209 $host = IP::combineHostAndPort( $server[
'target'], $server[
'port'] );
211 list( $config, $error, $retry ) = $this->fetchAllFromEtcdServer( $host );
212 if ( is_array( $config ) || !$retry ) {
217 $servers = $dsd->removeServer( $server, $servers );
218 }
while ( $servers );
220 return [ $config, $error, $retry ];
229 list( $rcode, $rdesc, , $rbody, $rerr ) = $this->
http->run( [
231 'url' =>
"{$this->protocol}://{$address}/v2/keys/{$this->directory}/?recursive=true",
232 'headers' => [
'content-type' =>
'application/json' ]
235 static $terminalCodes = [ 404 =>
true ];
236 if ( $rcode < 200 || $rcode > 399 ) {
239 strlen( $rerr ) ? $rerr :
"HTTP $rcode ($rdesc)",
240 empty( $terminalCodes[$rcode] )
244 return [ $this->parseResponse( $rbody ),
null,
false ];
246 return [
null,
$e->getMessage(),
false ];
257 $info = json_decode( $rbody,
true );
258 if ( $info ===
null ) {
261 if ( !isset( $info[
'node'] ) || !is_array( $info[
'node'] ) ) {
263 "Unexpected JSON response: Missing or invalid node at top level." );
266 $this->parseDirectory(
'', $info[
'node'], $config );
279 if ( !isset( $dirNode[
'nodes'] ) ) {
281 "Unexpected JSON response in dir '$dirName'; missing 'nodes' list." );
283 if ( !is_array( $dirNode[
'nodes'] ) ) {
285 "Unexpected JSON response in dir '$dirName'; 'nodes' is not an array." );
288 foreach ( $dirNode[
'nodes']
as $node ) {
289 $baseName = basename( $node[
'key'] );
290 $fullName = $dirName ===
'' ? $baseName :
"$dirName/$baseName";
291 if ( !empty( $node[
'dir'] ) ) {
292 $this->parseDirectory(
$fullName, $node, $config );
295 if ( !is_array(
$value ) || !array_key_exists(
'val',
$value ) ) {
309 if ( $this->encoding ===
'YAML' ) {
310 return yaml_parse( $string );
312 return json_decode( $string,
true );
Apache License January http
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
unserialize( $serialized)
interface is intended to be more or less compatible with the PHP memcached client.
Exceptions for config failures.
Interface for configuration instances.
parseDirectory( $dirName, $dirNode, &$config)
Recursively parse a directory node and populate the array passed by reference, throwing EtcdConfigPar...
setLogger(LoggerInterface $logger)
__construct(array $params)
parseResponse( $rbody)
Parse a response body, throwing EtcdConfigParseError if there is a validation error.
fetchAllFromEtcdServer( $address)
Simple store for keeping values in an associative array for the current process.
Class to handle concurrent HTTP requests.
deferred txt A few of the database updates required by various functions here can be deferred until after the result page is displayed to the user For updating the view updating the linked to tables after a etc PHP does not yet have any way to tell the server to actually return and disconnect while still running these but it might have such a feature in the future We handle these by creating a deferred update object and putting those objects on a global list
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
the array() calling protocol came about after MediaWiki 1.4rc1.
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return true
Allows to change the fields on the form that will be generated $name
processing should stop and the error should be shown to the user * false
returning false will NOT prevent logging $e
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
The most up to date schema for the tables in the database will always be tables sql in the maintenance directory