config: Use less generic cache key, and not fragmented by wiki
authorTimo Tijhof <krinklemail@gmail.com>
Mon, 1 May 2017 21:51:57 +0000 (14:51 -0700)
committerTimo Tijhof <krinklemail@gmail.com>
Mon, 1 May 2017 22:06:20 +0000 (15:06 -0700)
* Use makeGlobalKey instead of makeKey to avoid fragmenting
  the cache by the wiki-specific key space.

* Add "EtcdConfig" and $this->host to the cache key to make
  it less generic and less likely to conflict with unrelated values
  in case multiple instances exist somewhere in the MediaWiki run
  time (or during testing/debugging).

Adding $this->host should be fine given we were already including
the directory within that host. Although I do recognise that we
may want to encourage slow cache rollover instead of instance
cache rollover in case the host name needs to changed. Perhaps
we should have some kind of canonical name for the instance for
the purpose of caching so that directory or host can be changed
without changing the cache key, and thus have cache roll over
normally over the course of 10 seconds, instead of immediately.

On the other hand, deployment will likely already spread things
out a little bit.

In addition, due to the cache being kept indefinitely this means
the old key will stay indefinitely. Perhaps not an issue given
it's just one key, which will clear on restart eventually?

Bug: T156924
Change-Id: I622e6618f2d4171626e4b272bcb11a97a85cb770

includes/config/EtcdConfig.php

index f226f17..fd5c3f7 100644 (file)
@@ -54,8 +54,6 @@ class EtcdConfig implements Config, LoggerAwareInterface {
        private $skewCacheTTL;
        /** @var integer */
        private $timeout;
-       /** @var string */
-       private $directoryHash;
 
        /**
         * @param array $params Parameter map:
@@ -81,7 +79,6 @@ class EtcdConfig implements Config, LoggerAwareInterface {
                $this->host = $params['host'];
                $this->protocol = $params['protocol'];
                $this->directory = trim( $params['directory'], '/' );
-               $this->directoryHash = sha1( $this->directory );
                $this->encoding = $params['encoding'];
                $this->skewCacheTTL = $params['skewTTL'];
                $this->baseCacheTTL = max( $params['cacheTTL'] - $this->skewCacheTTL, 0 );
@@ -131,7 +128,11 @@ class EtcdConfig implements Config, LoggerAwareInterface {
                }
 
                $now = microtime( true );
-               $key = $this->srvCache->makeKey( 'variable', $this->directoryHash );
+               $key = $this->srvCache->makeGlobalKey(
+                       __CLASS__,
+                       $this->host,
+                       $this->directory
+               );
 
                // Get the cached value or block until it is regenerated (by this or another thread)...
                $data = null; // latest config info