From 3f52d74a8ed71350578a7293fc065cbf02bd52f6 Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Thu, 15 Oct 2020 03:23:47 +0000 Subject: [PATCH] TD-1696 --- src/inc/taoserror.h | 1 + src/inc/twal.h | 1 + src/vnode/src/vnodeMain.c | 16 +++++++++++----- src/wal/src/walMain.c | 12 ++++++++---- 4 files changed, 21 insertions(+), 9 deletions(-) diff --git a/src/inc/taoserror.h b/src/inc/taoserror.h index 2600bd2c55..08621a81c6 100644 --- a/src/inc/taoserror.h +++ b/src/inc/taoserror.h @@ -193,6 +193,7 @@ TAOS_DEFINE_ERROR(TSDB_CODE_VND_NO_DISK_PERMISSIONS, 0, 0x0506, "No write p TAOS_DEFINE_ERROR(TSDB_CODE_VND_NO_SUCH_FILE_OR_DIR, 0, 0x0507, "Missing data file") TAOS_DEFINE_ERROR(TSDB_CODE_VND_OUT_OF_MEMORY, 0, 0x0508, "Out of memory") TAOS_DEFINE_ERROR(TSDB_CODE_VND_APP_ERROR, 0, 0x0509, "Unexpected generic error in vnode") +TAOS_DEFINE_ERROR(TSDB_CODE_VND_INVALID_VRESION_FILE, 0, 0x050A, "Invalid version file") TAOS_DEFINE_ERROR(TSDB_CODE_VND_NOT_SYNCED, 0, 0x0511, "Database suspended") TAOS_DEFINE_ERROR(TSDB_CODE_VND_NO_WRITE_AUTH, 0, 0x0512, "Write operation denied") diff --git a/src/inc/twal.h b/src/inc/twal.h index cf570aefdc..92204abd7d 100644 --- a/src/inc/twal.h +++ b/src/inc/twal.h @@ -51,6 +51,7 @@ int walWrite(twalh, SWalHead *); void walFsync(twalh); int walRestore(twalh, void *pVnode, FWalWrite writeFp); int walGetWalFile(twalh, char *name, uint32_t *index); +int64_t walGetVersion(twalh); extern int wDebugFlag; diff --git a/src/vnode/src/vnodeMain.c b/src/vnode/src/vnodeMain.c index d89c383d6a..0c1f9d103b 100644 --- a/src/vnode/src/vnodeMain.c +++ b/src/vnode/src/vnodeMain.c @@ -239,8 +239,10 @@ int32_t vnodeOpen(int32_t vnode, char *rootDir) { code = vnodeReadVersion(pVnode); if (code != TSDB_CODE_SUCCESS) { - vnodeCleanUp(pVnode); - return code; + vError("vgId:%d, failed to read version, generate it from data file", pVnode->vgId); + // Allow vnode start even when read version fails, set version as walVersion or zero + // vnodeCleanUp(pVnode); + // return code; } pVnode->fversion = pVnode->version; @@ -292,6 +294,9 @@ int32_t vnodeOpen(int32_t vnode, char *rootDir) { } walRestore(pVnode->wal, pVnode, vnodeWriteToQueue); + if (pVnode->version == 0) { + pVnode->version = walGetVersion(pVnode->wal); + } SSyncInfo syncInfo; syncInfo.vgId = pVnode->vgId; @@ -947,6 +952,7 @@ static int32_t vnodeSaveVersion(SVnodeObj *pVnode) { len += snprintf(content + len, maxLen - len, "}\n"); fwrite(content, 1, len, fp); + fflush(fp); fclose(fp); vInfo("vgId:%d, save vnode version:%" PRId64 " succeed", pVnode->vgId, pVnode->fversion); @@ -960,7 +966,7 @@ static int32_t vnodeReadVersion(SVnodeObj *pVnode) { cJSON *root = NULL; int maxLen = 100; - terrno = TSDB_CODE_VND_APP_ERROR; + terrno = TSDB_CODE_VND_INVALID_VRESION_FILE; sprintf(versionFile, "%s/vnode%d/version.json", tsVnodeDir, pVnode->vgId); FILE *fp = fopen(versionFile, "r"); if (!fp) { @@ -974,7 +980,7 @@ static int32_t vnodeReadVersion(SVnodeObj *pVnode) { } content = calloc(1, maxLen + 1); - int len = fread(content, 1, maxLen, fp); + int len = fread(content, 1, maxLen, fp); if (len <= 0) { vError("vgId:%d, failed to read vnode version, content is null", pVnode->vgId); goto PARSE_OVER; @@ -999,6 +1005,6 @@ static int32_t vnodeReadVersion(SVnodeObj *pVnode) { PARSE_OVER: taosTFree(content); cJSON_Delete(root); - if(fp) fclose(fp); + if (fp) fclose(fp); return terrno; } diff --git a/src/wal/src/walMain.c b/src/wal/src/walMain.c index 7e0f987213..d7fd1b84c9 100644 --- a/src/wal/src/walMain.c +++ b/src/wal/src/walMain.c @@ -240,10 +240,7 @@ int walWrite(void *handle, SWalHead *pHead) { // no wal if (pWal->level == TAOS_WAL_NOLOG) return 0; - if (pHead->version <= pWal->version) { - wError("wal:%s, failed to write ver:%" PRIu64 ", last ver:%" PRIu64, pWal->name, pHead->version, pWal->version); - return 0; - } + if (pHead->version <= pWal->version) return 0; pHead->signature = walSignature; taosCalcChecksumAppend(0, (uint8_t *)pHead, sizeof(SWalHead)); @@ -560,3 +557,10 @@ static void walProcessFsyncTimer(void *param, void *tmrId) { pWal->timer = NULL; } } + +int64_t walGetVersion(twalh param) { + SWal *pWal = param; + if (pWal == 0) return 0; + + return pWal->version; +} \ No newline at end of file -- GitLab