2021-04-06 10:56:06 +00:00
|
|
|
package writecache
|
|
|
|
|
|
|
|
import (
|
2023-03-13 11:37:35 +00:00
|
|
|
"context"
|
2023-05-18 14:19:41 +00:00
|
|
|
"time"
|
2023-03-13 11:37:35 +00:00
|
|
|
|
2023-03-07 13:38:26 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/blobstor/common"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/util/logicerr"
|
2023-05-31 09:24:04 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-observability/tracing"
|
2023-03-07 13:38:26 +00:00
|
|
|
apistatus "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/client/status"
|
|
|
|
objectSDK "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object"
|
|
|
|
oid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id"
|
2022-07-07 12:56:05 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util/slice"
|
2021-04-06 10:56:06 +00:00
|
|
|
"go.etcd.io/bbolt"
|
2023-03-13 11:37:35 +00:00
|
|
|
"go.opentelemetry.io/otel/attribute"
|
|
|
|
"go.opentelemetry.io/otel/trace"
|
2021-04-06 10:56:06 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Get returns object from write-cache.
|
2022-03-17 08:03:58 +00:00
|
|
|
//
|
2022-04-21 11:28:05 +00:00
|
|
|
// Returns an error of type apistatus.ObjectNotFound if the requested object is missing in write-cache.
|
2023-03-13 11:37:35 +00:00
|
|
|
func (c *cache) Get(ctx context.Context, addr oid.Address) (*objectSDK.Object, error) {
|
2022-05-31 17:00:41 +00:00
|
|
|
saddr := addr.EncodeToString()
|
2021-04-06 10:56:06 +00:00
|
|
|
|
2023-03-13 11:37:35 +00:00
|
|
|
ctx, span := tracing.StartSpanFromContext(ctx, "writecache.Get",
|
|
|
|
trace.WithAttributes(
|
|
|
|
attribute.String("address", saddr),
|
|
|
|
))
|
|
|
|
defer span.End()
|
|
|
|
|
2023-05-18 14:19:41 +00:00
|
|
|
return c.getInternal(ctx, saddr, addr)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *cache) getInternal(ctx context.Context, saddr string, addr oid.Address) (*objectSDK.Object, error) {
|
|
|
|
found := false
|
2023-05-19 08:17:19 +00:00
|
|
|
storageType := StorageTypeUndefined
|
2023-05-18 14:19:41 +00:00
|
|
|
startedAt := time.Now()
|
|
|
|
defer func() {
|
|
|
|
c.metrics.Get(time.Since(startedAt), found, storageType)
|
|
|
|
}()
|
|
|
|
|
2021-10-13 11:23:03 +00:00
|
|
|
value, err := Get(c.db, []byte(saddr))
|
|
|
|
if err == nil {
|
2022-03-03 14:19:05 +00:00
|
|
|
obj := objectSDK.New()
|
2023-05-18 14:19:41 +00:00
|
|
|
found = true
|
2023-05-19 08:17:19 +00:00
|
|
|
storageType = StorageTypeDB
|
2021-04-06 10:56:06 +00:00
|
|
|
return obj, obj.Unmarshal(value)
|
|
|
|
}
|
|
|
|
|
2023-03-13 11:37:35 +00:00
|
|
|
res, err := c.fsTree.Get(ctx, common.GetPrm{Address: addr})
|
2021-04-06 10:56:06 +00:00
|
|
|
if err != nil {
|
2022-10-26 12:23:12 +00:00
|
|
|
return nil, logicerr.Wrap(apistatus.ObjectNotFound{})
|
2021-04-06 10:56:06 +00:00
|
|
|
}
|
|
|
|
|
2023-05-18 14:19:41 +00:00
|
|
|
found = true
|
2023-05-19 08:17:19 +00:00
|
|
|
storageType = StorageTypeFSTree
|
2022-07-08 07:09:48 +00:00
|
|
|
return res.Object, nil
|
2021-04-06 10:56:06 +00:00
|
|
|
}
|
2021-08-31 10:52:09 +00:00
|
|
|
|
|
|
|
// Head returns object header from write-cache.
|
2022-03-17 08:03:58 +00:00
|
|
|
//
|
2022-04-21 11:28:05 +00:00
|
|
|
// Returns an error of type apistatus.ObjectNotFound if the requested object is missing in write-cache.
|
2023-03-13 11:37:35 +00:00
|
|
|
func (c *cache) Head(ctx context.Context, addr oid.Address) (*objectSDK.Object, error) {
|
2023-05-18 14:19:41 +00:00
|
|
|
saddr := addr.EncodeToString()
|
|
|
|
|
2023-03-13 11:37:35 +00:00
|
|
|
ctx, span := tracing.StartSpanFromContext(ctx, "writecache.Head",
|
|
|
|
trace.WithAttributes(
|
2023-05-18 14:19:41 +00:00
|
|
|
attribute.String("address", saddr),
|
2023-03-13 11:37:35 +00:00
|
|
|
))
|
|
|
|
defer span.End()
|
|
|
|
|
2023-05-18 14:19:41 +00:00
|
|
|
obj, err := c.getInternal(ctx, saddr, addr)
|
2021-08-31 10:52:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-03-03 14:19:05 +00:00
|
|
|
return obj.CutPayload(), nil
|
2021-08-31 10:52:09 +00:00
|
|
|
}
|
2021-10-13 11:23:03 +00:00
|
|
|
|
|
|
|
// Get fetches object from the underlying database.
|
|
|
|
// Key should be a stringified address.
|
2022-03-17 08:03:58 +00:00
|
|
|
//
|
2022-04-21 11:28:05 +00:00
|
|
|
// Returns an error of type apistatus.ObjectNotFound if the requested object is missing in db.
|
2021-10-13 11:23:03 +00:00
|
|
|
func Get(db *bbolt.DB, key []byte) ([]byte, error) {
|
|
|
|
var value []byte
|
|
|
|
err := db.View(func(tx *bbolt.Tx) error {
|
|
|
|
b := tx.Bucket(defaultBucket)
|
|
|
|
if b == nil {
|
|
|
|
return ErrNoDefaultBucket
|
|
|
|
}
|
|
|
|
value = b.Get(key)
|
|
|
|
if value == nil {
|
2022-10-26 12:23:12 +00:00
|
|
|
return logicerr.Wrap(apistatus.ObjectNotFound{})
|
2021-10-13 11:23:03 +00:00
|
|
|
}
|
2022-07-07 12:56:05 +00:00
|
|
|
value = slice.Copy(value)
|
2021-10-13 11:23:03 +00:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
return value, err
|
|
|
|
}
|