20 use Psr\Log\LoggerInterface;
85 parent::__construct( $conf,
$type,
$key );
87 $this->serversByLabel = $conf[
'servers'];
89 $serverLabels = array_keys( $conf[
'servers'] );
90 $this->ring =
new HashRing( array_fill_keys( $serverLabels, 10 ) );
92 $conf[
'redisConfig'][
'serializer'] =
'none';
96 $this->keySha1 = sha1( $this->key );
97 $met = ini_get(
'max_execution_time' );
98 $this->lockTTL = $met ? 2 * $met : 3600;
100 if ( self::$active ===
null ) {
102 register_shutdown_function( [ __CLASS__,
'releaseAll' ] );
110 if ( !isset( $this->conn ) ) {
112 $servers = $this->ring->getLocations( $this->key, 3 );
114 foreach ( $servers as $server ) {
115 $conn = $this->pool->getConnection( $this->serversByLabel[$server], $this->logger );
147 if ( $this->slot ===
null ) {
157 '@phan-var RedisConnRef $conn';
163 local kSlots,kSlotsNextRelease,kWakeup,kWaiting = unpack(KEYS)
164 local rMaxWorkers,rExpiry,rSlot,rSlotTime,rAwakeAll,rTime = unpack(ARGV)
165 -- Add the slots back to the list (
if rSlot is
"w" then it is not a slot).
166 -- Treat the list as expired
if the
"next release" time sorted-
set is missing.
167 if rSlot ~=
'w' and redis.call(
'exists',kSlotsNextRelease) == 1 then
168 if 1*redis.call(
'zScore',kSlotsNextRelease,rSlot) ~= (rSlotTime + rExpiry) then
169 -- Slot lock expired and was released already
170 elseif redis.call(
'lLen',kSlots) >= 1*rMaxWorkers then
171 -- Slots somehow got out of sync; reset the list
for sanity
172 redis.call(
'del',kSlots,kSlotsNextRelease)
173 elseif redis.call(
'lLen',kSlots) == (1*rMaxWorkers - 1) and redis.call(
'zCard',kWaiting) == 0 then
174 -- Slot list will be made full; clear it to save space (it re-inits as needed)
175 -- since nothing is waiting on being unblocked by a push to the list
176 redis.call(
'del',kSlots,kSlotsNextRelease)
178 -- Add slot back to pool and update the
"next release" time
179 redis.call(
'rPush',kSlots,rSlot)
180 redis.call(
'zAdd',kSlotsNextRelease,rTime + 30,rSlot)
181 -- Always keep renewing the expiry on use
182 redis.call(
'expireAt',kSlots,math.ceil(rTime + rExpiry))
183 redis.call(
'expireAt',kSlotsNextRelease,math.ceil(rTime + rExpiry))
186 --
Update an ephemeral list to wake up other clients that can
187 -- reuse any cached work from
this process. Only
do this if no
188 -- slots are currently free (e.g. clients could be waiting).
189 if 1*rAwakeAll == 1 then
190 local count = redis.call(
'zCard',kWaiting)
192 redis.call(
'rPush',kWakeup,
'w')
194 redis.call(
'pexpire',kWakeup,1)
211 ( $this->
onRelease === self::AWAKE_ALL ) ? 1 : 0,
214 4 # number of first argument(s) that are keys
216 }
catch ( RedisException $e ) {
221 $this->slotTime =
null;
223 unset( self::$active[$this->session] );
235 if ( $this->slot !==
null ) {
245 '@phan-var RedisConnRef $conn';
247 $now = microtime(
true );
250 if ( ctype_digit(
$slot ) ) {
253 } elseif (
$slot ===
'QUEUE_FULL' ) {
256 } elseif (
$slot ===
'QUEUE_WAIT' ) {
275 return Status::newFatal(
'pool-error-unknown',
"Server gave slot '$slot'." );
277 }
catch ( RedisException $e ) {
281 if (
$slot !==
'w' ) {
302 local kSlots,kSlotsNextRelease,kSlotWaits = unpack(KEYS)
303 local rMaxWorkers,rMaxQueue,rTimeout,rExpiry,rSess,rTime = unpack(ARGV)
304 -- Initialize
if the
"next release" time sorted-
set is empty. The slot key
305 -- itself is empty
if all slots are busy or when nothing is initialized.
306 -- If the list is empty but the
set is not, then it is the latter
case.
307 -- For sanity,
if the list exists but not the
set, then reset everything.
308 if redis.call(
'exists',kSlotsNextRelease) == 0 then
309 redis.call(
'del',kSlots)
310 for i = 1,1*rMaxWorkers
do
311 redis.call(
'rPush',kSlots,i)
312 redis.call(
'zAdd',kSlotsNextRelease,-1,i)
314 -- Otherwise
do maintenance to clean up after network partitions
316 -- Find stale slot locks and add free them (avoid duplicates
for sanity)
317 local staleLocks = redis.call(
'zRangeByScore',kSlotsNextRelease,0,rTime)
318 for k,slot in ipairs(staleLocks)
do
319 redis.call(
'lRem',kSlots,0,slot)
320 redis.call(
'rPush',kSlots,slot)
321 redis.call(
'zAdd',kSlotsNextRelease,rTime + 30,slot)
323 -- Find stale wait slot entries and
remove them
324 redis.call(
'zRemRangeByScore',kSlotWaits,0,rTime - 2*rTimeout)
327 -- Try to acquire a slot
if possible now
328 if redis.call(
'lLen',kSlots) > 0 then
329 slot = redis.call(
'lPop',kSlots)
330 --
Update the slot
"next release" time
331 redis.call(
'zAdd',kSlotsNextRelease,rTime + rExpiry,slot)
332 elseif redis.call(
'zCard',kSlotWaits) >= 1*rMaxQueue then
336 -- Register
this process as waiting
337 redis.call(
'zAdd',kSlotWaits,rTime,rSess)
338 redis.call(
'expireAt',kSlotWaits,math.ceil(rTime + 2*rTimeout))
340 -- Always keep renewing the expiry on use
341 redis.call(
'expireAt',kSlots,math.ceil(rTime + rExpiry))
342 redis.call(
'expireAt',kSlotsNextRelease,math.ceil(rTime + rExpiry))
357 3 # number of first argument(s) that are keys
371 local kSlots,kSlotsNextRelease,kSlotWaits = unpack(KEYS)
372 local rSlot,rExpiry,rSess,rTime = unpack(ARGV)
373 -- If rSlot is
'w' then the client was told to wake up but got no slot
375 --
Update the slot
"next release" time
376 redis.call(
'zAdd',kSlotsNextRelease,rTime + rExpiry,rSlot)
377 -- Always keep renewing the expiry on use
378 redis.call(
'expireAt',kSlots,math.ceil(rTime + rExpiry))
379 redis.call(
'expireAt',kSlotsNextRelease,math.ceil(rTime + rExpiry))
381 -- Unregister
this process as waiting
382 redis.call(
'zRem',kSlotWaits,rSess)
395 3 # number of first argument(s) that are keys
403 return "poolcounter:l-slots-{$this->keySha1}-{$this->workers}";
410 return "poolcounter:z-renewtime-{$this->keySha1}-{$this->workers}";
417 return "poolcounter:z-wait-{$this->keySha1}-{$this->workers}";
424 return "poolcounter:l-wakeup-{$this->keySha1}-{$this->workers}";
431 foreach ( self::$active as $poolCounter ) {
433 if ( $poolCounter->slot !==
null ) {
434 $poolCounter->release();
436 }
catch ( Exception $e ) {