diff --git a/src/wal/inc/walInt.h b/src/wal/inc/walInt.h index 74a0184c28b84da48287a76d1c3b22a390f72ced..319b652dc747d2930d0b405778c323fb61e22557 100644 --- a/src/wal/inc/walInt.h +++ b/src/wal/inc/walInt.h @@ -38,22 +38,22 @@ extern int32_t wDebugFlag; #define WAL_SIGNATURE ((uint32_t)(0xFAFBFDFE)) #define WAL_PATH_LEN (TSDB_FILENAME_LEN + 12) #define WAL_FILE_LEN (TSDB_FILENAME_LEN + 32) +#define WAL_FILE_NUM 3 typedef struct { uint64_t version; + int64_t fileId; int32_t vgId; int32_t fd; int32_t keep; int32_t level; int32_t fsyncPeriod; int32_t fsyncSeq; - int64_t fileId; char path[WAL_PATH_LEN]; char name[WAL_FILE_LEN]; pthread_mutex_t mutex; } SWal; -// util int32_t walGetNextFile(SWal *pWal, int64_t *nextFileId); int32_t walGetOldFile(SWal *pWal, int64_t curFileId, int32_t minDiff, int64_t *oldFileId); diff --git a/src/wal/src/walMgmt.c b/src/wal/src/walMgmt.c index 72c0ef21a207b52029a489263b7c50ffe61bc187..52a8d9dbf0c5c78c1ff56e88325524c1651dc660 100644 --- a/src/wal/src/walMgmt.c +++ b/src/wal/src/walMgmt.c @@ -18,7 +18,6 @@ #include "taoserror.h" #include "talloc.h" #include "tref.h" -#include "tutil.h" #include "twal.h" #include "walInt.h" @@ -135,9 +134,8 @@ void walClose(void *handle) { static int32_t walInitObj(SWal *pWal) { if (taosMkDir(pWal->path, 0755) != 0) { - terrno = TAOS_SYSTEM_ERROR(errno); wError("vgId:%d, file:%s, failed to create directory since %s", pWal->vgId, pWal->path, strerror(errno)); - return terrno; + return TAOS_SYSTEM_ERROR(errno); } if (pWal->keep) { @@ -147,9 +145,8 @@ static int32_t walInitObj(SWal *pWal) { walRenew(pWal); if (pWal && pWal->fd < 0) { - terrno = TAOS_SYSTEM_ERROR(errno); wError("vgId:%d, file:%s, failed to open file since %s", pWal->vgId, pWal->path, strerror(errno)); - return terrno; + return TAOS_SYSTEM_ERROR(errno); } wDebug("vgId:%d, file is initialized", pWal->vgId); @@ -191,7 +188,7 @@ static void walFsyncAll() { wTrace("vgId:%d, do fsync, level:%d seq:%d rseq:%d", pWal->vgId, pWal->level, pWal->fsyncSeq, tsWal.seq); int32_t code = fsync(pWal->fd); if (code != 0) { - wError("vgId:%d, file:%s, fsync failed since %s", pWal->vgId, pWal->name, strerror(code)); + wError("vgId:%d, file:%s, failed to fsync since %s", pWal->vgId, pWal->name, strerror(code)); } } pWal = taosIterateRef(tsWal.refId, pWal); diff --git a/src/wal/src/walUtil.c b/src/wal/src/walUtil.c index b190c703bdcb44b569fabd756d08c791fe916b23..c4ef6df195c639dfb3fa96ec85ea36936333f5d8 100644 --- a/src/wal/src/walUtil.c +++ b/src/wal/src/walUtil.c @@ -82,4 +82,4 @@ int32_t walGetOldFile(SWal *pWal, int64_t curFileId, int32_t minDiff, int64_t *o wTrace("vgId:%d, path:%s, curFileId:%" PRId64 " oldFildId:%" PRId64, pWal->vgId, pWal->path, curFileId, *oldFileId); return 0; -} \ No newline at end of file +} diff --git a/src/wal/src/walWrite.c b/src/wal/src/walWrite.c index 54efdc1ce7a9dbe5ee7911556c891892042542d1..39a7edb2404d8c476f16e45611c5423b0b9685ac 100644 --- a/src/wal/src/walWrite.c +++ b/src/wal/src/walWrite.c @@ -51,7 +51,7 @@ int32_t walRenew(void *handle) { if (!pWal->keep) { // remove the oldest wal file int64_t oldFileId = -1; - if (walGetOldFile(pWal, pWal->fileId, 2, &oldFileId) == 0) { + if (walGetOldFile(pWal, pWal->fileId, WAL_FILE_NUM, &oldFileId) == 0) { char walName[WAL_FILE_LEN] = {0}; snprintf(walName, sizeof(walName), "%s/%s%" PRId64, pWal->path, WAL_PREFIX, oldFileId); @@ -161,12 +161,18 @@ int32_t walGetWalFile(void *handle, char *fileName, int64_t *fileId) { if (handle == NULL) return -1; SWal *pWal = handle; + // for keep + if (*fileId == 0) *fileId = -1; + pthread_mutex_lock(&(pWal->mutex)); + int32_t code = walGetNextFile(pWal, fileId); if (code >= 0) { sprintf(fileName, "wal/%s%" PRId64, WAL_PREFIX, *fileId); code = (*fileId == pWal->fileId) ? 0 : 1; } + + wTrace("vgId:%d, get wal file, code:%d curId:%" PRId64 " outId:%" PRId64, pWal->vgId, code, pWal->fileId, *fileId); pthread_mutex_unlock(&(pWal->mutex)); return code;