2020-11-17 12:23:15 +00:00
|
|
|
package shard
|
|
|
|
|
|
|
|
import (
|
2023-04-12 14:01:29 +00:00
|
|
|
"context"
|
2021-05-18 08:12:51 +00:00
|
|
|
"fmt"
|
|
|
|
|
2023-04-12 14:35:10 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/internal/logs"
|
2023-03-07 13:38:26 +00:00
|
|
|
objectCore "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/core/object"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/blobstor/common"
|
|
|
|
meta "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/metabase"
|
2023-05-31 09:24:04 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-observability/tracing"
|
2023-07-06 12:36:41 +00:00
|
|
|
objectSDK "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object"
|
2023-04-12 14:01:29 +00:00
|
|
|
"go.opentelemetry.io/otel/attribute"
|
|
|
|
"go.opentelemetry.io/otel/trace"
|
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 {
|
2024-10-01 15:19:12 +00:00
|
|
|
obj *objectSDK.Object
|
|
|
|
indexAttributes bool
|
2020-11-17 12:23:15 +00:00
|
|
|
}
|
|
|
|
|
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.
|
2023-07-06 12:36:41 +00:00
|
|
|
func (p *PutPrm) SetObject(obj *objectSDK.Object) {
|
2022-07-13 12:43:04 +00:00
|
|
|
p.obj = obj
|
2020-11-17 12:23:15 +00:00
|
|
|
}
|
|
|
|
|
2024-10-01 15:19:12 +00:00
|
|
|
func (p *PutPrm) SetIndexAttributes(v bool) {
|
|
|
|
p.indexAttributes = v
|
|
|
|
}
|
|
|
|
|
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.
|
2023-04-12 14:01:29 +00:00
|
|
|
func (s *Shard) Put(ctx context.Context, prm PutPrm) (PutRes, error) {
|
|
|
|
ctx, span := tracing.StartSpanFromContext(ctx, "Shard.Put",
|
|
|
|
trace.WithAttributes(
|
|
|
|
attribute.String("shard_id", s.ID().String()),
|
|
|
|
attribute.String("address", objectCore.AddressOf(prm.obj).EncodeToString()),
|
|
|
|
))
|
|
|
|
defer span.End()
|
|
|
|
|
2022-12-07 17:42:35 +00:00
|
|
|
s.m.RLock()
|
|
|
|
defer s.m.RUnlock()
|
|
|
|
|
|
|
|
m := s.info.Mode
|
2022-06-29 11:27:36 +00:00
|
|
|
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 {
|
2023-04-12 14:01:29 +00:00
|
|
|
res, err = s.writeCache.Put(ctx, 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 {
|
2024-10-21 07:22:54 +00:00
|
|
|
s.log.Debug(ctx, logs.ShardCantPutObjectToTheWritecacheTryingBlobstor,
|
2022-07-07 12:52:40 +00:00
|
|
|
zap.String("err", err.Error()))
|
|
|
|
}
|
2020-11-17 17:39:43 +00:00
|
|
|
|
2023-04-12 14:01:29 +00:00
|
|
|
res, err = s.blobStor.Put(ctx, putPrm)
|
2022-07-07 12:52:40 +00:00
|
|
|
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)
|
2024-10-01 15:19:12 +00:00
|
|
|
pPrm.SetIndexAttributes(prm.indexAttributes)
|
2024-01-15 06:04:25 +00:00
|
|
|
res, err := s.metaBase.Put(ctx, pPrm)
|
|
|
|
if err != nil {
|
2022-06-29 11:27:36 +00:00
|
|
|
// 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
|
|
|
|
2024-01-15 06:04:25 +00:00
|
|
|
if res.Inserted {
|
|
|
|
s.incObjectCounter(putPrm.Address.Container(), meta.IsUserObject(prm.obj))
|
|
|
|
s.addToPayloadSize(int64(prm.obj.PayloadSize()))
|
|
|
|
s.addToContainerSize(putPrm.Address.Container().EncodeToString(), int64(prm.obj.PayloadSize()))
|
|
|
|
}
|
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
|
|
|
}
|