[#1709] shard: Check if context canceled for shard iteration

If context has already been canceled, then there is no need to check other shards.
At the same time, it is necessary to avoid handling context cancellation
in each handler. Therefore, the context check has been moved to the shard
iteration method, which now returns an error.

Change-Id: I70030ace36593ce7d2b8376bee39fe82e9dbf88f
Signed-off-by: Dmitrii Stepanov <d.stepanov@yadro.com>
This commit is contained in:
Dmitrii Stepanov 2025-04-21 12:13:32 +03:00
parent a27e003508
commit 3a441f072f
Signed by: dstepanov-yadro
GPG key ID: 237AF1A763293BC0
12 changed files with 149 additions and 86 deletions

View file

@ -48,8 +48,9 @@ func (e *StorageEngine) ContainerSize(ctx context.Context, prm ContainerSizePrm)
defer elapsed("ContainerSize", e.metrics.AddMethodDuration)()
err = e.execIfNotBlocked(func() error {
res = e.containerSize(ctx, prm)
return nil
var csErr error
res, csErr = e.containerSize(ctx, prm)
return csErr
})
return
@ -69,8 +70,9 @@ func ContainerSize(ctx context.Context, e *StorageEngine, id cid.ID) (uint64, er
return res.Size(), nil
}
func (e *StorageEngine) containerSize(ctx context.Context, prm ContainerSizePrm) (res ContainerSizeRes) {
e.iterateOverUnsortedShards(func(sh hashedShard) (stop bool) {
func (e *StorageEngine) containerSize(ctx context.Context, prm ContainerSizePrm) (ContainerSizeRes, error) {
var res ContainerSizeRes
err := e.iterateOverUnsortedShards(ctx, func(sh hashedShard) (stop bool) {
var csPrm shard.ContainerSizePrm
csPrm.SetContainerID(prm.cnr)
@ -86,7 +88,7 @@ func (e *StorageEngine) containerSize(ctx context.Context, prm ContainerSizePrm)
return false
})
return
return res, err
}
// ListContainers returns a unique container IDs presented in the engine objects.
@ -96,8 +98,9 @@ func (e *StorageEngine) ListContainers(ctx context.Context, _ ListContainersPrm)
defer elapsed("ListContainers", e.metrics.AddMethodDuration)()
err = e.execIfNotBlocked(func() error {
res = e.listContainers(ctx)
return nil
var lcErr error
res, lcErr = e.listContainers(ctx)
return lcErr
})
return
@ -115,10 +118,10 @@ func ListContainers(ctx context.Context, e *StorageEngine) ([]cid.ID, error) {
return res.Containers(), nil
}
func (e *StorageEngine) listContainers(ctx context.Context) ListContainersRes {
func (e *StorageEngine) listContainers(ctx context.Context) (ListContainersRes, error) {
uniqueIDs := make(map[string]cid.ID)
e.iterateOverUnsortedShards(func(sh hashedShard) (stop bool) {
if err := e.iterateOverUnsortedShards(ctx, func(sh hashedShard) (stop bool) {
res, err := sh.ListContainers(ctx, shard.ListContainersPrm{})
if err != nil {
e.reportShardError(ctx, sh, "can't get list of containers", err)
@ -133,7 +136,9 @@ func (e *StorageEngine) listContainers(ctx context.Context) ListContainersRes {
}
return false
})
}); err != nil {
return ListContainersRes{}, err
}
result := make([]cid.ID, 0, len(uniqueIDs))
for _, v := range uniqueIDs {
@ -142,5 +147,5 @@ func (e *StorageEngine) listContainers(ctx context.Context) ListContainersRes {
return ListContainersRes{
containers: result,
}
}, nil
}