Giter Club home page Giter Club logo

Comments (8)

arthurkiller avatar arthurkiller commented on August 17, 2024

Thx, I will take a look at this

from rollingwriter.

arthurkiller avatar arthurkiller commented on August 17, 2024

@chaomai 非常感谢细测试代码并提出问题,开源社区社区因为您的付出而更加美好!

不过关于您的问题,目前尚有些疑问:

	m.lock.Lock()
	// ...
	m.lock.Lock()
  1. 这里有提到需要在 manager 中增加一个 lock ,而这个 lock 用于 guard 生成日志文件名的逻辑,这个地方不太理解为何会有 race ?因为 manager 本身不需要加锁的原因是只应该有一个 manager 实例,造成冲突是由于多个实例并发执行,那么这是使用方式的问题。
  2. 其次,假设真的发生了这样的冲突,那么后果其实也是符合预期的,因为文件名本身就是跟时间相关的,如果出现了 1 秒内频繁切分文件的需求,那么文件名的后缀就需要更加长以适应这种需求。因此如果文件名自定义格式使用 unixnanosecond 做后缀,理论上就不会有冲突发生。

感谢回复

from rollingwriter.

chaomai avatar chaomai commented on August 17, 2024

最后的代码手误,lock打错了。。。

// GenLogFileName generate the new log file name, filename should be absolute path
func (m *manager) GenLogFileName(c *Config) (filename string) {
	m.lock.Lock()
	// [path-to-log]/filename.log.2007010215041517
	if c.Compress {
		filename = path.Join(c.LogPath, c.FileName+".log.gz."+m.startAt.Format(c.TimeTagFormat))
	} else {
		filename = path.Join(c.LogPath, c.FileName+".log."+m.startAt.Format(c.TimeTagFormat))
	}
	// reset the start time to now
	m.startAt = time.Now()
	m.lock.Unlock()
	return
}

manager只有一个实例没错,但是GenLogFileName在执行时候,由于github.com/robfig/cron直接就在goroutine中运行了,因此可能存在多个goroutine都在跑GenLogFileName(如果前一个被cron调度的未执行完毕),进而出现多个goroutine共享同一个manager的现象。

from rollingwriter.

arthurkiller avatar arthurkiller commented on August 17, 2024

from rollingwriter.

arthurkiller avatar arthurkiller commented on August 17, 2024

@chaomai 不过我觉得目前来说应该不会有使用上的问题,因为这个只是生成一个字符串,没有数据竟态。

138 filename = path.Join(c.LogPath, c.FileName+".log.gz."+m.startAt.Format(c.TimeTagFormat))

所以,按照我 2 所说

假设真的发生了这样的冲突,那么后果其实也是符合预期的,因为文件名本身就是跟时间相关的,如果出现了 1 秒内频繁切分文件的需求,那么文件名的后缀就需要更加长以适应这种需求。因此如果文件名自定义格式使用 unixnanosecond 做后缀,理论上就不会有冲突发生。

不过这个问题我会记录下来,后面改造 cron 时候就内置进去了。再次感谢

from rollingwriter.

chaomai avatar chaomai commented on August 17, 2024

GenLogFileName中,对m.startAt是有读有写的。

另外,在writer中也有一个问题。具体复现方法是,

1. 首先做如下修改

diff --git a/rollingWriter.go b/rollingWriter.go
index 3f2fa6f..ab561ec 100644
--- a/rollingWriter.go
+++ b/rollingWriter.go
@@ -99,13 +99,13 @@ func NewDefaultConfig() Config {
                LogPath:                "./log",
                TimeTagFormat:          "200601021504",
                FileName:               "log",
-               MaxRemain:              -1,            // disable auto delete
+               MaxRemain:              5,            // disable auto delete
                RollingPolicy:          1,             // TimeRotate by default
-               RollingTimePattern:     "0 0 0 * * *", // Rolling at 00:00 AM everyday
+               RollingTimePattern:     "* * * * * *", // Rolling at 00:00 AM everyday
                RollingVolumeSize:      "1G",
                WriterMode:             "lock",
                BufferWriterThershould: 64,
-               Compress:               false,
+               Compress:               true,
        }
 }

diff --git a/writer_test.go b/writer_test.go
index 1d525f6..62fd1d4 100644
--- a/writer_test.go
+++ b/writer_test.go
@@ -155,7 +155,7 @@ func TestVolumeWriteParallel(t *testing.T) {
 }
 func TestWriteLockParallel(t *testing.T) {
        var writer io.WriteCloser
-       var c int = 1000
+       var c int = 10000000
        var l int = 1024

        t.Run("locked", func(t *testing.T) {

Compress: true可改可不改,改后更容易触发

2. 运行func TestWriteLockParallel(t *testing.T)

会报,

=== RUN   TestWriteLockParallel/locked
=== PAUSE TestWriteLockParallel/locked
=== CONT  TestWriteLockParallel/locked
2019/04/29 11:22:48 error in compress log file remove test/unittest.log.gz.201904291122.tmp: no such file or directory
2019/04/29 11:22:49 error in compress log file remove test/unittest.log.gz.201904291122.tmp: no such file or directory
2019/04/29 11:22:50 error in compress log file remove test/unittest.log.gz.201904291122.tmp: no such file or directory
2019/04/29 11:22:55 error in compress log file remove test/unittest.log.gz.201904291122.tmp: no such file or directory
2019/04/29 11:22:55 error in compress log file remove test/unittest.log.gz.201904291122.tmp: no such file or directory

我做了fix,想提一个pull request到dev,不过我发现dev落后master好多,要不提到master你看看?

from rollingwriter.

arthurkiller avatar arthurkiller commented on August 17, 2024

@chaomai 嗯,这个确实有问题,PR 可以先 rebase 下 master,然后再提

我已经更新了dev 的代码

from rollingwriter.

arthurkiller avatar arthurkiller commented on August 17, 2024

closed via #10

感谢贡献😆,别忘了 star✨这个仓库 @chaomai

from rollingwriter.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.