2
2
mirror of https://github.com/octoleo/restic.git synced 2024-11-29 16:23:59 +00:00

Remove tempdir for packerManager

This commit is contained in:
Alexander Neumann 2016-03-06 13:14:06 +01:00
parent 015cea0c50
commit cda7616c82
9 changed files with 27 additions and 69 deletions

View File

@ -26,10 +26,7 @@ func (cmd CmdInit) Execute(args []string) error {
"enter password again: ") "enter password again: ")
} }
s, err := repository.New(be) s := repository.New(be)
if err != nil {
return err
}
err = s.Init(cmd.global.password) err = s.Init(cmd.global.password)
if err != nil { if err != nil {

View File

@ -208,10 +208,7 @@ func (o GlobalOptions) OpenRepository() (*repository.Repository, error) {
return nil, err return nil, err
} }
s, err := repository.New(be) s := repository.New(be)
if err != nil {
return nil, err
}
if o.password == "" { if o.password == "" {
o.password = o.ReadPassword("enter password for repository: ") o.password = o.ReadPassword("enter password for repository: ")

View File

@ -78,10 +78,7 @@ func testArchiverDuplication(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
repo, err := repository.New(forgetfulBackend()) repo := repository.New(forgetfulBackend())
if err != nil {
t.Fatal(err)
}
err = repo.Init("foo") err = repo.Init("foo")
if err != nil { if err != nil {

View File

@ -239,8 +239,7 @@ func induceError(data []byte) {
func TestCheckerModifiedData(t *testing.T) { func TestCheckerModifiedData(t *testing.T) {
be := mem.New() be := mem.New()
repo, err := repository.New(be) repo := repository.New(be)
OK(t, err)
OK(t, repo.Init(TestPassword)) OK(t, repo.Init(TestPassword))
arch := restic.NewArchiver(repo) arch := restic.NewArchiver(repo)
@ -249,8 +248,7 @@ func TestCheckerModifiedData(t *testing.T) {
t.Logf("archived as %v", id.Str()) t.Logf("archived as %v", id.Str())
beError := &errorBackend{Backend: be} beError := &errorBackend{Backend: be}
checkRepo, err := repository.New(beError) checkRepo := repository.New(beError)
OK(t, err)
OK(t, checkRepo.SearchKey(TestPassword)) OK(t, checkRepo.SearchKey(TestPassword))
chkr := checker.New(checkRepo) chkr := checker.New(checkRepo)

View File

@ -25,7 +25,7 @@ type packerManager struct {
pm sync.Mutex pm sync.Mutex
packs []*pack.Packer packs []*pack.Packer
tempdir string pool sync.Pool
} }
const minPackSize = 4 * 1024 * 1024 const minPackSize = 4 * 1024 * 1024
@ -34,23 +34,21 @@ const maxPackers = 200
// NewPackerManager returns an new packer manager which writes temporary files // NewPackerManager returns an new packer manager which writes temporary files
// to a temporary directory // to a temporary directory
func NewPackerManager(be Saver, key *crypto.Key) (pm *packerManager, err error) { func NewPackerManager(be Saver, key *crypto.Key) *packerManager {
pm = &packerManager{ return &packerManager{
be: be, be: be,
key: key, key: key,
pool: sync.Pool{
New: func() interface{} {
return make([]byte, (minPackSize+maxPackSize)/2)
},
},
} }
pm.tempdir, err = ioutil.TempDir("", fmt.Sprintf("restic-packs-%d-", os.Getpid()))
if err != nil {
return nil, err
}
return pm, nil
} }
// findPacker returns a packer for a new blob of size bytes. Either a new one is // findPacker returns a packer for a new blob of size bytes. Either a new one is
// created or one is returned that already has some blobs. // created or one is returned that already has some blobs.
func (r *packerManager) findPacker(size uint) (*pack.Packer, error) { func (r *packerManager) findPacker(size uint) (packer *pack.Packer, err error) {
r.pm.Lock() r.pm.Lock()
defer r.pm.Unlock() defer r.pm.Unlock()
@ -69,7 +67,7 @@ func (r *packerManager) findPacker(size uint) (*pack.Packer, error) {
// no suitable packer found, return new // no suitable packer found, return new
debug.Log("Repo.findPacker", "create new pack for %d bytes", size) debug.Log("Repo.findPacker", "create new pack for %d bytes", size)
tmpfile, err := ioutil.TempFile(r.tempdir, "restic-pack-") tmpfile, err := ioutil.TempFile("", "restic-temp-pack-")
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -122,6 +120,11 @@ func (r *Repository) savePacker(p *pack.Packer) error {
debug.Log("Repo.savePacker", "saved as %v", h) debug.Log("Repo.savePacker", "saved as %v", h)
err = os.Remove(tmpfile.Name())
if err != nil {
return err
}
// update blobs in the index // update blobs in the index
for _, b := range p.Blobs() { for _, b := range p.Blobs() {
debug.Log("Repo.savePacker", " updating blob %v to pack %v", b.ID.Str(), id.Str()) debug.Log("Repo.savePacker", " updating blob %v to pack %v", b.ID.Str(), id.Str())
@ -144,10 +147,3 @@ func (r *packerManager) countPacker() int {
return len(r.packs) return len(r.packs)
} }
// removeTempdir deletes the temporary directory.
func (r *packerManager) removeTempdir() error {
err := os.RemoveAll(r.tempdir)
r.tempdir = ""
return err
}

View File

@ -120,10 +120,7 @@ func TestPackerManager(t *testing.T) {
rnd := rand.New(rand.NewSource(23)) rnd := rand.New(rand.NewSource(23))
be := mem.New() be := mem.New()
pm, err := NewPackerManager(be, crypto.NewRandomKey()) pm := NewPackerManager(be, crypto.NewRandomKey())
if err != nil {
t.Fatal(err)
}
blobBuf := make([]byte, maxBlobSize) blobBuf := make([]byte, maxBlobSize)
@ -131,20 +128,13 @@ func TestPackerManager(t *testing.T) {
bytes += flushRemainingPacks(t, rnd, be, pm) bytes += flushRemainingPacks(t, rnd, be, pm)
t.Logf("saved %d bytes", bytes) t.Logf("saved %d bytes", bytes)
err = pm.removeTempdir()
if err != nil {
t.Fatal(err)
}
} }
func BenchmarkPackerManager(t *testing.B) { func BenchmarkPackerManager(t *testing.B) {
rnd := rand.New(rand.NewSource(23)) rnd := rand.New(rand.NewSource(23))
be := &fakeBackend{} be := &fakeBackend{}
pm, err := NewPackerManager(be, crypto.NewRandomKey()) pm := NewPackerManager(be, crypto.NewRandomKey())
if err != nil {
t.Fatal(err)
}
blobBuf := make([]byte, maxBlobSize) blobBuf := make([]byte, maxBlobSize)
t.ResetTimer() t.ResetTimer()
@ -156,9 +146,4 @@ func BenchmarkPackerManager(t *testing.B) {
bytes += flushRemainingPacks(t, rnd, be, pm) bytes += flushRemainingPacks(t, rnd, be, pm)
t.Logf("saved %d bytes", bytes) t.Logf("saved %d bytes", bytes)
err = pm.removeTempdir()
if err != nil {
t.Fatal(err)
}
} }

View File

@ -27,19 +27,14 @@ type Repository struct {
} }
// New returns a new repository with backend be. // New returns a new repository with backend be.
func New(be backend.Backend) (*Repository, error) { func New(be backend.Backend) *Repository {
pm, err := NewPackerManager(be, nil)
if err != nil {
return nil, err
}
repo := &Repository{ repo := &Repository{
be: be, be: be,
idx: NewMasterIndex(), idx: NewMasterIndex(),
packerManager: pm, packerManager: NewPackerManager(be, nil),
} }
return repo, nil return repo
} }
// Find loads the list of all blobs of type t and searches for names which start // Find loads the list of all blobs of type t and searches for names which start
@ -304,7 +299,6 @@ func (r *Repository) Flush() error {
} }
} }
r.packs = r.packs[:0] r.packs = r.packs[:0]
return nil return nil
} }

View File

@ -61,10 +61,7 @@ func SetupRepo() *repository.Repository {
panic(err) panic(err)
} }
repo, err := repository.New(b) repo := repository.New(b)
if err != nil {
panic(err)
}
err = repo.Init(TestPassword) err = repo.Init(TestPassword)
if err != nil { if err != nil {
panic(err) panic(err)

View File

@ -213,10 +213,7 @@ func OpenLocalRepo(t testing.TB, dir string) *repository.Repository {
be, err := local.Open(dir) be, err := local.Open(dir)
OK(t, err) OK(t, err)
repo, err := repository.New(be) repo := repository.New(be)
if err != nil {
t.Fatal(err)
}
err = repo.SearchKey(TestPassword) err = repo.SearchKey(TestPassword)
OK(t, err) OK(t, err)