From 3e57707227d76690f4a889b0c355a28f4b386641 Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Tue, 22 Sep 2020 17:11:24 +0800 Subject: [PATCH 1/2] TD-1388 --- src/client/src/tscSQLParser.c | 2 +- src/inc/twal.h | 1 + src/mnode/src/mnodeDb.c | 8 ++-- src/vnode/src/vnodeMain.c | 6 +++ src/wal/src/walMain.c | 50 ++++++++++++++++++++++-- tests/script/general/db/alter_option.sim | 15 ++++--- 6 files changed, 69 insertions(+), 13 deletions(-) diff --git a/src/client/src/tscSQLParser.c b/src/client/src/tscSQLParser.c index a6efffe436..f19381fb29 100644 --- a/src/client/src/tscSQLParser.c +++ b/src/client/src/tscSQLParser.c @@ -5722,7 +5722,7 @@ int32_t tscCheckCreateDbParams(SSqlCmd* pCmd, SCMCreateDbMsg* pCreate) { char msg[512] = {0}; if (pCreate->walLevel != -1 && (pCreate->walLevel < TSDB_MIN_WAL_LEVEL || pCreate->walLevel > TSDB_MAX_WAL_LEVEL)) { - snprintf(msg, tListLen(msg), "invalid db option walLevel: %d, only 0-2 allowed", pCreate->walLevel); + snprintf(msg, tListLen(msg), "invalid db option walLevel: %d, only 1-2 allowed", pCreate->walLevel); return invalidSqlErrMsg(tscGetErrorMsgPayload(pCmd), msg); } diff --git a/src/inc/twal.h b/src/inc/twal.h index 4fdb7aa275..cf570aefdc 100644 --- a/src/inc/twal.h +++ b/src/inc/twal.h @@ -44,6 +44,7 @@ typedef void* twalh; // WAL HANDLE typedef int (*FWalWrite)(void *ahandle, void *pHead, int type); twalh walOpen(const char *path, const SWalCfg *pCfg); +int walAlter(twalh pWal, const SWalCfg *pCfg); void walClose(twalh); int walRenew(twalh); int walWrite(twalh, SWalHead *); diff --git a/src/mnode/src/mnodeDb.c b/src/mnode/src/mnodeDb.c index 54c049d242..8558350950 100644 --- a/src/mnode/src/mnodeDb.c +++ b/src/mnode/src/mnodeDb.c @@ -910,13 +910,13 @@ static SDbCfg mnodeGetAlterDbOption(SDbObj *pDb, SCMAlterDbMsg *pAlter) { } if (walLevel > 0 && walLevel != pDb->cfg.walLevel) { - mError("db:%s, can't alter walLevel option", pDb->name); - terrno = TSDB_CODE_MND_INVALID_DB_OPTION; + mDebug("db:%s, walLevel:%d change to %d", pDb->name, pDb->cfg.walLevel, walLevel); + newCfg.walLevel = walLevel; } if (fsyncPeriod >= 0 && fsyncPeriod != pDb->cfg.fsyncPeriod) { - mError("db:%s, can't alter fsyncPeriod option", pDb->name); - terrno = TSDB_CODE_MND_INVALID_DB_OPTION; + mDebug("db:%s, fsyncPeriod:%d change to %d", pDb->name, pDb->cfg.fsyncPeriod, fsyncPeriod); + newCfg.fsyncPeriod = fsyncPeriod; } if (replications > 0 && replications != pDb->cfg.replications) { diff --git a/src/vnode/src/vnodeMain.c b/src/vnode/src/vnodeMain.c index a4e88fb946..353dbbf373 100644 --- a/src/vnode/src/vnodeMain.c +++ b/src/vnode/src/vnodeMain.c @@ -186,6 +186,12 @@ int32_t vnodeAlter(void *param, SMDCreateVnodeMsg *pVnodeCfg) { return code; } + code = walAlter(pVnode->wal, &pVnode->walCfg); + if (code != TSDB_CODE_SUCCESS) { + pVnode->status = TAOS_VN_STATUS_READY; + return code; + } + code = syncReconfig(pVnode->sync, &pVnode->syncCfg); if (code != TSDB_CODE_SUCCESS) { pVnode->status = TAOS_VN_STATUS_READY; diff --git a/src/wal/src/walMain.c b/src/wal/src/walMain.c index bebad69f32..a90c1b171c 100644 --- a/src/wal/src/walMain.c +++ b/src/wal/src/walMain.c @@ -69,6 +69,13 @@ static void walModuleInitFunc() { wDebug("WAL module is initialized"); } +static inline bool walNeedFsyncTimer(SWal *pWal) { + if (pWal->fsyncPeriod > 0 && pWal->level == TAOS_WAL_FSYNC) { + return true; + } + return false; +} + void *walOpen(const char *path, const SWalCfg *pCfg) { SWal *pWal = calloc(sizeof(SWal), 1); if (pWal == NULL) { @@ -95,7 +102,7 @@ void *walOpen(const char *path, const SWalCfg *pCfg) { tstrncpy(pWal->path, path, sizeof(pWal->path)); pthread_mutex_init(&pWal->mutex, NULL); - if (pWal->fsyncPeriod > 0 && pWal->level == TAOS_WAL_FSYNC) { + if (walNeedFsyncTimer(pWal)) { pWal->timer = taosTmrStart(walProcessFsyncTimer, pWal->fsyncPeriod, pWal, walTmrCtrl); if (pWal->timer == NULL) { terrno = TAOS_SYSTEM_ERROR(errno); @@ -127,6 +134,37 @@ void *walOpen(const char *path, const SWalCfg *pCfg) { return pWal; } +int walAlter(twalh wal, const SWalCfg *pCfg) { + SWal *pWal = wal; + if (pWal == NULL) { + return TSDB_CODE_WAL_APP_ERROR; + } + + if (pWal->level == pCfg->walLevel && pWal->fsyncPeriod == pCfg->fsyncPeriod) { + wDebug("wal:%s, old walLevel:%d fsync:%d, new walLevel:%d fsync:%d not change", pWal->name, pWal->level, + pWal->fsyncPeriod, pCfg->walLevel, pCfg->fsyncPeriod); + return TSDB_CODE_SUCCESS; + } + + wInfo("wal:%s, change old walLevel:%d fsync:%d, new walLevel:%d fsync:%d", pWal->name, pWal->level, pWal->fsyncPeriod, + pCfg->walLevel, pCfg->fsyncPeriod); + + pthread_mutex_lock(&pWal->mutex); + pWal->level = pCfg->walLevel; + pWal->fsyncPeriod = pCfg->fsyncPeriod; + if (walNeedFsyncTimer(pWal)) { + wInfo("wal:%s, reset fsync timer, walLevel:%d fsyncPeriod:%d", pWal->name, pWal->level, pWal->fsyncPeriod); + taosTmrReset(walProcessFsyncTimer, pWal->fsyncPeriod, pWal, &pWal->timer,walTmrCtrl); + } else { + wInfo("wal:%s, stop fsync timer, walLevel:%d fsyncPeriod:%d", pWal->name, pWal->level, pWal->fsyncPeriod); + taosTmrStop(pWal->timer); + pWal->timer = NULL; + } + pthread_mutex_unlock(&pWal->mutex); + + return TSDB_CODE_SUCCESS; +} + void walClose(void *handle) { if (handle == NULL) return; @@ -484,6 +522,12 @@ static void walProcessFsyncTimer(void *param, void *tmrId) { if (fsync(pWal->fd) < 0) { wError("wal:%s, fsync failed(%s)", pWal->name, strerror(errno)); } - - pWal->timer = taosTmrStart(walProcessFsyncTimer, pWal->fsyncPeriod, pWal, walTmrCtrl); + + if (walNeedFsyncTimer(pWal)) { + pWal->timer = taosTmrStart(walProcessFsyncTimer, pWal->fsyncPeriod, pWal, walTmrCtrl); + } else { + wInfo("wal:%s, stop fsync timer for walLevel:%d fsyncPeriod:%d", pWal->name, pWal->level, pWal->fsyncPeriod); + taosTmrStop(pWal->timer); + pWal->timer = NULL; + } } diff --git a/tests/script/general/db/alter_option.sim b/tests/script/general/db/alter_option.sim index 49c75966ca..c8aa2480c5 100644 --- a/tests/script/general/db/alter_option.sim +++ b/tests/script/general/db/alter_option.sim @@ -218,7 +218,10 @@ if $data12_db != 1 then return -1 endi -sql_error alter database db wal 2 +sql alter database db wal 1 +sql alter database db wal 2 +sql alter database db wal 1 +sql alter database db wal 2 sql_error alter database db wal 0 sql_error alter database db wal 3 sql_error alter database db wal 4 @@ -226,11 +229,13 @@ sql_error alter database db wal -1 sql_error alter database db wal 1000 print ============== step fsync -sql_error alter database db fsync 2 -sql_error alter database db fsync 3 -sql_error alter database db fsync 4 +sql alter database db fsync 0 +sql alter database db fsync 1 +sql alter database db fsync 3600 +sql alter database db fsync 18000 +sql alter database db fsync 180000 +sql_error alter database db fsync 180001 sql_error alter database db fsync -1 -sql_error alter database db fsync 1000 print ============== step comp sql show databases From bec674296e65e642b55933f24eece27b4db86978 Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Tue, 22 Sep 2020 18:01:11 +0800 Subject: [PATCH 2/2] TD-1368 --- src/vnode/src/vnodeMain.c | 1 + 1 file changed, 1 insertion(+) diff --git a/src/vnode/src/vnodeMain.c b/src/vnode/src/vnodeMain.c index 353dbbf373..d89c383d6a 100644 --- a/src/vnode/src/vnodeMain.c +++ b/src/vnode/src/vnodeMain.c @@ -396,6 +396,7 @@ void vnodeRelease(void *pVnodeRaw) { if (0 == tsEnableVnodeBak) { vInfo("vgId:%d, vnode backup not enabled", pVnode->vgId); } else { + taosRemoveDir(newDir); taosRename(rootDir, newDir); }