Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
taosdata
TDengine
提交
14e1e470
T
TDengine
项目概览
taosdata
/
TDengine
1 年多 前同步成功
通知
1185
Star
22016
Fork
4786
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
T
TDengine
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1
Issue
1
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
14e1e470
编写于
9月 22, 2022
作者:
L
Liu Jicong
提交者:
GitHub
9月 22, 2022
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #16993 from taosdata/feature/wal
feat(wal): auto fix corrupt file
上级
16be3ca3
78b4d102
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
101 addition
and
8 deletion
+101
-8
source/libs/wal/src/walMeta.c
source/libs/wal/src/walMeta.c
+94
-7
source/libs/wal/src/walMgmt.c
source/libs/wal/src/walMgmt.c
+7
-1
未找到文件。
source/libs/wal/src/walMeta.c
浏览文件 @
14e1e470
...
@@ -116,7 +116,6 @@ static FORCE_INLINE int64_t walScanLogGetLastVer(SWal* pWal) {
...
@@ -116,7 +116,6 @@ static FORCE_INLINE int64_t walScanLogGetLastVer(SWal* pWal) {
}
}
#endif
#endif
}
}
// TODO truncate file
if
(
found
==
NULL
)
{
if
(
found
==
NULL
)
{
// file corrupted, no complete log
// file corrupted, no complete log
...
@@ -125,8 +124,20 @@ static FORCE_INLINE int64_t walScanLogGetLastVer(SWal* pWal) {
...
@@ -125,8 +124,20 @@ static FORCE_INLINE int64_t walScanLogGetLastVer(SWal* pWal) {
terrno
=
TSDB_CODE_WAL_FILE_CORRUPTED
;
terrno
=
TSDB_CODE_WAL_FILE_CORRUPTED
;
return
-
1
;
return
-
1
;
}
}
// truncate file
SWalCkHead
*
lastEntry
=
(
SWalCkHead
*
)
found
;
SWalCkHead
*
lastEntry
=
(
SWalCkHead
*
)
found
;
int64_t
retVer
=
lastEntry
->
head
.
version
;
int64_t
retVer
=
lastEntry
->
head
.
version
;
int64_t
lastEntryBeginOffset
=
offset
+
(
int64_t
)((
char
*
)
found
-
(
char
*
)
buf
);
int64_t
lastEntryEndOffset
=
lastEntryBeginOffset
+
sizeof
(
SWalCkHead
)
+
lastEntry
->
head
.
bodyLen
;
if
(
lastEntryEndOffset
!=
fileSize
)
{
wWarn
(
"vgId:%d repair meta truncate file %s to %ld, orig size %ld"
,
pWal
->
cfg
.
vgId
,
fnameStr
,
lastEntryEndOffset
,
fileSize
);
taosFtruncateFile
(
pFile
,
lastEntryEndOffset
);
((
SWalFileInfo
*
)
taosArrayGetLast
(
pWal
->
fileInfoSet
))
->
fileSize
=
lastEntryEndOffset
;
pWal
->
totSize
-=
(
fileSize
-
lastEntryEndOffset
);
}
taosCloseFile
(
&
pFile
);
taosCloseFile
(
&
pFile
);
taosMemoryFree
(
buf
);
taosMemoryFree
(
buf
);
...
@@ -226,16 +237,92 @@ int walCheckAndRepairMeta(SWal* pWal) {
...
@@ -226,16 +237,92 @@ int walCheckAndRepairMeta(SWal* pWal) {
}
}
}
}
// TODO: set fileSize and lastVer if necessary
return
0
;
return
0
;
}
}
int
walCheckAndRepairIdx
(
SWal
*
pWal
)
{
int
walCheckAndRepairIdx
(
SWal
*
pWal
)
{
// TODO: iterate all log files
int32_t
sz
=
taosArrayGetSize
(
pWal
->
fileInfoSet
);
// if idx not found, scan log and write idx
for
(
int32_t
i
=
0
;
i
<
sz
;
i
++
)
{
// if found, check complete by first and last entry of each idx file
SWalFileInfo
*
pFileInfo
=
taosArrayGet
(
pWal
->
fileInfoSet
,
i
);
// if idx incomplete, binary search last valid entry, and then build other part
char
fnameStr
[
WAL_FILE_LEN
];
walBuildIdxName
(
pWal
,
pFileInfo
->
firstVer
,
fnameStr
);
int64_t
fsize
;
TdFilePtr
pIdxFile
=
taosOpenFile
(
fnameStr
,
TD_FILE_READ
|
TD_FILE_WRITE
|
TD_FILE_CREATE
);
if
(
pIdxFile
==
NULL
)
{
ASSERT
(
0
);
terrno
=
TAOS_SYSTEM_ERROR
(
errno
);
wError
(
"vgId:%d, cannot open file %s, since %s"
,
pWal
->
cfg
.
vgId
,
fnameStr
,
terrstr
());
return
-
1
;
}
taosFStatFile
(
pIdxFile
,
&
fsize
,
NULL
);
if
(
fsize
==
(
pFileInfo
->
lastVer
-
pFileInfo
->
firstVer
+
1
)
*
sizeof
(
SWalIdxEntry
))
{
taosCloseFile
(
&
pIdxFile
);
continue
;
}
int32_t
left
=
fsize
%
sizeof
(
SWalIdxEntry
);
int64_t
offset
=
taosLSeekFile
(
pIdxFile
,
-
left
,
SEEK_END
);
if
(
left
!=
0
)
{
taosFtruncateFile
(
pIdxFile
,
offset
);
wWarn
(
"vgId:%d wal truncate file %s to offset %ld since size invalid, file size %ld"
,
pWal
->
cfg
.
vgId
,
fnameStr
,
offset
,
fsize
);
}
offset
-=
sizeof
(
SWalIdxEntry
);
SWalIdxEntry
idxEntry
=
{.
ver
=
pFileInfo
->
firstVer
};
while
(
1
)
{
if
(
offset
<
0
)
{
taosLSeekFile
(
pIdxFile
,
0
,
SEEK_SET
);
taosWriteFile
(
pIdxFile
,
&
idxEntry
,
sizeof
(
SWalIdxEntry
));
break
;
}
taosLSeekFile
(
pIdxFile
,
offset
,
SEEK_SET
);
int64_t
contLen
=
taosReadFile
(
pIdxFile
,
&
idxEntry
,
sizeof
(
SWalIdxEntry
));
if
(
contLen
<
0
||
contLen
!=
sizeof
(
SWalIdxEntry
))
{
terrno
=
TAOS_SYSTEM_ERROR
(
errno
);
return
-
1
;
}
if
((
idxEntry
.
ver
-
pFileInfo
->
firstVer
)
*
sizeof
(
SWalIdxEntry
)
!=
offset
)
{
taosFtruncateFile
(
pIdxFile
,
offset
);
wWarn
(
"vgId:%d wal truncate file %s to offset %ld since entry invalid, entry ver %ld, entry offset %ld"
,
pWal
->
cfg
.
vgId
,
fnameStr
,
offset
,
idxEntry
.
ver
,
idxEntry
.
offset
);
offset
-=
sizeof
(
SWalIdxEntry
);
}
else
{
break
;
}
}
if
(
idxEntry
.
ver
<
pFileInfo
->
lastVer
)
{
char
fLogNameStr
[
WAL_FILE_LEN
];
walBuildLogName
(
pWal
,
pFileInfo
->
firstVer
,
fLogNameStr
);
TdFilePtr
pLogFile
=
taosOpenFile
(
fLogNameStr
,
TD_FILE_READ
);
if
(
pLogFile
==
NULL
)
{
terrno
=
TAOS_SYSTEM_ERROR
(
errno
);
wError
(
"vgId:%d, cannot open file %s, since %s"
,
pWal
->
cfg
.
vgId
,
fLogNameStr
,
terrstr
());
return
-
1
;
}
while
(
idxEntry
.
ver
<
pFileInfo
->
lastVer
)
{
taosLSeekFile
(
pLogFile
,
idxEntry
.
offset
,
SEEK_SET
);
SWalCkHead
ckHead
;
taosReadFile
(
pLogFile
,
&
ckHead
,
sizeof
(
SWalCkHead
));
if
(
idxEntry
.
ver
!=
ckHead
.
head
.
version
)
{
// todo truncate this idx also
taosCloseFile
(
&
pLogFile
);
wError
(
"vgId:%d, invalid repair case, log seek to %ld to find ver %ld, actual ver %ld"
,
pWal
->
cfg
.
vgId
,
idxEntry
.
offset
,
idxEntry
.
ver
,
ckHead
.
head
.
version
);
return
-
1
;
}
idxEntry
.
ver
=
ckHead
.
head
.
version
+
1
;
idxEntry
.
offset
=
idxEntry
.
offset
+
sizeof
(
SWalCkHead
)
+
ckHead
.
head
.
bodyLen
;
wWarn
(
"vgId:%d wal idx append new entry %ld %ld"
,
pWal
->
cfg
.
vgId
,
idxEntry
.
ver
,
idxEntry
.
offset
);
taosWriteFile
(
pIdxFile
,
&
idxEntry
,
sizeof
(
SWalIdxEntry
));
}
taosCloseFile
(
&
pLogFile
);
}
taosCloseFile
(
&
pIdxFile
);
}
return
0
;
return
0
;
}
}
...
...
source/libs/wal/src/walMgmt.c
浏览文件 @
14e1e470
...
@@ -149,15 +149,21 @@ SWal *walOpen(const char *path, SWalCfg *pCfg) {
...
@@ -149,15 +149,21 @@ SWal *walOpen(const char *path, SWalCfg *pCfg) {
walLoadMeta
(
pWal
);
walLoadMeta
(
pWal
);
if
(
walCheckAndRepairMeta
(
pWal
)
<
0
)
{
if
(
walCheckAndRepairMeta
(
pWal
)
<
0
)
{
wError
(
"vgId:%d cannot open wal since repair meta file failed"
,
pWal
->
cfg
.
vgId
);
taosHashCleanup
(
pWal
->
pRefHash
);
taosHashCleanup
(
pWal
->
pRefHash
);
taosRemoveRef
(
tsWal
.
refSetId
,
pWal
->
refId
);
taosRemoveRef
(
tsWal
.
refSetId
,
pWal
->
refId
);
taosThreadMutexDestroy
(
&
pWal
->
mutex
);
taosThreadMutexDestroy
(
&
pWal
->
mutex
);
taosArrayDestroy
(
pWal
->
fileInfoSet
);
taosArrayDestroy
(
pWal
->
fileInfoSet
);
taosMemoryFree
(
pWal
);
return
NULL
;
return
NULL
;
}
}
if
(
walCheckAndRepairIdx
(
pWal
)
<
0
)
{
if
(
walCheckAndRepairIdx
(
pWal
)
<
0
)
{
wError
(
"vgId:%d cannot open wal since repair idx file failed"
,
pWal
->
cfg
.
vgId
);
taosHashCleanup
(
pWal
->
pRefHash
);
taosRemoveRef
(
tsWal
.
refSetId
,
pWal
->
refId
);
taosThreadMutexDestroy
(
&
pWal
->
mutex
);
taosArrayDestroy
(
pWal
->
fileInfoSet
);
return
NULL
;
}
}
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
,
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录