mirror of
https://github.com/restic/restic.git
synced 2026-11-23 13:52:39 +00:00
repository: change SaveBlobAsync to take a BlobBuffer
Ownership of the buffer transfers to SaveBlobAsync. Callers can now take a buffer from the buffer pool. Internally saveAndEncrypt now reuses pooled buffers.
This commit is contained in:
1 parent
4718be1aee
commit
30de2d9211
7 files changed
+40
-30
No files matched your search
@@ -493,7 +493,7 @@ func (repo *blobCountingSaver) SaveBlob(ctx context.Context, t restic.BlobType,
|
||||
return id, exists, size, err
|
||||
}
|
||||
|
||||
func (repo *blobCountingSaver) SaveBlobAsync(ctx context.Context, t restic.BlobType, buf []byte, id restic.ID, storeDuplicate bool, cb func(newID restic.ID, known bool, size int, err error)) {
|
||||
func (repo *blobCountingSaver) SaveBlobAsync(ctx context.Context, t restic.BlobType, buf *restic.BlobBuffer, id restic.ID, storeDuplicate bool, cb func(newID restic.ID, known bool, size int, err error)) {
|
||||
repo.saver.SaveBlobAsync(ctx, t, buf, id, storeDuplicate, func(newID restic.ID, known bool, size int, err error) {
|
||||
repo.count(known, restic.BlobHandle{ID: newID, Type: t})
|
||||
cb(newID, known, size, err)
|
||||
@@ -2350,7 +2350,7 @@ func (f *failSaveSaver) SaveBlob(ctx context.Context, t restic.BlobType, buf []b
|
||||
return f.saver.SaveBlob(ctx, t, buf, id, storeDuplicate)
|
||||
}
|
||||
|
||||
func (f *failSaveSaver) SaveBlobAsync(ctx context.Context, t restic.BlobType, buf []byte, id restic.ID, storeDuplicate bool, cb func(newID restic.ID, known bool, size int, err error)) {
|
||||
func (f *failSaveSaver) SaveBlobAsync(ctx context.Context, t restic.BlobType, buf *restic.BlobBuffer, id restic.ID, storeDuplicate bool, cb func(newID restic.ID, known bool, size int, err error)) {
|
||||
// limit concurrency to make test reliable
|
||||
f.semaphore <- struct{}{}
|
||||
|
||||
|
||||
@@ -256,7 +256,7 @@ func (s *fileSaver) saveFile(ctx context.Context, chnker restic.Chunker, chunkSt
|
||||
node.Content = append(node.Content, restic.ID{})
|
||||
lock.Unlock()
|
||||
|
||||
s.uploader.SaveBlobAsync(ctx, restic.DataBlob, chunkData, restic.ID{}, false, func(newID restic.ID, known bool, sizeInRepo int, err error) {
|
||||
s.uploader.SaveBlobAsync(ctx, restic.DataBlob, restic.NewBuffer(chunkData), restic.ID{}, false, func(newID restic.ID, known bool, sizeInRepo int, err error) {
|
||||
defer buf.Release()
|
||||
if err != nil {
|
||||
completeError(err)
|
||||
|
||||
@@ -137,7 +137,7 @@ func (s *treeSaver) save(ctx context.Context, job *saveTreeJob) (*data.Node, Ite
|
||||
)
|
||||
|
||||
ch := make(chan struct{}, 1)
|
||||
s.uploader.SaveBlobAsync(ctx, restic.TreeBlob, buf, restic.ID{}, false, func(newID restic.ID, cbKnown bool, cbSizeInRepo int, cbErr error) {
|
||||
s.uploader.SaveBlobAsync(ctx, restic.TreeBlob, restic.NewBuffer(buf), restic.ID{}, false, func(newID restic.ID, cbKnown bool, cbSizeInRepo int, cbErr error) {
|
||||
known = cbKnown
|
||||
length = len(buf)
|
||||
sizeInRepo = cbSizeInRepo
|
||||
|
||||
@@ -23,14 +23,14 @@ func (m *mockSaver) BlobBufferPool() *restic.BlobBufferPool {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *mockSaver) SaveBlobAsync(_ context.Context, _ restic.BlobType, buf []byte, id restic.ID, storeDuplicate bool, cb func(newID restic.ID, known bool, sizeInRepo int, err error)) {
|
||||
func (m *mockSaver) SaveBlobAsync(_ context.Context, _ restic.BlobType, buf *restic.BlobBuffer, id restic.ID, storeDuplicate bool, cb func(newID restic.ID, known bool, sizeInRepo int, err error)) {
|
||||
// Fake async operation
|
||||
go func() {
|
||||
m.mutex.Lock()
|
||||
m.saved[string(buf)]++
|
||||
m.saved[string(buf.Data)]++
|
||||
m.mutex.Unlock()
|
||||
|
||||
cb(restic.Hash(buf), false, len(buf), nil)
|
||||
cb(restic.Hash(buf.Data), false, len(buf.Data), nil)
|
||||
}()
|
||||
}
|
||||
|
||||
|
||||
@@ -383,10 +383,12 @@ func (r *Repository) getZstdDecoder() *zstd.Decoder {
|
||||
// is small enough, it will be packed together with other small blobs. The
|
||||
// caller must ensure that the id matches the data. Returned is the size data
|
||||
// occupies in the repo (compressed or not, including the encryption overhead).
|
||||
func (r *Repository) saveAndEncrypt(ctx context.Context, t restic.BlobType, data []byte, id restic.ID) (size int, err error) {
|
||||
debug.Log("save id %v (%v, %d bytes)", id, t, len(data))
|
||||
// plaintext is consumed and released before this function returns.
|
||||
func (r *Repository) saveAndEncrypt(ctx context.Context, t restic.BlobType, plaintext *restic.BlobBuffer, id restic.ID) (size int, err error) {
|
||||
debug.Log("save id %v (%v, %d bytes)", id, t, len(plaintext.Data))
|
||||
|
||||
uncompressedLength := 0
|
||||
scratch := plaintext
|
||||
if r.cfg.Version > 1 {
|
||||
// we have a repo v2, so compression is available. if the user opts to
|
||||
// not compress, we won't compress any data, but everything else is
|
||||
@@ -394,21 +396,24 @@ func (r *Repository) saveAndEncrypt(ctx context.Context, t restic.BlobType, data
|
||||
// uncompressedLength != 0 is used to indicate compressed data. Thus, a zero-sized blob
|
||||
// cannot be compressed. This special case is only relevant for tests, normal operation does not
|
||||
// generate zero-sized blobs.
|
||||
if len(data) > 0 && (r.opts.Compression != CompressionOff || t != restic.DataBlob) {
|
||||
uncompressedLength = len(data)
|
||||
data = r.getZstdEncoder().EncodeAll(data, nil)
|
||||
if len(plaintext.Data) > 0 && (r.opts.Compression != CompressionOff || t != restic.DataBlob) {
|
||||
uncompressedLength = len(plaintext.Data)
|
||||
scratch = r.blobBuffers.Get()
|
||||
scratch.Data = r.getZstdEncoder().EncodeAll(plaintext.Data, scratch.Data[:0])
|
||||
plaintext.Release()
|
||||
}
|
||||
}
|
||||
|
||||
cipherBuf := r.blobBuffers.Get()
|
||||
defer cipherBuf.Release() // last buffer in this method such that we can use defer
|
||||
nonce := crypto.NewRandomNonce()
|
||||
|
||||
ciphertext := make([]byte, 0, crypto.CiphertextLength(len(data)))
|
||||
ciphertext = append(ciphertext, nonce...)
|
||||
cipherBuf.Data = append(cipherBuf.Data, nonce...)
|
||||
|
||||
// encrypt blob
|
||||
ciphertext = r.key.Seal(ciphertext, nonce, data, nil)
|
||||
cipherBuf.Data = r.key.Seal(cipherBuf.Data, nonce, scratch.Data, nil)
|
||||
scratch.Release()
|
||||
|
||||
if err := r.verifyCiphertext(ciphertext, uncompressedLength, id); err != nil {
|
||||
if err := r.verifyCiphertext(cipherBuf.Data, uncompressedLength, id); err != nil {
|
||||
//nolint:revive,staticcheck // ignore linter warnings about error message spelling
|
||||
return 0, fmt.Errorf("Detected data corruption while saving blob %v: %w\nCorrupted blobs are either caused by hardware issues or software bugs. Please open an issue at https://github.com/restic/restic/issues/new/choose for further troubleshooting.", id, err)
|
||||
}
|
||||
@@ -425,7 +430,8 @@ func (r *Repository) saveAndEncrypt(ctx context.Context, t restic.BlobType, data
|
||||
panic(fmt.Sprintf("invalid type: %v", t))
|
||||
}
|
||||
|
||||
return pm.SaveBlob(ctx, t, id, ciphertext, uncompressedLength)
|
||||
size, err = pm.SaveBlob(ctx, t, id, cipherBuf.Data, uncompressedLength)
|
||||
return size, err
|
||||
}
|
||||
|
||||
func (r *Repository) verifyCiphertext(buf []byte, uncompressedLength int, id restic.ID) error {
|
||||
@@ -637,10 +643,10 @@ func (r *blobSaverRepo) BlobBufferPool() *restic.BlobBufferPool {
|
||||
}
|
||||
|
||||
func (r *blobSaverRepo) SaveBlob(ctx context.Context, t restic.BlobType, buf []byte, id restic.ID, storeDuplicate bool) (newID restic.ID, known bool, size int, err error) {
|
||||
return r.repo.saveBlob(ctx, t, buf, id, storeDuplicate)
|
||||
return r.repo.saveBlob(ctx, t, restic.NewBuffer(buf), id, storeDuplicate)
|
||||
}
|
||||
|
||||
func (r *blobSaverRepo) SaveBlobAsync(ctx context.Context, t restic.BlobType, buf []byte, id restic.ID, storeDuplicate bool, cb func(newID restic.ID, known bool, size int, err error)) {
|
||||
func (r *blobSaverRepo) SaveBlobAsync(ctx context.Context, t restic.BlobType, buf *restic.BlobBuffer, id restic.ID, storeDuplicate bool, cb func(newID restic.ID, known bool, size int, err error)) {
|
||||
r.repo.saveBlobAsync(ctx, t, buf, id, storeDuplicate, cb)
|
||||
}
|
||||
|
||||
@@ -1033,9 +1039,10 @@ func (r *Repository) Close() error {
|
||||
// Also returns if the blob was already known before.
|
||||
// If the blob was not known before, it returns the number of bytes the blob
|
||||
// occupies in the repo (compressed or not, including encryption overhead).
|
||||
func (r *Repository) saveBlob(ctx context.Context, t restic.BlobType, buf []byte, id restic.ID, storeDuplicate bool) (newID restic.ID, known bool, size int, err error) {
|
||||
func (r *Repository) saveBlob(ctx context.Context, t restic.BlobType, buf *restic.BlobBuffer, id restic.ID, storeDuplicate bool) (newID restic.ID, known bool, size int, err error) {
|
||||
|
||||
if int64(len(buf)) > math.MaxUint32 {
|
||||
if int64(len(buf.Data)) > math.MaxUint32 {
|
||||
buf.Release()
|
||||
return restic.ID{}, false, 0, fmt.Errorf("blob is larger than 4GB")
|
||||
}
|
||||
|
||||
@@ -1044,30 +1051,33 @@ func (r *Repository) saveBlob(ctx context.Context, t restic.BlobType, buf []byte
|
||||
// Special case the hash calculation for all zero chunks. This is especially
|
||||
// useful for sparse files containing large all zero regions. For these we can
|
||||
// process chunks as fast as we can read the from disk.
|
||||
if len(buf) == chunker.MinSize && restic.ZeroPrefixLen(buf) == chunker.MinSize {
|
||||
if len(buf.Data) == chunker.MinSize && restic.ZeroPrefixLen(buf.Data) == chunker.MinSize {
|
||||
newID = r.zeroChunk()
|
||||
} else {
|
||||
newID = restic.Hash(buf)
|
||||
newID = restic.Hash(buf.Data)
|
||||
}
|
||||
} else {
|
||||
newID = id
|
||||
}
|
||||
|
||||
// first try to add to pending blobs; if not successful, this blob is already known
|
||||
known = !r.idx.AddPending(restic.BlobHandle{ID: newID, Type: t}, uint(len(buf)))
|
||||
known = !r.idx.AddPending(restic.BlobHandle{ID: newID, Type: t}, uint(len(buf.Data)))
|
||||
|
||||
// only save when needed or explicitly told
|
||||
if !known || storeDuplicate {
|
||||
size, err = r.saveAndEncrypt(ctx, t, buf, newID)
|
||||
} else {
|
||||
buf.Release()
|
||||
}
|
||||
|
||||
return newID, known, size, err
|
||||
}
|
||||
|
||||
func (r *Repository) saveBlobAsync(ctx context.Context, t restic.BlobType, buf []byte, id restic.ID, storeDuplicate bool, cb func(newID restic.ID, known bool, size int, err error)) {
|
||||
func (r *Repository) saveBlobAsync(ctx context.Context, t restic.BlobType, buf *restic.BlobBuffer, id restic.ID, storeDuplicate bool, cb func(newID restic.ID, known bool, size int, err error)) {
|
||||
r.mainWg.Go(func() error {
|
||||
if ctx.Err() != nil {
|
||||
// fail fast if the context is cancelled
|
||||
buf.Release()
|
||||
cb(restic.ID{}, false, 0, ctx.Err())
|
||||
return ctx.Err()
|
||||
}
|
||||
|
||||
@@ -535,7 +535,7 @@ func TestSaveBlobAsync(t *testing.T) {
|
||||
for i := range numCalls {
|
||||
// Use unique data for each call
|
||||
testData := fmt.Appendf(nil, "test blob data %d", i)
|
||||
uploader.SaveBlobAsync(ctx, restic.DataBlob, testData, restic.ID{}, false,
|
||||
uploader.SaveBlobAsync(ctx, restic.DataBlob, restic.NewBuffer(testData), restic.ID{}, false,
|
||||
func(newID restic.ID, known bool, size int, err error) {
|
||||
defer wg.Done()
|
||||
resultsMutex.Lock()
|
||||
@@ -566,7 +566,7 @@ func TestSaveBlobAsyncErrorHandling(t *testing.T) {
|
||||
err := repo.WithBlobUploader(ctx, func(ctx context.Context, uploader restic.BlobSaverWithAsync) error {
|
||||
cancel()
|
||||
// Callback must be called even if the context is canceled
|
||||
uploader.SaveBlobAsync(ctx, restic.DataBlob, []byte("test blob data"), restic.ID{}, false,
|
||||
uploader.SaveBlobAsync(ctx, restic.DataBlob, restic.NewBuffer([]byte("test blob data")), restic.ID{}, false,
|
||||
func(newID restic.ID, known bool, size int, err error) {
|
||||
callbackCalled.Store(true)
|
||||
})
|
||||
|
||||
@@ -133,8 +133,8 @@ type BlobSaver interface {
|
||||
type BlobSaverAsync interface {
|
||||
BlobBufferPool() *BlobBufferPool
|
||||
// SaveBlobAsync saves a blob to the repository. ctx must be derived from the context created by WithBlobUploader.
|
||||
// The callback is called asynchronously from a different goroutine.
|
||||
SaveBlobAsync(ctx context.Context, tpe BlobType, buf []byte, id ID, storeDuplicate bool, cb func(newID ID, known bool, sizeInRepo int, err error))
|
||||
// The callback is called asynchronously from a different goroutine. The buffer will always be released before the callback is called.
|
||||
SaveBlobAsync(ctx context.Context, tpe BlobType, buf *BlobBuffer, id ID, storeDuplicate bool, cb func(newID ID, known bool, sizeInRepo int, err error))
|
||||
}
|
||||
|
||||
// Loader loads a blob from a repository.
|
||||
|
||||
Reference in new issue
Block a user