Skip to content

Commit

Permalink
Support mem_usage and free_effort for scan mode (#45)
Browse files Browse the repository at this point in the history
* Support mem_usage and free_effort for scan mode
  • Loading branch information
chenyang8094 authored Feb 15, 2023
1 parent 361fd4f commit dcda1dd
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 17 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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: |
Expand Down
80 changes: 64 additions & 16 deletions src/tairhash.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
};

Expand Down
19 changes: 19 additions & 0 deletions tests/tairhash.tcl
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit dcda1dd

Please sign in to comment.