2020-11-17 12:23:15 +00:00
|
|
|
package shard
|
|
|
|
|
|
|
|
import (
|
2021-05-18 08:12:51 +00:00
|
|
|
"fmt"
|
|
|
|
|
2022-07-07 12:52:40 +00:00
|
|
|
objectCore "github.com/nspcc-dev/neofs-node/pkg/core/object"
|
2022-07-06 13:41:35 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/local_object_storage/blobstor/common"
|
2020-12-08 09:56:14 +00:00
|
|
|
meta "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/metabase"
|
2022-03-03 14:19:05 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/object"
|
2021-04-06 10:56:06 +00:00
|
|
|
"go.uber.org/zap"
|
2020-11-17 12:23:15 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// PutPrm groups the parameters of Put operation.
|
|
|
|
type PutPrm struct {
|
|
|
|
obj *object.Object
|
|
|
|
}
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// PutRes groups the resulting values of Put operation.
|
2020-11-17 12:23:15 +00:00
|
|
|
type PutRes struct{}
|
|
|
|
|
2022-07-13 12:43:04 +00:00
|
|
|
// SetObject is a Put option to set object to save.
|
|
|
|
func (p *PutPrm) SetObject(obj *object.Object) {
|
|
|
|
p.obj = obj
|
2020-11-17 12:23:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Put saves the object in shard.
|
|
|
|
//
|
|
|
|
// Returns any error encountered that
|
|
|
|
// did not allow to completely save the object.
|
2021-12-27 11:04:07 +00:00
|
|
|
//
|
|
|
|
// Returns ErrReadOnlyMode error if shard is in "read-only" mode.
|
2022-05-31 11:50:39 +00:00
|
|
|
func (s *Shard) Put(prm PutPrm) (PutRes, error) {
|
2022-06-29 11:27:36 +00:00
|
|
|
m := s.GetMode()
|
|
|
|
if m.ReadOnly() {
|
2022-05-31 11:50:39 +00:00
|
|
|
return PutRes{}, ErrReadOnlyMode
|
2021-12-27 11:04:07 +00:00
|
|
|
}
|
|
|
|
|
2022-07-07 12:52:40 +00:00
|
|
|
data, err := prm.obj.Marshal()
|
|
|
|
if err != nil {
|
|
|
|
return PutRes{}, fmt.Errorf("cannot marshal object: %w", err)
|
|
|
|
}
|
|
|
|
|
2022-07-06 13:41:35 +00:00
|
|
|
var putPrm common.PutPrm // form Put parameters
|
|
|
|
putPrm.Object = prm.obj
|
2022-07-07 12:52:40 +00:00
|
|
|
putPrm.RawData = data
|
|
|
|
putPrm.Address = objectCore.AddressOf(prm.obj)
|
|
|
|
|
|
|
|
var res common.PutRes
|
2020-11-17 17:39:43 +00:00
|
|
|
|
2020-12-01 07:46:52 +00:00
|
|
|
// exist check are not performed there, these checks should be executed
|
|
|
|
// ahead of `Put` by storage engine
|
2022-09-30 10:41:37 +00:00
|
|
|
tryCache := s.hasWriteCache() && !m.NoMetabase()
|
|
|
|
if tryCache {
|
2022-07-07 12:52:40 +00:00
|
|
|
res, err = s.writeCache.Put(putPrm)
|
2021-04-06 10:56:06 +00:00
|
|
|
}
|
2022-09-30 10:41:37 +00:00
|
|
|
if err != nil || !tryCache {
|
2022-07-07 12:52:40 +00:00
|
|
|
if err != nil {
|
|
|
|
s.log.Debug("can't put object to the write-cache, trying blobstor",
|
|
|
|
zap.String("err", err.Error()))
|
|
|
|
}
|
2020-11-17 17:39:43 +00:00
|
|
|
|
2022-07-07 12:52:40 +00:00
|
|
|
res, err = s.blobStor.Put(putPrm)
|
|
|
|
if err != nil {
|
|
|
|
return PutRes{}, fmt.Errorf("could not put object to BLOB storage: %w", err)
|
|
|
|
}
|
2020-11-17 17:39:43 +00:00
|
|
|
}
|
|
|
|
|
2022-06-29 11:27:36 +00:00
|
|
|
if !m.NoMetabase() {
|
|
|
|
var pPrm meta.PutPrm
|
|
|
|
pPrm.SetObject(prm.obj)
|
2022-07-06 14:09:50 +00:00
|
|
|
pPrm.SetStorageID(res.StorageID)
|
2022-06-29 11:27:36 +00:00
|
|
|
if _, err := s.metaBase.Put(pPrm); err != nil {
|
|
|
|
// may we need to handle this case in a special way
|
|
|
|
// since the object has been successfully written to BlobStor
|
|
|
|
return PutRes{}, fmt.Errorf("could not put object to metabase: %w", err)
|
|
|
|
}
|
2022-08-19 16:49:09 +00:00
|
|
|
|
|
|
|
s.incObjectCounter()
|
2020-11-17 17:39:43 +00:00
|
|
|
}
|
2020-11-17 12:23:15 +00:00
|
|
|
|
2022-05-31 11:50:39 +00:00
|
|
|
return PutRes{}, nil
|
2020-11-17 12:23:15 +00:00
|
|
|
}
|