2020-11-17 12:26:03 +00:00
|
|
|
package engine
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
|
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/core/object"
|
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/local_object_storage/shard"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
|
|
|
// PutPrm groups the parameters of Put operation.
|
|
|
|
type PutPrm struct {
|
|
|
|
obj *object.Object
|
|
|
|
}
|
|
|
|
|
|
|
|
// PutRes groups resulting values of Put operation.
|
|
|
|
type PutRes struct{}
|
|
|
|
|
|
|
|
var errPutShard = errors.New("could not put object to any shard")
|
|
|
|
|
|
|
|
// WithObject is a Put option to set object to save.
|
|
|
|
//
|
|
|
|
// Option is required.
|
|
|
|
func (p *PutPrm) WithObject(obj *object.Object) *PutPrm {
|
|
|
|
if p != nil {
|
|
|
|
p.obj = obj
|
|
|
|
}
|
|
|
|
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
|
|
|
// Put saves the object to local storage.
|
|
|
|
//
|
|
|
|
// Returns any error encountered that
|
|
|
|
// did not allow to completely save the object.
|
2021-11-09 15:46:12 +00:00
|
|
|
//
|
|
|
|
// Returns an error if executions are blocked (see BlockExecution).
|
|
|
|
func (e *StorageEngine) Put(prm *PutPrm) (res *PutRes, err error) {
|
2021-11-10 15:00:30 +00:00
|
|
|
err = e.execIfNotBlocked(func() error {
|
2021-11-09 15:46:12 +00:00
|
|
|
res, err = e.put(prm)
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *StorageEngine) put(prm *PutPrm) (*PutRes, error) {
|
2021-03-16 08:14:56 +00:00
|
|
|
if e.metrics != nil {
|
|
|
|
defer elapsed(e.metrics.AddPutDuration)()
|
2021-03-15 13:09:27 +00:00
|
|
|
}
|
|
|
|
|
2022-03-03 13:52:27 +00:00
|
|
|
// In #1146 this check was parallelized, however, it became
|
|
|
|
// much slower on fast machines for 4 shards.
|
|
|
|
_, err := e.exists(prm.obj.Address())
|
2020-12-01 11:06:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-11-17 12:26:03 +00:00
|
|
|
|
2020-12-01 10:18:25 +00:00
|
|
|
existPrm := new(shard.ExistsPrm)
|
|
|
|
existPrm.WithAddress(prm.obj.Address())
|
|
|
|
|
|
|
|
finished := false
|
2020-11-17 12:26:03 +00:00
|
|
|
|
2022-01-31 14:58:32 +00:00
|
|
|
e.iterateOverSortedShards(prm.obj.Address(), func(ind int, sh hashedShard) (stop bool) {
|
2021-10-08 12:25:45 +00:00
|
|
|
e.mtx.RLock()
|
2022-01-31 14:58:32 +00:00
|
|
|
pool := e.shardPools[sh.ID().String()]
|
2021-10-08 12:25:45 +00:00
|
|
|
e.mtx.RUnlock()
|
|
|
|
|
|
|
|
exitCh := make(chan struct{})
|
|
|
|
|
2021-10-08 14:33:51 +00:00
|
|
|
if err := pool.Submit(func() {
|
2021-10-08 12:25:45 +00:00
|
|
|
defer close(exitCh)
|
|
|
|
|
2022-01-31 14:58:32 +00:00
|
|
|
exists, err := sh.Exists(existPrm)
|
2021-10-08 12:25:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return // this is not ErrAlreadyRemoved error so we can go to the next shard
|
2020-12-01 10:18:25 +00:00
|
|
|
}
|
|
|
|
|
2021-10-08 12:25:45 +00:00
|
|
|
if exists.Exists() {
|
|
|
|
if ind != 0 {
|
|
|
|
toMoveItPrm := new(shard.ToMoveItPrm)
|
|
|
|
toMoveItPrm.WithAddress(prm.obj.Address())
|
|
|
|
|
2022-01-31 14:58:32 +00:00
|
|
|
_, err = sh.ToMoveIt(toMoveItPrm)
|
2021-10-08 12:25:45 +00:00
|
|
|
if err != nil {
|
|
|
|
e.log.Warn("could not mark object for shard relocation",
|
2022-01-31 14:58:32 +00:00
|
|
|
zap.Stringer("shard", sh.ID()),
|
2021-10-08 12:25:45 +00:00
|
|
|
zap.String("error", err.Error()),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
2020-11-17 12:26:03 +00:00
|
|
|
|
2021-10-08 12:25:45 +00:00
|
|
|
finished = true
|
2020-11-17 12:26:03 +00:00
|
|
|
|
2021-10-08 12:25:45 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
putPrm := new(shard.PutPrm)
|
|
|
|
putPrm.WithObject(prm.obj)
|
2020-11-17 12:26:03 +00:00
|
|
|
|
2022-01-31 14:58:32 +00:00
|
|
|
_, err = sh.Put(putPrm)
|
2021-10-08 12:25:45 +00:00
|
|
|
if err != nil {
|
|
|
|
e.log.Warn("could not put object in shard",
|
2022-01-31 14:58:32 +00:00
|
|
|
zap.Stringer("shard", sh.ID()),
|
2021-10-08 12:25:45 +00:00
|
|
|
zap.String("error", err.Error()),
|
|
|
|
)
|
2020-11-17 12:26:03 +00:00
|
|
|
|
2021-10-08 12:25:45 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
finished = true
|
2021-10-08 14:33:51 +00:00
|
|
|
}); err != nil {
|
|
|
|
close(exitCh)
|
|
|
|
}
|
2020-11-17 12:26:03 +00:00
|
|
|
|
2021-10-08 12:25:45 +00:00
|
|
|
<-exitCh
|
2020-12-01 11:06:14 +00:00
|
|
|
|
2021-10-08 12:25:45 +00:00
|
|
|
return finished
|
2020-12-01 10:18:25 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
if !finished {
|
|
|
|
err = errPutShard
|
2020-11-17 12:26:03 +00:00
|
|
|
}
|
|
|
|
|
2020-12-01 10:18:25 +00:00
|
|
|
return nil, err
|
2020-11-17 12:26:03 +00:00
|
|
|
}
|
2020-11-19 08:15:49 +00:00
|
|
|
|
|
|
|
// Put writes provided object to local storage.
|
|
|
|
func Put(storage *StorageEngine, obj *object.Object) error {
|
|
|
|
_, err := storage.Put(new(PutPrm).
|
|
|
|
WithObject(obj),
|
|
|
|
)
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|