2016-08-31 20:39:36 +00:00
|
|
|
package archiver_test
|
2014-11-16 20:41:05 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"io"
|
|
|
|
"testing"
|
2015-12-09 20:09:49 +00:00
|
|
|
"time"
|
2014-11-16 20:41:05 +00:00
|
|
|
|
2016-02-14 14:29:28 +00:00
|
|
|
"restic"
|
2016-08-31 21:07:50 +00:00
|
|
|
"restic/archiver"
|
2016-02-14 14:29:28 +00:00
|
|
|
"restic/checker"
|
|
|
|
"restic/crypto"
|
2016-09-04 11:24:51 +00:00
|
|
|
"restic/repository"
|
2016-02-14 14:29:28 +00:00
|
|
|
. "restic/test"
|
2016-02-22 20:09:21 +00:00
|
|
|
|
2016-09-01 20:17:37 +00:00
|
|
|
"restic/errors"
|
2016-09-03 11:34:04 +00:00
|
|
|
|
|
|
|
"github.com/restic/chunker"
|
2014-11-16 20:41:05 +00:00
|
|
|
)
|
|
|
|
|
2015-04-05 22:22:19 +00:00
|
|
|
var testPol = chunker.Pol(0x3DA3358B4DC173)
|
2015-02-10 20:59:18 +00:00
|
|
|
|
2015-02-17 19:02:43 +00:00
|
|
|
type Rdr interface {
|
2015-02-09 22:39:16 +00:00
|
|
|
io.ReadSeeker
|
|
|
|
io.ReaderAt
|
2015-02-17 19:02:43 +00:00
|
|
|
}
|
2015-02-09 22:39:16 +00:00
|
|
|
|
2015-05-04 22:45:29 +00:00
|
|
|
func benchmarkChunkEncrypt(b testing.TB, buf, buf2 []byte, rd Rdr, key *crypto.Key) {
|
2015-02-09 22:39:16 +00:00
|
|
|
rd.Seek(0, 0)
|
2016-02-22 20:09:21 +00:00
|
|
|
ch := chunker.New(rd, testPol)
|
2015-02-09 22:39:16 +00:00
|
|
|
|
|
|
|
for {
|
2016-02-22 20:09:21 +00:00
|
|
|
chunk, err := ch.Next(buf)
|
2015-02-09 22:39:16 +00:00
|
|
|
|
2016-08-29 17:18:57 +00:00
|
|
|
if errors.Cause(err) == io.EOF {
|
2015-02-09 22:39:16 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2015-04-09 19:15:48 +00:00
|
|
|
OK(b, err)
|
2015-02-09 22:39:16 +00:00
|
|
|
|
2015-02-17 19:02:43 +00:00
|
|
|
// reduce length of buf
|
2016-02-22 20:09:21 +00:00
|
|
|
Assert(b, uint(len(chunk.Data)) == chunk.Length,
|
|
|
|
"invalid length: got %d, expected %d", len(chunk.Data), chunk.Length)
|
2015-02-09 22:39:16 +00:00
|
|
|
|
2016-02-22 20:09:21 +00:00
|
|
|
_, err = crypto.Encrypt(key, buf2, chunk.Data)
|
2015-04-09 19:15:48 +00:00
|
|
|
OK(b, err)
|
2015-02-09 22:39:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-16 20:41:05 +00:00
|
|
|
func BenchmarkChunkEncrypt(b *testing.B) {
|
2016-09-04 11:24:51 +00:00
|
|
|
repo, cleanup := repository.TestRepository(b)
|
2016-09-04 10:52:43 +00:00
|
|
|
defer cleanup()
|
2015-06-26 20:12:04 +00:00
|
|
|
|
2015-04-12 07:36:14 +00:00
|
|
|
data := Random(23, 10<<20) // 10MiB
|
2015-02-08 21:54:45 +00:00
|
|
|
rd := bytes.NewReader(data)
|
2014-11-16 20:41:05 +00:00
|
|
|
|
2015-05-04 22:14:07 +00:00
|
|
|
buf := make([]byte, chunker.MaxSize)
|
|
|
|
buf2 := make([]byte, chunker.MaxSize)
|
2015-02-17 19:02:43 +00:00
|
|
|
|
2014-11-16 20:41:05 +00:00
|
|
|
b.ResetTimer()
|
|
|
|
b.SetBytes(int64(len(data)))
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
2015-06-26 20:12:04 +00:00
|
|
|
benchmarkChunkEncrypt(b, buf, buf2, rd, repo.Key())
|
2015-02-09 22:39:16 +00:00
|
|
|
}
|
2015-02-17 19:02:43 +00:00
|
|
|
}
|
2015-02-09 22:39:16 +00:00
|
|
|
|
2015-05-04 22:45:29 +00:00
|
|
|
func benchmarkChunkEncryptP(b *testing.PB, buf []byte, rd Rdr, key *crypto.Key) {
|
2016-02-22 20:09:21 +00:00
|
|
|
ch := chunker.New(rd, testPol)
|
2014-11-16 20:41:05 +00:00
|
|
|
|
2015-02-09 22:39:16 +00:00
|
|
|
for {
|
2016-02-22 20:09:21 +00:00
|
|
|
chunk, err := ch.Next(buf)
|
2016-08-29 17:18:57 +00:00
|
|
|
if errors.Cause(err) == io.EOF {
|
2015-02-09 22:39:16 +00:00
|
|
|
break
|
|
|
|
}
|
2014-11-16 20:41:05 +00:00
|
|
|
|
2015-02-09 22:39:16 +00:00
|
|
|
// reduce length of chunkBuf
|
2016-02-22 20:09:21 +00:00
|
|
|
crypto.Encrypt(key, chunk.Data, chunk.Data)
|
2015-02-09 22:39:16 +00:00
|
|
|
}
|
|
|
|
}
|
2015-02-08 21:54:45 +00:00
|
|
|
|
2015-02-09 22:39:16 +00:00
|
|
|
func BenchmarkChunkEncryptParallel(b *testing.B) {
|
2016-09-04 11:24:51 +00:00
|
|
|
repo, cleanup := repository.TestRepository(b)
|
2016-09-04 10:52:43 +00:00
|
|
|
defer cleanup()
|
2015-02-09 22:39:16 +00:00
|
|
|
|
2015-04-12 07:36:14 +00:00
|
|
|
data := Random(23, 10<<20) // 10MiB
|
2015-02-09 22:39:16 +00:00
|
|
|
|
2015-05-04 22:14:07 +00:00
|
|
|
buf := make([]byte, chunker.MaxSize)
|
2015-02-17 19:02:43 +00:00
|
|
|
|
2015-02-09 22:39:16 +00:00
|
|
|
b.ResetTimer()
|
|
|
|
b.SetBytes(int64(len(data)))
|
|
|
|
|
|
|
|
b.RunParallel(func(pb *testing.PB) {
|
|
|
|
for pb.Next() {
|
|
|
|
rd := bytes.NewReader(data)
|
2015-06-26 20:12:04 +00:00
|
|
|
benchmarkChunkEncryptP(pb, buf, rd, repo.Key())
|
2014-11-16 20:41:05 +00:00
|
|
|
}
|
2015-02-09 22:39:16 +00:00
|
|
|
})
|
2014-11-16 20:41:05 +00:00
|
|
|
}
|
2015-02-10 20:59:18 +00:00
|
|
|
|
2015-04-26 15:44:38 +00:00
|
|
|
func archiveDirectory(b testing.TB) {
|
2016-09-04 11:24:51 +00:00
|
|
|
repo, cleanup := repository.TestRepository(b)
|
2016-09-04 10:52:43 +00:00
|
|
|
defer cleanup()
|
2015-02-10 20:59:18 +00:00
|
|
|
|
2016-08-31 21:07:50 +00:00
|
|
|
arch := archiver.New(repo)
|
2015-02-10 20:59:18 +00:00
|
|
|
|
2016-09-13 18:13:04 +00:00
|
|
|
_, id, err := arch.Snapshot(nil, []string{BenchArchiveDirectory}, nil, nil)
|
2015-04-30 01:41:51 +00:00
|
|
|
OK(b, err)
|
2015-02-10 20:59:18 +00:00
|
|
|
|
|
|
|
b.Logf("snapshot archived as %v", id)
|
|
|
|
}
|
2015-02-17 21:39:44 +00:00
|
|
|
|
2015-04-26 15:44:38 +00:00
|
|
|
func TestArchiveDirectory(t *testing.T) {
|
2015-06-28 11:15:35 +00:00
|
|
|
if BenchArchiveDirectory == "" {
|
2015-04-26 15:44:38 +00:00
|
|
|
t.Skip("benchdir not set, skipping TestArchiveDirectory")
|
|
|
|
}
|
|
|
|
|
|
|
|
archiveDirectory(t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkArchiveDirectory(b *testing.B) {
|
2015-06-28 11:15:35 +00:00
|
|
|
if BenchArchiveDirectory == "" {
|
2015-04-26 15:44:38 +00:00
|
|
|
b.Skip("benchdir not set, skipping BenchmarkArchiveDirectory")
|
|
|
|
}
|
|
|
|
|
2015-05-01 20:58:50 +00:00
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
archiveDirectory(b)
|
|
|
|
}
|
2015-02-17 21:39:44 +00:00
|
|
|
}
|
|
|
|
|
2016-09-03 11:34:04 +00:00
|
|
|
func countPacks(repo restic.Repository, t restic.FileType) (n uint) {
|
|
|
|
for _ = range repo.Backend().List(t, nil) {
|
|
|
|
n++
|
|
|
|
}
|
|
|
|
|
|
|
|
return n
|
|
|
|
}
|
|
|
|
|
2015-04-26 15:44:38 +00:00
|
|
|
func archiveWithDedup(t testing.TB) {
|
2016-09-04 11:24:51 +00:00
|
|
|
repo, cleanup := repository.TestRepository(t)
|
2016-09-04 10:52:43 +00:00
|
|
|
defer cleanup()
|
2015-06-26 20:12:04 +00:00
|
|
|
|
2015-06-28 11:15:35 +00:00
|
|
|
if BenchArchiveDirectory == "" {
|
2015-04-26 15:44:38 +00:00
|
|
|
t.Skip("benchdir not set, skipping TestArchiverDedup")
|
2015-02-17 22:40:37 +00:00
|
|
|
}
|
|
|
|
|
2015-04-26 15:44:38 +00:00
|
|
|
var cnt struct {
|
|
|
|
before, after, after2 struct {
|
|
|
|
packs, dataBlobs, treeBlobs uint
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-17 21:39:44 +00:00
|
|
|
// archive a few files
|
2016-09-04 10:52:43 +00:00
|
|
|
sn := archiver.TestSnapshot(t, repo, BenchArchiveDirectory, nil)
|
2015-03-09 21:58:17 +00:00
|
|
|
t.Logf("archived snapshot %v", sn.ID().Str())
|
2015-02-17 21:39:44 +00:00
|
|
|
|
|
|
|
// get archive stats
|
2016-09-03 11:34:04 +00:00
|
|
|
cnt.before.packs = countPacks(repo, restic.DataFile)
|
2016-08-31 20:39:36 +00:00
|
|
|
cnt.before.dataBlobs = repo.Index().Count(restic.DataBlob)
|
|
|
|
cnt.before.treeBlobs = repo.Index().Count(restic.TreeBlob)
|
2015-04-26 15:44:38 +00:00
|
|
|
t.Logf("packs %v, data blobs %v, tree blobs %v",
|
|
|
|
cnt.before.packs, cnt.before.dataBlobs, cnt.before.treeBlobs)
|
2015-02-17 21:39:44 +00:00
|
|
|
|
2015-03-09 21:58:17 +00:00
|
|
|
// archive the same files again, without parent snapshot
|
2016-09-04 10:52:43 +00:00
|
|
|
sn2 := archiver.TestSnapshot(t, repo, BenchArchiveDirectory, nil)
|
2015-03-09 21:58:17 +00:00
|
|
|
t.Logf("archived snapshot %v", sn2.ID().Str())
|
2015-02-17 21:39:44 +00:00
|
|
|
|
2015-04-26 15:44:38 +00:00
|
|
|
// get archive stats again
|
2016-09-03 11:34:04 +00:00
|
|
|
cnt.after.packs = countPacks(repo, restic.DataFile)
|
2016-08-31 20:39:36 +00:00
|
|
|
cnt.after.dataBlobs = repo.Index().Count(restic.DataBlob)
|
|
|
|
cnt.after.treeBlobs = repo.Index().Count(restic.TreeBlob)
|
2015-04-26 15:44:38 +00:00
|
|
|
t.Logf("packs %v, data blobs %v, tree blobs %v",
|
|
|
|
cnt.after.packs, cnt.after.dataBlobs, cnt.after.treeBlobs)
|
|
|
|
|
2015-05-04 20:11:53 +00:00
|
|
|
// if there are more data blobs, something is wrong
|
2015-04-26 15:44:38 +00:00
|
|
|
if cnt.after.dataBlobs > cnt.before.dataBlobs {
|
|
|
|
t.Fatalf("TestArchiverDedup: too many data blobs in repository: before %d, after %d",
|
|
|
|
cnt.before.dataBlobs, cnt.after.dataBlobs)
|
|
|
|
}
|
2015-03-09 21:58:17 +00:00
|
|
|
|
|
|
|
// archive the same files again, with a parent snapshot
|
2016-09-04 10:52:43 +00:00
|
|
|
sn3 := archiver.TestSnapshot(t, repo, BenchArchiveDirectory, sn2.ID())
|
2015-03-09 21:58:17 +00:00
|
|
|
t.Logf("archived snapshot %v, parent %v", sn3.ID().Str(), sn2.ID().Str())
|
|
|
|
|
2015-04-26 15:44:38 +00:00
|
|
|
// get archive stats again
|
2016-09-03 11:34:04 +00:00
|
|
|
cnt.after2.packs = countPacks(repo, restic.DataFile)
|
2016-08-31 20:39:36 +00:00
|
|
|
cnt.after2.dataBlobs = repo.Index().Count(restic.DataBlob)
|
|
|
|
cnt.after2.treeBlobs = repo.Index().Count(restic.TreeBlob)
|
2015-04-26 15:44:38 +00:00
|
|
|
t.Logf("packs %v, data blobs %v, tree blobs %v",
|
|
|
|
cnt.after2.packs, cnt.after2.dataBlobs, cnt.after2.treeBlobs)
|
|
|
|
|
2015-05-04 20:11:53 +00:00
|
|
|
// if there are more data blobs, something is wrong
|
2015-04-26 15:44:38 +00:00
|
|
|
if cnt.after2.dataBlobs > cnt.before.dataBlobs {
|
|
|
|
t.Fatalf("TestArchiverDedup: too many data blobs in repository: before %d, after %d",
|
|
|
|
cnt.before.dataBlobs, cnt.after2.dataBlobs)
|
2015-02-18 21:46:09 +00:00
|
|
|
}
|
2015-02-17 21:39:44 +00:00
|
|
|
}
|
2015-02-21 23:09:57 +00:00
|
|
|
|
2015-04-26 15:44:38 +00:00
|
|
|
func TestArchiveDedup(t *testing.T) {
|
|
|
|
archiveWithDedup(t)
|
|
|
|
}
|
|
|
|
|
2015-12-09 20:09:49 +00:00
|
|
|
// Saves several identical chunks concurrently and later checks that there are no
|
2015-12-06 16:35:22 +00:00
|
|
|
// unreferenced packs in the repository. See also #292 and #358.
|
2015-12-09 20:09:49 +00:00
|
|
|
func TestParallelSaveWithDuplication(t *testing.T) {
|
2015-12-09 20:38:03 +00:00
|
|
|
for seed := 0; seed < 10; seed++ {
|
2015-12-09 20:09:49 +00:00
|
|
|
testParallelSaveWithDuplication(t, seed)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testParallelSaveWithDuplication(t *testing.T, seed int) {
|
2016-09-04 11:24:51 +00:00
|
|
|
repo, cleanup := repository.TestRepository(t)
|
2016-09-04 10:52:43 +00:00
|
|
|
defer cleanup()
|
2015-12-06 16:35:22 +00:00
|
|
|
|
2015-12-09 20:38:03 +00:00
|
|
|
dataSizeMb := 128
|
2015-12-09 20:09:49 +00:00
|
|
|
duplication := 7
|
2015-12-06 16:35:22 +00:00
|
|
|
|
2016-08-31 21:07:50 +00:00
|
|
|
arch := archiver.New(repo)
|
2016-02-22 20:09:21 +00:00
|
|
|
chunks := getRandomData(seed, dataSizeMb*1024*1024)
|
2015-12-06 16:35:22 +00:00
|
|
|
|
|
|
|
errChannels := [](<-chan error){}
|
|
|
|
|
2015-12-09 20:09:49 +00:00
|
|
|
// interweaved processing of subsequent chunks
|
|
|
|
maxParallel := 2*duplication - 1
|
|
|
|
barrier := make(chan struct{}, maxParallel)
|
2015-12-06 16:35:22 +00:00
|
|
|
|
2015-12-09 20:09:49 +00:00
|
|
|
for _, c := range chunks {
|
|
|
|
for dupIdx := 0; dupIdx < duplication; dupIdx++ {
|
|
|
|
errChan := make(chan error)
|
|
|
|
errChannels = append(errChannels, errChan)
|
2015-12-06 16:35:22 +00:00
|
|
|
|
2016-02-22 20:09:21 +00:00
|
|
|
go func(c chunker.Chunk, errChan chan<- error) {
|
2015-12-09 20:09:49 +00:00
|
|
|
barrier <- struct{}{}
|
2015-12-06 16:35:22 +00:00
|
|
|
|
2016-08-31 21:07:50 +00:00
|
|
|
id := restic.Hash(c.Data)
|
2016-02-22 20:09:21 +00:00
|
|
|
time.Sleep(time.Duration(id[0]))
|
2016-08-31 20:39:36 +00:00
|
|
|
err := arch.Save(restic.DataBlob, c.Data, id)
|
2015-12-09 20:09:49 +00:00
|
|
|
<-barrier
|
|
|
|
errChan <- err
|
2016-02-22 20:09:21 +00:00
|
|
|
}(c, errChan)
|
2015-12-06 16:35:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, errChan := range errChannels {
|
|
|
|
OK(t, <-errChan)
|
|
|
|
}
|
|
|
|
|
|
|
|
OK(t, repo.Flush())
|
|
|
|
OK(t, repo.SaveIndex())
|
|
|
|
|
|
|
|
chkr := createAndInitChecker(t, repo)
|
|
|
|
assertNoUnreferencedPacks(t, chkr)
|
|
|
|
}
|
|
|
|
|
2016-02-22 20:09:21 +00:00
|
|
|
func getRandomData(seed int, size int) []chunker.Chunk {
|
2015-12-09 20:09:49 +00:00
|
|
|
buf := Random(seed, size)
|
2016-02-22 20:09:21 +00:00
|
|
|
var chunks []chunker.Chunk
|
|
|
|
chunker := chunker.New(bytes.NewReader(buf), testPol)
|
2015-12-06 16:35:22 +00:00
|
|
|
|
2015-12-09 20:09:49 +00:00
|
|
|
for {
|
2016-02-22 20:09:21 +00:00
|
|
|
c, err := chunker.Next(nil)
|
2016-08-29 17:18:57 +00:00
|
|
|
if errors.Cause(err) == io.EOF {
|
2015-12-09 20:09:49 +00:00
|
|
|
break
|
|
|
|
}
|
2015-12-06 16:35:22 +00:00
|
|
|
chunks = append(chunks, c)
|
|
|
|
}
|
|
|
|
|
2016-02-22 20:09:21 +00:00
|
|
|
return chunks
|
2015-12-06 16:35:22 +00:00
|
|
|
}
|
|
|
|
|
2016-08-31 21:07:50 +00:00
|
|
|
func createAndInitChecker(t *testing.T, repo restic.Repository) *checker.Checker {
|
2015-12-06 16:35:22 +00:00
|
|
|
chkr := checker.New(repo)
|
|
|
|
|
|
|
|
hints, errs := chkr.LoadIndex()
|
|
|
|
if len(errs) > 0 {
|
|
|
|
t.Fatalf("expected no errors, got %v: %v", len(errs), errs)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(hints) > 0 {
|
|
|
|
t.Errorf("expected no hints, got %v: %v", len(hints), hints)
|
|
|
|
}
|
|
|
|
|
|
|
|
return chkr
|
|
|
|
}
|
|
|
|
|
|
|
|
func assertNoUnreferencedPacks(t *testing.T, chkr *checker.Checker) {
|
|
|
|
done := make(chan struct{})
|
|
|
|
defer close(done)
|
|
|
|
|
|
|
|
errChan := make(chan error)
|
|
|
|
go chkr.Packs(errChan, done)
|
|
|
|
|
|
|
|
for err := range errChan {
|
|
|
|
OK(t, err)
|
|
|
|
}
|
|
|
|
}
|