diff options
author | Aaron Schulz <aschulz@wikimedia.org> | 2020-12-28 14:34:29 -0800 |
---|---|---|
committer | Aaron Schulz <aschulz@wikimedia.org> | 2021-01-25 14:36:29 -0800 |
commit | 57325ba3bdce3826d726cf6f6d26ad235af58f53 (patch) | |
tree | d5e386a313982c7f959719055753c5c42a00b86c /includes/objectcache | |
parent | 9834fd052cc7cb85e63ff1d2c2783a387e123d56 (diff) | |
download | mediawikicore-57325ba3bdce3826d726cf6f6d26ad235af58f53.tar.gz mediawikicore-57325ba3bdce3826d726cf6f6d26ad235af58f53.zip |
objectcache: add statsd key metrics to BagOStuff classes
Update SQL, REST, and redis subclasses to emit call count and
payload size metrics for cache key operations. These metrics
are bucketed by cache key collection (similar to WANCache).
Bug: T235705
Change-Id: Icaa3fa1ae9c8b0f664c26ce70b7e1c4fc5f92767
Diffstat (limited to 'includes/objectcache')
-rw-r--r-- | includes/objectcache/ObjectCache.php | 4 | ||||
-rw-r--r-- | includes/objectcache/SqlBagOStuff.php | 58 |
2 files changed, 50 insertions, 12 deletions
diff --git a/includes/objectcache/ObjectCache.php b/includes/objectcache/ObjectCache.php index 6226f91b1efd..32ece3183539 100644 --- a/includes/objectcache/ObjectCache.php +++ b/includes/objectcache/ObjectCache.php @@ -149,6 +149,10 @@ class ObjectCache { 'reportDupes' => true, ]; + if ( !isset( $params['stats'] ) ) { + $params['stats'] = MediaWikiServices::getInstance()->getStatsdDataFactory(); + } + if ( isset( $params['factory'] ) ) { return call_user_func( $params['factory'], $params ); } diff --git a/includes/objectcache/SqlBagOStuff.php b/includes/objectcache/SqlBagOStuff.php index ea886e5501af..f979583a843e 100644 --- a/includes/objectcache/SqlBagOStuff.php +++ b/includes/objectcache/SqlBagOStuff.php @@ -265,25 +265,41 @@ class SqlBagOStuff extends MediumSpecificBagOStuff { $blobs = $this->fetchBlobMulti( [ $key ] ); if ( array_key_exists( $key, $blobs ) ) { $blob = $blobs[$key]; - $value = $this->unserialize( $blob ); - if ( $getToken && $value !== false ) { + $result = $this->unserialize( $blob ); + if ( $getToken && $blob !== false ) { $casToken = $blob; } - - return $value; + $valueSize = strlen( $blob ); + } else { + $result = false; + $valueSize = false; } - return false; + $this->updateOpStats( self::METRIC_OP_GET, [ $key ], null, [ $valueSize ] ); + + return $result; } protected function doGetMulti( array $keys, $flags = 0 ) { $values = []; + $valueSizes = []; - $blobs = $this->fetchBlobMulti( $keys ); - foreach ( $blobs as $key => $blob ) { - $values[$key] = $this->unserialize( $blob ); + $blobsByKey = $this->fetchBlobMulti( $keys ); + foreach ( $keys as $key ) { + if ( array_key_exists( $key, $blobsByKey ) ) { + $blob = $blobsByKey[$key]; + $value = $this->unserialize( $blob ); + if ( $value !== false ) { + $values[$key] = $value; + } + $valueSizes[] = strlen( $blob ); + } else { + $valueSizes[] = false; + } } + $this->updateOpStats( self::METRIC_OP_GET, $keys, null, $valueSizes ); + return $values; } @@ -424,14 +440,17 @@ class SqlBagOStuff extends MediumSpecificBagOStuff { private function updateTable( $op, $db, $table, $tableKeys, $data, $dbExpiry ) { $success = true; + $valueSizes = []; if ( $op === self::$OP_ADD ) { $rows = []; foreach ( $tableKeys as $key ) { + $serialized = $this->serialize( $data[$key] ); $rows[] = [ 'keyname' => $key, - 'value' => $db->encodeBlob( $this->serialize( $data[$key] ) ), + 'value' => $db->encodeBlob( $serialized ), 'exptime' => $dbExpiry ]; + $valueSizes[] = strlen( $serialized ); } $db->delete( $table, @@ -444,18 +463,26 @@ class SqlBagOStuff extends MediumSpecificBagOStuff { $db->insert( $table, $rows, __METHOD__, [ 'IGNORE' ] ); $success = ( $db->affectedRows() == count( $rows ) ); + + $this->updateOpStats( self::METRIC_OP_ADD, $tableKeys, $valueSizes ); } elseif ( $op === self::$OP_SET ) { $rows = []; foreach ( $tableKeys as $key ) { + $serialized = $this->serialize( $data[$key] ); $rows[] = [ 'keyname' => $key, - 'value' => $db->encodeBlob( $this->serialize( $data[$key] ) ), + 'value' => $db->encodeBlob( $serialized ), 'exptime' => $dbExpiry ]; + $valueSizes[] = strlen( $serialized ); } $db->replace( $table, 'keyname', $rows, __METHOD__ ); + + $this->updateOpStats( self::METRIC_OP_SET, $tableKeys, $valueSizes ); } elseif ( $op === self::$OP_DELETE ) { $db->delete( $table, [ 'keyname' => $tableKeys ], __METHOD__ ); + + $this->updateOpStats( self::METRIC_OP_DELETE, $tableKeys ); } elseif ( $op === self::$OP_TOUCH ) { $db->update( $table, @@ -468,6 +495,8 @@ class SqlBagOStuff extends MediumSpecificBagOStuff { ); $success = ( $db->affectedRows() == count( $tableKeys ) ); + + $this->updateOpStats( self::METRIC_OP_CHANGE_TTL, $tableKeys ); } else { throw new InvalidArgumentException( "Invalid operation '$op'" ); } @@ -486,6 +515,7 @@ class SqlBagOStuff extends MediumSpecificBagOStuff { protected function doCas( $casToken, $key, $value, $exptime = 0, $flags = 0 ) { list( $shardIndex, $tableName ) = $this->getKeyLocation( $key ); $exptime = $this->getExpirationAsTimestamp( $exptime ); + $serialized = $this->serialize( $value ); /** @noinspection PhpUnusedLocalVariableInspection */ $silenceScope = $this->silenceTransactionProfiler(); @@ -498,7 +528,7 @@ class SqlBagOStuff extends MediumSpecificBagOStuff { $tableName, [ 'keyname' => $key, - 'value' => $db->encodeBlob( $this->serialize( $value ) ), + 'value' => $db->encodeBlob( $serialized ), 'exptime' => $exptime ? $db->timestamp( $exptime ) : $this->getMaxDateTime( $db ) @@ -521,6 +551,8 @@ class SqlBagOStuff extends MediumSpecificBagOStuff { $success = $this->waitForReplication( $shardIndex ) && $success; } + $this->updateOpStats( self::METRIC_OP_CAS, [ $key ], [ strlen( $serialized ) ] ); + return $success; } @@ -568,6 +600,8 @@ class SqlBagOStuff extends MediumSpecificBagOStuff { $this->handleWriteError( $e, $db, $shardIndex ); } + $this->updateOpStats( $step >= 0 ? self::METRIC_OP_INCR : self::METRIC_OP_DECR, [ $key ] ); + return $newCount; } @@ -575,7 +609,7 @@ class SqlBagOStuff extends MediumSpecificBagOStuff { return $this->incr( $key, -$value, $flags ); } - public function changeTTLMulti( array $keys, $exptime, $flags = 0 ) { + public function doChangeTTLMulti( array $keys, $exptime, $flags = 0 ) { return $this->modifyMulti( array_fill_keys( $keys, null ), $exptime, |