Skip to content

reader/{tailx,dirx}: clean expired submeta after sync #688

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 6, 2018
Merged

reader/{tailx,dirx}: clean expired submeta after sync #688

merged 1 commit into from
Aug 6, 2018

Conversation

unknwon
Copy link
Contributor

@unknwon unknwon commented Aug 3, 2018

JIRA: https://jira.qiniu.io/browse/PDR-7006

  1. tailx 和 dirx 在每次 SyncMeta 后清理过期的 submeta 目录
  2. 修复 dirx 检查过期时会扫到子目录的问题

@redHJ
Copy link
Collaborator

redHJ commented Aug 3, 2018

lgtm

@@ -341,6 +343,10 @@ func (r *Reader) SyncMeta() {
log.Errorf("Runner[%v] write meta[%v] failed: %v", r.meta.RunnerName, string(data), err)
return
}

if r.expire.Nanoseconds() > 0 {
r.meta.CleanExpiredSubMetas(r.expire * 3)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里不要用expire的3倍时间了,单独做个参数吧,默认30天

reader/meta.go Outdated

// CleanExpiredSubMetas 清除超过指定过期时长的 submetas 子目录,
// 如果 submetas 没有存放在该 meta 的子目录则调用此方法无效
func (m *Meta) CleanExpiredSubMetas(expire time.Duration) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个函数是跟在sync后面调用的,执行时间长会影响读取性能,可以设置个上次调用的时间,一小时调用一次就好了。

@@ -473,6 +473,16 @@ var ModeKeyOptions = map[string][]Option{
CheckRegex: "\\d+[hms]",
ToolTip: `当文件夹内所有的日志达到expire时间,则放弃追踪。写法为:数字加单位符号,组成字符串duration写法,支持时h、分m、秒s为单位,类似3h(3小时),10m(10分钟),5s(5秒),默认的expire时间是0s,即永不过期`,
},
{
KeyName: KeySubmetaExpire,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tailx也要,做个变量引用一下

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

reader/meta.go Outdated
func (m *Meta) CleanExpiredSubMetas(expire time.Duration) {
// 为避免频繁扫描目录影响性能,多次调用间隔必须大于最小阈值
if m.subMetaLastCleaned.Add(defaultSubMetaCleanInterval).After(time.Now()) {
log.Debugf("Meta %q has cleaned submetas at %s, ignored this time", m.Dir, m.subMetaLastCleaned)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里不要日志感觉比较好,倒是下面开始clean的时候可以加个Debug日志

@@ -581,6 +581,8 @@ func (r *Reader) SyncMeta() {
log.Errorf("%v sync meta WriteBuf error %v, buf %v", r.Name(), err, string(buf))
return
}

r.meta.CleanExpiredSubMetas(r.expire * 3)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里漏改了

@@ -128,6 +136,8 @@ func NewReader(meta *reader.Meta, conf conf.MapConf) (reader.Reader, error) {
dirReaders: newDirReaders(meta, expire, cachedLines),
logPathPattern: strings.TrimSuffix(logPathPattern, "/"),
statInterval: statInterval,
expire: expire,
submetaExpire: submetaExpire,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个值还要跟expire比较一下,如果小于expire,要报错

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

reader/meta.go Outdated
}

if fi.ModTime().Add(expire).Before(time.Now()) {
log.Debugf("Expired submeta %q has been removed with error %v", path, os.RemoveAll(path))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个打Info级别吧

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

执行删除和打印日志分离

reader/meta.go Outdated
numCleaned := 0
for path := range m.subMetaExpired {
if numCleaned >= maximumSubMetaCleanNumOneTime {
log.Infof("Cleaned %d expired submeta of %q and there are %d left, but break at this time", maximumSubMetaCleanNumOneTime, path, len(m.subMetaExpired))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

加一句: left expired submeta will be cleaned next time

reader/meta.go Outdated
// 二次确认 submeta 目录在删除前的一刻仍旧是过期状态才执行删除操作
if hasSubMetaExpired(path, expire) {
err := os.RemoveAll(path)
log.Infof("Expired submeta %q has been removed with error %v", path, err)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

numCleaned++ 应该移到这里

{
KeyName: KeySubmetaExpire,
ChooseOnly: false,
Default: "72h",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

720h

@wonderflow
Copy link
Contributor

文档对应修改

@unknwon
Copy link
Contributor Author

unknwon commented Aug 6, 2018

@wonderflow wonderflow merged commit cdfcc8a into qiniu:master Aug 6, 2018
@unknwon unknwon deleted the PDR-7006-clean-submeta branch August 6, 2018 07:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants