[#922] engine: Change interface of container operations

Add `error` to return. Improve docs.

Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
Leonard Lyubich 2021-11-10 18:26:12 +03:00 committed by Alex Vanin
parent a537334f33
commit 10f0bd91d6
2 changed files with 60 additions and 48 deletions

View file

@ -364,10 +364,21 @@ type localStorageLoad struct {
} }
func (d *localStorageLoad) Iterate(f loadcontroller.UsedSpaceFilter, h loadcontroller.UsedSpaceHandler) error { func (d *localStorageLoad) Iterate(f loadcontroller.UsedSpaceFilter, h loadcontroller.UsedSpaceHandler) error {
idList := engine.ListContainers(d.engine) idList, err := engine.ListContainers(d.engine)
if err != nil {
return fmt.Errorf("list containers on engine failure: %w", err)
}
for i := range idList { for i := range idList {
sz := engine.ContainerSize(d.engine, idList[i]) sz, err := engine.ContainerSize(d.engine, idList[i])
if err != nil {
d.log.Debug("failed to calculate container size in storage engine",
zap.Stringer("cid", idList[i]),
zap.String("error", err.Error()),
)
continue
}
d.log.Debug("container size in storage engine calculated successfully", d.log.Debug("container size in storage engine calculated successfully",
zap.Uint64("size", sz), zap.Uint64("size", sz),

View file

@ -6,63 +6,66 @@ import (
"go.uber.org/zap" "go.uber.org/zap"
) )
// ContainerSizePrm groups parameters of ContainerSize operation.
type ContainerSizePrm struct { type ContainerSizePrm struct {
cid *cid.ID cid *cid.ID
} }
// ContainerSizeRes resulting values of ContainerSize operation.
type ContainerSizeRes struct { type ContainerSizeRes struct {
size uint64 size uint64
} }
// ListContainersPrm groups parameters of ListContainers operation.
type ListContainersPrm struct{} type ListContainersPrm struct{}
// ListContainersRes groups resulting values of ListContainers operation.
type ListContainersRes struct { type ListContainersRes struct {
containers []*cid.ID containers []*cid.ID
} }
func (p *ContainerSizePrm) WithContainerID(cid *cid.ID) *ContainerSizePrm { // SetContainerID sets identifier of the container to estimate the size.
if p != nil { func (p *ContainerSizePrm) SetContainerID(cid *cid.ID) {
p.cid = cid p.cid = cid
} }
return p // Size returns calculated estimation of the container size.
} func (r ContainerSizeRes) Size() uint64 {
func (r *ContainerSizeRes) Size() uint64 {
return r.size return r.size
} }
func (r *ListContainersRes) Containers() []*cid.ID { // Containers returns list of identifiers of the containers in which local objects are stored.
func (r ListContainersRes) Containers() []*cid.ID {
return r.containers return r.containers
} }
// ContainerSize returns sum of estimation container sizes among all shards. // ContainerSize returns sum of estimation container sizes among all shards.
// //
// Returns empty result if executions are blocked (see BlockExecution). // Returns an error if executions are blocked (see BlockExecution).
func (e *StorageEngine) ContainerSize(prm *ContainerSizePrm) (res *ContainerSizeRes) { func (e *StorageEngine) ContainerSize(prm ContainerSizePrm) (res *ContainerSizeRes, err error) {
err := e.execIfNotBlocked(func() error { err = e.execIfNotBlocked(func() error {
res = e.containerSize(prm) res, err = e.containerSize(prm)
return nil return err
}) })
if err != nil {
e.log.Debug("container size exec failure",
zap.String("err", err.Error()),
)
}
if res == nil {
res = new(ContainerSizeRes)
}
return return
} }
// ContainerSize returns sum of estimation container sizes among all shards. // ContainerSize calls ContainerSize method on engine to calculate sum of estimation container sizes among all shards.
func ContainerSize(e *StorageEngine, id *cid.ID) uint64 { func ContainerSize(e *StorageEngine, id *cid.ID) (uint64, error) {
return e.ContainerSize(&ContainerSizePrm{cid: id}).Size() var prm ContainerSizePrm
prm.SetContainerID(id)
res, err := e.ContainerSize(prm)
if err != nil {
return 0, err
} }
func (e *StorageEngine) containerSize(prm *ContainerSizePrm) *ContainerSizeRes { return res.Size(), nil
}
func (e *StorageEngine) containerSize(prm ContainerSizePrm) (*ContainerSizeRes, error) {
if e.metrics != nil { if e.metrics != nil {
defer elapsed(e.metrics.AddEstimateContainerSizeDuration)() defer elapsed(e.metrics.AddEstimateContainerSizeDuration)()
} }
@ -85,36 +88,34 @@ func (e *StorageEngine) containerSize(prm *ContainerSizePrm) *ContainerSizeRes {
return false return false
}) })
return &res return &res, nil
} }
// ListContainers returns unique container IDs presented in the engine objects. // ListContainers returns unique container IDs presented in the engine objects.
// //
// Returns empty result if executions are blocked (see BlockExecution). // Returns an error if executions are blocked (see BlockExecution).
func (e *StorageEngine) ListContainers(_ *ListContainersPrm) (res *ListContainersRes) { func (e *StorageEngine) ListContainers(_ ListContainersPrm) (res *ListContainersRes, err error) {
err := e.execIfNotBlocked(func() error { err = e.execIfNotBlocked(func() error {
res = e.listContainers() res, err = e.listContainers()
return nil return err
}) })
if err != nil {
e.log.Debug("list containers exec failure",
zap.String("err", err.Error()),
)
}
if res == nil {
res = new(ListContainersRes)
}
return return
} }
// ListContainers returns unique container IDs presented in the engine objects. // ListContainers calls ListContainers method on engine to get unique container IDs presented in the engine objects.
func ListContainers(e *StorageEngine) []*cid.ID { func ListContainers(e *StorageEngine) ([]*cid.ID, error) {
return e.ListContainers(&ListContainersPrm{}).Containers() var prm ListContainersPrm
res, err := e.ListContainers(prm)
if err != nil {
return nil, err
} }
func (e *StorageEngine) listContainers() *ListContainersRes { return res.Containers(), nil
}
func (e *StorageEngine) listContainers() (*ListContainersRes, error) {
if e.metrics != nil { if e.metrics != nil {
defer elapsed(e.metrics.AddListContainersDuration)() defer elapsed(e.metrics.AddListContainersDuration)()
} }
@ -148,5 +149,5 @@ func (e *StorageEngine) listContainers() *ListContainersRes {
return &ListContainersRes{ return &ListContainersRes{
containers: result, containers: result,
} }, nil
} }