2020-11-17 12:26:03 +00:00
|
|
|
package engine
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
|
2022-12-23 17:35:35 +00:00
|
|
|
"github.com/TrueCloudLab/frostfs-node/pkg/core/object"
|
|
|
|
"github.com/TrueCloudLab/frostfs-node/pkg/local_object_storage/blobstor"
|
|
|
|
"github.com/TrueCloudLab/frostfs-node/pkg/local_object_storage/blobstor/common"
|
|
|
|
"github.com/TrueCloudLab/frostfs-node/pkg/local_object_storage/shard"
|
|
|
|
"github.com/TrueCloudLab/frostfs-node/pkg/util"
|
|
|
|
objectSDK "github.com/TrueCloudLab/frostfs-sdk-go/object"
|
|
|
|
oid "github.com/TrueCloudLab/frostfs-sdk-go/object/id"
|
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
|
|
|
}
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// PutRes groups the resulting values of Put operation.
|
2020-11-17 12:26:03 +00:00
|
|
|
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-05-23 13:12:32 +00:00
|
|
|
func (p *PutPrm) WithObject(obj *objectSDK.Object) {
|
2022-09-14 14:36:37 +00:00
|
|
|
p.obj = obj
|
2020-11-17 12:26:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
//
|
2022-04-21 11:28:05 +00:00
|
|
|
// Returns an error of type apistatus.ObjectAlreadyRemoved if the object has been marked as removed.
|
2022-05-31 11:56:59 +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
|
|
|
|
}
|
|
|
|
|
2022-05-31 11:56:59 +00:00
|
|
|
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 {
|
2022-05-31 11:56:59 +00:00
|
|
|
return PutRes{}, err
|
2020-12-01 11:06:14 +00:00
|
|
|
}
|
2020-11-17 12:26:03 +00:00
|
|
|
|
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()
|
|
|
|
|
2022-09-28 08:10:46 +00:00
|
|
|
putDone, exists := e.putToShard(sh, ind, pool, addr, prm.obj)
|
2022-09-13 11:18:00 +00:00
|
|
|
finished = putDone || exists
|
2022-09-12 11:48:06 +00:00
|
|
|
return finished
|
|
|
|
})
|
2021-10-08 12:25:45 +00:00
|
|
|
|
2022-09-12 11:48:06 +00:00
|
|
|
if !finished {
|
|
|
|
err = errPutShard
|
|
|
|
}
|
2021-10-08 12:25:45 +00:00
|
|
|
|
2022-09-12 11:48:06 +00:00
|
|
|
return PutRes{}, err
|
|
|
|
}
|
2022-07-27 18:38:28 +00:00
|
|
|
|
2022-09-13 11:18:00 +00:00
|
|
|
// putToShard puts object to sh.
|
|
|
|
// First return value is true iff put has been successfully done.
|
|
|
|
// Second return value is true iff object already exists.
|
2022-09-28 08:10:46 +00:00
|
|
|
func (e *StorageEngine) putToShard(sh hashedShard, ind int, pool util.WorkerPool, addr oid.Address, obj *objectSDK.Object) (bool, bool) {
|
2022-09-13 11:18:00 +00:00
|
|
|
var putSuccess, alreadyExists bool
|
2020-12-01 10:18:25 +00:00
|
|
|
|
2022-09-12 11:48:06 +00:00
|
|
|
exitCh := make(chan struct{})
|
2020-11-17 12:26:03 +00:00
|
|
|
|
2022-09-12 11:48:06 +00:00
|
|
|
if err := pool.Submit(func() {
|
|
|
|
defer close(exitCh)
|
2020-11-17 12:26:03 +00:00
|
|
|
|
2022-09-12 11:48:06 +00:00
|
|
|
var existPrm shard.ExistsPrm
|
|
|
|
existPrm.SetAddress(addr)
|
2021-10-08 12:25:45 +00:00
|
|
|
|
2022-09-12 11:48:06 +00:00
|
|
|
exists, err := sh.Exists(existPrm)
|
|
|
|
if err != nil {
|
|
|
|
if shard.IsErrObjectExpired(err) {
|
|
|
|
// object is already found but
|
|
|
|
// expired => do nothing with it
|
2022-09-13 11:18:00 +00:00
|
|
|
alreadyExists = true
|
2022-09-12 11:48:06 +00:00
|
|
|
}
|
2020-11-17 12:26:03 +00:00
|
|
|
|
2022-09-12 11:48:06 +00:00
|
|
|
return // this is not ErrAlreadyRemoved error so we can go to the next shard
|
|
|
|
}
|
2020-11-17 12:26:03 +00:00
|
|
|
|
2022-09-13 11:18:00 +00:00
|
|
|
alreadyExists = exists.Exists()
|
|
|
|
if alreadyExists {
|
2022-09-12 11:48:06 +00:00
|
|
|
if ind != 0 {
|
|
|
|
var toMoveItPrm shard.ToMoveItPrm
|
|
|
|
toMoveItPrm.SetAddress(addr)
|
|
|
|
|
|
|
|
_, 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()),
|
|
|
|
)
|
|
|
|
}
|
2021-10-08 12:25:45 +00:00
|
|
|
}
|
|
|
|
|
2022-09-12 11:48:06 +00:00
|
|
|
return
|
2021-10-08 14:33:51 +00:00
|
|
|
}
|
2020-11-17 12:26:03 +00:00
|
|
|
|
2022-09-12 11:48:06 +00:00
|
|
|
var putPrm shard.PutPrm
|
|
|
|
putPrm.SetObject(obj)
|
2020-12-01 11:06:14 +00:00
|
|
|
|
2022-09-12 11:48:06 +00:00
|
|
|
_, err = sh.Put(putPrm)
|
|
|
|
if err != nil {
|
2022-09-28 08:10:46 +00:00
|
|
|
if errors.Is(err, shard.ErrReadOnlyMode) || errors.Is(err, blobstor.ErrNoPlaceFound) ||
|
|
|
|
errors.Is(err, common.ErrReadOnly) || errors.Is(err, common.ErrNoSpace) {
|
|
|
|
e.log.Warn("could not put object to shard",
|
|
|
|
zap.Stringer("shard_id", sh.ID()),
|
|
|
|
zap.String("error", err.Error()))
|
|
|
|
return
|
|
|
|
}
|
2020-12-01 10:18:25 +00:00
|
|
|
|
2022-09-28 08:10:46 +00:00
|
|
|
e.reportShardError(sh, "could not put object to shard", err)
|
2022-09-12 11:48:06 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-09-13 11:18:00 +00:00
|
|
|
putSuccess = true
|
2022-09-12 11:48:06 +00:00
|
|
|
}); err != nil {
|
|
|
|
close(exitCh)
|
2020-11-17 12:26:03 +00:00
|
|
|
}
|
|
|
|
|
2022-09-12 11:48:06 +00:00
|
|
|
<-exitCh
|
|
|
|
|
2022-09-13 11:18:00 +00:00
|
|
|
return putSuccess, alreadyExists
|
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 {
|
2022-05-23 13:12:32 +00:00
|
|
|
var putPrm PutPrm
|
|
|
|
putPrm.WithObject(obj)
|
|
|
|
|
|
|
|
_, err := storage.Put(putPrm)
|
2020-11-19 08:15:49 +00:00
|
|
|
|
|
|
|
return err
|
|
|
|
}
|