77 parent::__construct( $conf,
$type,
$key );
79 $this->serversByLabel = $conf[
'servers'];
81 $serverLabels = array_keys( $conf[
'servers'] );
82 $this->ring =
new HashRing( array_fill_keys( $serverLabels, 10 ) );
84 $conf[
'redisConfig'][
'serializer'] =
'none';
85 $this->pool = RedisConnectionPool::singleton( $conf[
'redisConfig'] );
87 $this->keySha1 = sha1( $this->key );
88 $met = ini_get(
'max_execution_time' );
89 $this->lockTTL = $met ? 2 * (int)$met : 3600;
91 if ( self::$active ===
null ) {
93 register_shutdown_function( self::releaseAll( ... ) );
101 if ( !$this->conn ) {
103 $servers = $this->ring->getLocations( $this->key, 3 );
104 ArrayUtils::consistentHashSort( $servers, $this->key );
105 foreach ( $servers as $server ) {
106 $conn = $this->pool->getConnection( $this->serversByLabel[$server], $this->logger );
112 return Status::newFatal(
'pool-servererror', implode(
', ', $servers ) );
116 return Status::newGood( $this->conn );
141 if ( $this->slot ===
null ) {
146 if ( !$status->isOK() ) {
150 $conn = $status->value;
151 '@phan-var RedisConnRef $conn';
157 local kSlots,kSlotsNextRelease,kWakeup,kWaiting = unpack(KEYS)
158 local rMaxWorkers,rExpiry,rSlot,rSlotTime,rAwakeAll,rTime = unpack(ARGV)
159 -- Add the slots back to the list (
if rSlot is
"w" then it is not a slot).
160 -- Treat the list as expired
if the
"next release" time sorted-
set is missing.
161 if rSlot ~=
'w' and redis.call(
'exists',kSlotsNextRelease) == 1 then
162 if 1*redis.call(
'zScore',kSlotsNextRelease,rSlot) ~= (rSlotTime + rExpiry) then
163 -- Slot lock expired and was released already
164 elseif redis.call(
'lLen',kSlots) >= 1*rMaxWorkers then
165 -- Slots somehow got out of sync; reset the list
166 redis.call(
'del',kSlots,kSlotsNextRelease)
167 elseif redis.call(
'lLen',kSlots) == (1*rMaxWorkers - 1) and redis.call(
'zCard',kWaiting) == 0 then
168 -- Slot list will be made full; clear it to save space (it re-inits as needed)
169 -- since nothing is waiting on being unblocked by a push to the list
170 redis.call(
'del',kSlots,kSlotsNextRelease)
172 -- Add slot back to pool and update the
"next release" time
173 redis.call(
'rPush',kSlots,rSlot)
174 redis.call(
'zAdd',kSlotsNextRelease,rTime + 30,rSlot)
175 -- Always keep renewing the expiry on use
176 redis.call(
'expireAt',kSlots,math.ceil(rTime + rExpiry))
177 redis.call(
'expireAt',kSlotsNextRelease,math.ceil(rTime + rExpiry))
180 -- Update an ephemeral list to wake up other clients that can
181 -- reuse any cached work from
this process. Only
do this if no
182 -- slots are currently free (e.g. clients could be waiting).
183 if 1*rAwakeAll == 1 then
184 local count = redis.call(
'zCard',kWaiting)
186 redis.call(
'rPush',kWakeup,
'w')
188 redis.call(
'pexpire',kWakeup,1)
206 ( $this->
onRelease === self::AWAKE_ALL ) ? 1 : 0,
209 4 # number of first argument(s) that are keys
211 }
catch ( RedisException $e ) {
212 return Status::newFatal(
'pool-error-unknown', $e->getMessage() );
216 $this->slotTime =
null;
218 unset( self::$active[$this->session] );
295 static $script = <<<LUA
296 local kSlots,kSlotsNextRelease,kSlotWaits = unpack(KEYS)
297 local rMaxWorkers,rMaxQueue,rTimeout,rExpiry,rSess,rTime = unpack(ARGV)
298 -- Initialize
if the
"next release" time sorted-
set is empty. The slot key
299 -- itself is empty
if all slots are busy or when nothing is initialized.
300 -- If the list is empty but the
set is not, then it is the latter
case.
301 -- If the list exists but not the
set, then reset everything.
302 if redis.call(
'exists',kSlotsNextRelease) == 0 then
303 redis.call(
'del',kSlots)
304 for i = 1,1*rMaxWorkers
do
305 redis.call(
'rPush',kSlots,i)
306 redis.call(
'zAdd',kSlotsNextRelease,-1,i)
308 -- Otherwise
do maintenance to clean up after network partitions
310 -- Find stale slot locks and add free them (avoid duplicates)
311 local staleLocks = redis.call(
'zRangeByScore',kSlotsNextRelease,0,rTime)
312 for k,slot in ipairs(staleLocks)
do
313 redis.call(
'lRem',kSlots,0,slot)
314 redis.call(
'rPush',kSlots,slot)
315 redis.call(
'zAdd',kSlotsNextRelease,rTime + 30,slot)
317 -- Find stale wait slot entries and
remove them
318 redis.call(
'zRemRangeByScore',kSlotWaits,0,rTime - 2*rTimeout)
321 -- Try to acquire a slot
if possible now
322 if redis.call(
'lLen',kSlots) > 0 then
323 slot = redis.call(
'lPop',kSlots)
324 -- Update the slot
"next release" time
325 redis.call(
'zAdd',kSlotsNextRelease,rTime + rExpiry,slot)
326 elseif redis.call(
'zCard',kSlotWaits) >= 1*rMaxQueue then
330 -- Register
this process as waiting
331 redis.call(
'zAdd',kSlotWaits,rTime,rSess)
332 redis.call(
'expireAt',kSlotWaits,math.ceil(rTime + 2*rTimeout))
334 -- Always keep renewing the expiry on use
335 redis.call(
'expireAt',kSlots,math.ceil(rTime + rExpiry))
336 redis.call(
'expireAt',kSlotsNextRelease,math.ceil(rTime + rExpiry))
352 3 # number of first argument(s) that are keys
363 static $script = <<<LUA
364 local kSlots,kSlotsNextRelease,kSlotWaits = unpack(KEYS)
365 local rSlot,rExpiry,rSess,rTime = unpack(ARGV)
366 -- If rSlot is
'w' then the client was told to wake up but got no slot
368 -- Update the slot
"next release" time
369 redis.call(
'zAdd',kSlotsNextRelease,rTime + rExpiry,rSlot)
370 -- Always keep renewing the expiry on use
371 redis.call(
'expireAt',kSlots,math.ceil(rTime + rExpiry))
372 redis.call(
'expireAt',kSlotsNextRelease,math.ceil(rTime + rExpiry))
374 -- Unregister
this process as waiting
375 redis.call(
'zRem',kSlotWaits,rSess)
389 3 # number of first argument(s) that are keys