2011-12-20 03:52:06 +00:00
|
|
|
<?php
|
2012-05-07 07:11:33 +00:00
|
|
|
/**
|
|
|
|
|
* Version of LockManager based on using DB table locks.
|
|
|
|
|
*
|
|
|
|
|
* 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 LockManager
|
|
|
|
|
*/
|
2011-12-20 03:52:06 +00:00
|
|
|
|
2017-02-07 04:49:57 +00:00
|
|
|
use Wikimedia\Rdbms\Database;
|
2017-02-24 16:17:16 +00:00
|
|
|
use Wikimedia\Rdbms\DBError;
|
2020-01-10 00:00:51 +00:00
|
|
|
use Wikimedia\Rdbms\IDatabase;
|
2017-02-10 18:09:05 +00:00
|
|
|
|
2011-12-20 03:52:06 +00:00
|
|
|
/**
|
2013-02-03 08:00:50 +00:00
|
|
|
* Version of LockManager based on using named/row DB locks.
|
2013-02-01 18:29:10 +00:00
|
|
|
*
|
2011-12-20 03:52:06 +00:00
|
|
|
* This is meant for multi-wiki systems that may share files.
|
|
|
|
|
*
|
2016-08-15 21:29:04 +00:00
|
|
|
* All lock requests for a resource, identified by a hash string, will map to one bucket.
|
|
|
|
|
* Each bucket maps to one or several peer DBs, each on their own server.
|
2011-12-20 03:52:06 +00:00
|
|
|
* A majority of peer DBs must agree for a lock to be acquired.
|
|
|
|
|
*
|
|
|
|
|
* Caching is used to avoid hitting servers that are down.
|
|
|
|
|
*
|
|
|
|
|
* @ingroup LockManager
|
2012-01-13 23:30:46 +00:00
|
|
|
* @since 1.19
|
2011-12-20 03:52:06 +00:00
|
|
|
*/
|
2013-02-03 08:00:50 +00:00
|
|
|
abstract class DBLockManager extends QuorumLockManager {
|
2016-09-18 06:35:05 +00:00
|
|
|
/** @var array[]|IDatabase[] Map of (DB names => server config or IDatabase) */
|
2011-12-20 03:52:06 +00:00
|
|
|
protected $dbServers; // (DB name => server config array)
|
|
|
|
|
/** @var BagOStuff */
|
|
|
|
|
protected $statusCache;
|
|
|
|
|
|
|
|
|
|
protected $lockExpiry; // integer number of seconds
|
|
|
|
|
protected $safeDelay; // integer number of seconds
|
2016-07-22 22:38:05 +00:00
|
|
|
/** @var IDatabase[] Map Database connections (DB name => Database) */
|
2016-02-17 09:09:32 +00:00
|
|
|
protected $conns = [];
|
2011-12-20 03:52:06 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Construct a new instance from configuration.
|
2012-06-21 20:47:13 +00:00
|
|
|
*
|
2014-12-12 08:41:27 +00:00
|
|
|
* @param array $config Parameters include:
|
2012-09-15 17:58:56 +00:00
|
|
|
* - dbServers : Associative array of DB names to server configuration.
|
|
|
|
|
* Configuration is an associative array that includes:
|
|
|
|
|
* - host : DB server name
|
|
|
|
|
* - dbname : DB name
|
|
|
|
|
* - type : DB type (mysql,postgres,...)
|
|
|
|
|
* - user : DB user
|
|
|
|
|
* - password : DB user password
|
|
|
|
|
* - tablePrefix : DB table prefix
|
2016-09-28 22:53:02 +00:00
|
|
|
* - flags : DB flags; bitfield of IDatabase::DBO_* constants
|
2012-09-15 17:58:56 +00:00
|
|
|
* - dbsByBucket : Array of 1-16 consecutive integer keys, starting from 0,
|
|
|
|
|
* each having an odd-numbered list of DB names (peers) as values.
|
|
|
|
|
* - lockExpiry : Lock timeout (seconds) for dropped connections. [optional]
|
|
|
|
|
* This tells the DB server how long to wait before assuming
|
|
|
|
|
* connection failure and releasing all the locks for a session.
|
2016-09-18 06:35:05 +00:00
|
|
|
* - srvCache : A BagOStuff instance using APC or the like.
|
2011-12-20 03:52:06 +00:00
|
|
|
*/
|
|
|
|
|
public function __construct( array $config ) {
|
2012-03-04 21:08:55 +00:00
|
|
|
parent::__construct( $config );
|
|
|
|
|
|
2016-09-18 06:35:05 +00:00
|
|
|
$this->dbServers = $config['dbServers'];
|
2012-06-21 20:47:13 +00:00
|
|
|
// Sanitize srvsByBucket config to prevent PHP errors
|
|
|
|
|
$this->srvsByBucket = array_filter( $config['dbsByBucket'], 'is_array' );
|
|
|
|
|
$this->srvsByBucket = array_values( $this->srvsByBucket ); // consecutive
|
2011-12-20 03:52:06 +00:00
|
|
|
|
|
|
|
|
if ( isset( $config['lockExpiry'] ) ) {
|
|
|
|
|
$this->lockExpiry = $config['lockExpiry'];
|
|
|
|
|
} else {
|
|
|
|
|
$met = ini_get( 'max_execution_time' );
|
2018-06-11 09:16:48 +00:00
|
|
|
$this->lockExpiry = $met ?: 60; // use some sane amount if 0
|
2011-12-20 03:52:06 +00:00
|
|
|
}
|
|
|
|
|
$this->safeDelay = ( $this->lockExpiry <= 0 )
|
|
|
|
|
? 60 // pick a safe-ish number to match DB timeout default
|
|
|
|
|
: $this->lockExpiry; // cover worst case
|
|
|
|
|
|
2016-09-18 06:35:05 +00:00
|
|
|
// Tracks peers that couldn't be queried recently to avoid lengthy
|
|
|
|
|
// connection timeouts. This is useless if each bucket has one peer.
|
2017-10-06 22:17:58 +00:00
|
|
|
$this->statusCache = $config['srvCache'] ?? new HashBagOStuff();
|
2011-12-20 03:52:06 +00:00
|
|
|
}
|
|
|
|
|
|
2016-09-18 06:35:05 +00:00
|
|
|
/**
|
2018-07-26 16:31:49 +00:00
|
|
|
* @todo change this code to work in one batch
|
2016-09-18 06:35:05 +00:00
|
|
|
* @param string $lockSrv
|
|
|
|
|
* @param array $pathsByType
|
|
|
|
|
* @return StatusValue
|
|
|
|
|
*/
|
2013-06-16 20:48:17 +00:00
|
|
|
protected function getLocksOnServer( $lockSrv, array $pathsByType ) {
|
2016-09-18 04:42:56 +00:00
|
|
|
$status = StatusValue::newGood();
|
2013-06-16 20:48:17 +00:00
|
|
|
foreach ( $pathsByType as $type => $paths ) {
|
|
|
|
|
$status->merge( $this->doGetLocksOnServer( $lockSrv, $paths, $type ) );
|
|
|
|
|
}
|
2013-11-22 21:17:15 +00:00
|
|
|
|
2013-06-16 20:48:17 +00:00
|
|
|
return $status;
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-22 22:38:05 +00:00
|
|
|
abstract protected function doGetLocksOnServer( $lockSrv, array $paths, $type );
|
|
|
|
|
|
2013-06-16 20:48:17 +00:00
|
|
|
protected function freeLocksOnServer( $lockSrv, array $pathsByType ) {
|
2016-09-18 04:42:56 +00:00
|
|
|
return StatusValue::newGood();
|
2013-06-16 20:48:17 +00:00
|
|
|
}
|
|
|
|
|
|
2011-12-20 03:52:06 +00:00
|
|
|
/**
|
2012-06-21 20:47:13 +00:00
|
|
|
* @see QuorumLockManager::isServerUp()
|
2013-11-23 18:23:32 +00:00
|
|
|
* @param string $lockSrv
|
2012-06-21 20:47:13 +00:00
|
|
|
* @return bool
|
2011-12-20 03:52:06 +00:00
|
|
|
*/
|
2012-06-21 20:47:13 +00:00
|
|
|
protected function isServerUp( $lockSrv ) {
|
|
|
|
|
if ( !$this->cacheCheckFailures( $lockSrv ) ) {
|
|
|
|
|
return false; // recent failure to connect
|
2011-12-20 03:52:06 +00:00
|
|
|
}
|
2012-06-21 20:47:13 +00:00
|
|
|
try {
|
|
|
|
|
$this->getConnection( $lockSrv );
|
|
|
|
|
} catch ( DBError $e ) {
|
|
|
|
|
$this->cacheRecordFailure( $lockSrv );
|
2013-11-22 21:17:15 +00:00
|
|
|
|
2012-06-21 20:47:13 +00:00
|
|
|
return false; // failed to connect
|
2011-12-20 03:52:06 +00:00
|
|
|
}
|
2013-11-22 21:17:15 +00:00
|
|
|
|
2012-06-21 20:47:13 +00:00
|
|
|
return true;
|
2011-12-20 03:52:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get (or reuse) a connection to a lock DB
|
|
|
|
|
*
|
2013-11-23 18:23:32 +00:00
|
|
|
* @param string $lockDb
|
2015-10-06 05:39:37 +00:00
|
|
|
* @return IDatabase
|
2011-12-20 03:52:06 +00:00
|
|
|
* @throws DBError
|
2016-08-23 05:04:43 +00:00
|
|
|
* @throws UnexpectedValueException
|
2011-12-20 03:52:06 +00:00
|
|
|
*/
|
|
|
|
|
protected function getConnection( $lockDb ) {
|
|
|
|
|
if ( !isset( $this->conns[$lockDb] ) ) {
|
2016-09-18 06:35:05 +00:00
|
|
|
if ( $this->dbServers[$lockDb] instanceof IDatabase ) {
|
|
|
|
|
// Direct injected connection hande for $lockDB
|
|
|
|
|
$db = $this->dbServers[$lockDb];
|
|
|
|
|
} elseif ( is_array( $this->dbServers[$lockDb] ) ) {
|
|
|
|
|
// Parameters to construct a new database connection
|
2011-12-20 03:52:06 +00:00
|
|
|
$config = $this->dbServers[$lockDb];
|
2019-07-30 15:37:34 +00:00
|
|
|
$config['flags'] = ( $config['flags'] ?? 0 );
|
|
|
|
|
$config['flags'] &= ~( IDatabase::DBO_TRX | IDatabase::DBO_DEFAULT );
|
2016-09-26 22:40:07 +00:00
|
|
|
$db = Database::factory( $config['type'], $config );
|
2016-08-23 05:04:43 +00:00
|
|
|
} else {
|
|
|
|
|
throw new UnexpectedValueException( "No server called '$lockDb'." );
|
2011-12-20 03:52:06 +00:00
|
|
|
}
|
2016-09-18 06:35:05 +00:00
|
|
|
# If the connection drops, try to avoid letting the DB rollback
|
|
|
|
|
# and release the locks before the file operations are finished.
|
|
|
|
|
# This won't handle the case of DB server restarts however.
|
|
|
|
|
$options = [];
|
|
|
|
|
if ( $this->lockExpiry > 0 ) {
|
|
|
|
|
$options['connTimeout'] = $this->lockExpiry;
|
2011-12-20 03:52:06 +00:00
|
|
|
}
|
2016-09-18 06:35:05 +00:00
|
|
|
$db->setSessionOptions( $options );
|
|
|
|
|
$this->initConnection( $lockDb, $db );
|
2016-08-23 05:04:43 +00:00
|
|
|
|
2011-12-20 03:52:06 +00:00
|
|
|
$this->conns[$lockDb] = $db;
|
|
|
|
|
}
|
2013-11-22 21:17:15 +00:00
|
|
|
|
2011-12-20 03:52:06 +00:00
|
|
|
return $this->conns[$lockDb];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Do additional initialization for new lock DB connection
|
|
|
|
|
*
|
2013-11-23 18:23:32 +00:00
|
|
|
* @param string $lockDb
|
2015-10-06 05:39:37 +00:00
|
|
|
* @param IDatabase $db
|
2011-12-20 03:52:06 +00:00
|
|
|
* @throws DBError
|
|
|
|
|
*/
|
2015-10-06 05:39:37 +00:00
|
|
|
protected function initConnection( $lockDb, IDatabase $db ) {
|
2013-11-22 21:17:15 +00:00
|
|
|
}
|
2011-12-20 03:52:06 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Checks if the DB has not recently had connection/query errors.
|
|
|
|
|
* This just avoids wasting time on doomed connection attempts.
|
2012-06-21 20:47:13 +00:00
|
|
|
*
|
2014-04-19 15:19:17 +00:00
|
|
|
* @param string $lockDb
|
2011-12-20 03:52:06 +00:00
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
protected function cacheCheckFailures( $lockDb ) {
|
2016-09-18 06:35:05 +00:00
|
|
|
return ( $this->safeDelay > 0 )
|
2012-06-21 20:47:13 +00:00
|
|
|
? !$this->statusCache->get( $this->getMissKey( $lockDb ) )
|
|
|
|
|
: true;
|
2011-12-20 03:52:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Log a lock request failure to the cache
|
|
|
|
|
*
|
2013-11-23 18:23:32 +00:00
|
|
|
* @param string $lockDb
|
2011-12-20 03:52:06 +00:00
|
|
|
* @return bool Success
|
|
|
|
|
*/
|
|
|
|
|
protected function cacheRecordFailure( $lockDb ) {
|
2016-09-18 06:35:05 +00:00
|
|
|
return ( $this->safeDelay > 0 )
|
2012-06-21 20:47:13 +00:00
|
|
|
? $this->statusCache->set( $this->getMissKey( $lockDb ), 1, $this->safeDelay )
|
|
|
|
|
: true;
|
2011-12-20 03:52:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get a cache key for recent query misses for a DB
|
|
|
|
|
*
|
2013-11-23 18:23:32 +00:00
|
|
|
* @param string $lockDb
|
2011-12-20 03:52:06 +00:00
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
protected function getMissKey( $lockDb ) {
|
2012-06-21 20:47:13 +00:00
|
|
|
return 'dblockmanager:downservers:' . str_replace( ' ', '_', $lockDb );
|
2011-12-20 03:52:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Make sure remaining locks get cleared for sanity
|
|
|
|
|
*/
|
2019-12-05 17:52:55 +00:00
|
|
|
public function __destruct() {
|
2013-02-03 08:00:50 +00:00
|
|
|
$this->releaseAllLocks();
|
2012-08-07 23:47:25 +00:00
|
|
|
foreach ( $this->conns as $db ) {
|
2020-06-07 12:16:52 +00:00
|
|
|
$db->close( __METHOD__ );
|
2011-12-20 03:52:06 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|