4 * Class for cooperative locking of web resources
6 * Each resource is identified by a combination of the "resource type" (the application, the type
7 * of content, etc), and the resource's primary key or some other unique numeric ID.
9 * Currently, a resource can only be checked out by a single user. Other attempts to check it out result
10 * in the checkout failing. In the future, an option for multiple simulataneous checkouts could be added
11 * without much trouble.
13 * This could be done with named locks, except then it would be impossible to build a list of all the
14 * resources currently checked out for a given application. There's no good way to construct a query
15 * that answers the question, "What locks do you have starting with [foo]" This could be done really well
16 * with a concurrent, reliable, distributed key/value store, but we don't have one of those right now.
18 * @author Ian Baker <ian@wikimedia.org>
20 class ConcurrencyCheck
{
24 * @var $resourceType String The calling application or type of resource, conceptually like a namespace
25 * @var $user User object, the current user
26 * @var $expirationTime Integer (optional) How long should a checkout last, in seconds
28 public function __construct( $resourceType, $user, $expirationTime = null ) {
30 // All database calls are to the master, since the whole point of this class is maintaining
31 // concurrency. Most reads should come from cache anyway.
32 $this->dbw
= wfGetDb( DB_MASTER
);
35 // TODO: create a registry of all valid resourceTypes that client app can add to.
36 $this->resourceType
= $resourceType;
37 $this->setExpirationTime( $expirationTime );
41 * Check out a resource. This establishes an atomically generated, cooperative lock
42 * on a key. The lock is tied to the current user.
44 * @var $record Integer containing the record id to check out
45 * @var $override Boolean (optional) describing whether to override an existing checkout
48 public function checkout( $record, $override = null ) {
49 $memc = wfGetMainCache();
50 $this->validateId( $record );
52 $userId = $this->user
->getId();
53 $cacheKey = wfMemcKey( $this->resourceType
, $record );
55 // when operating with a single memcached cluster, it's reasonable to check the cache here.
56 global $wgConcurrencyTrustMemc;
57 if( $wgConcurrencyTrustMemc ) {
58 $cached = $memc->get( $cacheKey );
60 if( ! $override && $cached['userId'] != $userId && $cached['expiration'] > time() ) {
61 // this is already checked out.
67 // attempt an insert, check success (this is atomic)
72 'cc_resource_type' => $this->resourceType
,
73 'cc_record' => $record,
75 'cc_expiration' => time() +
$this->expirationTime
,
81 // if the insert succeeded, checkout is done.
82 if( $dbw->affectedRows() === 1 ) {
83 // delete any existing cache key. can't create a new key here
84 // since the insert didn't happen inside a transaction.
85 $memc->delete( $cacheKey );
89 // if the insert failed, it's necessary to check the expiration.
91 $row = $dbw->selectRow(
93 array( 'cc_user', 'cc_expiration' ),
95 'cc_resource_type' => $this->resourceType
,
96 'cc_record' => $record,
102 // not checked out by current user, checkout is unexpired, override is unset
103 if( ! ( $override ||
$row->cc_user
== $userId ||
$row->cc_expiration
<= time() ) ) {
104 // this was a cache miss. populate the cache with data from the db.
105 // cache is set to expire at the same time as the checkout, since it'll become invalid then anyway.
106 // inside this transaction, a row-level lock is established which ensures cache concurrency
107 $memc->set( $cacheKey, array( 'userId' => $row->cc_user
, 'expiration' => $row->cc_expiration
), $row->cc_expiration
- time() );
112 $expiration = time() +
$this->expirationTime
;
115 $res = $dbw->replace(
117 array( array('cc_resource_type', 'cc_record') ),
119 'cc_resource_type' => $this->resourceType
,
120 'cc_record' => $record,
121 'cc_user' => $userId,
122 'cc_expiration' => $expiration,
128 $memc->set( $cacheKey, array( 'userId' => $userId, 'expiration' => $expiration ), $this->expirationTime
);
135 * Check in a resource. Only works if the resource is checked out by the current user.
137 * @var $record Integer containing the record id to checkin
140 public function checkin( $record ) {
141 $memc = wfGetMainCache();
142 $this->validateId( $record );
144 $userId = $this->user
->getId();
145 $cacheKey = wfMemcKey( $this->resourceType
, $record );
150 'cc_resource_type' => $this->resourceType
,
151 'cc_record' => $record,
152 'cc_user' => $userId, // only the owner can perform a checkin
158 // check row count (this is atomic, select would not be)
159 if( $dbw->affectedRows() === 1 ) {
160 $memc->delete( $cacheKey );
168 * Remove all expired checkouts.
170 * @return Integer describing the number of records expired.
172 public function expire() {
173 $memc = wfGetMainCache();
177 // get the rows to remove from cache.
182 'cc_expiration <= ' . $now,
188 // build a list of rows to delete.
190 while( $res && $record = $res->fetchRow() ) {
191 $toExpire[] = $record['cc_record'];
194 // remove the rows from the db
198 'cc_expiration <= ' . $now,
204 // delete all those rows from cache
205 // outside a transaction because deletes don't require atomicity.
206 foreach( $toExpire as $expire ) {
207 $memc->delete( wfMemcKey( $this->resourceType
, $expire ) );
210 // return the number of rows removed.
211 return $dbw->affectedRows();
214 public function status( $keys ) {
215 $memc = wfGetMainCache();
219 $checkouts = array();
222 // validate keys, attempt to retrieve from cache.
223 foreach( $keys as $key ) {
224 $this->validateId( $key );
226 $cached = $memc->get( wfMemcKey( $this->resourceType
, $key ) );
227 if( $cached && $cached['expiration'] > $now ) {
228 $checkouts[$key] = array(
230 'cc_resource_type' => $this->resourceType
,
232 'cc_user' => $cached['userId'],
233 'cc_expiration' => $cached['expiration'],
241 // if there were cache misses...
243 // If it's time to go to the database, go ahead and expire old rows.
246 // the transaction seems incongruous, I know, but it's to keep the cache update atomic.
252 'cc_resource_type' => $this->resourceType
,
253 'cc_record IN (' . implode( ',', $toSelect ) . ')',
254 'cc_expiration > unix_timestamp(now())'
260 while( $res && $record = $res->fetchRow() ) {
261 $record['status'] = 'valid';
262 $checkouts[ $record['cc_record'] ] = $record;
264 // safe to store values since this is inside the transaction
266 wfMemcKey( $this->resourceType
, $record['cc_record'] ),
267 array( 'userId' => $record['cc_user'], 'expiration' => $record['cc_expiration'] ),
268 $record['cc_expiration'] - time()
272 // end the transaction.
276 // if a key was passed in but has no (unexpired) checkout, include it in the
277 // result set to make things easier and more consistent on the client-side.
278 foreach( $keys as $key ) {
279 if( ! array_key_exists( $key, $checkouts ) ) {
280 $checkouts[$key]['status'] = 'invalid';
287 public function listCheckouts() {
288 // TODO: fill in the function that lets you get the complete set of checkouts for a given application.
291 public function setUser ( $user ) {
295 public function setExpirationTime ( $expirationTime = null ) {
296 global $wgConcurrencyExpirationDefault, $wgConcurrencyExpirationMax, $wgConcurrencyExpirationMin;
298 // check to make sure the time is digits only, so it can be used in queries
299 // negative number are allowed, though mostly only used for testing
300 if( $expirationTime && preg_match('/^[\d-]+$/', $expirationTime) ) {
301 if( $expirationTime > $wgConcurrencyExpirationMax ) {
302 $this->expirationTime
= $wgConcurrencyExpirationMax; // if the number is too high, limit it to the max value.
303 } elseif ( $expirationTime < $wgConcurrencyExpirationMin ) {
304 $this->expirationTime
= $wgConcurrencyExpirationMin; // low limit, default -1 min
306 $this->expirationTime
= $expirationTime; // the amount of time before a checkout expires.
309 $this->expirationTime
= $wgConcurrencyExpirationDefault; // global default is 15 mins.
314 * Check to make sure a record ID is numeric, throw an exception if not.
316 * @var $record Integer
317 * @throws ConcurrencyCheckBadRecordIdException
320 private static function validateId ( $record ) {
321 if( ! preg_match('/^\d+$/', $record) ) {
322 throw new ConcurrencyCheckBadRecordIdException( 'Record ID ' . $record . ' must be a positive integer' );
325 // TODO: add a hook here for client-side validation.
330 class ConcurrencyCheckBadRecordIdException
extends MWException
{};