Use a smoother running average for avg_ttl in INFO.

Reported here:
https://www.reddit.com/r/redis/comments/42r0i0/avg_ttl_varies_a_lot/
This commit is contained in:
antirez 2016-01-26 15:28:22 +01:00
parent fe44a7cb60
commit 5bc7e019e1

View File

@ -864,18 +864,22 @@ void activeExpireCycle(int type) {
if ((de = dictGetRandomKey(db->expires)) == NULL) break; if ((de = dictGetRandomKey(db->expires)) == NULL) break;
ttl = dictGetSignedIntegerVal(de)-now; ttl = dictGetSignedIntegerVal(de)-now;
if (activeExpireCycleTryExpire(db,de,now)) expired++; if (activeExpireCycleTryExpire(db,de,now)) expired++;
if (ttl < 0) ttl = 0; if (ttl > 0) {
ttl_sum += ttl; /* We want the average TTL of keys yet not expired. */
ttl_samples++; ttl_sum += ttl;
ttl_samples++;
}
} }
/* Update the average TTL stats for this database. */ /* Update the average TTL stats for this database. */
if (ttl_samples) { if (ttl_samples) {
long long avg_ttl = ttl_sum/ttl_samples; long long avg_ttl = ttl_sum/ttl_samples;
/* Do a simple running average with a few samples.
* We just use the current estimate with a weight of 2%
* and the previous estimate with a weight of 98%. */
if (db->avg_ttl == 0) db->avg_ttl = avg_ttl; if (db->avg_ttl == 0) db->avg_ttl = avg_ttl;
/* Smooth the value averaging with the previous one. */ db->avg_ttl = (db->avg_ttl/50)*49 + (avg_ttl/50);
db->avg_ttl = (db->avg_ttl+avg_ttl)/2;
} }
/* We can't block forever here even if there are many keys to /* We can't block forever here even if there are many keys to