forked from TrueCloudLab/frostfs-node
ce76325840
Parallel check seems to be slower, so remove TODO. ``` name \ time/op old new pool Exists/2_shards-40 52.3µs ±23% 91.6µs ± 6% 84.2µs ± 1% Exists/4_shards-40 72.7µs ±11% 121.8µs ± 8% 116.9µs ± 5% name \ alloc/op old new pool Exists/2_shards-40 5.00kB ± 0% 5.03kB ± 0% 5.16kB ± 0% Exists/4_shards-40 9.89kB ± 0% 9.93kB ± 1% 10.19kB ± 0% name \ allocs/op old new pool Exists/2_shards-40 112 ± 1% 115 ± 1% 117 ± 1% Exists/4_shards-40 207 ± 1% 211 ± 1% 216 ± 1% ``` Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
135 lines
2.7 KiB
Go
135 lines
2.7 KiB
Go
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.
|
|
//
|
|
// Returns an error if executions are blocked (see BlockExecution).
|
|
func (e *StorageEngine) Put(prm *PutPrm) (res *PutRes, err error) {
|
|
err = e.execIfNotBlocked(func() error {
|
|
res, err = e.put(prm)
|
|
return err
|
|
})
|
|
|
|
return
|
|
}
|
|
|
|
func (e *StorageEngine) put(prm *PutPrm) (*PutRes, error) {
|
|
if e.metrics != nil {
|
|
defer elapsed(e.metrics.AddPutDuration)()
|
|
}
|
|
|
|
// In #1146 this check was parallelized, however, it became
|
|
// much slower on fast machines for 4 shards.
|
|
_, err := e.exists(prm.obj.Address())
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
existPrm := new(shard.ExistsPrm)
|
|
existPrm.WithAddress(prm.obj.Address())
|
|
|
|
finished := false
|
|
|
|
e.iterateOverSortedShards(prm.obj.Address(), func(ind int, sh hashedShard) (stop bool) {
|
|
e.mtx.RLock()
|
|
pool := e.shardPools[sh.ID().String()]
|
|
e.mtx.RUnlock()
|
|
|
|
exitCh := make(chan struct{})
|
|
|
|
if err := pool.Submit(func() {
|
|
defer close(exitCh)
|
|
|
|
exists, err := sh.Exists(existPrm)
|
|
if err != nil {
|
|
return // this is not ErrAlreadyRemoved error so we can go to the next shard
|
|
}
|
|
|
|
if exists.Exists() {
|
|
if ind != 0 {
|
|
toMoveItPrm := new(shard.ToMoveItPrm)
|
|
toMoveItPrm.WithAddress(prm.obj.Address())
|
|
|
|
_, err = sh.ToMoveIt(toMoveItPrm)
|
|
if err != nil {
|
|
e.log.Warn("could not mark object for shard relocation",
|
|
zap.Stringer("shard", sh.ID()),
|
|
zap.String("error", err.Error()),
|
|
)
|
|
}
|
|
}
|
|
|
|
finished = true
|
|
|
|
return
|
|
}
|
|
|
|
putPrm := new(shard.PutPrm)
|
|
putPrm.WithObject(prm.obj)
|
|
|
|
_, err = sh.Put(putPrm)
|
|
if err != nil {
|
|
e.log.Warn("could not put object in shard",
|
|
zap.Stringer("shard", sh.ID()),
|
|
zap.String("error", err.Error()),
|
|
)
|
|
|
|
return
|
|
}
|
|
|
|
finished = true
|
|
}); err != nil {
|
|
close(exitCh)
|
|
}
|
|
|
|
<-exitCh
|
|
|
|
return finished
|
|
})
|
|
|
|
if !finished {
|
|
err = errPutShard
|
|
}
|
|
|
|
return nil, err
|
|
}
|
|
|
|
// Put writes provided object to local storage.
|
|
func Put(storage *StorageEngine, obj *object.Object) error {
|
|
_, err := storage.Put(new(PutPrm).
|
|
WithObject(obj),
|
|
)
|
|
|
|
return err
|
|
}
|