Repository navigation
fix(core): stop log rotation from overwriting backups in the same millisecond #9871
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -307,10 +307,25 @@ func (l *LogWriter) open() error { | |
| } | ||
|
|
||
| func backupName(name string) string { | ||
| return nextBackupName(name, time.Now().UTC()) | ||
| } | ||
|
|
||
| // nextBackupName returns the backup path for name stamped with ts. backupTimeFormat only has millisecond resolution, | ||
| // so two rotations can land on the same stamp; renaming onto an existing backup (or compressing over its .gz) would | ||
| // silently drop the older one. On a collision the stamp is pushed forward until neither form exists, which keeps the | ||
| // name parseable by processOldLogFiles and the backups ordered. | ||
| func nextBackupName(name string, ts time.Time) string { | ||
| dir := filepath.Dir(name) | ||
| prefix, ext := prefixAndExt(name) | ||
| timestamp := time.Now().UTC().Format(backupTimeFormat) | ||
| return filepath.Join(dir, fmt.Sprintf("%s-%s%s", prefix, timestamp, ext)) | ||
| for { | ||
| candidate := filepath.Join(dir, fmt.Sprintf("%s-%s%s", prefix, ts.Format(backupTimeFormat), ext)) | ||
| _, errPlain := os.Lstat(candidate) | ||
| _, errGz := os.Lstat(candidate + ".gz") | ||
| if errPlain != nil && errGz != nil { | ||
| return candidate | ||
| } | ||
| ts = ts.Add(time.Millisecond) | ||
| } | ||
| } | ||
|
|
||
| func compress(src string) error { | ||
|
|
@@ -424,8 +439,8 @@ func processOldLogFiles(fp string, maxAge int64) ([]string, []string, error) { | |
| continue | ||
| } | ||
|
|
||
| _, e := prefixAndExt(fp) | ||
| tsString := f.Name()[len(defPrefix) : len(f.Name())-len(e)] | ||
| name := strings.TrimSuffix(f.Name(), ".gz") | ||
| tsString := name[len(defPrefix) : len(name)-len(defExt)] | ||
|
Comment on lines
+442
to
+443
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Preserve the extension of an uncompressed If
|
||
| ts, err := time.Parse(backupTimeFormat, tsString) | ||
| if err != nil { | ||
| continue | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Script executed:
sed -n 280,345p x/log_writer.go grep -n 'backupName\|func (l \*LogWriter) rotate' x/log_writer.goRepository: dgraph-io/dgraph
Length of output: 2283
Treat only a missing backup as a free name.
nextBackupNametreats everyos.Lstaterror as proof that a candidate is absent. Ifos.Lstatfails with a transient error other than non-existence, such as an I/O error, for an existing backup, the loop can return that occupied name. The lateros.Renameorcompresscan then replace the backup. Onlyos.ErrNotExistshould count as absent. Other errors should stop the search and return to the caller.backupNamecan return that error torotate.rotatealready returnserror, so the call at line 209 can return it as well.🐛 Suggested fix
Add
errorsto the imports if it is not already present. Update thebackupNamecall inrotateto check the returned error.