2023-06-09 06:34:04 +00:00
|
|
|
package services
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api/data"
|
2023-07-05 14:05:45 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api/middleware"
|
2024-09-27 09:18:41 +00:00
|
|
|
frosterr "git.frostfs.info/TrueCloudLab/frostfs-s3-gw/internal/frostfs/errors"
|
2023-06-09 06:34:04 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/pkg/service/tree"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/bearer"
|
|
|
|
treepool "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/pool/tree"
|
2024-09-27 09:18:41 +00:00
|
|
|
grpcservice "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/pool/tree/service"
|
2023-06-09 06:34:04 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type GetNodeByPathResponseInfoWrapper struct {
|
2024-09-27 09:18:41 +00:00
|
|
|
response *grpcservice.GetNodeByPathResponse_Info
|
2023-06-09 06:34:04 +00:00
|
|
|
}
|
|
|
|
|
2024-07-12 12:27:00 +00:00
|
|
|
func (n GetNodeByPathResponseInfoWrapper) GetNodeID() []uint64 {
|
|
|
|
return []uint64{n.response.GetNodeId()}
|
2023-06-09 06:34:04 +00:00
|
|
|
}
|
|
|
|
|
2024-07-12 12:27:00 +00:00
|
|
|
func (n GetNodeByPathResponseInfoWrapper) GetParentID() []uint64 {
|
|
|
|
return []uint64{n.response.GetParentId()}
|
2023-06-09 06:34:04 +00:00
|
|
|
}
|
|
|
|
|
2024-07-12 12:27:00 +00:00
|
|
|
func (n GetNodeByPathResponseInfoWrapper) GetTimestamp() []uint64 {
|
|
|
|
return []uint64{n.response.GetTimestamp()}
|
2023-06-09 06:34:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (n GetNodeByPathResponseInfoWrapper) GetMeta() []tree.Meta {
|
|
|
|
res := make([]tree.Meta, len(n.response.Meta))
|
|
|
|
for i, value := range n.response.Meta {
|
|
|
|
res[i] = value
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
type GetSubTreeResponseBodyWrapper struct {
|
2024-09-27 09:18:41 +00:00
|
|
|
response *grpcservice.GetSubTreeResponse_Body
|
2023-06-09 06:34:04 +00:00
|
|
|
}
|
|
|
|
|
2024-07-12 12:27:00 +00:00
|
|
|
func (n GetSubTreeResponseBodyWrapper) GetNodeID() []uint64 {
|
2023-06-09 06:34:04 +00:00
|
|
|
return n.response.GetNodeId()
|
|
|
|
}
|
|
|
|
|
2024-07-12 12:27:00 +00:00
|
|
|
func (n GetSubTreeResponseBodyWrapper) GetParentID() []uint64 {
|
2024-07-19 11:40:08 +00:00
|
|
|
resp := n.response.GetParentId()
|
|
|
|
if resp == nil {
|
|
|
|
// storage sends nil that should be interpreted as []uint64{0}
|
|
|
|
// due to protobuf compatibility, see 'GetSubTree' function
|
|
|
|
return []uint64{0}
|
|
|
|
}
|
|
|
|
return resp
|
2023-06-09 06:34:04 +00:00
|
|
|
}
|
|
|
|
|
2024-07-12 12:27:00 +00:00
|
|
|
func (n GetSubTreeResponseBodyWrapper) GetTimestamp() []uint64 {
|
2023-06-09 06:34:04 +00:00
|
|
|
return n.response.GetTimestamp()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n GetSubTreeResponseBodyWrapper) GetMeta() []tree.Meta {
|
|
|
|
res := make([]tree.Meta, len(n.response.Meta))
|
|
|
|
for i, value := range n.response.Meta {
|
|
|
|
res[i] = value
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
type PoolWrapper struct {
|
|
|
|
p *treepool.Pool
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewPoolWrapper(p *treepool.Pool) *PoolWrapper {
|
|
|
|
return &PoolWrapper{p: p}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *PoolWrapper) GetNodes(ctx context.Context, prm *tree.GetNodesParams) ([]tree.NodeResponse, error) {
|
|
|
|
poolPrm := treepool.GetNodesParams{
|
|
|
|
CID: prm.BktInfo.CID,
|
|
|
|
TreeID: prm.TreeID,
|
|
|
|
Path: prm.Path,
|
|
|
|
Meta: prm.Meta,
|
|
|
|
PathAttribute: tree.FileNameKey,
|
|
|
|
LatestOnly: prm.LatestOnly,
|
|
|
|
AllAttrs: prm.AllAttrs,
|
|
|
|
BearerToken: getBearer(ctx, prm.BktInfo),
|
|
|
|
}
|
|
|
|
|
|
|
|
nodes, err := w.p.GetNodes(ctx, poolPrm)
|
|
|
|
if err != nil {
|
|
|
|
return nil, handleError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
res := make([]tree.NodeResponse, len(nodes))
|
|
|
|
for i, info := range nodes {
|
|
|
|
res[i] = GetNodeByPathResponseInfoWrapper{info}
|
|
|
|
}
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
2024-08-21 12:10:34 +00:00
|
|
|
func (w *PoolWrapper) GetSubTree(ctx context.Context, bktInfo *data.BucketInfo, treeID string, rootID []uint64, depth uint32, sort bool) ([]tree.NodeResponse, error) {
|
|
|
|
order := treepool.NoneOrder
|
|
|
|
if sort {
|
|
|
|
order = treepool.AscendingOrder
|
|
|
|
}
|
2023-06-09 06:34:04 +00:00
|
|
|
poolPrm := treepool.GetSubTreeParams{
|
|
|
|
CID: bktInfo.CID,
|
|
|
|
TreeID: treeID,
|
|
|
|
RootID: rootID,
|
|
|
|
Depth: depth,
|
|
|
|
BearerToken: getBearer(ctx, bktInfo),
|
2024-08-21 12:10:34 +00:00
|
|
|
Order: order,
|
2023-06-09 06:34:04 +00:00
|
|
|
}
|
2024-07-19 11:40:08 +00:00
|
|
|
if len(rootID) == 1 && rootID[0] == 0 {
|
|
|
|
// storage node interprets 'nil' value as []uint64{0}
|
|
|
|
// gate wants to send 'nil' value instead of []uint64{0}, because
|
|
|
|
// it provides compatibility with previous tree service api where
|
|
|
|
// single uint64(0) value is dropped from signature
|
|
|
|
poolPrm.RootID = nil
|
|
|
|
}
|
2023-06-09 06:34:04 +00:00
|
|
|
|
|
|
|
subTreeReader, err := w.p.GetSubTree(ctx, poolPrm)
|
|
|
|
if err != nil {
|
|
|
|
return nil, handleError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var subtree []tree.NodeResponse
|
|
|
|
|
|
|
|
node, err := subTreeReader.Next()
|
|
|
|
for err == nil {
|
|
|
|
subtree = append(subtree, GetSubTreeResponseBodyWrapper{node})
|
|
|
|
node, err = subTreeReader.Next()
|
|
|
|
}
|
|
|
|
if err != nil && err != io.EOF {
|
|
|
|
return nil, handleError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return subtree, nil
|
|
|
|
}
|
|
|
|
|
2023-10-09 06:57:33 +00:00
|
|
|
type SubTreeStreamImpl struct {
|
2023-10-17 07:43:58 +00:00
|
|
|
r *treepool.SubTreeReader
|
2024-09-27 09:18:41 +00:00
|
|
|
buffer []*grpcservice.GetSubTreeResponse_Body
|
2023-10-17 07:43:58 +00:00
|
|
|
eof bool
|
|
|
|
index int
|
|
|
|
ln int
|
2023-10-09 06:57:33 +00:00
|
|
|
}
|
|
|
|
|
2023-10-17 07:43:58 +00:00
|
|
|
const bufSize = 1000
|
|
|
|
|
2023-10-09 06:57:33 +00:00
|
|
|
func (s *SubTreeStreamImpl) Next() (tree.NodeResponse, error) {
|
2023-10-17 07:43:58 +00:00
|
|
|
if s.index != -1 {
|
|
|
|
node := s.buffer[s.index]
|
|
|
|
s.index++
|
|
|
|
if s.index >= s.ln {
|
|
|
|
s.index = -1
|
|
|
|
}
|
|
|
|
return GetSubTreeResponseBodyWrapper{response: node}, nil
|
|
|
|
}
|
|
|
|
if s.eof {
|
|
|
|
return nil, io.EOF
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
s.ln, err = s.r.Read(s.buffer)
|
2023-10-09 06:57:33 +00:00
|
|
|
if err != nil {
|
|
|
|
if err != io.EOF {
|
2024-01-22 08:09:11 +00:00
|
|
|
return nil, fmt.Errorf("sub tree stream impl pool wrap: %w", handleError(err))
|
2023-10-09 06:57:33 +00:00
|
|
|
}
|
2024-01-22 08:09:11 +00:00
|
|
|
s.eof = true
|
2023-10-17 07:43:58 +00:00
|
|
|
}
|
|
|
|
if s.ln > 0 {
|
|
|
|
s.index = 0
|
2023-10-09 06:57:33 +00:00
|
|
|
}
|
|
|
|
|
2023-10-17 07:43:58 +00:00
|
|
|
return s.Next()
|
2023-10-09 06:57:33 +00:00
|
|
|
}
|
|
|
|
|
2024-07-12 12:27:00 +00:00
|
|
|
func (w *PoolWrapper) GetSubTreeStream(ctx context.Context, bktInfo *data.BucketInfo, treeID string, rootID []uint64, depth uint32) (tree.SubTreeStream, error) {
|
2023-10-09 06:57:33 +00:00
|
|
|
poolPrm := treepool.GetSubTreeParams{
|
|
|
|
CID: bktInfo.CID,
|
|
|
|
TreeID: treeID,
|
|
|
|
RootID: rootID,
|
|
|
|
Depth: depth,
|
|
|
|
BearerToken: getBearer(ctx, bktInfo),
|
2024-04-09 15:57:47 +00:00
|
|
|
Order: treepool.AscendingOrder,
|
2023-10-09 06:57:33 +00:00
|
|
|
}
|
2024-07-19 11:40:08 +00:00
|
|
|
if len(rootID) == 1 && rootID[0] == 0 {
|
|
|
|
// storage node interprets 'nil' value as []uint64{0}
|
|
|
|
// gate wants to send 'nil' value instead of []uint64{0}, because
|
|
|
|
// it provides compatibility with previous tree service api where
|
|
|
|
// single uint64(0) value is dropped from signature
|
|
|
|
poolPrm.RootID = nil
|
|
|
|
}
|
2023-10-09 06:57:33 +00:00
|
|
|
|
|
|
|
subTreeReader, err := w.p.GetSubTree(ctx, poolPrm)
|
|
|
|
if err != nil {
|
|
|
|
return nil, handleError(err)
|
|
|
|
}
|
|
|
|
|
2023-10-17 07:43:58 +00:00
|
|
|
return &SubTreeStreamImpl{
|
|
|
|
r: subTreeReader,
|
2024-09-27 09:18:41 +00:00
|
|
|
buffer: make([]*grpcservice.GetSubTreeResponse_Body, bufSize),
|
2023-10-17 07:43:58 +00:00
|
|
|
index: -1,
|
|
|
|
}, nil
|
2023-10-09 06:57:33 +00:00
|
|
|
}
|
|
|
|
|
2023-06-09 06:34:04 +00:00
|
|
|
func (w *PoolWrapper) AddNode(ctx context.Context, bktInfo *data.BucketInfo, treeID string, parent uint64, meta map[string]string) (uint64, error) {
|
|
|
|
nodeID, err := w.p.AddNode(ctx, treepool.AddNodeParams{
|
|
|
|
CID: bktInfo.CID,
|
|
|
|
TreeID: treeID,
|
|
|
|
Parent: parent,
|
|
|
|
Meta: meta,
|
|
|
|
BearerToken: getBearer(ctx, bktInfo),
|
|
|
|
})
|
|
|
|
return nodeID, handleError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *PoolWrapper) AddNodeByPath(ctx context.Context, bktInfo *data.BucketInfo, treeID string, path []string, meta map[string]string) (uint64, error) {
|
|
|
|
nodeID, err := w.p.AddNodeByPath(ctx, treepool.AddNodeByPathParams{
|
|
|
|
CID: bktInfo.CID,
|
|
|
|
TreeID: treeID,
|
|
|
|
Path: path,
|
|
|
|
Meta: meta,
|
|
|
|
PathAttribute: tree.FileNameKey,
|
|
|
|
BearerToken: getBearer(ctx, bktInfo),
|
|
|
|
})
|
|
|
|
return nodeID, handleError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *PoolWrapper) MoveNode(ctx context.Context, bktInfo *data.BucketInfo, treeID string, nodeID, parentID uint64, meta map[string]string) error {
|
|
|
|
return handleError(w.p.MoveNode(ctx, treepool.MoveNodeParams{
|
|
|
|
CID: bktInfo.CID,
|
|
|
|
TreeID: treeID,
|
|
|
|
NodeID: nodeID,
|
|
|
|
ParentID: parentID,
|
|
|
|
Meta: meta,
|
|
|
|
BearerToken: getBearer(ctx, bktInfo),
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *PoolWrapper) RemoveNode(ctx context.Context, bktInfo *data.BucketInfo, treeID string, nodeID uint64) error {
|
|
|
|
return handleError(w.p.RemoveNode(ctx, treepool.RemoveNodeParams{
|
|
|
|
CID: bktInfo.CID,
|
|
|
|
TreeID: treeID,
|
|
|
|
NodeID: nodeID,
|
|
|
|
BearerToken: getBearer(ctx, bktInfo),
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
|
|
|
func getBearer(ctx context.Context, bktInfo *data.BucketInfo) []byte {
|
2023-08-14 15:34:41 +00:00
|
|
|
if bd, err := middleware.GetBoxData(ctx); err == nil {
|
2023-06-09 06:34:04 +00:00
|
|
|
if bd.Gate.BearerToken != nil {
|
|
|
|
if bd.Gate.BearerToken.Impersonate() || bktInfo.Owner.Equals(bearer.ResolveIssuer(*bd.Gate.BearerToken)) {
|
|
|
|
return bd.Gate.BearerToken.Marshal()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleError(err error) error {
|
|
|
|
if err == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if errors.Is(err, treepool.ErrNodeNotFound) {
|
|
|
|
return fmt.Errorf("%w: %s", tree.ErrNodeNotFound, err.Error())
|
|
|
|
}
|
|
|
|
if errors.Is(err, treepool.ErrNodeAccessDenied) {
|
|
|
|
return fmt.Errorf("%w: %s", tree.ErrNodeAccessDenied, err.Error())
|
|
|
|
}
|
2024-09-27 09:18:41 +00:00
|
|
|
if frosterr.IsTimeoutError(err) {
|
2023-06-13 14:47:31 +00:00
|
|
|
return fmt.Errorf("%w: %s", tree.ErrGatewayTimeout, err.Error())
|
|
|
|
}
|
2023-06-09 06:34:04 +00:00
|
|
|
|
|
|
|
return err
|
|
|
|
}
|