Skip to content

Commit 634ffec

Browse files
committed
fixing rdb load to use a detached context
Signed-off-by: yairgott <[email protected]>
1 parent 2b055c9 commit 634ffec

File tree

1 file changed

+23
-14
lines changed

1 file changed

+23
-14
lines changed

src/schema_manager.cc

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -687,24 +687,38 @@ absl::Status SchemaManager::LoadIndicesFromAux(RedisModuleCtx *ctx,
687687
return absl::OkStatus();
688688
}
689689

690+
absl::StatusOr<vmsdk::UniqueRedisDetachedThreadSafeContext>
691+
CreateRDBDetachedContext(RedisModuleCtx *ctx, RedisModuleIO *rdb) {
692+
int db_num = RedisModule_GetDbIdFromIO(rdb);
693+
auto rdb_load_ctx = vmsdk::MakeUniqueRedisDetachedThreadSafeContext(
694+
RedisModule_GetDetachedThreadSafeContext(ctx));
695+
if (RedisModule_SelectDb(rdb_load_ctx.get(), db_num) != REDISMODULE_OK) {
696+
return absl::InternalError(absl::StrCat("Failed to select DB ", db_num,
697+
" for index schema RDB load"));
698+
}
699+
return rdb_load_ctx;
700+
}
701+
690702
absl::Status SchemaManager::AuxLoad(RedisModuleIO *rdb, int encver, int when) {
691703
if (when == REDISMODULE_AUX_BEFORE_RDB) {
692704
return absl::OkStatus();
693705
}
694-
auto ctx = RedisModule_GetContextFromIO(rdb);
695706

696707
auto aux_index_schema_count = RedisModule_LoadUnsigned(rdb);
697708
if (aux_index_schema_count > 0) {
698709
// Note that we need to subscribe now, so that we can get the loading
699710
// ended callback.
700711
SubscribeToServerEventsIfNeeded();
701712
}
713+
VMSDK_ASSIGN_OR_RETURN(
714+
auto rdb_load_ctx,
715+
CreateRDBDetachedContext(RedisModule_GetContextFromIO(rdb), rdb));
702716
if (staging_indices_due_to_repl_load_.Get()) {
703-
VMSDK_RETURN_IF_ERROR(
704-
StageIndicesFromAux(ctx, aux_index_schema_count, rdb, encver));
717+
VMSDK_RETURN_IF_ERROR(StageIndicesFromAux(
718+
rdb_load_ctx.get(), aux_index_schema_count, rdb, encver));
705719
} else {
706-
VMSDK_RETURN_IF_ERROR(
707-
LoadIndicesFromAux(ctx, aux_index_schema_count, rdb, encver));
720+
VMSDK_RETURN_IF_ERROR(LoadIndicesFromAux(
721+
rdb_load_ctx.get(), aux_index_schema_count, rdb, encver));
708722
}
709723

710724
return absl::OkStatus();
@@ -770,23 +784,18 @@ int SchemaManager::OnAuxLoadCallback(RedisModuleIO *rdb, int encver, int when) {
770784
absl::StatusOr<void *> SchemaManager::IndexSchemaRDBLoad(RedisModuleIO *rdb,
771785
int encoding_version) {
772786
// Make sure we create the index schema in the right DB.
773-
int db_num = RedisModule_GetDbIdFromIO(rdb);
774-
RedisModuleCtx *rdb_load_ctx =
775-
RedisModule_GetDetachedThreadSafeContext(detached_ctx_.get());
776-
if (RedisModule_SelectDb(rdb_load_ctx, db_num) != REDISMODULE_OK) {
777-
return absl::InternalError(absl::StrCat("Failed to select DB ", db_num,
778-
" for index schema RDB load"));
779-
}
787+
VMSDK_ASSIGN_OR_RETURN(auto rdb_load_ctx,
788+
CreateRDBDetachedContext(detached_ctx_.get(), rdb));
780789

781790
IndexSchema *index_schema_ptr = nullptr;
782791
if (staging_indices_due_to_repl_load_.Get()) {
783792
VMSDK_ASSIGN_OR_RETURN(
784793
index_schema_ptr,
785-
StageIndexFromRDB(rdb_load_ctx, rdb, encoding_version));
794+
StageIndexFromRDB(rdb_load_ctx.get(), rdb, encoding_version));
786795
} else {
787796
VMSDK_ASSIGN_OR_RETURN(
788797
index_schema_ptr,
789-
LoadIndexFromRDB(rdb_load_ctx, rdb, encoding_version));
798+
LoadIndexFromRDB(rdb_load_ctx.get(), rdb, encoding_version));
790799
}
791800

792801
// Note that we need to subscribe now, so that we can get the loading ended

0 commit comments

Comments
 (0)