Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
taosdata
TDengine
提交
9055f637
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看板
提交
9055f637
编写于
9月 30, 2022
作者:
B
Benguang Zhao
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix: ftruncate WAL log after the last valid WAL record properly in walScanLogGetLastVer
上级
76865258
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
42 addition
and
37 deletion
+42
-37
source/libs/wal/src/walMeta.c
source/libs/wal/src/walMeta.c
+42
-37
未找到文件。
source/libs/wal/src/walMeta.c
浏览文件 @
9055f637
...
...
@@ -45,12 +45,11 @@ static FORCE_INLINE int walBuildTmpMetaName(SWal* pWal, char* buf) {
static
FORCE_INLINE
int64_t
walScanLogGetLastVer
(
SWal
*
pWal
)
{
int32_t
sz
=
taosArrayGetSize
(
pWal
->
fileInfoSet
);
ASSERT
(
sz
>
0
);
#if 0
for (int i = 0; i < sz; i++) {
SWalFileInfo* pFileInfo = taosArrayGet(pWal->fileInfoSet, i)
;
if
(
sz
<=
0
)
{
wError
(
"No WAL log file found."
);
terrno
=
TSDB_CODE_WAL_FILE_CORRUPTED
;
return
-
1
;
}
#endif
SWalFileInfo
*
pLastFileInfo
=
taosArrayGet
(
pWal
->
fileInfoSet
,
sz
-
1
);
char
fnameStr
[
WAL_FILE_LEN
];
...
...
@@ -61,7 +60,7 @@ static FORCE_INLINE int64_t walScanLogGetLastVer(SWal* pWal) {
int32_t
readSize
=
TMIN
(
WAL_SCAN_BUF_SIZE
,
fileSize
);
pLastFileInfo
->
fileSize
=
fileSize
;
TdFilePtr
pFile
=
taosOpenFile
(
fnameStr
,
TD_FILE_READ
);
TdFilePtr
pFile
=
taosOpenFile
(
fnameStr
,
TD_FILE_READ
|
TD_FILE_WRITE
);
if
(
pFile
==
NULL
)
{
terrno
=
TAOS_SYSTEM_ERROR
(
errno
);
return
-
1
;
...
...
@@ -69,28 +68,30 @@ static FORCE_INLINE int64_t walScanLogGetLastVer(SWal* pWal) {
uint64_t
magic
=
WAL_MAGIC
;
char
*
buf
=
taosMemoryMalloc
(
readSize
+
5
);
char
*
buf
=
taosMemoryMalloc
(
readSize
+
sizeof
(
uint64_t
)
);
if
(
buf
==
NULL
)
{
taosCloseFile
(
&
pFile
);
terrno
=
TSDB_CODE_WAL_OUT_OF_MEMORY
;
return
-
1
;
goto
_err
;
}
int64_t
offset
;
offset
=
taosLSeekFile
(
pFile
,
-
readSize
,
SEEK_END
);
if
(
readSize
!=
taosReadFile
(
pFile
,
buf
,
readSize
))
{
taosMemoryFree
(
buf
);
taosCloseFile
(
&
pFile
);
terrno
=
TAOS_SYSTEM_ERROR
(
errno
);
return
-
1
;
goto
_err
;
}
int64_t
walCkHeadSz
=
sizeof
(
SWalCkHead
);
char
*
found
=
NULL
;
while
(
1
)
{
char
*
haystack
=
buf
;
char
*
candidate
;
char
*
candidate
=
NULL
;
while
((
candidate
=
tmemmem
(
haystack
,
readSize
-
(
haystack
-
buf
),
(
char
*
)
&
magic
,
sizeof
(
uint64_t
)))
!=
NULL
)
{
// read and validate
int64_t
len
=
readSize
-
(
candidate
-
buf
);
if
(
len
<
walCkHeadSz
)
{
break
;
}
SWalCkHead
*
logContent
=
(
SWalCkHead
*
)
candidate
;
if
(
walValidHeadCksum
(
logContent
)
==
0
&&
walValidBodyCksum
(
logContent
)
==
0
)
{
found
=
candidate
;
...
...
@@ -98,35 +99,26 @@ static FORCE_INLINE int64_t walScanLogGetLastVer(SWal* pWal) {
haystack
=
candidate
+
1
;
}
if
(
found
||
offset
==
0
)
break
;
offset
=
TMIN
(
0
,
offset
-
readSize
+
sizeof
(
uint64_t
));
int64_t
offset2
=
taosLSeekFile
(
pFile
,
offset
,
SEEK_SET
);
ASSERT
(
offset
==
offset2
);
i
f
(
readSize
!=
taosReadFile
(
pFile
,
buf
,
readSize
))
{
taosMemoryFree
(
buf
);
taosCloseFile
(
&
pFile
);
// go backwards, i.e. by at most one WAL scan buf size
offset
=
TMAX
(
0
,
offset
-
readSize
+
walCkHeadSz
);
i
nt64_t
ret
=
taosLSeekFile
(
pFile
,
offset
,
SEEK_SET
);
if
(
ret
<
0
)
{
wError
(
"failed to lseek file due to %s. offset:%lld"
,
strerror
(
errno
),
offset
);
terrno
=
TAOS_SYSTEM_ERROR
(
errno
);
return
-
1
;
goto
_err
;
}
#if 0
if (found == buf) {
SWalCkHead* logContent = (SWalCkHead*)found;
if (walValidHeadCksum(logContent) != 0 || walValidBodyCksum(logContent) != 0) {
// file has to be deleted
taosMemoryFree(buf);
taosCloseFile(&pFile);
terrno = TSDB_CODE_WAL_FILE_CORRUPTED;
return -1;
}
ASSERT
(
offset
==
ret
);
if
(
readSize
!=
taosReadFile
(
pFile
,
buf
,
readSize
))
{
terrno
=
TAOS_SYSTEM_ERROR
(
errno
);
goto
_err
;
}
#endif
}
if
(
found
==
NULL
)
{
// file corrupted, no complete log
// TODO delete and search in previous files
/*ASSERT(0);*/
wError
(
"WAL log file corrupted: no valid WAL record found. file: %s"
,
fnameStr
);
terrno
=
TSDB_CODE_WAL_FILE_CORRUPTED
;
return
-
1
;
goto
_err
;
}
// truncate file
...
...
@@ -137,15 +129,28 @@ static FORCE_INLINE int64_t walScanLogGetLastVer(SWal* pWal) {
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
);
if
(
taosFtruncateFile
(
pFile
,
lastEntryEndOffset
)
<
0
)
{
wError
(
"failed to truncate file due to %s. file:%s"
,
strerror
(
errno
),
fnameStr
);
terrno
=
TAOS_SYSTEM_ERROR
(
errno
);
goto
_err
;
}
if
(
taosFsyncFile
(
pFile
)
<
0
)
{
wError
(
"failed to fsync file due to %s. file:%s"
,
strerror
(
errno
),
fnameStr
);
terrno
=
TAOS_SYSTEM_ERROR
(
errno
);
goto
_err
;
}
((
SWalFileInfo
*
)
taosArrayGetLast
(
pWal
->
fileInfoSet
))
->
fileSize
=
lastEntryEndOffset
;
pWal
->
totSize
-=
(
fileSize
-
lastEntryEndOffset
);
}
taosCloseFile
(
&
pFile
);
taosMemoryFree
(
buf
);
return
retVer
;
_err:
taosCloseFile
(
&
pFile
);
taosMemoryFree
(
buf
);
return
-
1
;
}
int
walCheckAndRepairMeta
(
SWal
*
pWal
)
{
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录