1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
|
<?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
* @author Aaron Schulz
*/
/**
* Version of PoolCounter that uses Redis
*
* There are four main redis keys used to track each pool counter key:
* - poolcounter:l-slots-* : A list of available slot IDs for a pool.
* - poolcounter:z-renewtime-* : A sorted set of (slot ID, UNIX timestamp as score)
* used for tracking the next time a slot should be
* released. This is -1 when a slot is created, and is
* set when released (expired), locked, and unlocked.
* - poolcounter:z-wait-* : A sorted set of (slot ID, UNIX timestamp as score)
* used for tracking waiting processes (and wait time).
* - poolcounter:l-wakeup-* : A list pushed to for the sake of waking up processes
* when a any process in the pool finishes (lasts for 1ms).
* For a given pool key, all the redis keys start off non-existing and are deleted if not
* used for a while to prevent garbage from building up on the server. They are atomically
* re-initialized as needed. The "z-renewtime" key is used for detecting sessions which got
* slots but then disappeared. Stale entries from there have their timestamp updated and the
* corresponding slots freed up. The "z-wait" key is used for detecting processes registered
* as waiting but that disappeared. Stale entries from there are deleted and the corresponding
* slots are freed up. The worker count is included in all the redis key names as it does not
* vary within each $wgPoolCounterConf type and doing so handles configuration changes.
*
* This class requires Redis 2.6 as it makes use Lua scripts for fast atomic operations.
* Also this should be on a server plenty of RAM for the working set to avoid evictions.
* Evictions could temporarily allow wait queues to double in size or temporarily cause
* pools to appear as full when they are not. Using volatile-ttl and bumping memory-samples
* in redis.conf can be helpful otherwise.
*
* @ingroup Redis
* @since 1.23
*/
class PoolCounterRedis extends PoolCounter {
/** @var HashRing */
protected $ring;
/** @var RedisConnectionPool */
protected $pool;
/** @var array (server label => host) map */
protected $serversByLabel;
/** @var string SHA-1 of the key */
protected $keySha1;
/** @var int TTL for locks to expire (work should finish in this time) */
protected $lockTTL;
/** @var RedisConnRef */
protected $conn;
/** @var string Pool slot value */
protected $slot;
/** @var int AWAKE_* constant */
protected $onRelease;
/** @var string Unique string to identify this process */
protected $session;
/** @var int UNIX timestamp */
protected $slotTime;
const AWAKE_ONE = 1; // wake-up if when a slot can be taken from an existing process
const AWAKE_ALL = 2; // wake-up if an existing process finishes and wake up such others
/** @var PoolCounterRedis[] List of active PoolCounterRedis objects in this script */
protected static $active = null;
function __construct( $conf, $type, $key ) {
parent::__construct( $conf, $type, $key );
$this->serversByLabel = $conf['servers'];
$this->ring = new HashRing( array_fill_keys( array_keys( $conf['servers'] ), 100 ) );
$conf['redisConfig']['serializer'] = 'none'; // for use with Lua
$this->pool = RedisConnectionPool::singleton( $conf['redisConfig'] );
$this->keySha1 = sha1( $this->key );
$met = ini_get( 'max_execution_time' ); // usually 0 in CLI mode
$this->lockTTL = $met ? 2 * $met : 3600;
if ( self::$active === null ) {
self::$active = array();
register_shutdown_function( array( __CLASS__, 'releaseAll' ) );
}
}
/**
* @return Status Uses RediConnRef as value on success
*/
protected function getConnection() {
if ( !isset( $this->conn ) ) {
$conn = false;
$servers = $this->ring->getLocations( $this->key, 3 );
ArrayUtils::consistentHashSort( $servers, $this->key );
foreach ( $servers as $server ) {
$conn = $this->pool->getConnection( $this->serversByLabel[$server] );
if ( $conn ) {
break;
}
}
if ( !$conn ) {
return Status::newFatal( 'pool-servererror', implode( ', ', $servers ) );
}
$this->conn = $conn;
}
return Status::newGood( $this->conn );
}
function acquireForMe() {
$status = $this->precheckAcquire();
if ( !$status->isGood() ) {
return $status;
}
return $this->waitForSlotOrNotif( self::AWAKE_ONE );
}
function acquireForAnyone() {
$status = $this->precheckAcquire();
if ( !$status->isGood() ) {
return $status;
}
return $this->waitForSlotOrNotif( self::AWAKE_ALL );
}
function release() {
if ( $this->slot === null ) {
return Status::newGood( PoolCounter::NOT_LOCKED ); // not locked
}
$status = $this->getConnection();
if ( !$status->isOK() ) {
return $status;
}
$conn = $status->value;
static $script =
<<<LUA
local kSlots,kSlotsNextRelease,kWakeup,kWaiting = unpack(KEYS)
local rMaxWorkers,rExpiry,rSlot,rSlotTime,rAwakeAll,rTime = unpack(ARGV)
-- Add the slots back to the list (if rSlot is "w" then it is not a slot).
-- Treat the list as expired if the "next release" time sorted-set is missing.
if rSlot ~= 'w' and redis.call('exists',kSlotsNextRelease) == 1 then
if 1*redis.call('zScore',kSlotsNextRelease,rSlot) ~= (rSlotTime + rExpiry) then
-- Slot lock expired and was released already
elseif redis.call('lLen',kSlots) >= 1*rMaxWorkers then
-- Slots somehow got out of sync; reset the list for sanity
redis.call('del',kSlots,kSlotsNextRelease)
elseif redis.call('lLen',kSlots) == (1*rMaxWorkers - 1) and redis.call('zCard',kWaiting) == 0 then
-- Slot list will be made full; clear it to save space (it re-inits as needed)
-- since nothing is waiting on being unblocked by a push to the list
redis.call('del',kSlots,kSlotsNextRelease)
else
-- Add slot back to pool and update the "next release" time
redis.call('rPush',kSlots,rSlot)
redis.call('zAdd',kSlotsNextRelease,rTime + 30,rSlot)
-- Always keep renewing the expiry on use
redis.call('expireAt',kSlots,math.ceil(rTime + rExpiry))
redis.call('expireAt',kSlotsNextRelease,math.ceil(rTime + rExpiry))
end
end
-- Update an ephemeral list to wake up other clients that can
-- reuse any cached work from this process. Only do this if no
-- slots are currently free (e.g. clients could be waiting).
if 1*rAwakeAll == 1 then
local count = redis.call('zCard',kWaiting)
for i = 1,count do
redis.call('rPush',kWakeup,'w')
end
redis.call('pexpire',kWakeup,1)
end
return 1
LUA;
try {
$conn->luaEval( $script,
array(
$this->getSlotListKey(),
$this->getSlotRTimeSetKey(),
$this->getWakeupListKey(),
$this->getWaitSetKey(),
$this->workers,
$this->lockTTL,
$this->slot,
$this->slotTime, // used for CAS-style sanity check
( $this->onRelease === self::AWAKE_ALL ) ? 1 : 0,
microtime( true )
),
4 # number of first argument(s) that are keys
);
} catch ( RedisException $e ) {
return Status::newFatal( 'pool-error-unknown', $e->getMessage() );
}
$this->slot = null;
$this->slotTime = null;
$this->onRelease = null;
unset( self::$active[$this->session] );
$this->onRelease();
return Status::newGood( PoolCounter::RELEASED );
}
/**
* @param int $doWakeup AWAKE_* constant
* @return Status
*/
protected function waitForSlotOrNotif( $doWakeup ) {
if ( $this->slot !== null ) {
return Status::newGood( PoolCounter::LOCK_HELD ); // already acquired
}
$status = $this->getConnection();
if ( !$status->isOK() ) {
return $status;
}
$conn = $status->value;
$now = microtime( true );
try {
$slot = $this->initAndPopPoolSlotList( $conn, $now );
if ( ctype_digit( $slot ) ) {
// Pool slot acquired by this process
$slotTime = $now;
} elseif ( $slot === 'QUEUE_FULL' ) {
// Too many processes are waiting for pooled processes to finish
return Status::newGood( PoolCounter::QUEUE_FULL );
} elseif ( $slot === 'QUEUE_WAIT' ) {
// This process is now registered as waiting
$keys = ( $doWakeup == self::AWAKE_ALL )
// Wait for an open slot or wake-up signal (preferring the later)
? array( $this->getWakeupListKey(), $this->getSlotListKey() )
// Just wait for an actual pool slot
: array( $this->getSlotListKey() );
$res = $conn->blPop( $keys, $this->timeout );
if ( $res === array() ) {
$conn->zRem( $this->getWaitSetKey(), $this->session ); // no longer waiting
return Status::newGood( PoolCounter::TIMEOUT );
}
$slot = $res[1]; // pool slot or "w" for wake-up notifications
$slotTime = microtime( true ); // last microtime() was a few RTTs ago
// Unregister this process as waiting and bump slot "next release" time
$this->registerAcquisitionTime( $conn, $slot, $slotTime );
} else {
return Status::newFatal( 'pool-error-unknown', "Server gave slot '$slot'." );
}
} catch ( RedisException $e ) {
return Status::newFatal( 'pool-error-unknown', $e->getMessage() );
}
if ( $slot !== 'w' ) {
$this->slot = $slot;
$this->slotTime = $slotTime;
$this->onRelease = $doWakeup;
self::$active[$this->session] = $this;
}
$this->onAcquire();
return Status::newGood( $slot === 'w' ? PoolCounter::DONE : PoolCounter::LOCKED );
}
/**
* @param RedisConnRef $conn
* @param float $now UNIX timestamp
* @return string|bool False on failure
*/
protected function initAndPopPoolSlotList( RedisConnRef $conn, $now ) {
static $script =
<<<LUA
local kSlots,kSlotsNextRelease,kSlotWaits = unpack(KEYS)
local rMaxWorkers,rMaxQueue,rTimeout,rExpiry,rSess,rTime = unpack(ARGV)
-- Initialize if the "next release" time sorted-set is empty. The slot key
-- itself is empty if all slots are busy or when nothing is initialized.
-- If the list is empty but the set is not, then it is the later case.
-- For sanity, if the list exists but not the set, then reset everything.
if redis.call('exists',kSlotsNextRelease) == 0 then
redis.call('del',kSlots)
for i = 1,1*rMaxWorkers do
redis.call('rPush',kSlots,i)
redis.call('zAdd',kSlotsNextRelease,-1,i)
end
-- Otherwise do maintenance to clean up after network partitions
else
-- Find stale slot locks and add free them (avoid duplicates for sanity)
local staleLocks = redis.call('zRangeByScore',kSlotsNextRelease,0,rTime)
for k,slot in ipairs(staleLocks) do
redis.call('lRem',kSlots,0,slot)
redis.call('rPush',kSlots,slot)
redis.call('zAdd',kSlotsNextRelease,rTime + 30,slot)
end
-- Find stale wait slot entries and remove them
redis.call('zRemRangeByScore',kSlotWaits,0,rTime - 2*rTimeout)
end
local slot
-- Try to acquire a slot if possible now
if redis.call('lLen',kSlots) > 0 then
slot = redis.call('lPop',kSlots)
-- Update the slot "next release" time
redis.call('zAdd',kSlotsNextRelease,rTime + rExpiry,slot)
elseif redis.call('zCard',kSlotWaits) >= 1*rMaxQueue then
slot = 'QUEUE_FULL'
else
slot = 'QUEUE_WAIT'
-- Register this process as waiting
redis.call('zAdd',kSlotWaits,rTime,rSess)
redis.call('expireAt',kSlotWaits,math.ceil(rTime + 2*rTimeout))
end
-- Always keep renewing the expiry on use
redis.call('expireAt',kSlots,math.ceil(rTime + rExpiry))
redis.call('expireAt',kSlotsNextRelease,math.ceil(rTime + rExpiry))
return slot
LUA;
return $conn->luaEval( $script,
array(
$this->getSlotListKey(),
$this->getSlotRTimeSetKey(),
$this->getWaitSetKey(),
$this->workers,
$this->maxqueue,
$this->timeout,
$this->lockTTL,
$this->session,
$now
),
3 # number of first argument(s) that are keys
);
}
/**
* @param RedisConnRef $conn
* @param string $slot
* @param float $now
* @return int|bool False on failure
*/
protected function registerAcquisitionTime( RedisConnRef $conn, $slot, $now ) {
static $script =
<<<LUA
local kSlots,kSlotsNextRelease,kSlotWaits = unpack(KEYS)
local rSlot,rExpiry,rSess,rTime = unpack(ARGV)
-- If rSlot is 'w' then the client was told to wake up but got no slot
if rSlot ~= 'w' then
-- Update the slot "next release" time
redis.call('zAdd',kSlotsNextRelease,rTime + rExpiry,rSlot)
-- Always keep renewing the expiry on use
redis.call('expireAt',kSlots,math.ceil(rTime + rExpiry))
redis.call('expireAt',kSlotsNextRelease,math.ceil(rTime + rExpiry))
end
-- Unregister this process as waiting
redis.call('zRem',kSlotWaits,rSess)
return 1
LUA;
return $conn->luaEval( $script,
array(
$this->getSlotListKey(),
$this->getSlotRTimeSetKey(),
$this->getWaitSetKey(),
$slot,
$this->lockTTL,
$this->session,
$now
),
3 # number of first argument(s) that are keys
);
}
/**
* @return string
*/
protected function getSlotListKey() {
return "poolcounter:l-slots-{$this->keySha1}-{$this->workers}";
}
/**
* @return string
*/
protected function getSlotRTimeSetKey() {
return "poolcounter:z-renewtime-{$this->keySha1}-{$this->workers}";
}
/**
* @return string
*/
protected function getWaitSetKey() {
return "poolcounter:z-wait-{$this->keySha1}-{$this->workers}";
}
/**
* @return string
*/
protected function getWakeupListKey() {
return "poolcounter:l-wakeup-{$this->keySha1}-{$this->workers}";
}
/**
* Try to make sure that locks get released (even with exceptions and fatals)
*/
public static function releaseAll() {
foreach ( self::$active as $poolCounter ) {
try {
if ( $poolCounter->slot !== null ) {
$poolCounter->release();
}
} catch ( Exception $e ) {
}
}
}
}
|