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

Method activeExpireCycleCore

src/expire.cpp:316–560  ·  view source on GitHub ↗

static*/

Source from the content-addressed store, hash-verified

314#define ACTIVE_EXPIRE_CYCLE_ACCEPTABLE_STALE 10 /* % of stale keys after which
315 we do extra efforts. */
316/*static*/ void redisDbPersistentData::activeExpireCycleCore(int type) {
317 /* Adjust the running parameters according to the configured expire
318 * effort. The default effort is 1, and the maximum configurable effort
319 * is 10. */
320 unsigned long
321 effort = g_pserver->active_expire_effort-1, /* Rescale from 0 to 9. */
322 config_keys_per_loop = ACTIVE_EXPIRE_CYCLE_KEYS_PER_LOOP +
323 ACTIVE_EXPIRE_CYCLE_KEYS_PER_LOOP/4*effort,
324 config_cycle_fast_duration = ACTIVE_EXPIRE_CYCLE_FAST_DURATION +
325 ACTIVE_EXPIRE_CYCLE_FAST_DURATION/4*effort,
326 config_cycle_slow_time_perc = ACTIVE_EXPIRE_CYCLE_SLOW_TIME_PERC +
327 2*effort,
328 config_cycle_acceptable_stale = ACTIVE_EXPIRE_CYCLE_ACCEPTABLE_STALE-
329 effort;
330
331 /* This function has some global state in order to continue the work
332 * incrementally across calls. */
333 static unsigned int current_db = 0; /* Next DB to test. */
334 static int timelimit_exit = 0; /* Time limit hit in previous call? */
335 static long long last_fast_cycle = 0; /* When last fast cycle ran. */
336
337 int j, iteration = 0;
338 int dbs_per_call = CRON_DBS_PER_CALL;
339 long long start = ustime(), timelimit, elapsed;
340
341 /* When clients are paused the dataset should be static not just from the
342 * POV of clients not being able to write, but also from the POV of
343 * expires and evictions of keys not being performed. */
344 if (checkClientPauseTimeoutAndReturnIfPaused()) return;
345
346 if (type == ACTIVE_EXPIRE_CYCLE_FAST) {
347 /* Don't start a fast cycle if the previous cycle did not exit
348 * for time limit, unless the percentage of estimated stale keys is
349 * too high. Also never repeat a fast cycle for the same period
350 * as the fast cycle total duration itself. */
351 if (!timelimit_exit &&
352 g_pserver->stat_expired_stale_perc < config_cycle_acceptable_stale)
353 return;
354
355 if (start < last_fast_cycle + (long long)config_cycle_fast_duration*2)
356 return;
357
358 last_fast_cycle = start;
359 }
360
361 /* We usually should test CRON_DBS_PER_CALL per iteration, with
362 * two exceptions:
363 *
364 * 1) Don't test more DBs than we have.
365 * 2) If last time we hit the time limit, we want to scan all DBs
366 * in this iteration, as there is work to do in some DB and we don't want
367 * expired keys to use memory for too much time. */
368 if (dbs_per_call > cserver.dbnum || timelimit_exit)
369 dbs_per_call = cserver.dbnum;
370
371 /* We can use at max 'config_cycle_slow_time_perc' percentage of CPU
372 * time per iteration. Since this function gets called with a frequency of
373 * server.hz times per second, the following is the max amount of

Callers

nothing calls this directly

Calls 15

activeExpireCycleExpireFunction · 0.85
szFromObjFunction · 0.85
decrRefCountFunction · 0.85
expireSizeMethod · 0.80
FExpiresMethod · 0.80
getStorageCacheMethod · 0.80
ustimeFunction · 0.70
mstimeFunction · 0.70
createStringObjectFunction · 0.70
whenMethod · 0.45

Tested by

no test coverage detected