From: Aaron Schulz Date: Thu, 8 Sep 2016 05:47:37 +0000 (-0700) Subject: Move PECL memcached class to /libs X-Git-Tag: 1.31.0-rc.0~5598^2 X-Git-Url: http://git.cyclocoop.org/%22.%24info%5B?a=commitdiff_plain;h=05589cad6b1394ff5248f48ab150a2edef7ccd9a;p=lhc%2Fweb%2Fwiklou.git Move PECL memcached class to /libs Remove IP class dependency, which is simple enough here Change-Id: I3aed6ae6747d8d2b9e75b492e5d5293ba4ce9edd --- diff --git a/autoload.php b/autoload.php index 71f1809db9..309a7d0b55 100644 --- a/autoload.php +++ b/autoload.php @@ -910,7 +910,7 @@ $wgAutoloadLocalClasses = [ 'MemcLockManager' => __DIR__ . '/includes/filebackend/lockmanager/MemcLockManager.php', 'MemcachedBagOStuff' => __DIR__ . '/includes/libs/objectcache/MemcachedBagOStuff.php', 'MemcachedClient' => __DIR__ . '/includes/libs/objectcache/MemcachedClient.php', - 'MemcachedPeclBagOStuff' => __DIR__ . '/includes/objectcache/MemcachedPeclBagOStuff.php', + 'MemcachedPeclBagOStuff' => __DIR__ . '/includes/libs/objectcache/MemcachedPeclBagOStuff.php', 'MemcachedPhpBagOStuff' => __DIR__ . '/includes/libs/objectcache/MemcachedPhpBagOStuff.php', 'MemoizedCallable' => __DIR__ . '/includes/libs/MemoizedCallable.php', 'MemoryFileBackend' => __DIR__ . '/includes/filebackend/MemoryFileBackend.php', diff --git a/includes/libs/objectcache/MemcachedPeclBagOStuff.php b/includes/libs/objectcache/MemcachedPeclBagOStuff.php new file mode 100644 index 0000000000..5983c1b825 --- /dev/null +++ b/includes/libs/objectcache/MemcachedPeclBagOStuff.php @@ -0,0 +1,256 @@ +applyDefaultParams( $params ); + + if ( $params['persistent'] ) { + // The pool ID must be unique to the server/option combination. + // The Memcached object is essentially shared for each pool ID. + // We can only reuse a pool ID if we keep the config consistent. + $this->client = new Memcached( md5( serialize( $params ) ) ); + if ( count( $this->client->getServerList() ) ) { + $this->logger->debug( __METHOD__ . ": persistent Memcached object already loaded." ); + return; // already initialized; don't add duplicate servers + } + } else { + $this->client = new Memcached; + } + + if ( $params['use_binary_protocol'] ) { + $this->client->setOption( Memcached::OPT_BINARY_PROTOCOL, true ); + } + + if ( isset( $params['retry_timeout'] ) ) { + $this->client->setOption( Memcached::OPT_RETRY_TIMEOUT, $params['retry_timeout'] ); + } + + if ( isset( $params['server_failure_limit'] ) ) { + $this->client->setOption( Memcached::OPT_SERVER_FAILURE_LIMIT, $params['server_failure_limit'] ); + } + + // The compression threshold is an undocumented php.ini option for some + // reason. There's probably not much harm in setting it globally, for + // compatibility with the settings for the PHP client. + ini_set( 'memcached.compression_threshold', $params['compress_threshold'] ); + + // Set timeouts + $this->client->setOption( Memcached::OPT_CONNECT_TIMEOUT, $params['connect_timeout'] * 1000 ); + $this->client->setOption( Memcached::OPT_SEND_TIMEOUT, $params['timeout'] ); + $this->client->setOption( Memcached::OPT_RECV_TIMEOUT, $params['timeout'] ); + $this->client->setOption( Memcached::OPT_POLL_TIMEOUT, $params['timeout'] / 1000 ); + + // Set libketama mode since it's recommended by the documentation and + // is as good as any. There's no way to configure libmemcached to use + // hashes identical to the ones currently in use by the PHP client, and + // even implementing one of the libmemcached hashes in pure PHP for + // forwards compatibility would require MemcachedClient::get_sock() to be + // rewritten. + $this->client->setOption( Memcached::OPT_LIBKETAMA_COMPATIBLE, true ); + + // Set the serializer + switch ( $params['serializer'] ) { + case 'php': + $this->client->setOption( Memcached::OPT_SERIALIZER, Memcached::SERIALIZER_PHP ); + break; + case 'igbinary': + if ( !Memcached::HAVE_IGBINARY ) { + throw new InvalidArgumentException( + __CLASS__ . ': the igbinary extension is not available ' . + 'but igbinary serialization was requested.' + ); + } + $this->client->setOption( Memcached::OPT_SERIALIZER, Memcached::SERIALIZER_IGBINARY ); + break; + default: + throw new InvalidArgumentException( + __CLASS__ . ': invalid value for serializer parameter' + ); + } + $servers = []; + foreach ( $params['servers'] as $host ) { + if ( preg_match( '/^\[(.+)\]:(\d+)$/', $host, $m ) ) { + $servers[] = [ $m[1], (int)$m[2] ]; // (ip, port) + } elseif ( preg_match( '/^([^:]+):(\d+)$/', $host, $m ) ) { + $servers[] = [ $m[1], (int)$m[2] ]; // (ip or path, port) + } else { + $servers[] = [ $host, false ]; // (ip or path, port) + } + } + $this->client->addServers( $servers ); + } + + protected function applyDefaultParams( $params ) { + $params = parent::applyDefaultParams( $params ); + + if ( !isset( $params['use_binary_protocol'] ) ) { + $params['use_binary_protocol'] = false; + } + + if ( !isset( $params['serializer'] ) ) { + $params['serializer'] = 'php'; + } + + return $params; + } + + protected function getWithToken( $key, &$casToken, $flags = 0 ) { + $this->debugLog( "get($key)" ); + $result = $this->client->get( $this->validateKeyEncoding( $key ), null, $casToken ); + $result = $this->checkResult( $key, $result ); + return $result; + } + + public function set( $key, $value, $exptime = 0, $flags = 0 ) { + $this->debugLog( "set($key)" ); + return $this->checkResult( $key, parent::set( $key, $value, $exptime ) ); + } + + protected function cas( $casToken, $key, $value, $exptime = 0 ) { + $this->debugLog( "cas($key)" ); + return $this->checkResult( $key, parent::cas( $casToken, $key, $value, $exptime ) ); + } + + public function delete( $key ) { + $this->debugLog( "delete($key)" ); + $result = parent::delete( $key ); + if ( $result === false && $this->client->getResultCode() === Memcached::RES_NOTFOUND ) { + // "Not found" is counted as success in our interface + return true; + } else { + return $this->checkResult( $key, $result ); + } + } + + public function add( $key, $value, $exptime = 0 ) { + $this->debugLog( "add($key)" ); + return $this->checkResult( $key, parent::add( $key, $value, $exptime ) ); + } + + public function incr( $key, $value = 1 ) { + $this->debugLog( "incr($key)" ); + $result = $this->client->increment( $key, $value ); + return $this->checkResult( $key, $result ); + } + + public function decr( $key, $value = 1 ) { + $this->debugLog( "decr($key)" ); + $result = $this->client->decrement( $key, $value ); + return $this->checkResult( $key, $result ); + } + + /** + * Check the return value from a client method call and take any necessary + * action. Returns the value that the wrapper function should return. At + * present, the return value is always the same as the return value from + * the client, but some day we might find a case where it should be + * different. + * + * @param string $key The key used by the caller, or false if there wasn't one. + * @param mixed $result The return value + * @return mixed + */ + protected function checkResult( $key, $result ) { + if ( $result !== false ) { + return $result; + } + switch ( $this->client->getResultCode() ) { + case Memcached::RES_SUCCESS: + break; + case Memcached::RES_DATA_EXISTS: + case Memcached::RES_NOTSTORED: + case Memcached::RES_NOTFOUND: + $this->debugLog( "result: " . $this->client->getResultMessage() ); + break; + default: + $msg = $this->client->getResultMessage(); + $logCtx = []; + if ( $key !== false ) { + $server = $this->client->getServerByKey( $key ); + $logCtx['memcached-server'] = "{$server['host']}:{$server['port']}"; + $logCtx['memcached-key'] = $key; + $msg = "Memcached error for key \"{memcached-key}\" on server \"{memcached-server}\": $msg"; + } else { + $msg = "Memcached error: $msg"; + } + $this->logger->error( $msg, $logCtx ); + $this->setLastError( BagOStuff::ERR_UNEXPECTED ); + } + return $result; + } + + public function getMulti( array $keys, $flags = 0 ) { + $this->debugLog( 'getMulti(' . implode( ', ', $keys ) . ')' ); + foreach ( $keys as $key ) { + $this->validateKeyEncoding( $key ); + } + $result = $this->client->getMulti( $keys ) ?: []; + return $this->checkResult( false, $result ); + } + + /** + * @param array $data + * @param int $exptime + * @return bool + */ + public function setMulti( array $data, $exptime = 0 ) { + $this->debugLog( 'setMulti(' . implode( ', ', array_keys( $data ) ) . ')' ); + foreach ( array_keys( $data ) as $key ) { + $this->validateKeyEncoding( $key ); + } + $result = $this->client->setMulti( $data, $this->fixExpiry( $exptime ) ); + return $this->checkResult( false, $result ); + } + + public function changeTTL( $key, $expiry = 0 ) { + $this->debugLog( "touch($key)" ); + $result = $this->client->touch( $key, $expiry ); + return $this->checkResult( $key, $result ); + } +} diff --git a/includes/objectcache/MemcachedPeclBagOStuff.php b/includes/objectcache/MemcachedPeclBagOStuff.php deleted file mode 100644 index aefda7994c..0000000000 --- a/includes/objectcache/MemcachedPeclBagOStuff.php +++ /dev/null @@ -1,250 +0,0 @@ -applyDefaultParams( $params ); - - if ( $params['persistent'] ) { - // The pool ID must be unique to the server/option combination. - // The Memcached object is essentially shared for each pool ID. - // We can only reuse a pool ID if we keep the config consistent. - $this->client = new Memcached( md5( serialize( $params ) ) ); - if ( count( $this->client->getServerList() ) ) { - $this->logger->debug( __METHOD__ . ": persistent Memcached object already loaded." ); - return; // already initialized; don't add duplicate servers - } - } else { - $this->client = new Memcached; - } - - if ( $params['use_binary_protocol'] ) { - $this->client->setOption( Memcached::OPT_BINARY_PROTOCOL, true ); - } - - if ( isset( $params['retry_timeout'] ) ) { - $this->client->setOption( Memcached::OPT_RETRY_TIMEOUT, $params['retry_timeout'] ); - } - - if ( isset( $params['server_failure_limit'] ) ) { - $this->client->setOption( Memcached::OPT_SERVER_FAILURE_LIMIT, $params['server_failure_limit'] ); - } - - // The compression threshold is an undocumented php.ini option for some - // reason. There's probably not much harm in setting it globally, for - // compatibility with the settings for the PHP client. - ini_set( 'memcached.compression_threshold', $params['compress_threshold'] ); - - // Set timeouts - $this->client->setOption( Memcached::OPT_CONNECT_TIMEOUT, $params['connect_timeout'] * 1000 ); - $this->client->setOption( Memcached::OPT_SEND_TIMEOUT, $params['timeout'] ); - $this->client->setOption( Memcached::OPT_RECV_TIMEOUT, $params['timeout'] ); - $this->client->setOption( Memcached::OPT_POLL_TIMEOUT, $params['timeout'] / 1000 ); - - // Set libketama mode since it's recommended by the documentation and - // is as good as any. There's no way to configure libmemcached to use - // hashes identical to the ones currently in use by the PHP client, and - // even implementing one of the libmemcached hashes in pure PHP for - // forwards compatibility would require MemcachedClient::get_sock() to be - // rewritten. - $this->client->setOption( Memcached::OPT_LIBKETAMA_COMPATIBLE, true ); - - // Set the serializer - switch ( $params['serializer'] ) { - case 'php': - $this->client->setOption( Memcached::OPT_SERIALIZER, Memcached::SERIALIZER_PHP ); - break; - case 'igbinary': - if ( !Memcached::HAVE_IGBINARY ) { - throw new InvalidArgumentException( - __CLASS__ . ': the igbinary extension is not available ' . - 'but igbinary serialization was requested.' - ); - } - $this->client->setOption( Memcached::OPT_SERIALIZER, Memcached::SERIALIZER_IGBINARY ); - break; - default: - throw new InvalidArgumentException( - __CLASS__ . ': invalid value for serializer parameter' - ); - } - $servers = []; - foreach ( $params['servers'] as $host ) { - $servers[] = IP::splitHostAndPort( $host ); // (ip, port) - } - $this->client->addServers( $servers ); - } - - protected function applyDefaultParams( $params ) { - $params = parent::applyDefaultParams( $params ); - - if ( !isset( $params['use_binary_protocol'] ) ) { - $params['use_binary_protocol'] = false; - } - - if ( !isset( $params['serializer'] ) ) { - $params['serializer'] = 'php'; - } - - return $params; - } - - protected function getWithToken( $key, &$casToken, $flags = 0 ) { - $this->debugLog( "get($key)" ); - $result = $this->client->get( $this->validateKeyEncoding( $key ), null, $casToken ); - $result = $this->checkResult( $key, $result ); - return $result; - } - - public function set( $key, $value, $exptime = 0, $flags = 0 ) { - $this->debugLog( "set($key)" ); - return $this->checkResult( $key, parent::set( $key, $value, $exptime ) ); - } - - protected function cas( $casToken, $key, $value, $exptime = 0 ) { - $this->debugLog( "cas($key)" ); - return $this->checkResult( $key, parent::cas( $casToken, $key, $value, $exptime ) ); - } - - public function delete( $key ) { - $this->debugLog( "delete($key)" ); - $result = parent::delete( $key ); - if ( $result === false && $this->client->getResultCode() === Memcached::RES_NOTFOUND ) { - // "Not found" is counted as success in our interface - return true; - } else { - return $this->checkResult( $key, $result ); - } - } - - public function add( $key, $value, $exptime = 0 ) { - $this->debugLog( "add($key)" ); - return $this->checkResult( $key, parent::add( $key, $value, $exptime ) ); - } - - public function incr( $key, $value = 1 ) { - $this->debugLog( "incr($key)" ); - $result = $this->client->increment( $key, $value ); - return $this->checkResult( $key, $result ); - } - - public function decr( $key, $value = 1 ) { - $this->debugLog( "decr($key)" ); - $result = $this->client->decrement( $key, $value ); - return $this->checkResult( $key, $result ); - } - - /** - * Check the return value from a client method call and take any necessary - * action. Returns the value that the wrapper function should return. At - * present, the return value is always the same as the return value from - * the client, but some day we might find a case where it should be - * different. - * - * @param string $key The key used by the caller, or false if there wasn't one. - * @param mixed $result The return value - * @return mixed - */ - protected function checkResult( $key, $result ) { - if ( $result !== false ) { - return $result; - } - switch ( $this->client->getResultCode() ) { - case Memcached::RES_SUCCESS: - break; - case Memcached::RES_DATA_EXISTS: - case Memcached::RES_NOTSTORED: - case Memcached::RES_NOTFOUND: - $this->debugLog( "result: " . $this->client->getResultMessage() ); - break; - default: - $msg = $this->client->getResultMessage(); - $logCtx = []; - if ( $key !== false ) { - $server = $this->client->getServerByKey( $key ); - $logCtx['memcached-server'] = "{$server['host']}:{$server['port']}"; - $logCtx['memcached-key'] = $key; - $msg = "Memcached error for key \"{memcached-key}\" on server \"{memcached-server}\": $msg"; - } else { - $msg = "Memcached error: $msg"; - } - $this->logger->error( $msg, $logCtx ); - $this->setLastError( BagOStuff::ERR_UNEXPECTED ); - } - return $result; - } - - public function getMulti( array $keys, $flags = 0 ) { - $this->debugLog( 'getMulti(' . implode( ', ', $keys ) . ')' ); - foreach ( $keys as $key ) { - $this->validateKeyEncoding( $key ); - } - $result = $this->client->getMulti( $keys ) ?: []; - return $this->checkResult( false, $result ); - } - - /** - * @param array $data - * @param int $exptime - * @return bool - */ - public function setMulti( array $data, $exptime = 0 ) { - $this->debugLog( 'setMulti(' . implode( ', ', array_keys( $data ) ) . ')' ); - foreach ( array_keys( $data ) as $key ) { - $this->validateKeyEncoding( $key ); - } - $result = $this->client->setMulti( $data, $this->fixExpiry( $exptime ) ); - return $this->checkResult( false, $result ); - } - - public function changeTTL( $key, $expiry = 0 ) { - $this->debugLog( "touch($key)" ); - $result = $this->client->touch( $key, $expiry ); - return $this->checkResult( $key, $result ); - } -}