2022-01-18 11:39:55 +00:00
|
|
|
package shard
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/binary"
|
|
|
|
"errors"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
|
2022-07-07 12:03:45 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/local_object_storage/blobstor/common"
|
2022-10-26 12:23:12 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/local_object_storage/util/logicerr"
|
2022-01-20 16:52:30 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/local_object_storage/writecache"
|
2022-01-18 11:39:55 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var dumpMagic = []byte("NEOF")
|
|
|
|
|
2022-01-24 12:40:42 +00:00
|
|
|
// DumpPrm groups the parameters of Dump operation.
|
|
|
|
type DumpPrm struct {
|
2022-01-20 16:56:07 +00:00
|
|
|
path string
|
|
|
|
stream io.Writer
|
|
|
|
ignoreErrors bool
|
2022-01-18 11:39:55 +00:00
|
|
|
}
|
|
|
|
|
2022-01-24 12:40:42 +00:00
|
|
|
// WithPath is an Dump option to set the destination path.
|
2022-05-20 18:08:59 +00:00
|
|
|
func (p *DumpPrm) WithPath(path string) {
|
2022-01-18 11:39:55 +00:00
|
|
|
p.path = path
|
|
|
|
}
|
|
|
|
|
2022-01-24 12:40:42 +00:00
|
|
|
// WithStream is an Dump option to set the destination stream.
|
2022-01-18 11:39:55 +00:00
|
|
|
// It takes priority over `path` option.
|
2022-05-20 18:08:59 +00:00
|
|
|
func (p *DumpPrm) WithStream(r io.Writer) {
|
2022-01-18 11:39:55 +00:00
|
|
|
p.stream = r
|
|
|
|
}
|
|
|
|
|
2022-01-24 12:40:42 +00:00
|
|
|
// WithIgnoreErrors is an Dump option to allow ignore all errors during iteration.
|
2022-01-20 16:56:07 +00:00
|
|
|
// This includes invalid blobovniczas as well as corrupted objects.
|
2022-05-20 18:08:59 +00:00
|
|
|
func (p *DumpPrm) WithIgnoreErrors(ignore bool) {
|
2022-01-20 16:56:07 +00:00
|
|
|
p.ignoreErrors = ignore
|
|
|
|
}
|
|
|
|
|
2022-01-24 12:40:42 +00:00
|
|
|
// DumpRes groups the result fields of Dump operation.
|
|
|
|
type DumpRes struct {
|
2022-01-18 11:39:55 +00:00
|
|
|
count int
|
|
|
|
}
|
|
|
|
|
|
|
|
// Count return amount of object written.
|
2022-05-20 18:08:59 +00:00
|
|
|
func (r DumpRes) Count() int {
|
2022-01-18 11:39:55 +00:00
|
|
|
return r.count
|
|
|
|
}
|
|
|
|
|
2022-10-26 12:23:12 +00:00
|
|
|
var ErrMustBeReadOnly = logicerr.Wrap(errors.New("shard must be in read-only mode"))
|
2022-01-18 11:39:55 +00:00
|
|
|
|
2022-01-24 12:40:42 +00:00
|
|
|
// Dump dumps all objects from the shard to a file or stream.
|
2022-01-18 11:39:55 +00:00
|
|
|
//
|
|
|
|
// Returns any error encountered.
|
2022-05-31 11:50:39 +00:00
|
|
|
func (s *Shard) Dump(prm DumpPrm) (DumpRes, error) {
|
2022-01-18 11:39:55 +00:00
|
|
|
s.m.RLock()
|
|
|
|
defer s.m.RUnlock()
|
|
|
|
|
2022-06-29 11:27:36 +00:00
|
|
|
if !s.info.Mode.ReadOnly() {
|
2022-05-31 11:50:39 +00:00
|
|
|
return DumpRes{}, ErrMustBeReadOnly
|
2022-01-18 11:39:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
w := prm.stream
|
|
|
|
if w == nil {
|
|
|
|
f, err := os.OpenFile(prm.path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0640)
|
|
|
|
if err != nil {
|
2022-05-31 11:50:39 +00:00
|
|
|
return DumpRes{}, err
|
2022-01-18 11:39:55 +00:00
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
w = f
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := w.Write(dumpMagic)
|
|
|
|
if err != nil {
|
2022-05-31 11:50:39 +00:00
|
|
|
return DumpRes{}, err
|
2022-01-18 11:39:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var count int
|
|
|
|
|
|
|
|
if s.hasWriteCache() {
|
2022-05-20 15:43:05 +00:00
|
|
|
var iterPrm writecache.IterationPrm
|
|
|
|
|
|
|
|
iterPrm.WithIgnoreErrors(prm.ignoreErrors)
|
|
|
|
iterPrm.WithHandler(func(data []byte) error {
|
2022-01-18 13:40:11 +00:00
|
|
|
var size [4]byte
|
|
|
|
binary.LittleEndian.PutUint32(size[:], uint32(len(data)))
|
|
|
|
if _, err := w.Write(size[:]); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := w.Write(data); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
count++
|
|
|
|
return nil
|
2022-05-20 15:43:05 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
err := s.writeCache.Iterate(iterPrm)
|
2022-01-18 13:40:11 +00:00
|
|
|
if err != nil {
|
2022-05-31 11:50:39 +00:00
|
|
|
return DumpRes{}, err
|
2022-01-18 13:40:11 +00:00
|
|
|
}
|
2022-01-18 11:39:55 +00:00
|
|
|
}
|
|
|
|
|
2022-07-07 12:03:45 +00:00
|
|
|
var pi common.IteratePrm
|
|
|
|
pi.IgnoreErrors = prm.ignoreErrors
|
|
|
|
pi.Handler = func(elem common.IterationElement) error {
|
|
|
|
data := elem.ObjectData
|
2022-01-18 11:39:55 +00:00
|
|
|
|
|
|
|
var size [4]byte
|
|
|
|
binary.LittleEndian.PutUint32(size[:], uint32(len(data)))
|
|
|
|
if _, err := w.Write(size[:]); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := w.Write(data); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
count++
|
|
|
|
return nil
|
2022-07-07 12:03:45 +00:00
|
|
|
}
|
2022-01-18 11:39:55 +00:00
|
|
|
|
|
|
|
if _, err := s.blobStor.Iterate(pi); err != nil {
|
2022-05-31 11:50:39 +00:00
|
|
|
return DumpRes{}, err
|
2022-01-18 11:39:55 +00:00
|
|
|
}
|
|
|
|
|
2022-05-31 11:50:39 +00:00
|
|
|
return DumpRes{count: count}, nil
|
2022-01-18 11:39:55 +00:00
|
|
|
}
|