MCPcopy Create free account
hub / github.com/Snapchat/KeyDB / expireSlaveKeys

Function expireSlaveKeys

src/expire.cpp:606–663  ·  view source on GitHub ↗

Check the set of keys created by the master with an expire set in order to * check if they should be evicted. */

Source from the content-addressed store, hash-verified

604/* Check the set of keys created by the master with an expire set in order to
605 * check if they should be evicted. */
606void expireSlaveKeys(void) {
607 if (slaveKeysWithExpire == NULL ||
608 dictSize(slaveKeysWithExpire) == 0) return;
609
610 int cycles = 0, noexpire = 0;
611 mstime_t start = mstime();
612 while(1) {
613 dictEntry *de = dictGetRandomKey(slaveKeysWithExpire);
614 sds keyname = (sds)dictGetKey(de);
615 uint64_t dbids = dictGetUnsignedIntegerVal(de);
616 uint64_t new_dbids = 0;
617
618 /* Check the key against every database corresponding to the
619 * bits set in the value bitmap. */
620 int dbid = 0;
621 while(dbids && dbid < cserver.dbnum) {
622 if ((dbids & 1) != 0) {
623 redisDb *db = g_pserver->db[dbid];
624 auto itrDB = db->find(keyname);
625 int expired = 0;
626
627 if (itrDB != db->end() && itrDB->FExpires())
628 {
629 if (itrDB->expire.when() < start) {
630 size_t tried = 0;
631 expired = activeExpireCycleExpire(g_pserver->db[dbid],itrDB.key(),itrDB->expire,start,tried);
632 }
633 }
634
635 /* If the key was not expired in this DB, we need to set the
636 * corresponding bit in the new bitmap we set as value.
637 * At the end of the loop if the bitmap is zero, it means we
638 * no longer need to keep track of this key. */
639 if (itrDB != db->end() && itrDB->FExpires() && !expired) {
640 noexpire++;
641 new_dbids |= (uint64_t)1 << dbid;
642 }
643 }
644 dbid++;
645 dbids >>= 1;
646 }
647
648 /* Set the new bitmap as value of the key, in the dictionary
649 * of keys with an expire set directly in the writable replica. Otherwise
650 * if the bitmap is zero, we no longer need to keep track of it. */
651 if (new_dbids)
652 dictSetUnsignedIntegerVal(de,new_dbids);
653 else
654 dictDelete(slaveKeysWithExpire,keyname);
655
656 /* Stop conditions: found 3 keys we can't expire in a row or
657 * time limit was reached. */
658 cycles++;
659 if (noexpire > 3) break;
660 if ((cycles % 64) == 0 && mstime()-start > 1) break;
661 if (dictSize(slaveKeysWithExpire) == 0) break;
662 }
663}

Callers 1

databasesCronFunction · 0.85

Calls 9

dictGetRandomKeyFunction · 0.85
activeExpireCycleExpireFunction · 0.85
FExpiresMethod · 0.80
mstimeFunction · 0.70
dictDeleteFunction · 0.70
findMethod · 0.45
endMethod · 0.45
whenMethod · 0.45
keyMethod · 0.45

Tested by

no test coverage detected