|
37 | 37 |
|
38 | 38 | #include "server.h" |
39 | 39 |
|
| 40 | + |
| 41 | +/* TODO: comment */ |
| 42 | +#define EPPOOL_SIZE 20 |
| 43 | + |
| 44 | +/* Define expirationPool as an array of robj */ |
| 45 | +typedef robj **expirationPool; |
| 46 | +/* Create an array of expiration pool, one for each database. */ |
| 47 | +static expirationPool *ExpirationPools; |
| 48 | + |
| 49 | +void expirationPoolAlloc(void) { |
| 50 | + int dbnum = server.dbnum; |
| 51 | + |
| 52 | + ExpirationPools = zmalloc(sizeof(expirationPool) * dbnum); |
| 53 | + |
| 54 | + for (int i = 0; i < dbnum; i++) { |
| 55 | + ExpirationPools[i] = zmalloc(sizeof(struct robj*) * EPPOOL_SIZE); |
| 56 | + for (int j = 0; j < EPPOOL_SIZE; j++) { |
| 57 | + ExpirationPools[i][j] = NULL; |
| 58 | + } |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +void expirationPoolPopulate(serverDb *db, robj *val) { |
| 63 | + expirationPool pool = ExpirationPools[db->id]; |
| 64 | + int k; |
| 65 | + |
| 66 | + /* First check if the key already exists in the pool */ |
| 67 | + for (int i = 0; i < EPPOOL_SIZE; i++) { |
| 68 | + if (pool[i] && pool[i] == val) { |
| 69 | + return; /* Key already exists in the pool, no need to insert again */ |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + k = 0; |
| 74 | + while (k < EPPOOL_SIZE && pool[k] && objectGetExpire(pool[k]) > objectGetExpire(val)) k++; |
| 75 | + |
| 76 | + /* If we can't insert (all slots filled with sooner-to-expire entries), return */ |
| 77 | + if (k == 0 && pool[EPPOOL_SIZE - 1] != NULL) { |
| 78 | + return; |
| 79 | + } else if (k < EPPOOL_SIZE && pool[k] == NULL) { |
| 80 | + /* Inserting into empty position. No setup needed before insert. */ |
| 81 | + } else { |
| 82 | + /* Inserting in the middle. Now k points to the first element |
| 83 | + * with expire time greater than the element to insert. */ |
| 84 | + if (pool[EPPOOL_SIZE - 1] == NULL) { |
| 85 | + /* Free space on the right? Insert at k shifting |
| 86 | + * all the elements from k to end to the right. */ |
| 87 | + memmove(pool + k + 1, pool + k, sizeof(pool[0]) * (EPPOOL_SIZE - k - 1)); |
| 88 | + } else { |
| 89 | + /* No free space on right? Insert at k-1 */ |
| 90 | + k--; |
| 91 | + /* Shift all elements on the left of k (included) to the |
| 92 | + * left, so we discard the element with smallest expire time. */ |
| 93 | + if (pool[0]) decrRefCount(pool[0]); |
| 94 | + memmove(pool, pool + 1, sizeof(pool[0]) * k); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + /* Store the entry and increment its refcount */ |
| 99 | + pool[k] = val; |
| 100 | + incrRefCount(val); |
| 101 | + |
| 102 | + for (int i = 0; i < EPPOOL_SIZE; i++) { |
| 103 | + if (pool[i]) |
| 104 | + serverLog(LL_WARNING, "epiration pool index: %d, expiretime: %lld", i, objectGetExpire(pool[i])); |
| 105 | + } |
| 106 | +} |
| 107 | + |
40 | 108 | /*----------------------------------------------------------------------------- |
41 | 109 | * Incremental collection of expired keys. |
42 | 110 | * |
@@ -145,6 +213,9 @@ void expireScanCallback(void *privdata, void *entry) { |
145 | 213 | /* We want the average TTL of keys yet not expired. */ |
146 | 214 | data->ttl_sum += ttl; |
147 | 215 | data->ttl_samples++; |
| 216 | + |
| 217 | + /* Try to add this entry to the expiration pool for future expiration */ |
| 218 | + expirationPoolPopulate(data->db, val); |
148 | 219 | } |
149 | 220 | data->sampled++; |
150 | 221 | } |
@@ -252,6 +323,38 @@ void activeExpireCycle(int type) { |
252 | 323 |
|
253 | 324 | if (kvstoreSize(db->expires)) dbs_performed++; |
254 | 325 |
|
| 326 | + /* First, try to expire keys from the expire pool */ |
| 327 | + robj **pool = ExpirationPools[db->id]; |
| 328 | + long long now = mstime(); |
| 329 | + int expired_from_pool = 0; |
| 330 | + |
| 331 | + /* Traverse from back to front, stop when we find a non-expired key */ |
| 332 | + for (int i = EPPOOL_SIZE - 1; i >= 0; i--) { |
| 333 | + if (pool[i] == NULL) continue; |
| 334 | + if (objectGetExpire(pool[i]) <= now) { |
| 335 | + /* Key is expired, try to expire it */ |
| 336 | + if (activeExpireCycleTryExpire(db, pool[i], now)) { |
| 337 | + expired_from_pool++; |
| 338 | + /* Propagate the DEL command */ |
| 339 | + postExecutionUnitOperations(); |
| 340 | + } |
| 341 | + |
| 342 | + /* Clean up the pool entry */ |
| 343 | + decrRefCount(pool[i]); |
| 344 | + pool[i] = NULL; |
| 345 | + } else { |
| 346 | + /* Since we're traversing from back to front and keys are sorted by expire time, |
| 347 | + * if we find a non-expired key, all keys before it are also not expired */ |
| 348 | + break; |
| 349 | + } |
| 350 | + } |
| 351 | + |
| 352 | + /* If we found expired keys in the pool, update stats */ |
| 353 | + if (expired_from_pool > 0) { |
| 354 | + total_expired += expired_from_pool; |
| 355 | + total_sampled += expired_from_pool; |
| 356 | + } |
| 357 | + |
255 | 358 | /* Continue to expire if at the end of the cycle there are still |
256 | 359 | * a big percentage of keys to expire, compared to the number of keys |
257 | 360 | * we scanned. The percentage, stored in config_cycle_acceptable_stale |
|
0 commit comments