summaryrefslogtreecommitdiff
path: root/includes/objectcache/MemcachedClient.php
diff options
context:
space:
mode:
authorPierre Schmitz <pierre@archlinux.de>2013-01-18 16:46:04 +0100
committerPierre Schmitz <pierre@archlinux.de>2013-01-18 16:46:04 +0100
commit63601400e476c6cf43d985f3e7b9864681695ed4 (patch)
treef7846203a952e38aaf66989d0a4702779f549962 /includes/objectcache/MemcachedClient.php
parent8ff01378c9e0207f9169b81966a51def645b6a51 (diff)
Update to MediaWiki 1.20.2
this update includes: * adjusted Arch Linux skin * updated FluxBBAuthPlugin * patch for https://bugzilla.wikimedia.org/show_bug.cgi?id=44024
Diffstat (limited to 'includes/objectcache/MemcachedClient.php')
-rw-r--r--includes/objectcache/MemcachedClient.php281
1 files changed, 174 insertions, 107 deletions
diff --git a/includes/objectcache/MemcachedClient.php b/includes/objectcache/MemcachedClient.php
index 868ad69f..536ba6ea 100644
--- a/includes/objectcache/MemcachedClient.php
+++ b/includes/objectcache/MemcachedClient.php
@@ -1,5 +1,7 @@
<?php
/**
+ * Memcached client for PHP.
+ *
* +---------------------------------------------------------------------------+
* | memcached client, PHP |
* +---------------------------------------------------------------------------+
@@ -257,7 +259,7 @@ class MWMemcached {
$this->_host_dead = array();
$this->_timeout_seconds = 0;
- $this->_timeout_microseconds = isset( $args['timeout'] ) ? $args['timeout'] : 100000;
+ $this->_timeout_microseconds = isset( $args['timeout'] ) ? $args['timeout'] : 500000;
$this->_connect_timeout = isset( $args['connect_timeout'] ) ? $args['connect_timeout'] : 0.1;
$this->_connect_attempts = 2;
@@ -328,27 +330,36 @@ class MWMemcached {
$this->stats['delete'] = 1;
}
$cmd = "delete $key $time\r\n";
- if( !$this->_safe_fwrite( $sock, $cmd, strlen( $cmd ) ) ) {
- $this->_dead_sock( $sock );
+ if( !$this->_fwrite( $sock, $cmd ) ) {
return false;
}
- $res = trim( fgets( $sock ) );
+ $res = $this->_fgets( $sock );
if ( $this->_debug ) {
$this->_debugprint( sprintf( "MemCache: delete %s (%s)\n", $key, $res ) );
}
- if ( $res == "DELETED" ) {
+ if ( $res == "DELETED" || $res == "NOT_FOUND" ) {
return true;
}
+
return false;
}
+ /**
+ * @param $key
+ * @param $timeout int
+ * @return bool
+ */
public function lock( $key, $timeout = 0 ) {
/* stub */
return true;
}
+ /**
+ * @param $key
+ * @return bool
+ */
public function unlock( $key ) {
/* stub */
return true;
@@ -427,8 +438,7 @@ class MWMemcached {
}
$cmd = "get $key\r\n";
- if ( !$this->_safe_fwrite( $sock, $cmd, strlen( $cmd ) ) ) {
- $this->_dead_sock( $sock );
+ if ( !$this->_fwrite( $sock, $cmd ) ) {
wfProfileOut( __METHOD__ );
return false;
}
@@ -471,7 +481,7 @@ class MWMemcached {
$this->stats['get_multi'] = 1;
}
$sock_keys = array();
-
+ $socks = array();
foreach ( $keys as $key ) {
$sock = $this->get_sock( $key );
if ( !is_resource( $sock ) ) {
@@ -479,24 +489,23 @@ class MWMemcached {
}
$key = is_array( $key ) ? $key[1] : $key;
if ( !isset( $sock_keys[$sock] ) ) {
- $sock_keys[$sock] = array();
+ $sock_keys[ intval( $sock ) ] = array();
$socks[] = $sock;
}
- $sock_keys[$sock][] = $key;
+ $sock_keys[ intval( $sock ) ][] = $key;
}
+ $gather = array();
// Send out the requests
foreach ( $socks as $sock ) {
$cmd = 'get';
- foreach ( $sock_keys[$sock] as $key ) {
+ foreach ( $sock_keys[ intval( $sock ) ] as $key ) {
$cmd .= ' ' . $key;
}
$cmd .= "\r\n";
- if ( $this->_safe_fwrite( $sock, $cmd, strlen( $cmd ) ) ) {
+ if ( $this->_fwrite( $sock, $cmd ) ) {
$gather[] = $sock;
- } else {
- $this->_dead_sock( $sock );
}
}
@@ -559,12 +568,6 @@ class MWMemcached {
* Passes through $cmd to the memcache server connected by $sock; returns
* output as an array (null array if no output)
*
- * NOTE: due to a possible bug in how PHP reads while using fgets(), each
- * line may not be terminated by a \r\n. More specifically, my testing
- * has shown that, on FreeBSD at least, each line is terminated only
- * with a \n. This is with the PHP flag auto_detect_line_endings set
- * to falase (the default).
- *
* @param $sock Resource: socket to send command on
* @param $cmd String: command to run
*
@@ -575,12 +578,13 @@ class MWMemcached {
return array();
}
- if ( !$this->_safe_fwrite( $sock, $cmd, strlen( $cmd ) ) ) {
+ if ( !$this->_fwrite( $sock, $cmd ) ) {
return array();
}
+ $ret = array();
while ( true ) {
- $res = fgets( $sock );
+ $res = $this->_fgets( $sock );
$ret[] = $res;
if ( preg_match( '/^END/', $res ) ) {
break;
@@ -717,15 +721,19 @@ class MWMemcached {
wfRestoreWarnings();
}
if ( !$sock ) {
- if ( $this->_debug ) {
- $this->_debugprint( "Error connecting to $host: $errstr\n" );
- }
+ $this->_error_log( "Error connecting to $host: $errstr\n" );
+ $this->_dead_host( $host );
return false;
}
// Initialise timeout
stream_set_timeout( $sock, $this->_timeout_seconds, $this->_timeout_microseconds );
+ // If the connection was persistent, flush the read buffer in case there
+ // was a previous incomplete request on this connection
+ if ( $this->_persistent ) {
+ $this->_flush_read_buffer( $sock );
+ }
return true;
}
@@ -744,6 +752,9 @@ class MWMemcached {
$this->_dead_host( $host );
}
+ /**
+ * @param $host
+ */
function _dead_host( $host ) {
$parts = explode( ':', $host );
$ip = $parts[0];
@@ -769,13 +780,12 @@ class MWMemcached {
}
if ( $this->_single_sock !== null ) {
- $this->_flush_read_buffer( $this->_single_sock );
return $this->sock_to_host( $this->_single_sock );
}
$hv = is_array( $key ) ? intval( $key[0] ) : $this->_hashfunc( $key );
-
if ( $this->_buckets === null ) {
+ $bu = array();
foreach ( $this->_servers as $v ) {
if ( is_array( $v ) ) {
for( $i = 0; $i < $v[1]; $i++ ) {
@@ -794,7 +804,6 @@ class MWMemcached {
$host = $this->_buckets[$hv % $this->_bucketcount];
$sock = $this->sock_to_host( $host );
if ( is_resource( $sock ) ) {
- $this->_flush_read_buffer( $sock );
return $sock;
}
$hv = $this->_hashfunc( $hv . $realkey );
@@ -815,7 +824,7 @@ class MWMemcached {
* @access private
*/
function _hashfunc( $key ) {
- # Hash function must on [0,0x7ffffff]
+ # Hash function must be in [0,0x7ffffff]
# We take the first 31 bits of the MD5 hash, which unlike the hash
# function used in a previous version of this client, works
return hexdec( substr( md5( $key ), 0, 8 ) ) & 0x7fffffff;
@@ -850,11 +859,11 @@ class MWMemcached {
} else {
$this->stats[$cmd] = 1;
}
- if ( !$this->_safe_fwrite( $sock, "$cmd $key $amt\r\n" ) ) {
- return $this->_dead_sock( $sock );
+ if ( !$this->_fwrite( $sock, "$cmd $key $amt\r\n" ) ) {
+ return null;
}
- $line = fgets( $sock );
+ $line = $this->_fgets( $sock );
$match = array();
if ( !preg_match( '/^(\d+)/', $line, $match ) ) {
return null;
@@ -870,58 +879,42 @@ class MWMemcached {
*
* @param $sock Resource: socket to read from
* @param $ret Array: returned values
+ * @return boolean True for success, false for failure
*
* @access private
*/
function _load_items( $sock, &$ret ) {
while ( 1 ) {
- $decl = fgets( $sock );
- if ( $decl == "END\r\n" ) {
+ $decl = $this->_fgets( $sock );
+ if( $decl === false ) {
+ return false;
+ } elseif ( $decl == "END" ) {
return true;
- } elseif ( preg_match( '/^VALUE (\S+) (\d+) (\d+)\r\n$/', $decl, $match ) ) {
+ } elseif ( preg_match( '/^VALUE (\S+) (\d+) (\d+)$/', $decl, $match ) ) {
list( $rkey, $flags, $len ) = array( $match[1], $match[2], $match[3] );
- $bneed = $len + 2;
- $offset = 0;
-
- while ( $bneed > 0 ) {
- $data = fread( $sock, $bneed );
- $n = strlen( $data );
- if ( $n == 0 ) {
- break;
- }
- $offset += $n;
- $bneed -= $n;
- if ( isset( $ret[$rkey] ) ) {
- $ret[$rkey] .= $data;
- } else {
- $ret[$rkey] = $data;
- }
+ $data = $this->_fread( $sock, $len + 2 );
+ if ( $data === false ) {
+ return false;
}
-
- if ( $offset != $len + 2 ) {
- // Something is borked!
- if ( $this->_debug ) {
- $this->_debugprint( sprintf( "Something is borked! key %s expecting %d got %d length\n", $rkey, $len + 2, $offset ) );
- }
-
- unset( $ret[$rkey] );
- $this->_close_sock( $sock );
+ if ( substr( $data, -2 ) !== "\r\n" ) {
+ $this->_handle_error( $sock,
+ 'line ending missing from data block from $1' );
return false;
}
+ $data = substr( $data, 0, -2 );
+ $ret[$rkey] = $data;
if ( $this->_have_zlib && $flags & self::COMPRESSED ) {
$ret[$rkey] = gzuncompress( $ret[$rkey] );
}
- $ret[$rkey] = rtrim( $ret[$rkey] );
-
if ( $flags & self::SERIALIZED ) {
$ret[$rkey] = unserialize( $ret[$rkey] );
}
} else {
- $this->_debugprint( "Error parsing memcached response\n" );
- return 0;
+ $this->_handle_error( $sock, 'Error parsing response from $1' );
+ return false;
}
}
}
@@ -960,15 +953,6 @@ class MWMemcached {
$this->stats[$cmd] = 1;
}
- // TTLs higher than 30 days will be detected as absolute TTLs
- // (UNIX timestamps), and will result in the cache entry being
- // discarded immediately because the expiry is in the past.
- // Clamp expiries >30d at 30d, unless they're >=1e9 in which
- // case they are likely to really be absolute (1e9 = 2011-09-09)
- if ( $exp > 2592000 && $exp < 1000000000 ) {
- $exp = 2592000;
- }
-
$flags = 0;
if ( !is_scalar( $val ) ) {
@@ -996,11 +980,11 @@ class MWMemcached {
$flags |= self::COMPRESSED;
}
}
- if ( !$this->_safe_fwrite( $sock, "$cmd $key $flags $exp $len\r\n$val\r\n" ) ) {
- return $this->_dead_sock( $sock );
+ if ( !$this->_fwrite( $sock, "$cmd $key $flags $exp $len\r\n$val\r\n" ) ) {
+ return false;
}
- $line = trim( fgets( $sock ) );
+ $line = $this->_fgets( $sock );
if ( $this->_debug ) {
$this->_debugprint( sprintf( "%s %s (%s)\n", $cmd, $key, $line ) );
@@ -1037,7 +1021,7 @@ class MWMemcached {
}
if ( !$this->_connect_sock( $sock, $host ) ) {
- return $this->_dead_host( $host );
+ return null;
}
// Do not buffer writes
@@ -1048,49 +1032,136 @@ class MWMemcached {
return $this->_cache_sock[$host];
}
- function _debugprint( $str ) {
- print( $str );
+ /**
+ * @param $text string
+ */
+ function _debugprint( $text ) {
+ global $wgDebugLogGroups;
+ if( !isset( $wgDebugLogGroups['memcached'] ) ) {
+ # Prefix message since it will end up in main debug log file
+ $text = "memcached: $text";
+ }
+ wfDebugLog( 'memcached', $text );
+ }
+
+ /**
+ * @param $text string
+ */
+ function _error_log( $text ) {
+ wfDebugLog( 'memcached-serious', "Memcached error: $text" );
}
/**
- * Write to a stream, timing out after the correct amount of time
+ * Write to a stream. If there is an error, mark the socket dead.
*
- * @return Boolean: false on failure, true on success
+ * @param $sock The socket
+ * @param $buf The string to write
+ * @return bool True on success, false on failure
*/
- /*
- function _safe_fwrite( $f, $buf, $len = false ) {
- stream_set_blocking( $f, 0 );
+ function _fwrite( $sock, $buf ) {
+ $bytesWritten = 0;
+ $bufSize = strlen( $buf );
+ while ( $bytesWritten < $bufSize ) {
+ $result = fwrite( $sock, $buf );
+ $data = stream_get_meta_data( $sock );
+ if ( $data['timed_out'] ) {
+ $this->_handle_error( $sock, 'timeout writing to $1' );
+ return false;
+ }
+ // Contrary to the documentation, fwrite() returns zero on error in PHP 5.3.
+ if ( $result === false || $result === 0 ) {
+ $this->_handle_error( $sock, 'error writing to $1' );
+ return false;
+ }
+ $bytesWritten += $result;
+ }
- if ( $len === false ) {
- wfDebug( "Writing " . strlen( $buf ) . " bytes\n" );
- $bytesWritten = fwrite( $f, $buf );
- } else {
- wfDebug( "Writing $len bytes\n" );
- $bytesWritten = fwrite( $f, $buf, $len );
+ return true;
+ }
+
+ /**
+ * Handle an I/O error. Mark the socket dead and log an error.
+ */
+ function _handle_error( $sock, $msg ) {
+ $peer = stream_socket_get_name( $sock, true /** remote **/ );
+ if ( strval( $peer ) === '' ) {
+ $peer = array_search( $sock, $this->_cache_sock );
+ if ( $peer === false ) {
+ $peer = '[unknown host]';
+ }
}
- $n = stream_select( $r = null, $w = array( $f ), $e = null, 10, 0 );
- # $this->_timeout_seconds, $this->_timeout_microseconds );
+ $msg = str_replace( '$1', $peer, $msg );
+ $this->_error_log( "$msg\n" );
+ $this->_dead_sock( $sock );
+ }
- wfDebug( "stream_select returned $n\n" );
- stream_set_blocking( $f, 1 );
- return $n == 1;
- return $bytesWritten;
- }*/
+ /**
+ * Read the specified number of bytes from a stream. If there is an error,
+ * mark the socket dead.
+ *
+ * @param $sock The socket
+ * @param $len The number of bytes to read
+ * @return The string on success, false on failure.
+ */
+ function _fread( $sock, $len ) {
+ $buf = '';
+ while ( $len > 0 ) {
+ $result = fread( $sock, $len );
+ $data = stream_get_meta_data( $sock );
+ if ( $data['timed_out'] ) {
+ $this->_handle_error( $sock, 'timeout reading from $1' );
+ return false;
+ }
+ if ( $result === false ) {
+ $this->_handle_error( $sock, 'error reading buffer from $1' );
+ return false;
+ }
+ if ( $result === '' ) {
+ // This will happen if the remote end of the socket is shut down
+ $this->_handle_error( $sock, 'unexpected end of file reading from $1' );
+ return false;
+ }
+ $len -= strlen( $result );
+ $buf .= $result;
+ }
+ return $buf;
+ }
/**
- * Original behaviour
+ * Read a line from a stream. If there is an error, mark the socket dead.
+ * The \r\n line ending is stripped from the response.
+ *
+ * @param $sock The socket
+ * @return The string on success, false on failure
*/
- function _safe_fwrite( $f, $buf, $len = false ) {
- if ( $len === false ) {
- $bytesWritten = fwrite( $f, $buf );
+ function _fgets( $sock ) {
+ $result = fgets( $sock );
+ // fgets() may return a partial line if there is a select timeout after
+ // a successful recv(), so we have to check for a timeout even if we
+ // got a string response.
+ $data = stream_get_meta_data( $sock );
+ if ( $data['timed_out'] ) {
+ $this->_handle_error( $sock, 'timeout reading line from $1' );
+ return false;
+ }
+ if ( $result === false ) {
+ $this->_handle_error( $sock, 'error reading line from $1' );
+ return false;
+ }
+ if ( substr( $result, -2 ) === "\r\n" ) {
+ $result = substr( $result, 0, -2 );
+ } elseif ( substr( $result, -1 ) === "\n" ) {
+ $result = substr( $result, 0, -1 );
} else {
- $bytesWritten = fwrite( $f, $buf, $len );
+ $this->_handle_error( $sock, 'line ending missing in response from $1' );
+ return false;
}
- return $bytesWritten;
+ return $result;
}
/**
* Flush the read buffer of a stream
+ * @param $f Resource
*/
function _flush_read_buffer( $f ) {
if ( !is_resource( $f ) ) {
@@ -1108,12 +1179,8 @@ class MWMemcached {
// }}}
}
-// vim: sts=3 sw=3 et
// }}}
class MemCachedClientforWiki extends MWMemcached {
- function _debugprint( $text ) {
- wfDebug( "memcached: $text" );
- }
}