2018-04-30 13:13:03 +00:00
|
|
|
package archiver
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/restic/restic/internal/debug"
|
|
|
|
"github.com/restic/restic/internal/restic"
|
2022-05-27 17:08:50 +00:00
|
|
|
"golang.org/x/sync/errgroup"
|
2018-04-30 13:13:03 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// TreeSaver concurrently saves incoming trees to the repo.
|
|
|
|
type TreeSaver struct {
|
2022-08-19 21:08:13 +00:00
|
|
|
saveBlob func(ctx context.Context, t restic.BlobType, buf *Buffer) FutureBlob
|
2018-04-30 13:13:03 +00:00
|
|
|
errFn ErrorFunc
|
|
|
|
|
2020-04-13 14:20:59 +00:00
|
|
|
ch chan<- saveTreeJob
|
2018-04-30 13:13:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewTreeSaver returns a new tree saver. A worker pool with treeWorkers is
|
|
|
|
// started, it is stopped when ctx is cancelled.
|
2022-08-19 21:08:13 +00:00
|
|
|
func NewTreeSaver(ctx context.Context, wg *errgroup.Group, treeWorkers uint, saveBlob func(ctx context.Context, t restic.BlobType, buf *Buffer) FutureBlob, errFn ErrorFunc) *TreeSaver {
|
2018-04-30 13:13:03 +00:00
|
|
|
ch := make(chan saveTreeJob)
|
|
|
|
|
|
|
|
s := &TreeSaver{
|
|
|
|
ch: ch,
|
2022-08-19 21:08:13 +00:00
|
|
|
saveBlob: saveBlob,
|
2018-04-30 13:13:03 +00:00
|
|
|
errFn: errFn,
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := uint(0); i < treeWorkers; i++ {
|
2022-05-27 17:08:50 +00:00
|
|
|
wg.Go(func() error {
|
|
|
|
return s.worker(ctx, ch)
|
2018-05-08 20:28:37 +00:00
|
|
|
})
|
2018-04-30 13:13:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2022-05-27 17:08:50 +00:00
|
|
|
func (s *TreeSaver) TriggerShutdown() {
|
|
|
|
close(s.ch)
|
|
|
|
}
|
|
|
|
|
2018-04-30 13:13:03 +00:00
|
|
|
// Save stores the dir d and returns the data once it has been completed.
|
2022-05-29 09:57:10 +00:00
|
|
|
func (s *TreeSaver) Save(ctx context.Context, snPath string, target string, node *restic.Node, nodes []FutureNode, complete CompleteFunc) FutureNode {
|
|
|
|
fn, ch := newFutureNode()
|
2018-05-08 20:28:37 +00:00
|
|
|
job := saveTreeJob{
|
2020-04-22 20:23:02 +00:00
|
|
|
snPath: snPath,
|
2022-05-29 09:57:10 +00:00
|
|
|
target: target,
|
2020-04-22 20:23:02 +00:00
|
|
|
node: node,
|
|
|
|
nodes: nodes,
|
|
|
|
ch: ch,
|
|
|
|
complete: complete,
|
2018-04-30 13:13:03 +00:00
|
|
|
}
|
2018-05-08 20:28:37 +00:00
|
|
|
select {
|
|
|
|
case s.ch <- job:
|
|
|
|
case <-ctx.Done():
|
2018-05-12 19:40:31 +00:00
|
|
|
debug.Log("not saving tree, context is cancelled")
|
2018-05-08 20:28:37 +00:00
|
|
|
close(ch)
|
|
|
|
}
|
2018-04-30 13:13:03 +00:00
|
|
|
|
2022-05-29 09:57:10 +00:00
|
|
|
return fn
|
2018-04-30 13:13:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type saveTreeJob struct {
|
2020-04-22 20:23:02 +00:00
|
|
|
snPath string
|
2022-05-29 09:57:10 +00:00
|
|
|
target string
|
2020-04-22 20:23:02 +00:00
|
|
|
node *restic.Node
|
2022-05-29 09:57:10 +00:00
|
|
|
nodes []FutureNode
|
|
|
|
ch chan<- futureNodeResult
|
2020-04-22 20:23:02 +00:00
|
|
|
complete CompleteFunc
|
2018-04-30 13:13:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// save stores the nodes as a tree in the repo.
|
2022-05-29 09:57:10 +00:00
|
|
|
func (s *TreeSaver) save(ctx context.Context, job *saveTreeJob) (*restic.Node, ItemStats, error) {
|
2018-04-30 13:13:03 +00:00
|
|
|
var stats ItemStats
|
2022-05-29 09:57:10 +00:00
|
|
|
node := job.node
|
|
|
|
nodes := job.nodes
|
|
|
|
// allow GC of nodes array once the loop is finished
|
|
|
|
job.nodes = nil
|
2018-04-30 13:13:03 +00:00
|
|
|
|
2022-05-21 11:33:08 +00:00
|
|
|
builder := restic.NewTreeJSONBuilder()
|
2021-09-26 15:18:42 +00:00
|
|
|
|
2022-05-29 09:57:10 +00:00
|
|
|
for i, fn := range nodes {
|
|
|
|
// fn is a copy, so clear the original value explicitly
|
|
|
|
nodes[i] = FutureNode{}
|
|
|
|
fnr := fn.take(ctx)
|
2018-04-30 13:13:03 +00:00
|
|
|
|
|
|
|
// return the error if it wasn't ignored
|
2022-05-29 09:57:10 +00:00
|
|
|
if fnr.err != nil {
|
|
|
|
debug.Log("err for %v: %v", fnr.snPath, fnr.err)
|
|
|
|
fnr.err = s.errFn(fnr.target, fnr.err)
|
|
|
|
if fnr.err == nil {
|
2018-04-30 13:13:03 +00:00
|
|
|
// ignore error
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-05-29 09:57:10 +00:00
|
|
|
return nil, stats, fnr.err
|
2018-04-30 13:13:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// when the error is ignored, the node could not be saved, so ignore it
|
2022-05-29 09:57:10 +00:00
|
|
|
if fnr.node == nil {
|
|
|
|
debug.Log("%v excluded: %v", fnr.snPath, fnr.target)
|
2018-04-30 13:13:03 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-05-29 09:57:10 +00:00
|
|
|
debug.Log("insert %v", fnr.node.Name)
|
2022-05-21 11:33:08 +00:00
|
|
|
err := builder.AddNode(fnr.node)
|
2018-04-30 13:13:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, stats, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-19 21:08:13 +00:00
|
|
|
buf, err := builder.Finalize()
|
2018-04-30 13:13:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, stats, err
|
|
|
|
}
|
|
|
|
|
2022-08-19 21:08:13 +00:00
|
|
|
b := &Buffer{Data: buf}
|
|
|
|
res := s.saveBlob(ctx, restic.TreeBlob, b)
|
|
|
|
|
|
|
|
sbr := res.Take(ctx)
|
|
|
|
if !sbr.known {
|
|
|
|
stats.TreeBlobs++
|
|
|
|
stats.TreeSize += uint64(sbr.length)
|
|
|
|
stats.TreeSizeInRepo += uint64(sbr.sizeInRepo)
|
|
|
|
}
|
|
|
|
// The context was canceled in the meantime, id might be invalid
|
|
|
|
if ctx.Err() != nil {
|
|
|
|
return nil, stats, ctx.Err()
|
|
|
|
}
|
|
|
|
|
|
|
|
node.Subtree = &sbr.id
|
2018-04-30 13:13:03 +00:00
|
|
|
return node, stats, nil
|
|
|
|
}
|
|
|
|
|
2018-05-08 20:28:37 +00:00
|
|
|
func (s *TreeSaver) worker(ctx context.Context, jobs <-chan saveTreeJob) error {
|
2018-04-30 13:13:03 +00:00
|
|
|
for {
|
|
|
|
var job saveTreeJob
|
2022-05-27 17:08:50 +00:00
|
|
|
var ok bool
|
2018-04-30 13:13:03 +00:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
2018-05-08 20:28:37 +00:00
|
|
|
return nil
|
2022-05-27 17:08:50 +00:00
|
|
|
case job, ok = <-jobs:
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
2018-04-30 13:13:03 +00:00
|
|
|
}
|
2022-05-29 09:57:10 +00:00
|
|
|
|
|
|
|
node, stats, err := s.save(ctx, &job)
|
2018-05-08 20:28:37 +00:00
|
|
|
if err != nil {
|
|
|
|
debug.Log("error saving tree blob: %v", err)
|
2018-05-12 19:40:31 +00:00
|
|
|
close(job.ch)
|
|
|
|
return err
|
2018-05-08 20:28:37 +00:00
|
|
|
}
|
|
|
|
|
2020-04-22 20:23:02 +00:00
|
|
|
if job.complete != nil {
|
|
|
|
job.complete(node, stats)
|
|
|
|
}
|
2022-05-29 09:57:10 +00:00
|
|
|
job.ch <- futureNodeResult{
|
|
|
|
snPath: job.snPath,
|
|
|
|
target: job.target,
|
|
|
|
node: node,
|
|
|
|
stats: stats,
|
2018-04-30 13:13:03 +00:00
|
|
|
}
|
|
|
|
close(job.ch)
|
|
|
|
}
|
|
|
|
}
|