2022-01-18 11:39:55 +00:00
|
|
|
package shard
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/binary"
|
|
|
|
"errors"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
|
2022-06-28 14:05:08 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/local_object_storage/shard/mode"
|
2022-03-03 14:19:05 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/object"
|
2022-01-18 11:39:55 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// ErrInvalidMagic is returned when dump format is invalid.
|
|
|
|
var ErrInvalidMagic = errors.New("invalid magic")
|
|
|
|
|
|
|
|
// RestorePrm groups the parameters of Restore operation.
|
|
|
|
type RestorePrm struct {
|
2022-01-20 10:45:29 +00:00
|
|
|
path string
|
|
|
|
stream io.Reader
|
|
|
|
ignoreErrors bool
|
2022-01-18 11:39:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// WithPath is a Restore option to set the destination path.
|
2022-05-20 18:08:59 +00:00
|
|
|
func (p *RestorePrm) WithPath(path string) {
|
2022-01-18 11:39:55 +00:00
|
|
|
p.path = path
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithStream is a Restore option to set the stream to read objects from.
|
|
|
|
// It takes priority over `WithPath` option.
|
2022-05-20 18:08:59 +00:00
|
|
|
func (p *RestorePrm) WithStream(r io.Reader) {
|
2022-01-18 11:39:55 +00:00
|
|
|
p.stream = r
|
|
|
|
}
|
|
|
|
|
2022-01-20 10:45:29 +00:00
|
|
|
// WithIgnoreErrors is a Restore option which allows to ignore errors encountered during restore.
|
|
|
|
// Corrupted objects will not be processed.
|
2022-05-20 18:08:59 +00:00
|
|
|
func (p *RestorePrm) WithIgnoreErrors(ignore bool) {
|
2022-01-20 10:45:29 +00:00
|
|
|
p.ignoreErrors = ignore
|
|
|
|
}
|
|
|
|
|
2022-01-18 11:39:55 +00:00
|
|
|
// RestoreRes groups the result fields of Restore operation.
|
|
|
|
type RestoreRes struct {
|
2022-01-20 10:45:29 +00:00
|
|
|
count int
|
|
|
|
failed int
|
2022-01-18 11:39:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Count return amount of object written.
|
2022-05-20 18:08:59 +00:00
|
|
|
func (r RestoreRes) Count() int {
|
2022-01-18 11:39:55 +00:00
|
|
|
return r.count
|
|
|
|
}
|
|
|
|
|
2022-01-20 10:45:29 +00:00
|
|
|
// FailCount return amount of object skipped.
|
2022-05-20 18:08:59 +00:00
|
|
|
func (r RestoreRes) FailCount() int {
|
2022-01-20 10:45:29 +00:00
|
|
|
return r.failed
|
|
|
|
}
|
|
|
|
|
2022-01-24 12:40:42 +00:00
|
|
|
// Restore restores objects from the dump prepared by Dump.
|
2022-01-18 11:39:55 +00:00
|
|
|
//
|
|
|
|
// Returns any error encountered.
|
2022-05-31 11:50:39 +00:00
|
|
|
func (s *Shard) Restore(prm RestorePrm) (RestoreRes, error) {
|
2022-01-18 11:39:55 +00:00
|
|
|
// Disallow changing mode during restore.
|
|
|
|
s.m.RLock()
|
|
|
|
defer s.m.RUnlock()
|
|
|
|
|
2022-06-28 14:05:08 +00:00
|
|
|
if s.info.Mode != mode.ReadWrite {
|
2022-05-31 11:50:39 +00:00
|
|
|
return RestoreRes{}, ErrReadOnlyMode
|
2022-01-18 11:39:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
r := prm.stream
|
|
|
|
if r == nil {
|
|
|
|
f, err := os.OpenFile(prm.path, os.O_RDONLY, os.ModeExclusive)
|
|
|
|
if err != nil {
|
2022-05-31 11:50:39 +00:00
|
|
|
return RestoreRes{}, err
|
2022-01-18 11:39:55 +00:00
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
r = f
|
|
|
|
}
|
|
|
|
|
|
|
|
var m [4]byte
|
|
|
|
_, _ = io.ReadFull(r, m[:])
|
|
|
|
if !bytes.Equal(m[:], dumpMagic) {
|
2022-05-31 11:50:39 +00:00
|
|
|
return RestoreRes{}, ErrInvalidMagic
|
2022-01-18 11:39:55 +00:00
|
|
|
}
|
|
|
|
|
2022-05-20 18:08:59 +00:00
|
|
|
var putPrm PutPrm
|
|
|
|
|
2022-01-20 10:45:29 +00:00
|
|
|
var count, failCount int
|
2022-01-18 11:39:55 +00:00
|
|
|
var data []byte
|
|
|
|
var size [4]byte
|
|
|
|
for {
|
|
|
|
// If there are less than 4 bytes left, `Read` returns nil error instead of
|
|
|
|
// io.ErrUnexpectedEOF, thus `ReadFull` is used.
|
|
|
|
_, err := io.ReadFull(r, size[:])
|
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, io.EOF) {
|
|
|
|
break
|
|
|
|
}
|
2022-05-31 11:50:39 +00:00
|
|
|
return RestoreRes{}, err
|
2022-01-18 11:39:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sz := binary.LittleEndian.Uint32(size[:])
|
|
|
|
if uint32(cap(data)) < sz {
|
|
|
|
data = make([]byte, sz)
|
|
|
|
} else {
|
|
|
|
data = data[:sz]
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = r.Read(data)
|
|
|
|
if err != nil {
|
2022-05-31 11:50:39 +00:00
|
|
|
return RestoreRes{}, err
|
2022-01-18 11:39:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
obj := object.New()
|
|
|
|
err = obj.Unmarshal(data)
|
|
|
|
if err != nil {
|
2022-01-20 10:45:29 +00:00
|
|
|
if prm.ignoreErrors {
|
|
|
|
failCount++
|
|
|
|
continue
|
|
|
|
}
|
2022-05-31 11:50:39 +00:00
|
|
|
return RestoreRes{}, err
|
2022-01-18 11:39:55 +00:00
|
|
|
}
|
|
|
|
|
2022-07-13 12:43:04 +00:00
|
|
|
putPrm.SetObject(obj)
|
2022-05-20 18:08:59 +00:00
|
|
|
_, err = s.Put(putPrm)
|
2022-01-18 11:39:55 +00:00
|
|
|
if err != nil {
|
2022-05-31 11:50:39 +00:00
|
|
|
return RestoreRes{}, err
|
2022-01-18 11:39:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
count++
|
|
|
|
}
|
|
|
|
|
2022-05-31 11:50:39 +00:00
|
|
|
return RestoreRes{count: count, failed: failCount}, nil
|
2022-01-18 11:39:55 +00:00
|
|
|
}
|