Compare commits

...

2 commits

4 changed files with 93 additions and 23 deletions

View file

@ -20,7 +20,6 @@ import (
containerMorph "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/services/container/morph"
containerGRPC "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/api/container/grpc"
cid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id"
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/user"
"git.frostfs.info/TrueCloudLab/policy-engine/pkg/engine"
"go.uber.org/zap"
"google.golang.org/grpc"
@ -221,7 +220,7 @@ type morphContainerReader struct {
src containerCore.Source
lister interface {
ContainersOf(*user.ID) ([]cid.ID, error)
ContainersOf(*containerMorph.ClientPrm) ([]cid.ID, error)
}
}
@ -233,8 +232,8 @@ func (x *morphContainerReader) DeletionInfo(id cid.ID) (*containerCore.DelInfo,
return x.src.DeletionInfo(id)
}
func (x *morphContainerReader) ContainersOf(id *user.ID) ([]cid.ID, error) {
return x.lister.ContainersOf(id)
func (x *morphContainerReader) ContainersOf(prm *containerMorph.ClientPrm) ([]cid.ID, error) {
return x.lister.ContainersOf(prm)
}
type morphContainerWriter struct {

View file

@ -5,6 +5,8 @@ import (
"fmt"
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/morph/client"
container "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/services/container/morph"
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/api/refs"
cid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id"
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/user"
"github.com/nspcc-dev/neo-go/pkg/rpcclient/unwrap"
@ -15,15 +17,33 @@ import (
// to the specified user of FrostFS system. If idUser is nil, returns the list of all containers.
//
// If remote RPC does not support neo-go session API, fallback to List() method.
func (c *Client) ContainersOf(idUser *user.ID) ([]cid.ID, error) {
func (c *Client) ContainersOf(prm *container.ClientPrm) ([]cid.ID, error) {
var rawID []byte
var idUser *user.ID
doSend := false
if prm != nil {
idUser = prm.OwnerID
doSend = prm.Stream != nil
}
if idUser != nil {
rawID = idUser.WalletBytes()
}
// We would like to have batch size as big as possible,
// to reduce the number of round-trips and avoid creating sessions.
// The limit depends on 2 things:
// 1. VM limits: max 2048 items on stack.
// 2. JSON encoded size for the item with type = 128k.
// It turns out, that for container ID the second limit is hit first,
// 512 is big enough value and it is beautiful.
const batchSize = 512
var cidList []cid.ID
cb := func(item stackitem.Item) error {
// ids := make([]cid.ID, 0, 512)
// wasSent := false
appendCIDtoList := func(item stackitem.Item) error {
rawID, err := client.BytesFromStackItem(item)
if err != nil {
return fmt.Errorf("get byte array from stack item (%s): %w", containersOfMethod, err)
@ -38,25 +58,53 @@ func (c *Client) ContainersOf(idUser *user.ID) ([]cid.ID, error) {
cidList = append(cidList, id)
return nil
}
// We would like to have batch size as big as possible,
// to reduce the number of round-trips and avoid creating sessions.
// The limit depends on 2 things:
// 1. VM limits: max 2048 items on stack.
// 2. JSON encoded size for the item with type = 128k.
// It turns out, that for container ID the second limit is hit first,
// 512 is big enough value and it is beautiful.
const batchSize = 512
cb := appendCIDtoList
if doSend {
cb = func(item stackitem.Item) error {
// if len(cidList) == 0 {
// wasSent = false
// }
if err := appendCIDtoList(item); err != nil {
return err
}
// ids = append(ids, id)
// if len(cidList) == batchSize { // that's wrong check, need to fix
// wasSent = true
prm.Resp.GetBody().SetContainerIDs(getRefCIDList(cidList))
// cidList = make([]cid.ID, 0, 512)
return prm.Stream.Send(prm.Resp)
// do send() and return
// }
// wasSent = false
// return nil
}
}
cnrHash := c.client.ContractAddress()
err := c.client.Morph().TestInvokeIterator(cb, batchSize, cnrHash, containersOfMethod, rawID)
if err != nil {
if errors.Is(err, unwrap.ErrNoSessionID) {
return c.list(idUser)
return c.list(idUser) // <- here it returns the WHOLE list, need to break in batches n send()
}
return nil, err
}
// if !wasSent && doSend {
// // do send() the rest
// prm.Resp.GetBody().SetContainerIDs(getRefCIDList(cidList))
// prm.Stream.Send(prm.Resp)
// }
return cidList, nil
}
func getRefCIDList(cnrs []cid.ID) []refs.ContainerID {
cidList := make([]refs.ContainerID, len(cnrs))
for i := range cnrs {
cnrs[i].WriteToV2(&cidList[i])
}
return cidList
}

View file

@ -29,7 +29,14 @@ type Reader interface {
// ContainersOf returns a list of container identifiers belonging
// to the specified user of FrostFS system. Returns the identifiers
// of all FrostFS containers if pointer to owner identifier is nil.
ContainersOf(*user.ID) ([]cid.ID, error)
ContainersOf(*ClientPrm) ([]cid.ID, error)
}
type ClientPrm struct {
OwnerID *user.ID
// Stream interface + signer??
Stream containerSvc.ListStream
Resp *container.ListStreamResponse
}
// Writer is an interface of container storage updater.
@ -185,7 +192,11 @@ func (s *morphExecutor) List(_ context.Context, body *container.ListRequestBody)
return nil, fmt.Errorf("invalid user ID: %w", err)
}
cnrs, err := s.rdr.ContainersOf(&id)
prm := &ClientPrm{
OwnerID: &id,
}
cnrs, err := s.rdr.ContainersOf(prm)
if err != nil {
return nil, err
}
@ -215,20 +226,27 @@ func (s *morphExecutor) ListStream(_ context.Context, req *container.ListStreamR
return fmt.Errorf("invalid user ID: %w", err)
}
cnrs, err := s.rdr.ContainersOf(&id)
resBody := new(container.ListStreamResponseBody)
r := new(container.ListStreamResponse)
r.SetBody(resBody)
prm := &ClientPrm{
OwnerID: &id,
Stream: stream,
Resp: r,
}
cnrs, err := s.rdr.ContainersOf(prm)
if err != nil {
return err
}
// here starts duplicated sending, need to add condition
cidList := make([]refs.ContainerID, len(cnrs))
for i := range cnrs {
cnrs[i].WriteToV2(&cidList[i])
}
resBody := new(container.ListStreamResponseBody)
resBody.SetContainerIDs(cidList)
r := new(container.ListStreamResponse)
r.SetBody(resBody)
return stream.Send(r)
}

View file

@ -7,6 +7,7 @@ import (
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/morph/client/container"
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/morph/client/netmap"
containerMorph "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/services/container/morph"
control "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/services/control/ir"
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/api/refs"
cid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id"
@ -138,7 +139,11 @@ func (s *Server) RemoveContainer(ctx context.Context, req *control.RemoveContain
return nil, status.Error(codes.InvalidArgument, "failed to read owner: "+err.Error())
}
cids, err := s.containerClient.ContainersOf(&owner)
prm := &containerMorph.ClientPrm{
OwnerID: &owner,
}
cids, err := s.containerClient.ContainersOf(prm)
if err != nil {
return nil, fmt.Errorf("failed to get owner's containers: %w", err)
}