summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorantirez <antirez@gmail.com>2010-09-15 15:01:37 +0200
committerantirez <antirez@gmail.com>2010-09-22 11:17:06 +0200
commit3306aefc870ccfbd1136a9d4a80cf90e73f692c0 (patch)
tree6e8d5803c13f43632df0f752a9424dda65881315
parent068eb3bf44db191418199f1b9fe89c2b93d20104 (diff)
downloadredis-3306aefc870ccfbd1136a9d4a80cf90e73f692c0.tar.gz
Changed the dict resize policy when BGSAVEs are in progress to a more
dynamic algorithm where an overbooking up to 5 times is tolerated, but after this threshold is reached the resizing is performed even if there are child processes running.
-rw-r--r--dict.c26
-rw-r--r--redis.c1
2 files changed, 21 insertions, 6 deletions
diff --git a/dict.c b/dict.c
index d5010708c..0b799095b 100644
--- a/dict.c
+++ b/dict.c
@@ -49,8 +49,13 @@
/* Using dictEnableResize() / dictDisableResize() we make possible to
* enable/disable resizing of the hash table as needed. This is very important
* for Redis, as we use copy-on-write and don't want to move too much memory
- * around when there is a child performing saving operations. */
+ * around when there is a child performing saving operations.
+ *
+ * Note that even when dict_can_resize is set to 0, not all resizes are
+ * prevented: an hash table is still allowed to grow if the ratio between
+ * the number of elements and the buckets > dict_force_resize_ratio. */
static int dict_can_resize = 1;
+static unsigned int dict_force_resize_ratio = 5;
/* ---------------------------- Utility funcitons --------------------------- */
@@ -522,14 +527,23 @@ dictEntry *dictGetRandomKey(dict *d)
/* Expand the hash table if needed */
static int _dictExpandIfNeeded(dict *d)
{
- /* If the hash table is empty expand it to the intial size,
- * if the table is "full" dobule its size. */
+ /* Incremental rehashing already in progress. Return. */
if (dictIsRehashing(d)) return DICT_OK;
- if (d->ht[0].size == 0)
- return dictExpand(d, DICT_HT_INITIAL_SIZE);
- if (d->ht[0].used >= d->ht[0].size && dict_can_resize)
+
+ /* If the hash table is empty expand it to the intial size. */
+ if (d->ht[0].size == 0) return dictExpand(d, DICT_HT_INITIAL_SIZE);
+
+ /* If we reached the 1:1 ratio, and we are allowed to resize the hash
+ * table (global setting) or we should avoid it but the ratio between
+ * elements/buckets is over the "safe" threshold, we resize doubling
+ * the number of buckets. */
+ if (d->ht[0].used >= d->ht[0].size &&
+ (dict_can_resize ||
+ d->ht[0].used/d->ht[0].size > dict_force_resize_ratio))
+ {
return dictExpand(d, ((d->ht[0].size > d->ht[0].used) ?
d->ht[0].size : d->ht[0].used)*2);
+ }
return DICT_OK;
}
diff --git a/redis.c b/redis.c
index af908ec44..df27661a6 100644
--- a/redis.c
+++ b/redis.c
@@ -8227,6 +8227,7 @@ static void freeMemoryIfNeeded(void) {
}
}
deleteKey(server.db+j,minkey);
+ server.stat_expiredkeys++;
}
}
if (!freed) return; /* nothing to free... */