From dcda1dd770d835aa2d58156423501a3fb0f828f3 Mon Sep 17 00:00:00 2001 From: chenyang8094 Date: Wed, 15 Feb 2023 18:24:35 +0800 Subject: [PATCH] Support mem_usage and free_effort for scan mode (#45) * Support mem_usage and free_effort for scan mode --- .github/workflows/ci.yml | 2 +- src/tairhash.c | 80 ++++++++++++++++++++++++++++++++-------- tests/tairhash.tcl | 19 ++++++++++ 3 files changed, 84 insertions(+), 17 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 5986e40..ec72ce5 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -40,7 +40,7 @@ jobs: sudo apt-get install git git clone https://github.com/redis/redis cd redis - git checkout 6.0 + git checkout 6.2 make REDIS_CFLAGS='-Werror' BUILD_TLS=yes - name: make tairhash run: | diff --git a/src/tairhash.c b/src/tairhash.c index 33de35c..bd97c84 100755 --- a/src/tairhash.c +++ b/src/tairhash.c @@ -2664,22 +2664,22 @@ int TairHashTypeActiveExpireInfo_RedisCommand(RedisModuleCtx *ctx, RedisModuleSt ctx, "\r\n" "# Active expire statistics\r\n" - "enable_active_expire:%d\r\n" - "tair_hash_active_expire_period:%lld\r\n" - "tair_hash_active_expire_keys_per_loop:%lld\r\n" - "tair_hash_active_expire_dbs_per_loop:%lld\r\n" - "tair_hash_active_expire_last_time_msec:%lld\r\n" - "tair_hash_active_expire_max_time_msec:%lld\r\n" - "tair_hash_active_expire_avg_time_msec:%lld\r\n" - "tair_hash_passive_expire_keys_per_loop:%lld\r\n", - g_expire_algorithm.enable_active_expire, - g_expire_algorithm.active_expire_period, - g_expire_algorithm.keys_per_active_loop, - g_expire_algorithm.dbs_per_active_loop, - g_expire_algorithm.stat_last_active_expire_time_msec, - g_expire_algorithm.stat_max_active_expire_time_msec, - g_expire_algorithm.stat_avg_active_expire_time_msec, - g_expire_algorithm.keys_per_passive_loop); + "enable_active_expire:%ld\r\n" + "tair_hash_active_expire_period:%ld\r\n" + "tair_hash_active_expire_keys_per_loop:%ld\r\n" + "tair_hash_active_expire_dbs_per_loop:%ld\r\n" + "tair_hash_active_expire_last_time_msec:%ld\r\n" + "tair_hash_active_expire_max_time_msec:%ld\r\n" + "tair_hash_active_expire_avg_time_msec:%ld\r\n" + "tair_hash_passive_expire_keys_per_loop:%ld\r\n", + (long)g_expire_algorithm.enable_active_expire, + (long)g_expire_algorithm.active_expire_period, + (long)g_expire_algorithm.keys_per_active_loop, + (long)g_expire_algorithm.dbs_per_active_loop, + (long)g_expire_algorithm.stat_last_active_expire_time_msec, + (long) g_expire_algorithm.stat_max_active_expire_time_msec, + (long)g_expire_algorithm.stat_avg_active_expire_time_msec, + (long)g_expire_algorithm.keys_per_passive_loop); size_t a_len, d_len, t_size = 0; const char *a_buf = RedisModule_StringPtrLen(info_a, &a_len); @@ -2892,6 +2892,51 @@ size_t TairHashTypeEffort2(RedisModuleKeyOptCtx *ctx, const void *value) { tairHashObj *o = (tairHashObj *)value; return dictSize(o->hash) + o->expire_index->length; } +#else + +size_t TairHashTypeMemUsage(const void *value) { + tairHashObj *o = (tairHashObj *)value; + + uint64_t size = 0; + RedisModuleString *skey; + size_t skeylen = 0; + + if (!o) { + return size; + } + + m_dictIterator *di; + m_dictEntry *de; + + if (o->hash) { + size += sizeof(*o); + + di = m_dictGetIterator(o->hash); + while ((de = m_dictNext(di)) != NULL) { + TairHashVal *val = (TairHashVal *)dictGetVal(de); + skey = dictGetKey(de); + size += sizeof(*val); + RedisModule_StringPtrLen(skey, &skeylen); + size += skeylen; + size_t len; + RedisModule_StringPtrLen(val->value, &len); + size += len; + } + m_dictReleaseIterator(di); + } + + if (o->expire_index) { + size += o->expire_index->length * sizeof(m_zskiplistNode); + } + + return size; +} + +size_t TairHashTypeEffort(RedisModuleString *key, const void *value) { + REDISMODULE_NOT_USED(key); + tairHashObj *o = (tairHashObj *)value; + return dictSize(o->hash) + o->expire_index->length; +} #endif @@ -3061,6 +3106,9 @@ int __attribute__((visibility("default"))) RedisModule_OnLoad(RedisModuleCtx *ct .copy2 = TairHashTypeCopy2, .free_effort2 = TairHashTypeEffort2, .mem_usage2 = TairHashTypeMemUsage2, +#else + .mem_usage = TairHashTypeMemUsage, + .free_effort = TairHashTypeEffort, #endif }; diff --git a/tests/tairhash.tcl b/tests/tairhash.tcl index 7627247..6832fe9 100755 --- a/tests/tairhash.tcl +++ b/tests/tairhash.tcl @@ -1382,6 +1382,25 @@ start_server {tags {"tairhash"} overrides {bind 0.0.0.0}} { assert_equal $res "tairhash-" } + test {tairhash memusage} { + r del tairhashkey + + assert_equal 1 [r exhset tairhashkey field1 val1] + + set usage1 [r memory usage tairhashkey] + + assert_equal 1 [r exhset tairhashkey field2 val2] + + set usage2 [r memory usage tairhashkey] + + assert_equal 1 [r exhset tairhashkey field3 val3] + + set usage3 [r memory usage tairhashkey] + + assert {$usage2 > $usage1} + assert {$usage3 > $usage2} + } + test {tairhash get when last field expired} { r del tairhashkey