go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/cv/internal/rpc/admin/delete_entities.go (about)

     1  // Copyright 2024 The LUCI Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package admin
    16  
    17  import (
    18  	"context"
    19  
    20  	"go.chromium.org/luci/common/errors"
    21  	"go.chromium.org/luci/common/retry/transient"
    22  	"go.chromium.org/luci/gae/service/datastore"
    23  	"go.chromium.org/luci/server/dsmapper"
    24  )
    25  
    26  const deleteEntityKind = "migration.VerifiedCQDRun"
    27  
    28  var deleteEntitiesKey = dsmapper.JobConfig{
    29  	Mapper: "delete-entities",
    30  	Query: dsmapper.Query{
    31  		Kind: deleteEntityKind,
    32  	},
    33  	PageSize:   32,
    34  	ShardCount: 4,
    35  }
    36  
    37  var deleteEntitiesFactory = func(_ context.Context, j *dsmapper.Job, _ int) (dsmapper.Mapper, error) {
    38  	return func(ctx context.Context, keys []*datastore.Key) error {
    39  		if len(keys) == 0 {
    40  			return nil
    41  		}
    42  		var merrs errors.MultiError
    43  		switch err := datastore.Delete(ctx, keys); {
    44  		case err == nil:
    45  			return nil
    46  		case errors.As(err, merrs):
    47  			for _, err := range merrs {
    48  				if !errors.Is(err, datastore.ErrNoSuchEntity) {
    49  					return errors.Annotate(merrs, "failed to delete keys").Tag(transient.Tag).Err()
    50  				}
    51  			}
    52  			return nil
    53  		default:
    54  			return errors.Annotate(err, "failed to delete keys").Tag(transient.Tag).Err()
    55  		}
    56  	}, nil
    57  }