From fe2ce533370a52a55fc7697d820e3ea42eb0a9af Mon Sep 17 00:00:00 2001 From: Daxin Wang Date: Fri, 30 Dec 2022 14:11:28 +0800 Subject: [PATCH] Add comments Signed-off-by: Daxin Wang --- .../consumer/exporter/cameraexporter/filewriter.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/collector/pkg/component/consumer/exporter/cameraexporter/filewriter.go b/collector/pkg/component/consumer/exporter/cameraexporter/filewriter.go index 571c637c8..24ffc4107 100644 --- a/collector/pkg/component/consumer/exporter/cameraexporter/filewriter.go +++ b/collector/pkg/component/consumer/exporter/cameraexporter/filewriter.go @@ -124,6 +124,8 @@ func (fw *fileWriter) rotateFiles(baseDir string) error { return nil } // Remove the older files and remove half of them one time to decrease the frequency + // of deleting files. Note this is different from rotating log files. For log files, + // we could delete one file at a time because the action "rotate" is in a low frequency. toBeRotated = toBeRotated[:len(toBeRotated)-fw.config.MaxFileCount/2+1] // Remove the stale files asynchronously go func() { @@ -135,7 +137,8 @@ func (fw *fileWriter) rotateFiles(baseDir string) error { return nil } -// getDirEntryInTimeOrder returns the directory entries slice in chronological order +// getDirEntryInTimeOrder returns the directory entries slice in chronological order. +// The result files are sorted based on their modification time. func getDirEntryInTimeOrder(path string) ([]os.DirEntry, error) { f, err := os.Open(path) if err != nil { @@ -148,6 +151,9 @@ func getDirEntryInTimeOrder(path string) ([]os.DirEntry, error) { } }(f) dirs, err := f.ReadDir(-1) + // Sort the files based on their modification time. We don't sort them based on the + // timestamp in the file name because they are similar but the later one costs more CPU + // considering that we have to split the file name first. sort.Slice(dirs, func(i, j int) bool { fileInfoA, err := dirs[i].Info() if err != nil {