From 69a559164a9011de6b16b560bdd99bb9755dbc0d Mon Sep 17 00:00:00 2001 From: slguan Date: Wed, 22 Apr 2020 10:59:27 +0800 Subject: [PATCH] add dnode offline timer --- src/mnode/inc/mgmtDnode.h | 2 ++ src/mnode/src/mgmtDnode.c | 2 ++ src/mnode/src/mgmtMain.c | 2 +- src/vnode/src/vnodeMain.c | 76 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 81 insertions(+), 1 deletion(-) diff --git a/src/mnode/inc/mgmtDnode.h b/src/mnode/inc/mgmtDnode.h index c0e04aae05..8acd4e9117 100644 --- a/src/mnode/inc/mgmtDnode.h +++ b/src/mnode/inc/mgmtDnode.h @@ -42,6 +42,8 @@ void * mgmtGetDnodeByIp(uint32_t ip); void mgmtUpdateDnode(SDnodeObj *pDnode); int32_t mgmtDropDnode(SDnodeObj *pDnode); +extern int32_t tsAccessSquence; + #ifdef __cplusplus } #endif diff --git a/src/mnode/src/mgmtDnode.c b/src/mnode/src/mgmtDnode.c index 4db7415684..4f959605c0 100644 --- a/src/mnode/src/mgmtDnode.c +++ b/src/mnode/src/mgmtDnode.c @@ -38,6 +38,7 @@ void *tsDnodeSdb = NULL; int32_t tsDnodeUpdateSize = 0; +int32_t tsAccessSquence = 0; extern void * tsVgroupSdb; static int32_t mgmtCreateDnode(uint32_t ip); @@ -323,6 +324,7 @@ void mgmtProcessDnodeStatusMsg(SRpcMsg *rpcMsg) { pDnode->alternativeRole = pStatus->alternativeRole; pDnode->totalVnodes = pStatus->numOfTotalVnodes; pDnode->moduleStatus = pStatus->moduleStatus; + pDnode->lastAccess = tsAccessSquence; if (pStatus->dnodeId == 0) { mTrace("dnode:%d, first access, privateIp:%s, name:%s", pDnode->dnodeId, taosIpStr(pDnode->privateIp), pDnode->dnodeName); diff --git a/src/mnode/src/mgmtMain.c b/src/mnode/src/mgmtMain.c index ac41e9c120..e01b1e7756 100644 --- a/src/mnode/src/mgmtMain.c +++ b/src/mnode/src/mgmtMain.c @@ -149,12 +149,12 @@ void mgmtCleanUpSystem() { mgmtCleanUpShell(); mgmtCleanupDClient(); mgmtCleanupDServer(); + mgmtCleanUpAccts(); mgmtCleanUpTables(); mgmtCleanUpVgroups(); mgmtCleanUpDbs(); mgmtCleanupDnodes(); mgmtCleanUpUsers(); - mgmtCleanUpAccts(); sdbCleanUp(); taosTmrCleanUp(tsMgmtTmr); tsMgmtIsRunning = false; diff --git a/src/vnode/src/vnodeMain.c b/src/vnode/src/vnodeMain.c index 261be8e5f0..5fd337ceca 100644 --- a/src/vnode/src/vnodeMain.c +++ b/src/vnode/src/vnodeMain.c @@ -38,6 +38,8 @@ static void vnodeBuildVloadMsg(char *pNode, void * param); static int vnodeWalCallback(void *arg); static int32_t vnodeSaveCfg(SMDCreateVnodeMsg *pVnodeCfg); static int32_t vnodeReadCfg(SVnodeObj *pVnode); +static int32_t vnodeSaveVersion(SVnodeObj *pVnode); +static int32_t vnodeReadVersion(SVnodeObj *pVnode); static int vnodeWalCallback(void *arg); static uint32_t vnodeGetFileInfo(void *ahandle, char *name, uint32_t *index, int32_t *size); static int vnodeGetWalInfo(void *ahandle, char *name, uint32_t *index); @@ -151,6 +153,8 @@ int32_t vnodeOpen(int32_t vnode, char *rootDir) { return code; } + vnodeReadVersion(pVnode); + pVnode->wqueue = dnodeAllocateWqueue(pVnode); pVnode->rqueue = dnodeAllocateRqueue(pVnode); @@ -303,6 +307,7 @@ static void vnodeCleanUp(SVnodeObj *pVnode) { //syncStop(pVnode->sync); tsdbCloseRepo(pVnode->tsdb); walClose(pVnode->wal); + vnodeSaveVersion(pVnode); vnodeRelease(pVnode); } @@ -562,3 +567,74 @@ PARSE_OVER: fclose(fp); return ret; } + + +static int32_t vnodeSaveVersion(SVnodeObj *pVnode) { + char versionFile[TSDB_FILENAME_LEN + 30] = {0}; + sprintf(versionFile, "%s/vnode%d/version.json", tsVnodeDir, pVnode->vgId); + FILE *fp = fopen(versionFile, "w"); + if (!fp) { + dError("pVnode:%p vgId:%d, failed to open vnode version file for write, error:%s", pVnode, pVnode->vgId); + return errno; + } + + int32_t len = 0; + int32_t maxLen = 30; + char * content = calloc(1, maxLen + 1); + + len += snprintf(content + len, maxLen - len, "{\n"); + len += snprintf(content + len, maxLen - len, " \"version\": %" PRId64 "\n", pVnode->version); + len += snprintf(content + len, maxLen - len, "}\n"); + + fwrite(content, 1, len, fp); + fclose(fp); + free(content); + + dPrint("pVnode:%p vgId:%d, save vnode version successed", pVnode, pVnode->vgId); + + return 0; +} + +static int32_t vnodeReadVersion(SVnodeObj *pVnode) { + char versionFile[TSDB_FILENAME_LEN + 30] = {0}; + sprintf(versionFile, "%s/vnode%d/version.json", tsVnodeDir, pVnode->vgId); + FILE *fp = fopen(versionFile, "w"); + if (!fp) { + dError("pVnode:%p vgId:%d, failed to open vnode version file for write, error:%s", pVnode, pVnode->vgId); + return errno; + } + + int ret = TSDB_CODE_OTHERS; + int maxLen = 100; + char *content = calloc(1, maxLen + 1); + int len = fread(content, 1, maxLen, fp); + if (len <= 0) { + free(content); + fclose(fp); + dError("pVnode:%p vgId:%d, failed to read vnode version, content is null", pVnode, pVnode->vgId); + return false; + } + + cJSON *root = cJSON_Parse(content); + if (root == NULL) { + dError("pVnode:%p vgId:%d, failed to read vnode version, invalid json format", pVnode, pVnode->vgId); + goto PARSE_OVER; + } + + cJSON *version = cJSON_GetObjectItem(root, "version"); + if (!version || version->type != cJSON_Number) { + dError("pVnode:%p vgId:%d, failed to read vnode version, version not found", pVnode, pVnode->vgId); + goto PARSE_OVER; + } + pVnode->version = version->valueint; + + ret = 0; + + dPrint("pVnode:%p vgId:%d, read vnode version successed, version:%%" PRId64, pVnode, pVnode->vgId, pVnode->version); + +PARSE_OVER: + free(content); + cJSON_Delete(root); + fclose(fp); + return ret; +} \ No newline at end of file -- GitLab