Skip to content

Commit 8e686c7

Browse files
[2.10] Improve usage of RedisModuleCtx (#5181)
Improve usage of RedisModuleCtx (#5178) use a valid RedisModuleCtx when logging, generate detached context with the module name instead of the default "<module>" (cherry picked from commit a93aa64) Co-authored-by: GuyAv46 <47632673+GuyAv46@users.noreply.github.com>
1 parent b72fc38 commit 8e686c7

6 files changed

Lines changed: 15 additions & 18 deletions

File tree

src/fork_gc.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ static void FGC_sendFixed(ForkGC *fgc, const void *buff, size_t len) {
5656
if (size != len) {
5757
perror("broken pipe, exiting GC fork: write() failed");
5858
// just exit, do not abort(), which will trigger a watchdog on RLEC, causing adverse effects
59-
RedisModule_Log(NULL, "warning", "GC fork: broken pipe, exiting");
59+
RedisModule_Log(fgc->ctx, "warning", "GC fork: broken pipe, exiting");
6060
exit(1);
6161
}
6262
}
@@ -1416,7 +1416,7 @@ ForkGC *FGC_New(StrongRef spec_ref, GCCallbacks *callbacks) {
14161416
forkGc->retryInterval.tv_nsec = 0;
14171417

14181418
forkGc->cleanNumericEmptyNodes = RSGlobalConfig.gcConfigParams.forkGc.forkGCCleanNumericEmptyNodes;
1419-
forkGc->ctx = RedisModule_GetThreadSafeContext(NULL);
1419+
forkGc->ctx = RedisModule_GetDetachedThreadSafeContext(RSDummyContext);
14201420

14211421
callbacks->onTerm = onTerminateCb;
14221422
callbacks->periodicCallback = periodicCb;

src/indexer.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ int Indexer_Add(DocumentIndexer *indexer, RSAddDocumentCtx *aCtx) {
390390
DocumentIndexer *NewIndexer(IndexSpec *spec) {
391391
DocumentIndexer *indexer = rm_calloc(1, sizeof(*indexer));
392392

393-
indexer->redisCtx = RedisModule_GetThreadSafeContext(NULL);
393+
indexer->redisCtx = RedisModule_GetDetachedThreadSafeContext(RSDummyContext);
394394
indexer->specId = spec->uniqueId;
395395
indexer->specKeyName =
396396
RedisModule_CreateStringPrintf(indexer->redisCtx, INDEX_SPEC_KEY_FMT, spec->name);

src/module.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -869,14 +869,12 @@ Version supportedVersion = {
869869
.patchVersion = 0,
870870
};
871871

872-
static void GetRedisVersion() {
873-
RedisModuleCtx *ctx = RedisModule_GetThreadSafeContext(NULL);
872+
static void GetRedisVersion(RedisModuleCtx *ctx) {
874873
RedisModuleCallReply *reply = RedisModule_Call(ctx, "info", "c", "server");
875874
if (!reply) {
876875
// could not get version, it can only happened when running the tests.
877876
// set redis version to supported version.
878877
redisVersion = supportedVersion;
879-
RedisModule_FreeThreadSafeContext(ctx);
880878
return;
881879
}
882880
RedisModule_Assert(RedisModule_CallReplyType(reply) == REDISMODULE_REPLY_STRING);
@@ -897,7 +895,7 @@ static void GetRedisVersion() {
897895
n = sscanf(enterpriseStr, "rlec_version:%d.%d.%d-%d", &rlecVersion.majorVersion,
898896
&rlecVersion.minorVersion, &rlecVersion.buildVersion, &rlecVersion.patchVersion);
899897
if (n != 4) {
900-
RedisModule_Log(NULL, "warning", "Could not extract enterprise version");
898+
RedisModule_Log(ctx, "warning", "Could not extract enterprise version");
901899
}
902900
}
903901

@@ -913,7 +911,6 @@ static void GetRedisVersion() {
913911
RedisModule_FreeCallReply(reply);
914912
}
915913

916-
RedisModule_FreeThreadSafeContext(ctx);
917914
}
918915

919916
void GetFormattedRedisVersion(char *buf, size_t len) {
@@ -958,7 +955,7 @@ int RediSearch_InitModuleInternal(RedisModuleCtx *ctx, RedisModuleString **argv,
958955
return REDISMODULE_ERR;
959956
}
960957

961-
GetRedisVersion();
958+
GetRedisVersion(ctx);
962959

963960
char ver[64];
964961
GetFormattedRedisVersion(ver, sizeof(ver));

src/rules.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ RSLanguage SchemaRule_HashLang(RedisModuleCtx *rctx, const SchemaRule *rule, Red
221221
const char *lang_s = RedisModule_StringPtrLen(lang_rms, &len);
222222
lang = RSLanguage_Find(lang_s, len);
223223
if (lang == RS_LANG_UNSUPPORTED) {
224-
RedisModule_Log(NULL, "warning", "invalid language for key %s", kname);
224+
RedisModule_Log(rctx, "warning", "invalid language for key %s", kname);
225225
lang = rule->lang_default;
226226
}
227227
done:
@@ -254,13 +254,13 @@ RSLanguage SchemaRule_JsonLang(RedisModuleCtx *ctx, const SchemaRule *rule,
254254
size_t len;
255255
RedisJSON langJson = japi->next(jsonIter);
256256
if (!langJson || japi->getString(langJson, &langStr, &len) != REDISMODULE_OK) {
257-
RedisModule_Log(NULL, "warning", "invalid field %s for key %s: not a string", rule->lang_field, kname);
257+
RedisModule_Log(ctx, "warning", "invalid field %s for key %s: not a string", rule->lang_field, kname);
258258
goto done;
259259
}
260260

261261
lang = RSLanguage_Find(langStr, len);
262262
if (lang == RS_LANG_UNSUPPORTED) {
263-
RedisModule_Log(NULL, "warning", "invalid language for key %s", kname);
263+
RedisModule_Log(ctx, "warning", "invalid language for key %s", kname);
264264
lang = rule->lang_default;
265265
goto done;
266266
}
@@ -290,7 +290,7 @@ double SchemaRule_HashScore(RedisModuleCtx *rctx, const SchemaRule *rule, RedisM
290290

291291
rv = RedisModule_StringToDouble(score_rms, &score);
292292
if (rv != REDISMODULE_OK) {
293-
RedisModule_Log(NULL, "warning", "invalid score for key %s", kname);
293+
RedisModule_Log(rctx, "warning", "invalid score for key %s", kname);
294294
score = rule->score_default;
295295
}
296296
done:
@@ -320,7 +320,7 @@ double SchemaRule_JsonScore(RedisModuleCtx *ctx, const SchemaRule *rule,
320320

321321
RedisJSON scoreJson = japi->next(jsonIter);
322322
if (!scoreJson || japi->getDouble(scoreJson, &score) != REDISMODULE_OK) {
323-
RedisModule_Log(NULL, "warning", "invalid field %s for key %s", rule->score_field, kname);
323+
RedisModule_Log(ctx, "warning", "invalid field %s for key %s", rule->score_field, kname);
324324
}
325325

326326
done:

src/spec.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2157,7 +2157,7 @@ static void Indexes_ScanProc(RedisModuleCtx *ctx, RedisModuleString *keyname, Re
21572157
static void Indexes_ScanAndReindexTask(IndexesScanner *scanner) {
21582158
RS_LOG_ASSERT(scanner, "invalid IndexesScanner");
21592159

2160-
RedisModuleCtx *ctx = RedisModule_GetThreadSafeContext(NULL);
2160+
RedisModuleCtx *ctx = RedisModule_GetDetachedThreadSafeContext(RSDummyContext);
21612161
RedisModuleScanCursor *cursor = RedisModule_ScanCursorCreate();
21622162
RedisModule_ThreadSafeContextLock(ctx);
21632163

@@ -2220,7 +2220,7 @@ static void IndexSpec_ScanAndReindexAsync(StrongRef spec_ref) {
22202220
reindexPool = redisearch_thpool_create(1, DEFAULT_HIGH_PRIORITY_BIAS_THRESHOLD, LogCallback, "reindex");
22212221
}
22222222
#ifdef _DEBUG
2223-
RedisModule_Log(NULL, "notice", "Register index %s for async scan", ((IndexSpec*)StrongRef_Get(spec_ref))->name);
2223+
RedisModule_Log(RSDummyContext, "notice", "Register index %s for async scan", ((IndexSpec*)StrongRef_Get(spec_ref))->name);
22242224
#endif
22252225
IndexesScanner *scanner = IndexesScanner_New(spec_ref);
22262226
redisearch_thpool_add_work(reindexPool, (redisearch_thpool_proc)Indexes_ScanAndReindexTask, scanner, THPOOL_PRIORITY_HIGH);
@@ -3012,7 +3012,7 @@ SpecOpIndexingCtx *Indexes_FindMatchingSchemaRules(RedisModuleCtx *ctx, RedisMod
30123012
RLookupKey *k = RLookup_GetKey_LoadEx(&r->lk, UNDERSCORE_KEY, strlen(UNDERSCORE_KEY), UNDERSCORE_KEY, RLOOKUP_F_NOFLAGS);
30133013
RSValue *v = RLookup_GetItem(k, &r->row);
30143014
const char *x = RSValue_StringPtrLen(v, NULL);
3015-
RedisModule_Log(NULL, "notice", "Indexes_FindMatchingSchemaRules: x=%s", x);
3015+
RedisModule_Log(RSDummyContext, "notice", "Indexes_FindMatchingSchemaRules: x=%s", x);
30163016
const char *f = "name";
30173017
k = RLookup_GetKeyEx(&r->lk, f, strlen(f), RLOOKUP_M_READ, RLOOKUP_F_NOFLAGS);
30183018
if (k) {

src/vector_index.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -519,7 +519,7 @@ void VecSim_TieredParams_Init(TieredIndexParams *params, StrongRef sp_ref) {
519519

520520
void VecSimLogCallback(void *ctx, const char *level, const char *message) {
521521
VecSimLogCtx *log_ctx = (VecSimLogCtx *)ctx;
522-
RedisModule_Log(NULL, level, "vector index '%s' - %s", log_ctx->index_field_name, message);
522+
RedisModule_Log(RSDummyContext, level, "vector index '%s' - %s", log_ctx->index_field_name, message);
523523
}
524524

525525
int VecSim_CallTieredIndexesGC(WeakRef spRef) {

0 commit comments

Comments
 (0)