Skip to content

Commit 6155198

Browse files
committed
Unify RedisModuleDefragFunc and RedisModuleTypeDefragFunc
1 parent 467ddbb commit 6155198

File tree

3 files changed

+18
-13
lines changed

3 files changed

+18
-13
lines changed

src/defrag.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1225,8 +1225,7 @@ static doneStatus defragModuleGlobals(monotime endtime, void *ctx) {
12251225
defrag_module_ctx->module_ctx.cursor = &defrag_module_ctx->cursor;
12261226

12271227
/* Call the module's defrag callback function and check if more work remains. */
1228-
defrag_module_ctx->module->defrag_cb(&defrag_module_ctx->module_ctx);
1229-
if (defrag_module_ctx->cursor != 0)
1228+
if (defrag_module_ctx->module->defrag_cb(&defrag_module_ctx->module_ctx) != 0)
12301229
return DEFRAG_NOT_DONE;
12311230

12321231
return DEFRAG_DONE;

src/redismodule.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -839,7 +839,8 @@ typedef struct RedisModuleDefragCtx RedisModuleDefragCtx;
839839
/* Function pointers needed by both the core and modules, these needs to be
840840
* exposed since you can't cast a function pointer to (void *). */
841841
typedef void (*RedisModuleInfoFunc)(RedisModuleInfoCtx *ctx, int for_crash_report);
842-
typedef void (*RedisModuleDefragFunc)(RedisModuleDefragCtx *ctx);
842+
typedef int (*RedisModuleDefragFunc)(RedisModuleDefragCtx *ctx);
843+
typedef void (*RedisModuleDefragEventFunc)(RedisModuleDefragCtx *ctx);
843844
typedef void (*RedisModuleUserChangedFunc) (uint64_t client_id, void *privdata);
844845

845846
/* ------------------------- End of common defines ------------------------ */
@@ -1305,7 +1306,7 @@ REDISMODULE_API int *(*RedisModule_GetCommandKeys)(RedisModuleCtx *ctx, RedisMod
13051306
REDISMODULE_API int *(*RedisModule_GetCommandKeysWithFlags)(RedisModuleCtx *ctx, RedisModuleString **argv, int argc, int *num_keys, int **out_flags) REDISMODULE_ATTR;
13061307
REDISMODULE_API const char *(*RedisModule_GetCurrentCommandName)(RedisModuleCtx *ctx) REDISMODULE_ATTR;
13071308
REDISMODULE_API int (*RedisModule_RegisterDefragFunc)(RedisModuleCtx *ctx, RedisModuleDefragFunc func) REDISMODULE_ATTR;
1308-
REDISMODULE_API int (*RedisModule_RegisterDefragCallbacks)(RedisModuleCtx *ctx, RedisModuleDefragFunc start, RedisModuleDefragFunc end) REDISMODULE_ATTR;
1309+
REDISMODULE_API int (*RedisModule_RegisterDefragCallbacks)(RedisModuleCtx *ctx, RedisModuleDefragEventFunc start, RedisModuleDefragEventFunc end) REDISMODULE_ATTR;
13091310
REDISMODULE_API void *(*RedisModule_DefragAlloc)(RedisModuleDefragCtx *ctx, void *ptr) REDISMODULE_ATTR;
13101311
REDISMODULE_API void *(*RedisModule_DefragAllocRaw)(RedisModuleDefragCtx *ctx, size_t size) REDISMODULE_ATTR;
13111312
REDISMODULE_API void (*RedisModule_DefragFreeRaw)(RedisModuleDefragCtx *ctx, void *ptr) REDISMODULE_ATTR;

tests/modules/defragtest.c

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,21 +39,26 @@ static void createGlobalStrings(RedisModuleCtx *ctx, unsigned long count)
3939
}
4040
}
4141

42-
static void defragGlobalStrings(RedisModuleDefragCtx *ctx)
42+
static int defragGlobalStrings(RedisModuleDefragCtx *ctx)
4343
{
4444
unsigned long cursor = 0;
4545
RedisModule_DefragCursorGet(ctx, &cursor);
4646

4747
RedisModule_Assert(cursor < global_strings_len);
48-
RedisModuleString *new = RedisModule_DefragRedisModuleString(ctx, global_strings[cursor]);
49-
global_attempts++;
50-
if (new != NULL) {
51-
global_strings[cursor] = new;
52-
global_defragged++;
53-
}
48+
for (; cursor < global_strings_len; cursor++) {
49+
RedisModuleString *new = RedisModule_DefragRedisModuleString(ctx, global_strings[cursor]);
50+
global_attempts++;
51+
if (new != NULL) {
52+
global_strings[cursor] = new;
53+
global_defragged++;
54+
}
5455

55-
cursor++;
56-
RedisModule_DefragCursorSet(ctx, cursor == global_strings_len ? 0 : cursor);
56+
if (cursor % 16 == 0 && RedisModule_DefragShouldStop(ctx)) {
57+
RedisModule_DefragCursorSet(ctx, cursor);
58+
return 1;
59+
}
60+
}
61+
return 0;
5762
}
5863

5964
static void defragStart(RedisModuleDefragCtx *ctx) {

0 commit comments

Comments
 (0)