From 3926e76c32d802eff83a41617d53f47b1586b867 Mon Sep 17 00:00:00 2001 From: Alex Vanin Date: Fri, 22 Jan 2021 14:37:28 +0300 Subject: [PATCH] [#337] engine: Get all container sizes estimation from shards Objects of one container can be split among shards, so engine should iterate over all available shards to sum all size estimations. Signed-off-by: Alex Vanin --- pkg/local_object_storage/engine/container.go | 59 ++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 pkg/local_object_storage/engine/container.go diff --git a/pkg/local_object_storage/engine/container.go b/pkg/local_object_storage/engine/container.go new file mode 100644 index 00000000..008cea4e --- /dev/null +++ b/pkg/local_object_storage/engine/container.go @@ -0,0 +1,59 @@ +package engine + +import ( + "github.com/nspcc-dev/neofs-api-go/pkg/container" + "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/shard" + "go.uber.org/zap" +) + +type ContainerSizePrm struct { + cid *container.ID +} + +type ContainerSizeRes struct { + size uint64 +} + +func (p *ContainerSizePrm) WithContainerID(cid *container.ID) *ContainerSizePrm { + if p != nil { + p.cid = cid + } + + return p +} + +func (r *ContainerSizeRes) Size() uint64 { + return r.size +} + +// ContainerSize returns sum of estimation container sizes among all shards. +func (e *StorageEngine) ContainerSize(prm *ContainerSizePrm) *ContainerSizeRes { + return &ContainerSizeRes{ + size: e.containerSize(prm.cid), + } +} + +// ContainerSize returns sum of estimation container sizes among all shards. +func ContainerSize(e *StorageEngine, id *container.ID) uint64 { + return e.ContainerSize(&ContainerSizePrm{cid: id}).Size() +} + +func (e *StorageEngine) containerSize(id *container.ID) (total uint64) { + e.iterateOverUnsortedShards(func(s *shard.Shard) (stop bool) { + size, err := shard.ContainerSize(s, id) + if err != nil { + e.log.Warn("can't get container size", + zap.Stringer("shard_id", s.ID()), + zap.Stringer("container_id", id), + zap.String("error", err.Error())) + + return false + } + + total += size + + return false + }) + + return total +}