Skip to content

Commit

Permalink
[GC] improve log file GC performance
Browse files Browse the repository at this point in the history
Don't keep the log while removing old files
  • Loading branch information
csweichel committed May 12, 2022
1 parent 954fb77 commit 60ccbd1
Showing 1 changed file with 9 additions and 24 deletions.
33 changes: 9 additions & 24 deletions pkg/store/logfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,35 +42,20 @@ func NewFileLogStore(base string) (*FileLogStore, error) {

// GarbageCollect removes all log files older than the given duration.
func (fs *FileLogStore) GarbageCollect(olderThan time.Duration) error {
fs.mu.Lock()
defer fs.mu.Unlock()

// clean known files first - this way we can skip those which are currently
// open for writing.
openForWriting := make(map[string]struct{})
for id, f := range fs.files {
if !f.closed {
openForWriting[f.fn] = struct{}{}
continue
}
fn := filepath.Join(fs.Base, f.fn)
stat, err := os.Stat(fn)
if err != nil {
// cannot stat file - it might not even exist for some reason.
continue
}
t := fileAge(stat)
func() {
fs.mu.Lock()
defer fs.mu.Unlock()

if time.Since(t) <= olderThan {
continue
}

// we ignore the error here because we don't want a single
// file to break the removal of others.
_ = os.Remove(fn)
for _, f := range fs.files {
if !f.closed {
openForWriting[f.fn] = struct{}{}

delete(fs.files, id)
}
}
}
}()

// let's look at all the files in the base path, excluding those
// of which we know they're open for writing.
Expand Down

0 comments on commit 60ccbd1

Please sign in to comment.