fix(storage-users): cap the errors collected while purging the trash-bin

When the storage provider is down, the gateway answers PurgeRecycle with
a failed status instead of a transport error, so every expired item adds
an error. Keep the first ten and count the rest.
This commit is contained in:
Junker der Provinz committed 2026-12-04 10:07:41 +01:00
1 parent 4db5963734
commit ced4256a9f
2 files changed
+56 -8

No files matched your search

+33 -8
View File
@@ -17,7 +17,7 @@ import (
// the provided executantID must have space access.
// removeBefore specifies how long an item must be in the trash-bin to be deleted,
// items that stay there for a shorter time are ignored and kept in place.
// Items the storage cannot purge are skipped, and their errors are returned together at the end.
// Items the storage cannot purge are skipped, and up to ten of their errors are returned together at the end.
func PurgeTrashBin(serviceAccountID string, deleteBefore time.Time, spaceType SpaceType, gatewaySelector pool.Selectable[apiGateway.GatewayAPIClient], serviceAccountSecret string) error {
gatewayClient, err := gatewaySelector.Next()
if err != nil {
@@ -47,7 +47,7 @@ func PurgeTrashBin(serviceAccountID string, deleteBefore time.Time, spaceType Sp
return err
}
var errs []error
var errs purgeErrors
for _, storageSpace := range listStorageSpacesResponse.StorageSpaces {
if typ := storageSpace.GetSpaceType(); typ != "personal" && typ != "project" {
// ignore spaces that are neither personal nor project
@@ -59,11 +59,11 @@ func PurgeTrashBin(serviceAccountID string, deleteBefore time.Time, spaceType Sp
gatewayClient, err = gatewaySelector.Next()
if err != nil {
return errors.Join(append(errs, err)...)
return errs.join(err)
}
listRecycleResponse, err := gatewayClient.ListRecycle(ctx, &apiProvider.ListRecycleRequest{Ref: storageSpaceReference})
if err != nil {
return errors.Join(append(errs, err)...)
return errs.join(err)
}
for _, recycleItem := range listRecycleResponse.GetRecycleItems() {
@@ -74,21 +74,46 @@ func PurgeTrashBin(serviceAccountID string, deleteBefore time.Time, spaceType Sp
gatewayClient, err = gatewaySelector.Next()
if err != nil {
return errors.Join(append(errs, err)...)
return errs.join(err)
}
purgeRecycleResponse, err := gatewayClient.PurgeRecycle(ctx, &apiProvider.PurgeRecycleRequest{
Ref: storageSpaceReference,
Key: recycleItem.Key,
})
if err != nil {
return errors.Join(append(errs, err)...)
return errs.join(err)
}
if purgeRecycleResponse.GetStatus().GetCode() != apiRpc.Code_CODE_OK {
err := errtypes.NewErrtypeFromStatus(purgeRecycleResponse.GetStatus())
errs = append(errs, fmt.Errorf("recycle item %s in space %s: %w", recycleItem.GetKey(), storageSpace.GetId().GetOpaqueId(), err))
errs.add(fmt.Errorf("recycle item %s in space %s: %w", recycleItem.GetKey(), storageSpace.GetId().GetOpaqueId(), err))
}
}
}
return errors.Join(errs...)
return errs.join()
}
const maxReportedPurgeErrors = 10
// purgeErrors keeps the first errors of a run, so a storage provider that is down and fails
// every item does not end up as a log line of several megabytes.
type purgeErrors struct {
errs []error
dropped int
}
func (p *purgeErrors) add(err error) {
if len(p.errs) < maxReportedPurgeErrors {
p.errs = append(p.errs, err)
return
}
p.dropped++
}
func (p *purgeErrors) join(errs ...error) error {
all := p.errs
if p.dropped > 0 {
all = append(all, fmt.Errorf("and %d more", p.dropped))
}
return errors.Join(append(all, errs...)...)
}
@@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"errors"
"fmt"
"time"
apiGateway "github.com/cs3org/go-cs3apis/cs3/gateway/v1beta1"
@@ -250,6 +251,28 @@ var _ = Describe("trash", func() {
Expect(recycleItems["project"]).To(BeEmpty())
Expect(err).To(MatchError(ContainSubstring("broken")))
})
It("reports only the first failures when many items cannot be purged", func() {
before := utils.TimeToTS(now.Add(-1 * time.Minute))
var items []*apiProvider.RecycleItem
for i := range 25 {
items = append(items, &apiProvider.RecycleItem{Key: fmt.Sprintf("item-%02d", i), DeletionTime: before})
}
listStorageSpacesResponse.StorageSpaces = []*apiProvider.StorageSpace{personalSpace}
gatewayClient.On("GetUser", mock.Anything, mock.Anything).Return(getUserResponse, nil)
gatewayClient.On("Authenticate", mock.Anything, mock.Anything).Return(authenticateResponse, nil)
gatewayClient.On("ListStorageSpaces", mock.Anything, mock.Anything).Return(listStorageSpacesResponse, nil)
gatewayClient.On("ListRecycle", mock.Anything, mock.Anything).Return(&apiProvider.ListRecycleResponse{RecycleItems: items}, nil)
gatewayClient.On("PurgeRecycle", mock.Anything, mock.Anything).Return(&apiProvider.PurgeRecycleResponse{
Status: status.NewUnavailable(ctx, "gateway could not call PurgeRecycle"),
}, nil)
err := task.PurgeTrashBin("service-user-id", now, task.Personal, gatewaySelector, "")
gatewayClient.AssertNumberOfCalls(GinkgoT(), "PurgeRecycle", 25)
Expect(err).To(MatchError(ContainSubstring("item-09")))
Expect(err).NotTo(MatchError(ContainSubstring("item-10")))
Expect(err).To(MatchError(ContainSubstring("and 15 more")))
})
It("stops purging when the gateway cannot be reached", func() {
before := utils.TimeToTS(now.Add(-1 * time.Minute))
listStorageSpacesResponse.StorageSpaces = []*apiProvider.StorageSpace{personalSpace}