MediaWiki  master
PoolCounterRedis.php
Go to the documentation of this file.
1 <?php
20 use Psr\Log\LoggerInterface;
21 
55  protected $ring;
57  protected $pool;
59  protected $logger;
61  protected $serversByLabel;
63  protected $keySha1;
65  protected $lockTTL;
67  protected $conn;
69  protected $slot;
71  protected $onRelease;
73  protected $session;
75  protected $slotTime;
76 
77  private const AWAKE_ONE = 1; // wake-up if when a slot can be taken from an existing process
78  private const AWAKE_ALL = 2; // wake-up if an existing process finishes and wake up such others
79 
81  protected static $active = null;
82 
83  public function __construct( $conf, $type, $key ) {
84  parent::__construct( $conf, $type, $key );
85 
86  $this->serversByLabel = $conf['servers'];
87 
88  $serverLabels = array_keys( $conf['servers'] );
89  $this->ring = new HashRing( array_fill_keys( $serverLabels, 10 ) );
90 
91  $conf['redisConfig']['serializer'] = 'none'; // for use with Lua
92  $this->pool = RedisConnectionPool::singleton( $conf['redisConfig'] );
93  $this->logger = \MediaWiki\Logger\LoggerFactory::getInstance( 'redis' );
94 
95  $this->keySha1 = sha1( $this->key );
96  $met = ini_get( 'max_execution_time' ); // usually 0 in CLI mode
97  $this->lockTTL = $met ? 2 * (int)$met : 3600;
98 
99  if ( self::$active === null ) {
100  self::$active = [];
101  register_shutdown_function( [ __CLASS__, 'releaseAll' ] );
102  }
103  }
104 
108  protected function getConnection() {
109  if ( !isset( $this->conn ) ) {
110  $conn = false;
111  $servers = $this->ring->getLocations( $this->key, 3 );
112  ArrayUtils::consistentHashSort( $servers, $this->key );
113  foreach ( $servers as $server ) {
114  $conn = $this->pool->getConnection( $this->serversByLabel[$server], $this->logger );
115  if ( $conn ) {
116  break;
117  }
118  }
119  if ( !$conn ) {
120  return Status::newFatal( 'pool-servererror', implode( ', ', $servers ) );
121  }
122  $this->conn = $conn;
123  }
124  return Status::newGood( $this->conn );
125  }
126 
127  public function acquireForMe( $timeout = null ) {
128  $status = $this->precheckAcquire();
129  if ( !$status->isGood() ) {
130  return $status;
131  }
132 
133  return $this->waitForSlotOrNotif( self::AWAKE_ONE, $timeout );
134  }
135 
136  public function acquireForAnyone( $timeout = null ) {
137  $status = $this->precheckAcquire();
138  if ( !$status->isGood() ) {
139  return $status;
140  }
141 
142  return $this->waitForSlotOrNotif( self::AWAKE_ALL, $timeout );
143  }
144 
145  public function release() {
146  if ( $this->slot === null ) {
147  return Status::newGood( PoolCounter::NOT_LOCKED ); // not locked
148  }
149 
150  $status = $this->getConnection();
151  if ( !$status->isOK() ) {
152  return $status;
153  }
155  $conn = $status->value;
156  '@phan-var RedisConnRef $conn';
157 
158  // phpcs:disable Generic.Files.LineLength
159  static $script =
161 <<<LUA
162  local kSlots,kSlotsNextRelease,kWakeup,kWaiting = unpack(KEYS)
163  local rMaxWorkers,rExpiry,rSlot,rSlotTime,rAwakeAll,rTime = unpack(ARGV)
164  -- Add the slots back to the list (if rSlot is "w" then it is not a slot).
165  -- Treat the list as expired if the "next release" time sorted-set is missing.
166  if rSlot ~= 'w' and redis.call('exists',kSlotsNextRelease) == 1 then
167  if 1*redis.call('zScore',kSlotsNextRelease,rSlot) ~= (rSlotTime + rExpiry) then
168  -- Slot lock expired and was released already
169  elseif redis.call('lLen',kSlots) >= 1*rMaxWorkers then
170  -- Slots somehow got out of sync; reset the list
171  redis.call('del',kSlots,kSlotsNextRelease)
172  elseif redis.call('lLen',kSlots) == (1*rMaxWorkers - 1) and redis.call('zCard',kWaiting) == 0 then
173  -- Slot list will be made full; clear it to save space (it re-inits as needed)
174  -- since nothing is waiting on being unblocked by a push to the list
175  redis.call('del',kSlots,kSlotsNextRelease)
176  else
177  -- Add slot back to pool and update the "next release" time
178  redis.call('rPush',kSlots,rSlot)
179  redis.call('zAdd',kSlotsNextRelease,rTime + 30,rSlot)
180  -- Always keep renewing the expiry on use
181  redis.call('expireAt',kSlots,math.ceil(rTime + rExpiry))
182  redis.call('expireAt',kSlotsNextRelease,math.ceil(rTime + rExpiry))
183  end
184  end
185  -- Update an ephemeral list to wake up other clients that can
186  -- reuse any cached work from this process. Only do this if no
187  -- slots are currently free (e.g. clients could be waiting).
188  if 1*rAwakeAll == 1 then
189  local count = redis.call('zCard',kWaiting)
190  for i = 1,count do
191  redis.call('rPush',kWakeup,'w')
192  end
193  redis.call('pexpire',kWakeup,1)
194  end
195  return 1
196 LUA;
197  // phpcs:enable
198 
199  try {
200  $conn->luaEval( $script,
201  [
202  $this->getSlotListKey(),
203  $this->getSlotRTimeSetKey(),
204  $this->getWakeupListKey(),
205  $this->getWaitSetKey(),
206  $this->workers,
207  $this->lockTTL,
208  $this->slot,
209  $this->slotTime, // used for CAS-style check
210  ( $this->onRelease === self::AWAKE_ALL ) ? 1 : 0,
211  microtime( true )
212  ],
213  4 # number of first argument(s) that are keys
214  );
215  } catch ( RedisException $e ) {
216  return Status::newFatal( 'pool-error-unknown', $e->getMessage() );
217  }
218 
219  $this->slot = null;
220  $this->slotTime = null;
221  $this->onRelease = null;
222  unset( self::$active[$this->session] );
223 
224  $this->onRelease();
225 
227  }
228 
234  protected function waitForSlotOrNotif( $doWakeup, $timeout = null ) {
235  if ( $this->slot !== null ) {
236  return Status::newGood( PoolCounter::LOCK_HELD ); // already acquired
237  }
238 
239  $status = $this->getConnection();
240  if ( !$status->isOK() ) {
241  return $status;
242  }
244  $conn = $status->value;
245  '@phan-var RedisConnRef $conn';
246 
247  $now = microtime( true );
249  try {
250  $slot = $this->initAndPopPoolSlotList( $conn, $now );
251  if ( ctype_digit( $slot ) ) {
252  // Pool slot acquired by this process
253  $slotTime = $now;
254  } elseif ( $slot === 'QUEUE_FULL' ) {
255  // Too many processes are waiting for pooled processes to finish
257  } elseif ( $slot === 'QUEUE_WAIT' ) {
258  // This process is now registered as waiting
259  $keys = ( $doWakeup == self::AWAKE_ALL )
260  // Wait for an open slot or wake-up signal (preferring the latter)
261  ? [ $this->getWakeupListKey(), $this->getSlotListKey() ]
262  // Just wait for an actual pool slot
263  : [ $this->getSlotListKey() ];
264 
265  $res = $conn->blPop( $keys, $timeout );
266  if ( $res === [] ) {
267  $conn->zRem( $this->getWaitSetKey(), $this->session ); // no longer waiting
269  }
270 
271  $slot = $res[1]; // pool slot or "w" for wake-up notifications
272  $slotTime = microtime( true ); // last microtime() was a few RTTs ago
273  // Unregister this process as waiting and bump slot "next release" time
275  } else {
276  return Status::newFatal( 'pool-error-unknown', "Server gave slot '$slot'." );
277  }
278  } catch ( RedisException $e ) {
279  return Status::newFatal( 'pool-error-unknown', $e->getMessage() );
280  }
281 
282  if ( $slot !== 'w' ) {
283  $this->slot = $slot;
284  $this->slotTime = $slotTime;
285  $this->onRelease = $doWakeup;
286  self::$active[$this->session] = $this;
287  }
288 
289  $this->onAcquire();
290 
292  }
293 
299  protected function initAndPopPoolSlotList( RedisConnRef $conn, $now ) {
300  static $script =
302 <<<LUA
303  local kSlots,kSlotsNextRelease,kSlotWaits = unpack(KEYS)
304  local rMaxWorkers,rMaxQueue,rTimeout,rExpiry,rSess,rTime = unpack(ARGV)
305  -- Initialize if the "next release" time sorted-set is empty. The slot key
306  -- itself is empty if all slots are busy or when nothing is initialized.
307  -- If the list is empty but the set is not, then it is the latter case.
308  -- If the list exists but not the set, then reset everything.
309  if redis.call('exists',kSlotsNextRelease) == 0 then
310  redis.call('del',kSlots)
311  for i = 1,1*rMaxWorkers do
312  redis.call('rPush',kSlots,i)
313  redis.call('zAdd',kSlotsNextRelease,-1,i)
314  end
315  -- Otherwise do maintenance to clean up after network partitions
316  else
317  -- Find stale slot locks and add free them (avoid duplicates)
318  local staleLocks = redis.call('zRangeByScore',kSlotsNextRelease,0,rTime)
319  for k,slot in ipairs(staleLocks) do
320  redis.call('lRem',kSlots,0,slot)
321  redis.call('rPush',kSlots,slot)
322  redis.call('zAdd',kSlotsNextRelease,rTime + 30,slot)
323  end
324  -- Find stale wait slot entries and remove them
325  redis.call('zRemRangeByScore',kSlotWaits,0,rTime - 2*rTimeout)
326  end
327  local slot
328  -- Try to acquire a slot if possible now
329  if redis.call('lLen',kSlots) > 0 then
330  slot = redis.call('lPop',kSlots)
331  -- Update the slot "next release" time
332  redis.call('zAdd',kSlotsNextRelease,rTime + rExpiry,slot)
333  elseif redis.call('zCard',kSlotWaits) >= 1*rMaxQueue then
334  slot = 'QUEUE_FULL'
335  else
336  slot = 'QUEUE_WAIT'
337  -- Register this process as waiting
338  redis.call('zAdd',kSlotWaits,rTime,rSess)
339  redis.call('expireAt',kSlotWaits,math.ceil(rTime + 2*rTimeout))
340  end
341  -- Always keep renewing the expiry on use
342  redis.call('expireAt',kSlots,math.ceil(rTime + rExpiry))
343  redis.call('expireAt',kSlotsNextRelease,math.ceil(rTime + rExpiry))
344  return slot
345 LUA;
346  return $conn->luaEval( $script,
347  [
348  $this->getSlotListKey(),
349  $this->getSlotRTimeSetKey(),
350  $this->getWaitSetKey(),
351  $this->workers,
352  $this->maxqueue,
353  $this->timeout,
354  $this->lockTTL,
355  $this->session,
356  $now
357  ],
358  3 # number of first argument(s) that are keys
359  );
360  }
361 
368  protected function registerAcquisitionTime( RedisConnRef $conn, $slot, $now ) {
369  static $script =
371 <<<LUA
372  local kSlots,kSlotsNextRelease,kSlotWaits = unpack(KEYS)
373  local rSlot,rExpiry,rSess,rTime = unpack(ARGV)
374  -- If rSlot is 'w' then the client was told to wake up but got no slot
375  if rSlot ~= 'w' then
376  -- Update the slot "next release" time
377  redis.call('zAdd',kSlotsNextRelease,rTime + rExpiry,rSlot)
378  -- Always keep renewing the expiry on use
379  redis.call('expireAt',kSlots,math.ceil(rTime + rExpiry))
380  redis.call('expireAt',kSlotsNextRelease,math.ceil(rTime + rExpiry))
381  end
382  -- Unregister this process as waiting
383  redis.call('zRem',kSlotWaits,rSess)
384  return 1
385 LUA;
386  return $conn->luaEval( $script,
387  [
388  $this->getSlotListKey(),
389  $this->getSlotRTimeSetKey(),
390  $this->getWaitSetKey(),
391  $slot,
392  $this->lockTTL,
393  $this->session,
394  $now
395  ],
396  3 # number of first argument(s) that are keys
397  );
398  }
399 
403  protected function getSlotListKey() {
404  return "poolcounter:l-slots-{$this->keySha1}-{$this->workers}";
405  }
406 
410  protected function getSlotRTimeSetKey() {
411  return "poolcounter:z-renewtime-{$this->keySha1}-{$this->workers}";
412  }
413 
417  protected function getWaitSetKey() {
418  return "poolcounter:z-wait-{$this->keySha1}-{$this->workers}";
419  }
420 
424  protected function getWakeupListKey() {
425  return "poolcounter:l-wakeup-{$this->keySha1}-{$this->workers}";
426  }
427 
431  public static function releaseAll() {
432  $e = null;
433  foreach ( self::$active as $poolCounter ) {
434  try {
435  if ( $poolCounter->slot !== null ) {
436  $poolCounter->release();
437  }
438  } catch ( Exception $e ) {
439  }
440  }
441  if ( $e ) {
442  throw $e;
443  }
444  }
445 }
static consistentHashSort(&$array, $key, $separator="\000")
Sort the given array in a pseudo-random order which depends only on the given key and each element va...
Definition: ArrayUtils.php:49
Convenience class for weighted consistent hash rings.
Definition: HashRing.php:44
static getInstance( $channel)
Get a named logger instance from the currently configured logger factory.
Version of PoolCounter that uses Redis.
acquireForAnyone( $timeout=null)
I want to do this task, but if anyone else does it instead, it's also fine for me.
__construct( $conf, $type, $key)
int null $onRelease
AWAKE_* constant.
float null $slotTime
UNIX timestamp.
LoggerInterface $logger
initAndPopPoolSlotList(RedisConnRef $conn, $now)
array $serversByLabel
(server label => host) map
string $session
Unique string to identify this process.
RedisConnRef $conn
static releaseAll()
Try to make sure that locks get released (even with exceptions and fatals)
release()
I have successfully finished my task.
static PoolCounterRedis[] $active
List of active PoolCounterRedis objects in this script.
waitForSlotOrNotif( $doWakeup, $timeout=null)
registerAcquisitionTime(RedisConnRef $conn, $slot, $now)
acquireForMe( $timeout=null)
I want to do this task and I need to do it myself.
int $lockTTL
TTL for locks to expire (work should finish in this time)
string null $slot
Pool slot value.
string $keySha1
SHA-1 of the key.
RedisConnectionPool $pool
Semaphore semantics to restrict how many workers may concurrently perform a task.
Definition: PoolCounter.php:49
const QUEUE_FULL
Definition: PoolCounter.php:57
const LOCKED
Definition: PoolCounter.php:51
onAcquire()
Update any lock tracking information when the lock is acquired.
const TIMEOUT
Definition: PoolCounter.php:58
string $key
All workers with the same key share the lock.
Definition: PoolCounter.php:62
const RELEASED
Definition: PoolCounter.php:52
precheckAcquire()
Checks that the lock request is sensible.
const NOT_LOCKED
Definition: PoolCounter.php:56
int $timeout
Maximum time in seconds to wait for the lock.
Definition: PoolCounter.php:75
const LOCK_HELD
Definition: PoolCounter.php:59
onRelease()
Update any lock tracking information when the lock is released.
Helper class to handle automatically marking connections as reusable (via RAII pattern)
luaEval( $script, array $params, $numKeys)
static singleton(array $options)
static newFatal( $message,... $parameters)
Factory function for fatal errors.
Definition: StatusValue.php:73
static newGood( $value=null)
Factory function for good results.
Definition: StatusValue.php:85