-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
262 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package pogreb | ||
|
||
import ( | ||
"io" | ||
"os" | ||
|
||
"github.com/akrylysov/pogreb/fs" | ||
) | ||
|
||
func touchFile(fsys fs.FileSystem, path string) error { | ||
f, err := fsys.OpenFile(path, os.O_CREATE|os.O_TRUNC, os.FileMode(0640)) | ||
if err != nil { | ||
return err | ||
} | ||
return f.Close() | ||
} | ||
|
||
// Backup creates a database backup at the specified path. | ||
func (db *DB) Backup(path string) error { | ||
// Make sure the compaction is not running during backup. | ||
db.maintenanceMu.Lock() | ||
defer db.maintenanceMu.Unlock() | ||
|
||
if err := db.opts.rootFS.MkdirAll(path, 0755); err != nil { | ||
return err | ||
} | ||
|
||
db.mu.RLock() | ||
var segments []*segment | ||
activeSegmentSizes := make(map[uint16]int64) | ||
for _, seg := range db.datalog.segmentsBySequenceID() { | ||
segments = append(segments, seg) | ||
if !seg.meta.Full { | ||
// Save the size of the active segments to copy only the data persisted up to the point | ||
// of when the backup started. | ||
activeSegmentSizes[seg.id] = seg.size | ||
} | ||
} | ||
db.mu.RUnlock() | ||
|
||
srcFS := db.opts.FileSystem | ||
dstFS := fs.Sub(db.opts.rootFS, path) | ||
|
||
for _, seg := range segments { | ||
name := segmentName(seg.id, seg.sequenceID) | ||
mode := os.FileMode(0640) | ||
srcFile, err := srcFS.OpenFile(name, os.O_RDONLY, mode) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
dstFile, err := dstFS.OpenFile(name, os.O_CREATE|os.O_RDWR|os.O_TRUNC, mode) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if srcSize, ok := activeSegmentSizes[seg.id]; ok { | ||
if _, err := io.CopyN(dstFile, srcFile, srcSize); err != nil { | ||
return err | ||
} | ||
} else { | ||
if _, err := io.Copy(dstFile, srcFile); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
if err := srcFile.Close(); err != nil { | ||
return err | ||
} | ||
if err := dstFile.Close(); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
if err := touchFile(dstFS, lockName); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package pogreb | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/akrylysov/pogreb/internal/assert" | ||
) | ||
|
||
const testDBBackupName = testDBName + ".backup" | ||
|
||
func TestBackup(t *testing.T) { | ||
opts := &Options{ | ||
maxSegmentSize: 1024, | ||
compactionMinSegmentSize: 520, | ||
compactionMinFragmentation: 0.02, | ||
} | ||
|
||
run := func(name string, f func(t *testing.T, db *DB)) bool { | ||
return t.Run(name, func(t *testing.T) { | ||
db, err := createTestDB(opts) | ||
assert.Nil(t, err) | ||
f(t, db) | ||
assert.Nil(t, db.Close()) | ||
_ = cleanDir(testDBBackupName) | ||
}) | ||
} | ||
|
||
run("empty", func(t *testing.T, db *DB) { | ||
assert.Nil(t, db.Backup(testDBBackupName)) | ||
db2, err := Open(testDBBackupName, opts) | ||
assert.Nil(t, err) | ||
assert.Nil(t, db2.Close()) | ||
}) | ||
|
||
run("single segment", func(t *testing.T, db *DB) { | ||
assert.Nil(t, db.Put([]byte{0}, []byte{0})) | ||
assert.Equal(t, 1, countSegments(t, db)) | ||
assert.Nil(t, db.Backup(testDBBackupName)) | ||
db2, err := Open(testDBBackupName, opts) | ||
assert.Nil(t, err) | ||
v, err := db2.Get([]byte{0}) | ||
assert.Equal(t, []byte{0}, v) | ||
assert.Nil(t, err) | ||
assert.Nil(t, db2.Close()) | ||
}) | ||
|
||
run("multiple segments", func(t *testing.T, db *DB) { | ||
for i := byte(0); i < 100; i++ { | ||
assert.Nil(t, db.Put([]byte{i}, []byte{i})) | ||
} | ||
assert.Equal(t, 3, countSegments(t, db)) | ||
assert.Nil(t, db.Backup(testDBBackupName)) | ||
db2, err := Open(testDBBackupName, opts) | ||
assert.Equal(t, 3, countSegments(t, db2)) | ||
assert.Nil(t, err) | ||
for i := byte(0); i < 100; i++ { | ||
v, err := db2.Get([]byte{i}) | ||
assert.Nil(t, err) | ||
assert.Equal(t, []byte{i}, v) | ||
} | ||
assert.Nil(t, db2.Close()) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.