91 parent::__construct( $conf,
$type,
$key );
93 $this->serversByLabel = $conf[
'servers'];
95 $serverLabels = array_keys( $conf[
'servers'] );
96 $this->ring =
new HashRing( array_fill_keys( $serverLabels, 10 ) );
98 $conf[
'redisConfig'][
'serializer'] =
'none';
99 $this->pool = RedisConnectionPool::singleton( $conf[
'redisConfig'] );
101 $this->keySha1 = sha1( $this->key );
102 $met = ini_get(
'max_execution_time' );
103 $this->lockTTL = $met ? 2 * (int)$met : 3600;
105 if ( self::$active ===
null ) {
107 register_shutdown_function( [ __CLASS__,
'releaseAll' ] );
115 if ( !$this->conn ) {
117 $servers = $this->ring->getLocations( $this->key, 3 );
118 ArrayUtils::consistentHashSort( $servers, $this->key );
119 foreach ( $servers as $server ) {
120 $conn = $this->pool->getConnection( $this->serversByLabel[$server], $this->logger );
126 return Status::newFatal(
'pool-servererror', implode(
', ', $servers ) );
130 return Status::newGood( $this->conn );
152 if ( $this->slot ===
null ) {
157 if ( !$status->isOK() ) {
161 $conn = $status->value;
162 '@phan-var RedisConnRef $conn';
168 local kSlots,kSlotsNextRelease,kWakeup,kWaiting = unpack(KEYS)
169 local rMaxWorkers,rExpiry,rSlot,rSlotTime,rAwakeAll,rTime = unpack(ARGV)
170 -- Add the slots back to the list (
if rSlot is
"w" then it is not a slot).
171 -- Treat the list as expired
if the
"next release" time sorted-
set is missing.
172 if rSlot ~=
'w' and redis.call(
'exists',kSlotsNextRelease) == 1 then
173 if 1*redis.call(
'zScore',kSlotsNextRelease,rSlot) ~= (rSlotTime + rExpiry) then
174 -- Slot lock expired and was released already
175 elseif redis.call(
'lLen',kSlots) >= 1*rMaxWorkers then
176 -- Slots somehow got out of sync; reset the list
177 redis.call(
'del',kSlots,kSlotsNextRelease)
178 elseif redis.call(
'lLen',kSlots) == (1*rMaxWorkers - 1) and redis.call(
'zCard',kWaiting) == 0 then
179 -- Slot list will be made full; clear it to save space (it re-inits as needed)
180 -- since nothing is waiting on being unblocked by a push to the list
181 redis.call(
'del',kSlots,kSlotsNextRelease)
183 -- Add slot back to pool and update the
"next release" time
184 redis.call(
'rPush',kSlots,rSlot)
185 redis.call(
'zAdd',kSlotsNextRelease,rTime + 30,rSlot)
186 -- Always keep renewing the expiry on use
187 redis.call(
'expireAt',kSlots,math.ceil(rTime + rExpiry))
188 redis.call(
'expireAt',kSlotsNextRelease,math.ceil(rTime + rExpiry))
191 -- Update an ephemeral list to wake up other clients that can
192 -- reuse any cached work from
this process. Only
do this if no
193 -- slots are currently free (e.g. clients could be waiting).
194 if 1*rAwakeAll == 1 then
195 local count = redis.call(
'zCard',kWaiting)
197 redis.call(
'rPush',kWakeup,
'w')
199 redis.call(
'pexpire',kWakeup,1)
217 ( $this->
onRelease === self::AWAKE_ALL ) ? 1 : 0,
220 4 # number of first argument(s) that are keys
222 }
catch ( RedisException $e ) {
223 return Status::newFatal(
'pool-error-unknown', $e->getMessage() );
227 $this->slotTime =
null;
229 unset( self::$active[$this->session] );
306 static $script = <<<LUA
307 local kSlots,kSlotsNextRelease,kSlotWaits = unpack(KEYS)
308 local rMaxWorkers,rMaxQueue,rTimeout,rExpiry,rSess,rTime = unpack(ARGV)
309 -- Initialize
if the
"next release" time sorted-
set is empty. The slot key
310 -- itself is empty
if all slots are busy or when nothing is initialized.
311 -- If the list is empty but the
set is not, then it is the latter
case.
312 -- If the list exists but not the
set, then reset everything.
313 if redis.call(
'exists',kSlotsNextRelease) == 0 then
314 redis.call(
'del',kSlots)
315 for i = 1,1*rMaxWorkers
do
316 redis.call(
'rPush',kSlots,i)
317 redis.call(
'zAdd',kSlotsNextRelease,-1,i)
319 -- Otherwise
do maintenance to clean up after network partitions
321 -- Find stale slot locks and add free them (avoid duplicates)
322 local staleLocks = redis.call(
'zRangeByScore',kSlotsNextRelease,0,rTime)
323 for k,slot in ipairs(staleLocks)
do
324 redis.call(
'lRem',kSlots,0,slot)
325 redis.call(
'rPush',kSlots,slot)
326 redis.call(
'zAdd',kSlotsNextRelease,rTime + 30,slot)
328 -- Find stale wait slot entries and
remove them
329 redis.call(
'zRemRangeByScore',kSlotWaits,0,rTime - 2*rTimeout)
332 -- Try to acquire a slot
if possible now
333 if redis.call(
'lLen',kSlots) > 0 then
334 slot = redis.call(
'lPop',kSlots)
335 -- Update the slot
"next release" time
336 redis.call(
'zAdd',kSlotsNextRelease,rTime + rExpiry,slot)
337 elseif redis.call(
'zCard',kSlotWaits) >= 1*rMaxQueue then
341 -- Register
this process as waiting
342 redis.call(
'zAdd',kSlotWaits,rTime,rSess)
343 redis.call(
'expireAt',kSlotWaits,math.ceil(rTime + 2*rTimeout))
345 -- Always keep renewing the expiry on use
346 redis.call(
'expireAt',kSlots,math.ceil(rTime + rExpiry))
347 redis.call(
'expireAt',kSlotsNextRelease,math.ceil(rTime + rExpiry))
363 3 # number of first argument(s) that are keys
374 static $script = <<<LUA
375 local kSlots,kSlotsNextRelease,kSlotWaits = unpack(KEYS)
376 local rSlot,rExpiry,rSess,rTime = unpack(ARGV)
377 -- If rSlot is
'w' then the client was told to wake up but got no slot
379 -- Update the slot
"next release" time
380 redis.call(
'zAdd',kSlotsNextRelease,rTime + rExpiry,rSlot)
381 -- Always keep renewing the expiry on use
382 redis.call(
'expireAt',kSlots,math.ceil(rTime + rExpiry))
383 redis.call(
'expireAt',kSlotsNextRelease,math.ceil(rTime + rExpiry))
385 -- Unregister
this process as waiting
386 redis.call(
'zRem',kSlotWaits,rSess)
400 3 # number of first argument(s) that are keys