objectcache: split out StorageAwareness/ExpirationAwareness from IExpiringStore

Add more detailed QOS_* constants while at it

Bug: T236412
Change-Id: Ia862c5111a3daf10a34fc78163301629228efa6b
This commit is contained in:
Aaron Schulz 2020-01-09 14:10:08 -08:00
parent 0a21fd837f
commit 69950da666
9 changed files with 179 additions and 74 deletions

View file

@ -645,7 +645,7 @@ $wgAutoloadLocalClasses = [
'IDBAccessObject' => __DIR__ . '/includes/dao/IDBAccessObject.php',
'IDatabase' => __DIR__ . '/includes/libs/rdbms/database/IDatabase.php',
'IEContentAnalyzer' => __DIR__ . '/includes/libs/mime/IEContentAnalyzer.php',
'IExpiringStore' => __DIR__ . '/includes/libs/objectcache/IExpiringStore.php',
'IExpiringStore' => __DIR__ . '/includes/libs/objectcache/utils/IExpiringStore.php',
'IJobSpecification' => __DIR__ . '/includes/jobqueue/IJobSpecification.php',
'ILanguageConverter' => __DIR__ . '/includes/language/ILanguageConverter.php',
'ILocalizedException' => __DIR__ . '/includes/exception/ILocalizedException.php',
@ -1650,6 +1650,8 @@ $wgAutoloadLocalClasses = [
'Wikimedia\\DependencyStore\\SqlModuleDependencyStore' => __DIR__ . '/includes/resourceloader/dependencystore/SqlModuleDependencyStore.php',
'Wikimedia\\Http\\HttpAcceptNegotiator' => __DIR__ . '/includes/libs/http/HttpAcceptNegotiator.php',
'Wikimedia\\Http\\HttpAcceptParser' => __DIR__ . '/includes/libs/http/HttpAcceptParser.php',
'Wikimedia\\LightweightObjectStore\\ExpirationAwareness' => __DIR__ . '/includes/libs/objectcache/utils/ExpirationAwareness.php',
'Wikimedia\\LightweightObjectStore\\StorageAwareness' => __DIR__ . '/includes/libs/objectcache/utils/StorageAwareness.php',
'Wikimedia\\Rdbms\\AtomicSectionIdentifier' => __DIR__ . '/includes/libs/rdbms/database/utils/AtomicSectionIdentifier.php',
'Wikimedia\\Rdbms\\Blob' => __DIR__ . '/includes/libs/rdbms/encasing/Blob.php',
'Wikimedia\\Rdbms\\ChronologyProtector' => __DIR__ . '/includes/libs/rdbms/ChronologyProtector.php',

View file

@ -21,6 +21,7 @@
* @ingroup Cache
*/
use Wikimedia\Assert\Assert;
use Wikimedia\LightweightObjectStore\ExpirationAwareness;
/**
* Handles a simple LRU key/value map with a maximum number of entries
@ -34,7 +35,7 @@ use Wikimedia\Assert\Assert;
* @ingroup Cache
* @since 1.23
*/
class MapCacheLRU implements IExpiringStore, Serializable {
class MapCacheLRU implements ExpirationAwareness, Serializable {
/** @var array Map of (key => value) */
private $cache = [];
/** @var array Map of (key => (UNIX timestamp, (field => UNIX timestamp))) */

View file

@ -29,6 +29,8 @@
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use Wikimedia\LightweightObjectStore\ExpirationAwareness;
use Wikimedia\LightweightObjectStore\StorageAwareness;
use Wikimedia\ScopedCallback;
/**
@ -60,7 +62,12 @@ use Wikimedia\ScopedCallback;
*
* @ingroup Cache
*/
abstract class BagOStuff implements IExpiringStore, IStoreKeyEncoder, LoggerAwareInterface {
abstract class BagOStuff implements
ExpirationAwareness,
StorageAwareness,
IStoreKeyEncoder,
LoggerAwareInterface
{
/** @var LoggerInterface */
protected $logger;

View file

@ -1,66 +0,0 @@
<?php
/**
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* http://www.gnu.org/copyleft/gpl.html
*
* @file
* @ingroup Cache
*/
/**
* Generic interface for lightweight expiring object stores.
*
* Provides convenient TTL constants.
*
* @ingroup Cache
* @since 1.27
*/
interface IExpiringStore {
// Constants for TTL values, in seconds
const TTL_SECOND = 1;
const TTL_MINUTE = 60;
const TTL_HOUR = 3600;
const TTL_DAY = 86400; // 24 * 3600
const TTL_WEEK = 604800; // 7 * 24 * 3600
const TTL_MONTH = 2592000; // 30 * 24 * 3600
const TTL_YEAR = 31536000; // 365 * 24 * 3600
// Shorthand process cache TTLs (useful for web requests and CLI mode)
const TTL_PROC_SHORT = 3; // reasonably strict cache time that last the life of quick requests
const TTL_PROC_LONG = 30; // loose cache time that can survive slow web requests
const TTL_INDEFINITE = 0;
// Emulation/fallback medium attribute (e.g. SQLBagOStuff)
const ATTR_EMULATION = 1;
// Quality of service constants for ATTR_EMULATION (higher means faster)
const QOS_EMULATION_SQL = 1;
// Replica synchronization/consistency attribute of medium when SYNC_WRITES is used
const ATTR_SYNCWRITES = 2;
// Quality of service constants for ATTR_SYNCWRITES (higher means more consistent)
const QOS_SYNCWRITES_NONE = 1; // replication only supports eventual consistency or less
const QOS_SYNCWRITES_BE = 2; // best effort synchronous with limited retries
const QOS_SYNCWRITES_QC = 3; // write quorum applied directly to state machines where R+W > N
const QOS_SYNCWRITES_SS = 4; // strict-serializable, nodes refuse reads if possible stale
// Generic "unknown" value that is useful for comparisons (e.g. always good enough)
const QOS_UNKNOWN = INF;
const ERR_NONE = 0; // no error
const ERR_NO_RESPONSE = 1; // no response
const ERR_UNREACHABLE = 2; // can't connect
const ERR_UNEXPECTED = 3; // response gave some error
}

View file

@ -0,0 +1,57 @@
<?php
/**
* Generic interface providing Time-To-Live constants for expirable object storage
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* http://www.gnu.org/copyleft/gpl.html
*
* @file
* @ingroup Cache
*/
namespace Wikimedia\LightweightObjectStore;
/**
* Generic interface providing Time-To-Live constants for expirable object storage
*
* @ingroup Cache
* @since 1.35
*/
interface ExpirationAwareness {
/** @var int One second, in seconds */
public const TTL_SECOND = 1;
/** @var int One minute, in seconds */
public const TTL_MINUTE = 60;
/** @var int One hour, in seconds */
public const TTL_HOUR = 3600;
/** @var int One day, in seconds */
public const TTL_DAY = 86400; // 24 * 3600
/** @var int One week, in seconds */
public const TTL_WEEK = 604800; // 7 * 24 * 3600
/** @var int One month, in seconds */
public const TTL_MONTH = 2592000; // 30 * 24 * 3600
/** @var int One year, in seconds */
public const TTL_YEAR = 31536000; // 365 * 24 * 3600
/** @var int Reasonably strict cache time that last the life of quick requests */
public const TTL_PROC_SHORT = 3;
/** @var int Loose cache time that can survive slow web requests */
public const TTL_PROC_LONG = 30;
/** @var int Idom for "store indefinitely" */
public const TTL_INDEFINITE = 0;
/** @var int Idiom for "do not store the newly generated result" */
public const TTL_UNCACHEABLE = -1;
}

View file

@ -0,0 +1,15 @@
<?php
use Wikimedia\LightweightObjectStore\ExpirationAwareness;
use Wikimedia\LightweightObjectStore\StorageAwareness;
/**
* Generic interface providing TTL constants for lightweight expiring object stores
*
* @ingroup Cache
* @since 1.27
* @deprecated 1.35
*/
interface IExpiringStore extends StorageAwareness, ExpirationAwareness {
}

View file

@ -0,0 +1,85 @@
<?php
/**
* Generic interface providing error code and quality-of-service constants for object stores
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* http://www.gnu.org/copyleft/gpl.html
*
* @file
* @ingroup Cache
*/
namespace Wikimedia\LightweightObjectStore;
/**
* Generic interface providing error code and quality-of-service constants for object stores
*
* @ingroup Cache
* @since 1.35
*/
interface StorageAwareness {
/** @var int No error */
public const ERR_NONE = 0;
/** @var int No store server/medium response */
public const ERR_NO_RESPONSE = 1;
/** @var int Cannot connect to store server/medium */
public const ERR_UNREACHABLE = 2;
/** @var int Operation failed */
public const ERR_UNEXPECTED = 3;
/** @var int Emulation/fallback mode; see ATTR_EMULATION_*; higher is better */
public const ATTR_EMULATION = 1;
/** @var int Multi-DC consistency of SYNC_WRITES; see ATTR_SYNCWRITES_*; higher is better */
public const ATTR_SYNCWRITES = 2;
/** @var int Locality; see ATTR_LOCALITY_*; higher is better */
public const ATTR_LOCALITY = 3;
/** @var int Durability; see ATTR_DURABILITY_*; higher is better */
public const ATTR_DURABILITY = 4;
/** @var int Fallback disk-based SQL store */
public const QOS_EMULATION_SQL = 1;
/** @var int Asynchronous; eventually consistent or even "best effort" replicated */
public const QOS_SYNCWRITES_NONE = 1;
/** @var int Synchronous; eventually consistent or even "best effort" replicated */
public const QOS_SYNCWRITES_BE = 2;
/** @var int Synchronous with quorum; (replicas read + replicas written) > quorum */
public const QOS_SYNCWRITES_QC = 3;
/** @var int Synchronous; strict serializable */
public const QOS_SYNCWRITES_SS = 4;
/** @var int Data is replicated accross a wide area network */
public const QOS_LOCALITY_WAN = 1;
/** @var int Data is stored on servers within the local area network */
public const QOS_LOCALITY_LAN = 2;
/** @var int Data is stored in RAM owned by another process or in the local filesystem */
public const QOS_LOCALITY_SRV = 3;
/** @var int Data is stored in RAM owned by this process */
public const QOS_LOCALITY_PROC = 4;
/** @var int Data is never saved to begin with (blackhole store) */
public const QOS_DURABILITY_NONE = 1;
/** @var int Data is lost at the end of the current web request or CLI script */
public const QOS_DURABILITY_SCRIPT = 2;
/** @var int Data is lost once the service storing the data restarts */
public const QOS_DURABILITY_SERVICE = 3;
/** @var int Data is saved to disk, though without immediate fsync() */
public const QOS_DURABILITY_DISK = 4;
/** @var int Data is saved to disk via an RDBMS, usually with immediate fsync() */
public const QOS_DURABILITY_RDBMS = 5;
/** @var int Generic "unknown" value; useful for comparisons (always "good enough") */
public const QOS_UNKNOWN = INF;
}

View file

@ -23,6 +23,8 @@ use Liuggio\StatsdClient\Factory\StatsdDataFactoryInterface;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use Wikimedia\LightweightObjectStore\ExpirationAwareness;
use Wikimedia\LightweightObjectStore\StorageAwareness;
/**
* Multi-datacenter aware caching interface
@ -113,7 +115,12 @@ use Psr\Log\NullLogger;
* @ingroup Cache
* @since 1.26
*/
class WANObjectCache implements IExpiringStore, IStoreKeyEncoder, LoggerAwareInterface {
class WANObjectCache implements
ExpirationAwareness,
StorageAwareness,
IStoreKeyEncoder,
LoggerAwareInterface
{
/** @var BagOStuff The local datacenter cache */
protected $cache;
/** @var MapCacheLRU[] Map of group PHP instance caches */
@ -157,9 +164,6 @@ class WANObjectCache implements IExpiringStore, IStoreKeyEncoder, LoggerAwareInt
/** @var int Seconds to tombstone keys on delete() and treat as volatile after invalidation */
const HOLDOFF_TTL = self::MAX_COMMIT_DELAY + self::MAX_READ_LAG + 1;
/** @var int Idiom for getWithSetCallback() meaning "do not store the callback result" */
const TTL_UNCACHEABLE = -1;
/** @var int Consider regeneration if the key will expire within this many seconds */
const LOW_TTL = 30;
/** @var int Max TTL, in seconds, to store keys when a data sourced is lagged */

View file

@ -171,7 +171,7 @@ class WANObjectCacheReaper implements LoggerAwareInterface {
'ctime' => $curValue ? $curValue['ctime'] : date( 'c' )
];
},
IExpiringStore::TTL_INDEFINITE
BagOStuff::TTL_INDEFINITE
);
$pos = $lastOkEvent['pos'];