forked from TrueCloudLab/frostfs-s3-gw
[#429] Refactor tree service
Signed-off-by: Angira Kekteeva <kira@nspcc.ru>
This commit is contained in:
parent
dace32760a
commit
b03ae827fb
3 changed files with 90 additions and 66 deletions
|
@ -254,7 +254,7 @@ func (n *layer) GetBucketSettings(ctx context.Context, bktInfo *data.BucketInfo)
|
||||||
|
|
||||||
settings, err := n.treeService.GetSettingsNode(ctx, &bktInfo.CID, "version")
|
settings, err := n.treeService.GetSettingsNode(ctx, &bktInfo.CID, "version")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if !errorsStd.Is(err, ErrNotFound) {
|
if !errorsStd.Is(err, ErrNodeNotFound) {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
settings = &data.BucketSettings{}
|
settings = &data.BucketSettings{}
|
||||||
|
|
|
@ -15,9 +15,9 @@ type TreeService interface {
|
||||||
|
|
||||||
// GetSettingsNode retrieves the settings node from the tree service and form data.BucketSettings.
|
// GetSettingsNode retrieves the settings node from the tree service and form data.BucketSettings.
|
||||||
//
|
//
|
||||||
// If node is not found returns ErrNotFound error.
|
// If node is not found returns ErrNodeNotFound error.
|
||||||
GetSettingsNode(context.Context, *cid.ID, string) (*data.BucketSettings, error)
|
GetSettingsNode(context.Context, *cid.ID, string) (*data.BucketSettings, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ErrNotFound is returned from Tree service in case of not found error.
|
// ErrNodeNotFound is returned from Tree service in case of not found error.
|
||||||
var ErrNotFound = errors.New("not found")
|
var ErrNodeNotFound = errors.New("not found")
|
||||||
|
|
|
@ -11,23 +11,29 @@ import (
|
||||||
"github.com/nspcc-dev/neofs-s3-gw/api/layer"
|
"github.com/nspcc-dev/neofs-s3-gw/api/layer"
|
||||||
"github.com/nspcc-dev/neofs-s3-gw/internal/neofs/services/tree"
|
"github.com/nspcc-dev/neofs-s3-gw/internal/neofs/services/tree"
|
||||||
cid "github.com/nspcc-dev/neofs-sdk-go/container/id"
|
cid "github.com/nspcc-dev/neofs-sdk-go/container/id"
|
||||||
|
oid "github.com/nspcc-dev/neofs-sdk-go/object/id"
|
||||||
"google.golang.org/grpc"
|
"google.golang.org/grpc"
|
||||||
"google.golang.org/grpc/credentials/insecure"
|
"google.golang.org/grpc/credentials/insecure"
|
||||||
)
|
)
|
||||||
|
|
||||||
type TreeClient struct {
|
type (
|
||||||
|
TreeClient struct {
|
||||||
conn *grpc.ClientConn
|
conn *grpc.ClientConn
|
||||||
service tree.TreeServiceClient
|
service tree.TreeServiceClient
|
||||||
}
|
}
|
||||||
|
|
||||||
type Node struct {
|
TreeNode struct {
|
||||||
id uint64
|
ID uint64
|
||||||
meta map[string]string
|
ObjID *oid.ID
|
||||||
|
TimeStamp uint64
|
||||||
|
Meta map[string]string
|
||||||
}
|
}
|
||||||
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
versioningEnabledKV = "versioning_enabled"
|
versioningEnabledKV = "versioning_enabled"
|
||||||
lockConfigurationKV = "lock_configuration"
|
lockConfigurationKV = "lock_configuration"
|
||||||
|
oidKv = "OID"
|
||||||
fileNameKV = "FileName"
|
fileNameKV = "FileName"
|
||||||
systemNameKV = "SystemName"
|
systemNameKV = "SystemName"
|
||||||
|
|
||||||
|
@ -49,36 +55,39 @@ func NewTreeClient(addr string) (*TreeClient, error) {
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func newNode(nodeInfo *tree.GetNodeByPathResponse_Info) *Node {
|
func newTreeNode(nodeInfo *tree.GetNodeByPathResponse_Info) (*TreeNode, error) {
|
||||||
|
var objID *oid.ID
|
||||||
meta := make(map[string]string, len(nodeInfo.GetMeta()))
|
meta := make(map[string]string, len(nodeInfo.GetMeta()))
|
||||||
|
|
||||||
for _, kv := range nodeInfo.GetMeta() {
|
for _, kv := range nodeInfo.GetMeta() {
|
||||||
|
if kv.GetKey() == oidKv {
|
||||||
|
objID = new(oid.ID)
|
||||||
|
err := objID.DecodeString(string(kv.GetValue()))
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
meta[kv.GetKey()] = string(kv.GetValue())
|
meta[kv.GetKey()] = string(kv.GetValue())
|
||||||
}
|
}
|
||||||
|
|
||||||
return &Node{
|
return &TreeNode{
|
||||||
id: nodeInfo.GetNodeId(),
|
ID: nodeInfo.GetNodeId(),
|
||||||
meta: meta,
|
ObjID: objID,
|
||||||
}
|
TimeStamp: nodeInfo.Timestamp,
|
||||||
|
Meta: meta,
|
||||||
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (n *Node) ID() uint64 {
|
func (n *TreeNode) Get(key string) (string, bool) {
|
||||||
return n.id
|
value, ok := n.Meta[key]
|
||||||
}
|
|
||||||
|
|
||||||
func (n *Node) Meta() map[string]string {
|
|
||||||
return n.meta
|
|
||||||
}
|
|
||||||
|
|
||||||
func (n *Node) Get(key string) (string, bool) {
|
|
||||||
value, ok := n.meta[key]
|
|
||||||
return value, ok
|
return value, ok
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *TreeClient) GetSettingsNode(ctx context.Context, cnrID *cid.ID, treeID string) (*data.BucketSettings, error) {
|
func (c *TreeClient) GetSettingsNode(ctx context.Context, cnrID *cid.ID, treeID string) (*data.BucketSettings, error) {
|
||||||
keysToReturn := []string{versioningEnabledKV, lockConfigurationKV}
|
keysToReturn := []string{versioningEnabledKV, lockConfigurationKV}
|
||||||
path := []string{settingsFileName}
|
node, err := c.getSystemNode(ctx, cnrID, treeID, settingsFileName, keysToReturn)
|
||||||
node, err := c.getSystemNode(ctx, cnrID, treeID, path, keysToReturn)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("couldn't get node: %w", err)
|
return nil, fmt.Errorf("couldn't get node: %w", err)
|
||||||
}
|
}
|
||||||
|
@ -101,9 +110,8 @@ func (c *TreeClient) GetSettingsNode(ctx context.Context, cnrID *cid.ID, treeID
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *TreeClient) PutSettingsNode(ctx context.Context, cnrID *cid.ID, treeID string, settings *data.BucketSettings) error {
|
func (c *TreeClient) PutSettingsNode(ctx context.Context, cnrID *cid.ID, treeID string, settings *data.BucketSettings) error {
|
||||||
path := []string{settingsFileName}
|
node, err := c.getSystemNode(ctx, cnrID, treeID, settingsFileName, []string{})
|
||||||
node, err := c.getSystemNode(ctx, cnrID, treeID, path, []string{})
|
isErrNotFound := errors.Is(err, layer.ErrNodeNotFound)
|
||||||
isErrNotFound := errors.Is(err, layer.ErrNotFound)
|
|
||||||
if err != nil && !isErrNotFound {
|
if err != nil && !isErrNotFound {
|
||||||
return fmt.Errorf("couldn't get node: %w", err)
|
return fmt.Errorf("couldn't get node: %w", err)
|
||||||
}
|
}
|
||||||
|
@ -115,7 +123,7 @@ func (c *TreeClient) PutSettingsNode(ctx context.Context, cnrID *cid.ID, treeID
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return c.moveNode(ctx, cnrID, treeID, node.ID(), 0, meta)
|
return c.moveNode(ctx, cnrID, treeID, node.ID, 0, meta)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *TreeClient) Close() error {
|
func (c *TreeClient) Close() error {
|
||||||
|
@ -136,49 +144,66 @@ func metaFromSettings(settings *data.BucketSettings) map[string]string {
|
||||||
return results
|
return results
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *TreeClient) getSystemNode(ctx context.Context, cnrID *cid.ID, treeID string, path, meta []string) (*Node, error) {
|
func (c *TreeClient) getSystemNode(ctx context.Context, cnrID *cid.ID, treeID, path string, meta []string) (*TreeNode, error) {
|
||||||
return c.getNode(ctx, cnrID, treeID, systemNameKV, path, meta)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *TreeClient) getRegularNode(ctx context.Context, cnrID *cid.ID, treeID string, path, meta []string) (*Node, error) {
|
|
||||||
return c.getNode(ctx, cnrID, treeID, fileNameKV, path, meta)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *TreeClient) getNode(ctx context.Context, cnrID *cid.ID, treeID, pathAttr string, path, meta []string) (*Node, error) {
|
|
||||||
nodes, err := c.getNodes(ctx, cnrID, treeID, pathAttr, path, meta)
|
|
||||||
if err != nil {
|
|
||||||
if strings.Contains(err.Error(), "not found") {
|
|
||||||
return nil, layer.ErrNotFound
|
|
||||||
}
|
|
||||||
return nil, fmt.Errorf("couldn't get nodes: %w", err)
|
|
||||||
}
|
|
||||||
if len(nodes) == 0 {
|
|
||||||
return nil, layer.ErrNotFound
|
|
||||||
}
|
|
||||||
if len(nodes) != 1 {
|
|
||||||
return nil, fmt.Errorf("found more than one node")
|
|
||||||
}
|
|
||||||
|
|
||||||
return newNode(nodes[0]), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *TreeClient) getNodes(ctx context.Context, cnrID *cid.ID, treeID, pathAttr string, path, meta []string) ([]*tree.GetNodeByPathResponse_Info, error) {
|
|
||||||
request := &tree.GetNodeByPathRequest{
|
request := &tree.GetNodeByPathRequest{
|
||||||
Body: &tree.GetNodeByPathRequest_Body{
|
Body: &tree.GetNodeByPathRequest_Body{
|
||||||
ContainerId: []byte(cnrID.EncodeToString()),
|
ContainerId: []byte(cnrID.EncodeToString()),
|
||||||
TreeId: treeID,
|
TreeId: treeID,
|
||||||
Path: path,
|
Path: []string{path},
|
||||||
Attributes: meta,
|
Attributes: meta,
|
||||||
PathAttribute: pathAttr,
|
PathAttribute: systemNameKV,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
resp, err := c.service.GetNodeByPath(ctx, request)
|
||||||
|
if err != nil {
|
||||||
|
if strings.Contains(err.Error(), "not found") {
|
||||||
|
return nil, layer.ErrNodeNotFound
|
||||||
|
}
|
||||||
|
return nil, fmt.Errorf("couldn't get nodes: %w", err)
|
||||||
|
}
|
||||||
|
if len(resp.Body.GetNodes()) == 0 {
|
||||||
|
return nil, layer.ErrNodeNotFound
|
||||||
|
}
|
||||||
|
if len(resp.Body.GetNodes()) != 1 {
|
||||||
|
return nil, fmt.Errorf("found more than one node")
|
||||||
|
}
|
||||||
|
|
||||||
|
return newTreeNode(resp.Body.Nodes[0])
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *TreeClient) getSystemNodesWithOID(ctx context.Context, cnrID *cid.ID, treeID, path string, meta []string, latestOnly bool) ([]*TreeNode, error) {
|
||||||
|
meta = append(meta, oidKv)
|
||||||
|
|
||||||
|
r := &tree.GetNodeByPathRequest{
|
||||||
|
Body: &tree.GetNodeByPathRequest_Body{
|
||||||
|
ContainerId: []byte(cnrID.EncodeToString()),
|
||||||
|
TreeId: treeID,
|
||||||
|
PathAttribute: systemNameKV,
|
||||||
|
Path: []string{path},
|
||||||
|
Attributes: meta,
|
||||||
|
LatestOnly: latestOnly,
|
||||||
|
AllAttributes: false,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
resp, err := c.service.GetNodeByPath(ctx, request)
|
resp, err := c.service.GetNodeByPath(ctx, r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to get node path: %w", err)
|
if strings.Contains(err.Error(), "not found") {
|
||||||
|
return nil, layer.ErrNodeNotFound
|
||||||
|
}
|
||||||
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return resp.GetBody().GetNodes(), nil
|
nodes := make([]*TreeNode, 0, len(resp.Body.Nodes))
|
||||||
|
for _, n := range resp.Body.GetNodes() {
|
||||||
|
node, err := newTreeNode(n)
|
||||||
|
if err != nil {
|
||||||
|
|
||||||
|
}
|
||||||
|
nodes = append(nodes, node)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nodes, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *TreeClient) addNode(ctx context.Context, cnrID *cid.ID, treeID string, parent uint64, meta map[string]string) (uint64, error) {
|
func (c *TreeClient) addNode(ctx context.Context, cnrID *cid.ID, treeID string, parent uint64, meta map[string]string) (uint64, error) {
|
||||||
|
@ -212,7 +237,6 @@ func (c *TreeClient) moveNode(ctx context.Context, cnrID *cid.ID, treeID string,
|
||||||
|
|
||||||
_, err := c.service.Move(ctx, request)
|
_, err := c.service.Move(ctx, request)
|
||||||
return err
|
return err
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func metaToKV(meta map[string]string) []*tree.KeyValue {
|
func metaToKV(meta map[string]string) []*tree.KeyValue {
|
||||||
|
|
Loading…
Reference in a new issue