From 9958b3d8d27130525b1c3b0a4f95bf08d23b620a Mon Sep 17 00:00:00 2001 From: lichuang Date: Thu, 20 May 2021 18:19:27 +0800 Subject: [PATCH] [TD-1920]add mnode compact wal functions --- src/common/inc/tglobal.h | 2 ++ src/common/src/tglobal.c | 2 ++ src/dnode/src/dnodeMain.c | 11 +++++++++++ src/mnode/src/mnodeMain.c | 4 +--- src/mnode/src/mnodeSdb.c | 33 +++++++++++++++++++++++++-------- 5 files changed, 41 insertions(+), 11 deletions(-) diff --git a/src/common/inc/tglobal.h b/src/common/inc/tglobal.h index abb4d39ee1..e07c3611d7 100644 --- a/src/common/inc/tglobal.h +++ b/src/common/inc/tglobal.h @@ -149,6 +149,8 @@ extern char configDir[]; extern char tsVnodeDir[]; extern char tsDnodeDir[]; extern char tsMnodeDir[]; +extern char tsMnodeBakDir[]; +extern char tsMnodeTmpDir[]; extern char tsDataDir[]; extern char tsLogDir[]; extern char tsScriptDir[]; diff --git a/src/common/src/tglobal.c b/src/common/src/tglobal.c index ab036b021c..d6bbc288ad 100644 --- a/src/common/src/tglobal.c +++ b/src/common/src/tglobal.c @@ -183,6 +183,8 @@ char configDir[TSDB_FILENAME_LEN] = {0}; char tsVnodeDir[TSDB_FILENAME_LEN] = {0}; char tsDnodeDir[TSDB_FILENAME_LEN] = {0}; char tsMnodeDir[TSDB_FILENAME_LEN] = {0}; +char tsMnodeTmpDir[TSDB_FILENAME_LEN] = {0}; +char tsMnodeBakDir[TSDB_FILENAME_LEN] = {0}; char tsDataDir[TSDB_FILENAME_LEN] = {0}; char tsScriptDir[TSDB_FILENAME_LEN] = {0}; char tsTempDir[TSDB_FILENAME_LEN] = "/tmp/"; diff --git a/src/dnode/src/dnodeMain.c b/src/dnode/src/dnodeMain.c index 48680e20d9..ee4dfcf852 100644 --- a/src/dnode/src/dnodeMain.c +++ b/src/dnode/src/dnodeMain.c @@ -217,6 +217,17 @@ static int32_t dnodeInitStorage() { sprintf(tsDnodeDir, "%s/dnode", tsDataDir); // sprintf(tsVnodeBakDir, "%s/vnode_bak", tsDataDir); + if (tsCompactMnodeWal == 1) { + sprintf(tsMnodeTmpDir, "%s/mnode_tmp", tsDataDir); + tfsRmdir(tsMnodeTmpDir); + if (dnodeCreateDir(tsMnodeTmpDir) < 0) { + dError("failed to create dir: %s, reason: %s", tsMnodeTmpDir, strerror(errno)); + return -1; + } + + sprintf(tsMnodeBakDir, "%s/mnode_bak", tsDataDir); + //tfsRmdir(tsMnodeBakDir); + } //TODO(dengyihao): no need to init here if (dnodeCreateDir(tsMnodeDir) < 0) { dError("failed to create dir: %s, reason: %s", tsMnodeDir, strerror(errno)); diff --git a/src/mnode/src/mnodeMain.c b/src/mnode/src/mnodeMain.c index d9abe16120..d3511a4e62 100644 --- a/src/mnode/src/mnodeMain.c +++ b/src/mnode/src/mnodeMain.c @@ -59,8 +59,6 @@ static SStep tsMnodeSteps[] = { static SStep tsMnodeCompactSteps[] = { {"cluster", mnodeCompactCluster, NULL}, - -/* {"dnodes", mnodeCompactDnodes, NULL}, {"mnodes", mnodeCompactMnodes, NULL}, {"accts", mnodeCompactAccts, NULL}, @@ -68,7 +66,7 @@ static SStep tsMnodeCompactSteps[] = { {"dbs", mnodeCompactDbs, NULL}, {"vgroups", mnodeCompactVgroups, NULL}, {"tables", mnodeCompactTables, NULL}, -*/ + }; static void mnodeInitTimer(); diff --git a/src/mnode/src/mnodeSdb.c b/src/mnode/src/mnodeSdb.c index 79081945d9..9e3b87671e 100644 --- a/src/mnode/src/mnodeSdb.c +++ b/src/mnode/src/mnodeSdb.c @@ -20,6 +20,7 @@ #include "tutil.h" #include "tref.h" #include "tbn.h" +#include "tfs.h" #include "tqueue.h" #include "twal.h" #include "tsync.h" @@ -1154,26 +1155,42 @@ int32_t sdbGetReplicaNum() { int32_t mnodeCompactWal() { sdbInfo("vgId:1, start compact mnode wal..."); - // close wal + // close old wal walFsync(tsSdbMgmt.wal, true); walClose(tsSdbMgmt.wal); - // change wal to wal_bak dir - char temp[TSDB_FILENAME_LEN] = {0}; + // reset version,then compacted wal log can start from version 1 + tsSdbMgmt.version = 0; + + // change wal to wal_tmp dir SWalCfg walCfg = {.vgId = 1, .walLevel = TAOS_WAL_FSYNC, .keep = TAOS_WAL_KEEP, .fsyncPeriod = 0}; - sprintf(temp, "%s/wal_tmp", tsMnodeDir); - if (mkdir(temp, 0755) != 0 && errno != EEXIST) { - return -1; - } + char temp[TSDB_FILENAME_LEN] = {0}; + sprintf(temp, "%s/wal", tsMnodeTmpDir); tsSdbMgmt.wal = walOpen(temp, &walCfg); walRenew(tsSdbMgmt.wal); // compact memory tables info to wal tmp dir - mnodeCompactComponents(); + if (mnodeCompactComponents() != 0) { + tfsRmdir(tsMnodeTmpDir); + return -1; + } // close wal walFsync(tsSdbMgmt.wal, true); walClose(tsSdbMgmt.wal); + // rename old wal to wal_bak + if (taosRename(tsMnodeDir, tsMnodeBakDir) != 0) { + return -1; + } + + // rename wal_tmp to wal + if (taosRename(tsMnodeTmpDir, tsMnodeDir) != 0) { + return -1; + } + + // del wal_tmp dir + sdbInfo("vgId:1, compact mnode wal success"); + return 0; } \ No newline at end of file -- GitLab