Use ContainersOf for container list fetching #172

Merged
fyrchik merged 3 commits from fyrchik/frostfs-node:fix-container-list into master 2023-03-27 14:32:56 +00:00
6 changed files with 104 additions and 10 deletions

View File

@ -242,7 +242,7 @@ func newCachedContainerLister(c *cntClient.Client, ttl time.Duration) ttlContain
}
}
list, err := c.List(id)
list, err := c.ContainersOf(id)
if err != nil {
return nil, err
}
@ -260,7 +260,7 @@ func newCachedContainerLister(c *cntClient.Client, ttl time.Duration) ttlContain
// the cache.
func (s ttlContainerLister) List(id *user.ID) ([]cid.ID, error) {
if id == nil {
return s.client.List(nil)
return s.client.ContainersOf(nil)
}
item, err := s.inner.get(id.EncodeToString())

View File

@ -31,7 +31,7 @@ func (c cnrSource) Get(id cid.ID) (*container.Container, error) {
}
func (c cnrSource) List() ([]cid.ID, error) {
return c.cli.List(nil)
return c.cli.ContainersOf(nil)
}
func initTreeService(c *cfg) {

View File

@ -13,7 +13,7 @@ import (
var ErrInvalidIRNode = errors.New("node is not in the inner ring list")
func (ap *Processor) selectContainersToAudit(epoch uint64) ([]cid.ID, error) {
containers, err := ap.containerClient.List(nil)
containers, err := ap.containerClient.ContainersOf(nil)
if err != nil {
return nil, fmt.Errorf("can't get list of containers to start audit: %w", err)
}

View File

@ -19,6 +19,7 @@ import (
"github.com/nspcc-dev/neo-go/pkg/rpcclient/gas"
"github.com/nspcc-dev/neo-go/pkg/rpcclient/nep17"
"github.com/nspcc-dev/neo-go/pkg/rpcclient/rolemgmt"
"github.com/nspcc-dev/neo-go/pkg/rpcclient/unwrap"
sc "github.com/nspcc-dev/neo-go/pkg/smartcontract"
"github.com/nspcc-dev/neo-go/pkg/smartcontract/trigger"
"github.com/nspcc-dev/neo-go/pkg/util"
@ -200,6 +201,45 @@ func (c *Client) Invoke(contract util.Uint160, fee fixedn.Fixed8, method string,
return nil
}
// TestInvokeIterator invokes contract method returning an iterator and executes cb on each element.
// If cb returns an error, the session is closed and this error is returned as-is.
// If the remove neo-go node does not support sessions, `unwrap.ErrNoSessionID` is returned.
func (c *Client) TestInvokeIterator(cb func(stackitem.Item) error, contract util.Uint160, method string, args ...interface{}) error {
c.switchLock.RLock()
defer c.switchLock.RUnlock()
if c.inactive {
return ErrConnectionLost
}
val, err := c.rpcActor.Call(contract, method, args...)
if err != nil {
return err
} else if val.State != HaltState {
return wrapFrostFSError(&notHaltStateError{state: val.State, exception: val.FaultException})
}
sid, r, err := unwrap.SessionIterator(val, err)
if err != nil {
return err
}
defer func() {
_ = c.rpcActor.TerminateSession(sid)
}()
items, err := c.rpcActor.TraverseIterator(sid, &r, 0)
for err == nil && len(items) != 0 {
for i := range items {
if err = cb(items[i]); err != nil {
return err
}
}
items, err = c.rpcActor.TraverseIterator(sid, &r, 0)
}
return err
}
// TestInvoke invokes contract method locally in neo-go node. This method should
// be used to read data from smart-contract.
func (c *Client) TestInvoke(contract util.Uint160, method string, args ...any) (res []stackitem.Item, err error) {

View File

@ -22,12 +22,13 @@ type Client struct {
}
const (
putMethod = "put"
deleteMethod = "delete"
getMethod = "get"
listMethod = "list"
eaclMethod = "eACL"
setEACLMethod = "setEACL"
putMethod = "put"
deleteMethod = "delete"
getMethod = "get"
listMethod = "list"
containersOfMethod = "containersOf"
eaclMethod = "eACL"
setEACLMethod = "setEACL"
startEstimationMethod = "startContainerEstimation"
stopEstimationMethod = "stopContainerEstimation"

View File

@ -0,0 +1,53 @@
package container
import (
"errors"
"fmt"
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/morph/client"
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"
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
)
// ContainersOf returns a list of container identifiers belonging
// 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) {
var rawID []byte
if idUser != nil {
rawID = idUser.WalletBytes()
}
var cidList []cid.ID
cb := func(item stackitem.Item) error {
rawID, err := client.BytesFromStackItem(item)
if err != nil {
return fmt.Errorf("could not get byte array from stack item (%s): %w", containersOfMethod, err)
}
var id cid.ID
err = id.Decode(rawID)
if err != nil {
return fmt.Errorf("decode container ID: %w", err)
}
cidList = append(cidList, id)
return nil
}
cnrHash := c.client.ContractAddress()
err := c.client.Morph().TestInvokeIterator(cb, cnrHash, containersOfMethod, rawID)
if err != nil {
if errors.Is(err, unwrap.ErrNoSessionID) {
return c.List(idUser)
}
return nil, err
}
return cidList, nil
}