提交 a6303b07 编写于 作者: L Liu Jicong

force repair

上级 c22de779
...@@ -65,6 +65,7 @@ static inline int64_t walScanLogGetLastVer(SWal* pWal) { ...@@ -65,6 +65,7 @@ static inline int64_t walScanLogGetLastVer(SWal* pWal) {
struct stat statbuf; struct stat statbuf;
stat(fnameStr, &statbuf); stat(fnameStr, &statbuf);
int readSize = MIN(WAL_MAX_SIZE + 2, statbuf.st_size); int readSize = MIN(WAL_MAX_SIZE + 2, statbuf.st_size);
pLastFileInfo->fileSize = statbuf.st_size;
FileFd fd = taosOpenFileRead(fnameStr); FileFd fd = taosOpenFileRead(fnameStr);
if (fd < 0) { if (fd < 0) {
...@@ -91,7 +92,7 @@ static inline int64_t walScanLogGetLastVer(SWal* pWal) { ...@@ -91,7 +92,7 @@ static inline int64_t walScanLogGetLastVer(SWal* pWal) {
char* haystack = buf; char* haystack = buf;
char* found = NULL; char* found = NULL;
char *candidate = NULL; char *candidate;
while((candidate = tmemmem(haystack, readSize - (haystack - buf), (char*)&magic, sizeof(uint64_t))) != NULL) { while((candidate = tmemmem(haystack, readSize - (haystack - buf), (char*)&magic, sizeof(uint64_t))) != NULL) {
// read and validate // read and validate
SWalHead *logContent = (SWalHead*)candidate; SWalHead *logContent = (SWalHead*)candidate;
...@@ -142,7 +143,6 @@ int walCheckAndRepairMeta(SWal* pWal) { ...@@ -142,7 +143,6 @@ int walCheckAndRepairMeta(SWal* pWal) {
SWalFileInfo fileInfo; SWalFileInfo fileInfo;
memset(&fileInfo, -1, sizeof(SWalFileInfo)); memset(&fileInfo, -1, sizeof(SWalFileInfo));
sscanf(name, "%" PRId64 ".log", &fileInfo.firstVer); sscanf(name, "%" PRId64 ".log", &fileInfo.firstVer);
FileFd fd = taosOpenFileRead(ent->d_name);
//get lastVer //get lastVer
//get size //get size
taosArrayPush(pLogInfoArray, &fileInfo); taosArrayPush(pLogInfoArray, &fileInfo);
...@@ -159,28 +159,25 @@ int walCheckAndRepairMeta(SWal* pWal) { ...@@ -159,28 +159,25 @@ int walCheckAndRepairMeta(SWal* pWal) {
} }
int newSz = taosArrayGetSize(pLogInfoArray); int newSz = taosArrayGetSize(pLogInfoArray);
// case 1. meta file not exist / cannot be parsed // case 1. meta file not exist / cannot be parsed
if (pWal->fileInfoSet == NULL && newSz != 0) { if (oldSz < newSz) {
// recover fileInfo set
pWal->fileInfoSet = pLogInfoArray;
if (newSz != 0) {
// recover meta version
pWal->vers.firstVer = ((SWalFileInfo*)taosArrayGet(pLogInfoArray, 0))->firstVer;
pWal->writeCur = newSz - 1;
}
// recover file size
} else if (oldSz < newSz) {
for (int i = oldSz; i < newSz; i++) { for (int i = oldSz; i < newSz; i++) {
SWalFileInfo *pFileInfo = taosArrayGet(pLogInfoArray, i); SWalFileInfo *pFileInfo = taosArrayGet(pLogInfoArray, i);
taosArrayPush(pWal->fileInfoSet, pFileInfo); taosArrayPush(pWal->fileInfoSet, pFileInfo);
} }
pWal->writeCur = newSz - 1; pWal->writeCur = newSz - 1;
} pWal->vers.firstVer = ((SWalFileInfo*)taosArrayGet(pLogInfoArray, 0))->firstVer;
if (pWal->fileInfoSet && taosArrayGetSize(pWal->fileInfoSet) != 0) {
pWal->vers.lastVer = walScanLogGetLastVer(pWal); pWal->vers.lastVer = walScanLogGetLastVer(pWal);
((SWalFileInfo*)taosArrayGetLast(pWal->fileInfoSet))->lastVer = pWal->vers.lastVer;
ASSERT(pWal->vers.lastVer != -1); ASSERT(pWal->vers.lastVer != -1);
}
int code = walSaveMeta(pWal);
if (code < 0) {
taosArrayDestroy(pLogInfoArray);
return -1;
}
}
// case 2. versions in meta not match log // case 2. versions in meta not match log
// or some log not included in meta // or some log not included in meta
// (e.g. program killed) // (e.g. program killed)
...@@ -204,14 +201,11 @@ int walCheckAndRepairMeta(SWal* pWal) { ...@@ -204,14 +201,11 @@ int walCheckAndRepairMeta(SWal* pWal) {
} }
#endif #endif
int code = walSaveMeta(pWal);
if (code < 0) {
return -1;
}
// get last version of this file // get last version of this file
// //
// rebuild meta // rebuild meta
taosArrayDestroy(pLogInfoArray);
return 0; return 0;
} }
...@@ -419,6 +413,10 @@ int walLoadMeta(SWal* pWal) { ...@@ -419,6 +413,10 @@ int walLoadMeta(SWal* pWal) {
} }
memset(buf, 0, size + 5); memset(buf, 0, size + 5);
FileFd fd = taosOpenFileRead(fnameStr); FileFd fd = taosOpenFileRead(fnameStr);
if (fd < 0) {
terrno = TSDB_CODE_WAL_FILE_CORRUPTED;
return -1;
}
if (taosReadFile(fd, buf, size) != size) { if (taosReadFile(fd, buf, size) != size) {
terrno = TAOS_SYSTEM_ERROR(errno); terrno = TAOS_SYSTEM_ERROR(errno);
taosCloseFile(fd); taosCloseFile(fd);
......
...@@ -122,7 +122,9 @@ SWal *walOpen(const char *path, SWalCfg *pCfg) { ...@@ -122,7 +122,9 @@ SWal *walOpen(const char *path, SWalCfg *pCfg) {
return NULL; return NULL;
} }
if (walLoadMeta(pWal) < 0 && walCheckAndRepairMeta(pWal) < 0) { walLoadMeta(pWal);
if (walCheckAndRepairMeta(pWal) < 0) {
taosRemoveRef(tsWal.refSetId, pWal->refId); taosRemoveRef(tsWal.refSetId, pWal->refId);
pthread_mutex_destroy(&pWal->mutex); pthread_mutex_destroy(&pWal->mutex);
taosArrayDestroy(pWal->fileInfoSet); taosArrayDestroy(pWal->fileInfoSet);
...@@ -131,6 +133,7 @@ SWal *walOpen(const char *path, SWalCfg *pCfg) { ...@@ -131,6 +133,7 @@ SWal *walOpen(const char *path, SWalCfg *pCfg) {
} }
if (walCheckAndRepairIdx(pWal) < 0) { if (walCheckAndRepairIdx(pWal) < 0) {
} }
wDebug("vgId:%d, wal:%p is opened, level:%d fsyncPeriod:%d", pWal->cfg.vgId, pWal, pWal->cfg.level, wDebug("vgId:%d, wal:%p is opened, level:%d fsyncPeriod:%d", pWal->cfg.vgId, pWal, pWal->cfg.level,
......
...@@ -380,4 +380,26 @@ TEST_F(WalRetentionEnv, repairMeta1) { ...@@ -380,4 +380,26 @@ TEST_F(WalRetentionEnv, repairMeta1) {
ASSERT_EQ(code, 0); ASSERT_EQ(code, 0);
} }
for (int i = 0; i < 1000; i++) {
int ver = rand() % 200;
code = walReadWithHandle(pRead, ver);
ASSERT_EQ(code, 0);
// printf("rrbody: \n");
// for(int i = 0; i < pRead->pHead->head.len; i++) {
// printf("%d ", pRead->pHead->head.body[i]);
//}
// printf("\n");
ASSERT_EQ(pRead->pHead->head.version, ver);
ASSERT_EQ(pRead->curVersion, ver + 1);
char newStr[100];
sprintf(newStr, "%s-%d", ranStr, ver);
int len = strlen(newStr);
ASSERT_EQ(pRead->pHead->head.len, len);
for (int j = 0; j < len; j++) {
EXPECT_EQ(newStr[j], pRead->pHead->head.body[j]);
}
}
} }
...@@ -85,7 +85,6 @@ if $data02 != 2 then ...@@ -85,7 +85,6 @@ if $data02 != 2 then
return -1 return -1
endi endi
return
system sh/exec.sh -n dnode1 -s stop -x SIGKILL system sh/exec.sh -n dnode1 -s stop -x SIGKILL
system sh/exec.sh -n dnode1 -s start system sh/exec.sh -n dnode1 -s start
...@@ -104,4 +103,4 @@ if $rows != 2 then ...@@ -104,4 +103,4 @@ if $rows != 2 then
return -1 return -1
endi endi
system sh/exec.sh -n dnode1 -s stop -x SIGINT system sh/exec.sh -n dnode1 -s stop -x SIGINT
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册