From f538cba2728ae52ff58a8b7825f97f01b8d797db Mon Sep 17 00:00:00 2001 From: Xiang Li Date: Fri, 12 Dec 2014 14:38:18 -0800 Subject: [PATCH] *: do not backup files still in use --- etcdctl/command/backup_command.go | 2 +- wal/wal.go | 16 +++++++++++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/etcdctl/command/backup_command.go b/etcdctl/command/backup_command.go index a970f57ab..778cbcc44 100644 --- a/etcdctl/command/backup_command.go +++ b/etcdctl/command/backup_command.go @@ -67,7 +67,7 @@ func handleBackup(c *cli.Context) { } } - w, err := wal.OpenAtIndex(srcWAL, index) + w, err := wal.OpenAtIndexUntilUsing(srcWAL, index) if err != nil { log.Fatal(err) } diff --git a/wal/wal.go b/wal/wal.go index 5f56934d8..36480b0b2 100644 --- a/wal/wal.go +++ b/wal/wal.go @@ -21,6 +21,7 @@ import ( "fmt" "hash/crc32" "io" + "log" "os" "path" "reflect" @@ -123,6 +124,14 @@ func Create(dirpath string, metadata []byte) (*WAL, error) { // index. The WAL cannot be appended to before reading out all of its // previous records. func OpenAtIndex(dirpath string, index uint64) (*WAL, error) { + return openAtIndex(dirpath, index, true) +} + +func OpenAtIndexUntilUsing(dirpath string, index uint64) (*WAL, error) { + return openAtIndex(dirpath, index, false) +} + +func openAtIndex(dirpath string, index uint64, all bool) (*WAL, error) { names, err := fileutil.ReadDir(dirpath) if err != nil { return nil, err @@ -153,7 +162,12 @@ func OpenAtIndex(dirpath string, index uint64) (*WAL, error) { } err = l.TryLock() if err != nil { - return nil, err + if all { + return nil, err + } else { + log.Printf("wal: opened all the files until %s, since it is still in use by an etcd server", name) + break + } } rcs = append(rcs, f) ls = append(ls, l)