2023-06-22 11:55:30 +00:00
|
|
|
package writecachebadger
|
2021-04-06 10:56:06 +00:00
|
|
|
|
|
|
|
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-06-15 10:19:36 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/internal/metaerr"
|
2023-03-07 13:38:26 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/util/logicerr"
|
2023-06-22 11:55:30 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/writecache"
|
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"
|
2023-06-22 11:55:30 +00:00
|
|
|
"github.com/dgraph-io/badger/v4"
|
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) {
|
2023-06-22 11:55:30 +00:00
|
|
|
_, span := tracing.StartSpanFromContext(ctx, "writecache.Get",
|
2023-03-13 11:37:35 +00:00
|
|
|
trace.WithAttributes(
|
2023-06-22 11:55:30 +00:00
|
|
|
attribute.String("address", addr.EncodeToString()),
|
2023-03-13 11:37:35 +00:00
|
|
|
))
|
|
|
|
defer span.End()
|
|
|
|
|
2023-06-22 11:55:30 +00:00
|
|
|
obj, err := c.getInternal(addr)
|
2023-06-15 10:19:36 +00:00
|
|
|
return obj, metaerr.Wrap(err)
|
2023-05-18 14:19:41 +00:00
|
|
|
}
|
|
|
|
|
2023-06-22 11:55:30 +00:00
|
|
|
func (c *cache) getInternal(addr oid.Address) (*objectSDK.Object, error) {
|
2023-05-18 14:19:41 +00:00
|
|
|
found := false
|
2023-06-22 11:55:30 +00:00
|
|
|
storageType := writecache.StorageTypeUndefined
|
2023-05-18 14:19:41 +00:00
|
|
|
startedAt := time.Now()
|
|
|
|
defer func() {
|
|
|
|
c.metrics.Get(time.Since(startedAt), found, storageType)
|
|
|
|
}()
|
|
|
|
|
2023-06-22 11:55:30 +00:00
|
|
|
k := addr2key(addr)
|
|
|
|
value, err := Get(c.db, k[:])
|
2021-10-13 11:23:03 +00:00
|
|
|
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-06-22 11:55:30 +00:00
|
|
|
storageType = writecache.StorageTypeDB
|
2021-04-06 10:56:06 +00:00
|
|
|
return obj, obj.Unmarshal(value)
|
|
|
|
}
|
|
|
|
|
2023-08-04 11:14:07 +00:00
|
|
|
return nil, logicerr.Wrap(new(apistatus.ObjectNotFound))
|
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-06-22 11:55:30 +00:00
|
|
|
_, span := tracing.StartSpanFromContext(ctx, "writecache.Head",
|
2023-03-13 11:37:35 +00:00
|
|
|
trace.WithAttributes(
|
2023-06-22 11:55:30 +00:00
|
|
|
attribute.String("address", addr.EncodeToString()),
|
2023-03-13 11:37:35 +00:00
|
|
|
))
|
|
|
|
defer span.End()
|
|
|
|
|
2023-06-22 11:55:30 +00:00
|
|
|
obj, err := c.getInternal(addr)
|
2021-08-31 10:52:09 +00:00
|
|
|
if err != nil {
|
2023-06-15 10:19:36 +00:00
|
|
|
return nil, metaerr.Wrap(err)
|
2021-08-31 10:52:09 +00:00
|
|
|
}
|
|
|
|
|
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.
|
2023-06-22 11:55:30 +00:00
|
|
|
func Get(db *badger.DB, key []byte) ([]byte, error) {
|
2021-10-13 11:23:03 +00:00
|
|
|
var value []byte
|
2023-06-22 11:55:30 +00:00
|
|
|
|
|
|
|
err := db.View(func(tx *badger.Txn) error {
|
|
|
|
it, err := tx.Get(key)
|
|
|
|
if err != nil {
|
|
|
|
if err == badger.ErrKeyNotFound {
|
2023-08-04 11:14:07 +00:00
|
|
|
return logicerr.Wrap(new(apistatus.ObjectNotFound))
|
2023-06-22 11:55:30 +00:00
|
|
|
}
|
|
|
|
return err
|
2021-10-13 11:23:03 +00:00
|
|
|
}
|
2023-06-22 11:55:30 +00:00
|
|
|
v, err := it.ValueCopy(nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2021-10-13 11:23:03 +00:00
|
|
|
}
|
2023-06-22 11:55:30 +00:00
|
|
|
value = v
|
2021-10-13 11:23:03 +00:00
|
|
|
return nil
|
|
|
|
})
|
2023-06-22 11:55:30 +00:00
|
|
|
|
2023-06-15 10:19:36 +00:00
|
|
|
return value, metaerr.Wrap(err)
|
2021-10-13 11:23:03 +00:00
|
|
|
}
|