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"
|
2022-03-03 14:19:05 +00:00
|
|
|
objectSDK "github.com/nspcc-dev/neofs-sdk-go/object"
|
2020-11-17 12:26:03 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
|
|
|
// PutPrm groups the parameters of Put operation.
|
|
|
|
type PutPrm struct {
|
2022-03-03 14:19:05 +00:00
|
|
|
obj *objectSDK.Object
|
2020-11-17 12:26:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
2022-03-03 14:19:05 +00:00
|
|
|
func (p *PutPrm) WithObject(obj *objectSDK.Object) *PutPrm {
|
2020-11-17 12:26:03 +00:00
|
|
|
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).
|
2022-03-17 08:03:58 +00:00
|
|
|
//
|
|
|
|
// Returns apistatus.ObjectAlreadyRemoved if object has been marked as removed.
|
2021-11-09 15:46:12 +00:00
|
|
|
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 14:19:05 +00:00
|
|
|
addr := object.AddressOf(prm.obj)
|
|
|
|
|
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.
|
2022-03-03 14:19:05 +00:00
|
|
|
_, err := e.exists(addr)
|
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)
|
2022-03-03 14:19:05 +00:00
|
|
|
existPrm.WithAddress(addr)
|
2020-12-01 10:18:25 +00:00
|
|
|
|
|
|
|
finished := false
|
2020-11-17 12:26:03 +00:00
|
|
|
|
2022-03-03 14:19:05 +00:00
|
|
|
e.iterateOverSortedShards(addr, 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)
|
2022-03-03 14:19:05 +00:00
|
|
|
toMoveItPrm.WithAddress(addr)
|
2021-10-08 12:25:45 +00:00
|
|
|
|
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.
|
2022-03-03 14:19:05 +00:00
|
|
|
func Put(storage *StorageEngine, obj *objectSDK.Object) error {
|
2020-11-19 08:15:49 +00:00
|
|
|
_, err := storage.Put(new(PutPrm).
|
|
|
|
WithObject(obj),
|
|
|
|
)
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|