fix: tmr in windows can't re-init issue

This commit is contained in:
dapan 2023-05-19 14:19:29 +08:00
parent a61f0533cf
commit efbc3fc7c6
3 changed files with 40 additions and 10 deletions

View File

@ -716,6 +716,7 @@ int32_t ctgGetDBCfg(SCatalog* pCtg, SRequestConnInfo* pConn, const char* dbFName
int32_t catalogInit(SCatalogCfg* cfg) { int32_t catalogInit(SCatalogCfg* cfg) {
qDebug("catalogInit start");
if (gCtgMgmt.pCluster) { if (gCtgMgmt.pCluster) {
qError("catalog already initialized"); qError("catalog already initialized");
CTG_ERR_RET(TSDB_CODE_CTG_INVALID_INPUT); CTG_ERR_RET(TSDB_CODE_CTG_INVALID_INPUT);

View File

@ -37,6 +37,7 @@
#include "tglobal.h" #include "tglobal.h"
#include "trpc.h" #include "trpc.h"
#include "tvariant.h" #include "tvariant.h"
#include "ttimer.h"
namespace { namespace {
@ -150,6 +151,7 @@ void ctgTestInitLogFile() {
tsAsyncLog = 0; tsAsyncLog = 0;
qDebugFlag = 159; qDebugFlag = 159;
tmrDebugFlag = 159;
strcpy(tsLogDir, TD_LOG_DIR_PATH); strcpy(tsLogDir, TD_LOG_DIR_PATH);
ctgdEnableDebug("api", true); ctgdEnableDebug("api", true);

View File

@ -113,7 +113,7 @@ typedef struct time_wheel_t {
static int32_t tsMaxTmrCtrl = TSDB_MAX_VNODES_PER_DB + 100; static int32_t tsMaxTmrCtrl = TSDB_MAX_VNODES_PER_DB + 100;
static TdThreadOnce tmrModuleInit = PTHREAD_ONCE_INIT; static int32_t tmrModuleInit = 0;
static TdThreadMutex tmrCtrlMutex; static TdThreadMutex tmrCtrlMutex;
static tmr_ctrl_t* tmrCtrls; static tmr_ctrl_t* tmrCtrls;
static tmr_ctrl_t* unusedTmrCtrl = NULL; static tmr_ctrl_t* unusedTmrCtrl = NULL;
@ -512,11 +512,11 @@ bool taosTmrReset(TAOS_TMR_CALLBACK fp, int32_t mseconds, void* param, void* han
return stopped; return stopped;
} }
static void taosTmrModuleInit(void) { static int32_t taosTmrModuleInit(void) {
tmrCtrls = taosMemoryMalloc(sizeof(tmr_ctrl_t) * tsMaxTmrCtrl); tmrCtrls = taosMemoryMalloc(sizeof(tmr_ctrl_t) * tsMaxTmrCtrl);
if (tmrCtrls == NULL) { if (tmrCtrls == NULL) {
tmrError("failed to allocate memory for timer controllers."); tmrError("failed to allocate memory for timer controllers.");
return; return -1;
} }
memset(&timerMap, 0, sizeof(timerMap)); memset(&timerMap, 0, sizeof(timerMap));
@ -535,14 +535,14 @@ static void taosTmrModuleInit(void) {
time_wheel_t* wheel = wheels + i; time_wheel_t* wheel = wheels + i;
if (taosThreadMutexInit(&wheel->mutex, NULL) != 0) { if (taosThreadMutexInit(&wheel->mutex, NULL) != 0) {
tmrError("failed to create the mutex for wheel, reason:%s", strerror(errno)); tmrError("failed to create the mutex for wheel, reason:%s", strerror(errno));
return; return -1;
} }
wheel->nextScanAt = now + wheel->resolution; wheel->nextScanAt = now + wheel->resolution;
wheel->index = 0; wheel->index = 0;
wheel->slots = (tmr_obj_t**)taosMemoryCalloc(wheel->size, sizeof(tmr_obj_t*)); wheel->slots = (tmr_obj_t**)taosMemoryCalloc(wheel->size, sizeof(tmr_obj_t*));
if (wheel->slots == NULL) { if (wheel->slots == NULL) {
tmrError("failed to allocate wheel slots"); tmrError("failed to allocate wheel slots");
return; return -1;
} }
timerMap.size += wheel->size; timerMap.size += wheel->size;
} }
@ -551,20 +551,48 @@ static void taosTmrModuleInit(void) {
timerMap.slots = (timer_list_t*)taosMemoryCalloc(timerMap.size, sizeof(timer_list_t)); timerMap.slots = (timer_list_t*)taosMemoryCalloc(timerMap.size, sizeof(timer_list_t));
if (timerMap.slots == NULL) { if (timerMap.slots == NULL) {
tmrError("failed to allocate hash map"); tmrError("failed to allocate hash map");
return; return -1;
} }
tmrQhandle = taosInitScheduler(10000, taosTmrThreads, "tmr", NULL); tmrQhandle = taosInitScheduler(10000, taosTmrThreads, "tmr", NULL);
taosInitTimer(taosTimerLoopFunc, MSECONDS_PER_TICK); taosInitTimer(taosTimerLoopFunc, MSECONDS_PER_TICK);
tmrDebug("timer module is initialized, number of threads: %d", taosTmrThreads); tmrDebug("timer module is initialized, number of threads: %d", taosTmrThreads);
return 2;
}
static int32_t taosTmrInitModule(void) {
if (atomic_load_32(&tmrModuleInit) == 2) {
return 0;
}
if (atomic_load_32(&tmrModuleInit) < 0) {
return -1;
}
while (true) {
if (0 == atomic_val_compare_exchange_32(&tmrModuleInit, 0, 1)) {
atomic_store_32(&tmrModuleInit, taosTmrModuleInit());
} else if (atomic_load_32(&tmrModuleInit) < 0) {
return -1;
} else if (atomic_load_32(&tmrModuleInit) == 2) {
return 0;
} else {
taosMsleep(1);
}
}
return -1;
} }
void* taosTmrInit(int32_t maxNumOfTmrs, int32_t resolution, int32_t longest, const char* label) { void* taosTmrInit(int32_t maxNumOfTmrs, int32_t resolution, int32_t longest, const char* label) {
const char* ret = taosMonotonicInit(); const char* ret = taosMonotonicInit();
tmrDebug("ttimer monotonic clock source:%s", ret); tmrDebug("ttimer monotonic clock source:%s", ret);
taosThreadOnce(&tmrModuleInit, taosTmrModuleInit); if (taosTmrInitModule() < 0) {
return NULL;
}
taosThreadMutexLock(&tmrCtrlMutex); taosThreadMutexLock(&tmrCtrlMutex);
tmr_ctrl_t* ctrl = unusedTmrCtrl; tmr_ctrl_t* ctrl = unusedTmrCtrl;
@ -581,6 +609,7 @@ void* taosTmrInit(int32_t maxNumOfTmrs, int32_t resolution, int32_t longest, con
} }
tstrncpy(ctrl->label, label, sizeof(ctrl->label)); tstrncpy(ctrl->label, label, sizeof(ctrl->label));
tmrDebug("%s timer controller is initialized, number of timer controllers: %d.", label, numOfTmrCtrl); tmrDebug("%s timer controller is initialized, number of timer controllers: %d.", label, numOfTmrCtrl);
return ctrl; return ctrl;
} }
@ -629,8 +658,6 @@ void taosTmrCleanUp(void* handle) {
tmrCtrls = NULL; tmrCtrls = NULL;
unusedTmrCtrl = NULL; unusedTmrCtrl = NULL;
#if defined(LINUX) atomic_store_32(&tmrModuleInit, 0);
tmrModuleInit = PTHREAD_ONCE_INIT; // to support restart
#endif
} }
} }