2021-01-22 11:37:28 +00:00
|
|
|
package engine
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/local_object_storage/shard"
|
2021-11-10 07:08:33 +00:00
|
|
|
cid "github.com/nspcc-dev/neofs-sdk-go/container/id"
|
2021-01-22 11:37:28 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
2021-11-10 15:26:12 +00:00
|
|
|
// ContainerSizePrm groups parameters of ContainerSize operation.
|
2021-01-22 11:37:28 +00:00
|
|
|
type ContainerSizePrm struct {
|
2021-05-31 11:03:17 +00:00
|
|
|
cid *cid.ID
|
2021-01-22 11:37:28 +00:00
|
|
|
}
|
|
|
|
|
2021-11-10 15:26:12 +00:00
|
|
|
// ContainerSizeRes resulting values of ContainerSize operation.
|
2021-01-22 11:37:28 +00:00
|
|
|
type ContainerSizeRes struct {
|
|
|
|
size uint64
|
|
|
|
}
|
|
|
|
|
2021-11-10 15:26:12 +00:00
|
|
|
// ListContainersPrm groups parameters of ListContainers operation.
|
2021-01-22 13:21:45 +00:00
|
|
|
type ListContainersPrm struct{}
|
|
|
|
|
2021-11-10 15:26:12 +00:00
|
|
|
// ListContainersRes groups resulting values of ListContainers operation.
|
2021-01-22 13:21:45 +00:00
|
|
|
type ListContainersRes struct {
|
2021-05-31 11:03:17 +00:00
|
|
|
containers []*cid.ID
|
2021-01-22 13:21:45 +00:00
|
|
|
}
|
|
|
|
|
2021-11-10 15:26:12 +00:00
|
|
|
// SetContainerID sets identifier of the container to estimate the size.
|
|
|
|
func (p *ContainerSizePrm) SetContainerID(cid *cid.ID) {
|
|
|
|
p.cid = cid
|
2021-01-22 11:37:28 +00:00
|
|
|
}
|
|
|
|
|
2021-11-10 15:26:12 +00:00
|
|
|
// Size returns calculated estimation of the container size.
|
|
|
|
func (r ContainerSizeRes) Size() uint64 {
|
2021-01-22 11:37:28 +00:00
|
|
|
return r.size
|
|
|
|
}
|
|
|
|
|
2021-11-10 15:26:12 +00:00
|
|
|
// Containers returns list of identifiers of the containers in which local objects are stored.
|
|
|
|
func (r ListContainersRes) Containers() []*cid.ID {
|
2021-01-22 13:21:45 +00:00
|
|
|
return r.containers
|
|
|
|
}
|
|
|
|
|
2021-01-22 11:37:28 +00:00
|
|
|
// ContainerSize returns sum of estimation container sizes among all shards.
|
2021-11-09 15:46:12 +00:00
|
|
|
//
|
2021-11-10 15:26:12 +00:00
|
|
|
// Returns an error if executions are blocked (see BlockExecution).
|
|
|
|
func (e *StorageEngine) ContainerSize(prm ContainerSizePrm) (res *ContainerSizeRes, err error) {
|
|
|
|
err = e.execIfNotBlocked(func() error {
|
|
|
|
res, err = e.containerSize(prm)
|
|
|
|
return err
|
2021-11-09 15:46:12 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
return
|
2021-01-22 11:37:28 +00:00
|
|
|
}
|
|
|
|
|
2021-11-10 15:26:12 +00:00
|
|
|
// ContainerSize calls ContainerSize method on engine to calculate sum of estimation container sizes among all shards.
|
|
|
|
func ContainerSize(e *StorageEngine, id *cid.ID) (uint64, error) {
|
|
|
|
var prm ContainerSizePrm
|
|
|
|
|
|
|
|
prm.SetContainerID(id)
|
|
|
|
|
|
|
|
res, err := e.ContainerSize(prm)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return res.Size(), nil
|
2021-01-22 11:37:28 +00:00
|
|
|
}
|
|
|
|
|
2021-11-10 15:26:12 +00:00
|
|
|
func (e *StorageEngine) containerSize(prm ContainerSizePrm) (*ContainerSizeRes, error) {
|
2021-11-09 15:46:12 +00:00
|
|
|
if e.metrics != nil {
|
|
|
|
defer elapsed(e.metrics.AddEstimateContainerSizeDuration)()
|
|
|
|
}
|
|
|
|
|
|
|
|
var res ContainerSizeRes
|
|
|
|
|
2021-01-22 11:37:28 +00:00
|
|
|
e.iterateOverUnsortedShards(func(s *shard.Shard) (stop bool) {
|
2021-11-09 15:46:12 +00:00
|
|
|
size, err := shard.ContainerSize(s, prm.cid)
|
2021-01-22 11:37:28 +00:00
|
|
|
if err != nil {
|
|
|
|
e.log.Warn("can't get container size",
|
|
|
|
zap.Stringer("shard_id", s.ID()),
|
2021-11-09 15:46:12 +00:00
|
|
|
zap.Stringer("container_id", prm.cid),
|
2021-01-22 11:37:28 +00:00
|
|
|
zap.String("error", err.Error()))
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-11-09 15:46:12 +00:00
|
|
|
res.size += size
|
2021-01-22 11:37:28 +00:00
|
|
|
|
|
|
|
return false
|
|
|
|
})
|
|
|
|
|
2021-11-10 15:26:12 +00:00
|
|
|
return &res, nil
|
2021-01-22 11:37:28 +00:00
|
|
|
}
|
2021-01-22 13:21:45 +00:00
|
|
|
|
|
|
|
// ListContainers returns unique container IDs presented in the engine objects.
|
2021-11-09 15:46:12 +00:00
|
|
|
//
|
2021-11-10 15:26:12 +00:00
|
|
|
// Returns an error if executions are blocked (see BlockExecution).
|
|
|
|
func (e *StorageEngine) ListContainers(_ ListContainersPrm) (res *ListContainersRes, err error) {
|
|
|
|
err = e.execIfNotBlocked(func() error {
|
|
|
|
res, err = e.listContainers()
|
|
|
|
return err
|
2021-11-09 15:46:12 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
return
|
2021-01-22 13:21:45 +00:00
|
|
|
}
|
|
|
|
|
2021-11-10 15:26:12 +00:00
|
|
|
// ListContainers calls ListContainers method on engine to get unique container IDs presented in the engine objects.
|
|
|
|
func ListContainers(e *StorageEngine) ([]*cid.ID, error) {
|
|
|
|
var prm ListContainersPrm
|
|
|
|
|
|
|
|
res, err := e.ListContainers(prm)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return res.Containers(), nil
|
2021-01-22 13:21:45 +00:00
|
|
|
}
|
|
|
|
|
2021-11-10 15:26:12 +00:00
|
|
|
func (e *StorageEngine) listContainers() (*ListContainersRes, error) {
|
2021-11-09 15:46:12 +00:00
|
|
|
if e.metrics != nil {
|
|
|
|
defer elapsed(e.metrics.AddListContainersDuration)()
|
|
|
|
}
|
|
|
|
|
2021-05-31 11:03:17 +00:00
|
|
|
uniqueIDs := make(map[string]*cid.ID)
|
2021-01-22 13:21:45 +00:00
|
|
|
|
|
|
|
e.iterateOverUnsortedShards(func(s *shard.Shard) (stop bool) {
|
|
|
|
cnrs, err := shard.ListContainers(s)
|
|
|
|
if err != nil {
|
|
|
|
e.log.Warn("can't get list of containers",
|
|
|
|
zap.Stringer("shard_id", s.ID()),
|
|
|
|
zap.String("error", err.Error()))
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := range cnrs {
|
|
|
|
id := cnrs[i].String()
|
|
|
|
if _, ok := uniqueIDs[id]; !ok {
|
|
|
|
uniqueIDs[id] = cnrs[i]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
})
|
|
|
|
|
2021-05-31 11:03:17 +00:00
|
|
|
result := make([]*cid.ID, 0, len(uniqueIDs))
|
2021-01-22 13:21:45 +00:00
|
|
|
for _, v := range uniqueIDs {
|
|
|
|
result = append(result, v)
|
|
|
|
}
|
|
|
|
|
2021-11-09 15:46:12 +00:00
|
|
|
return &ListContainersRes{
|
|
|
|
containers: result,
|
2021-11-10 15:26:12 +00:00
|
|
|
}, nil
|
2021-01-22 13:21:45 +00:00
|
|
|
}
|