go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/cv/internal/rpc/admin/backfill_retention_key.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  	"go.chromium.org/luci/cv/internal/common"
    26  	"go.chromium.org/luci/cv/internal/tryjob"
    27  )
    28  
    29  var backfillRetentionKey = dsmapper.JobConfig{
    30  	Mapper: "backfill-retention-key",
    31  	Query: dsmapper.Query{
    32  		Kind: "Tryjob",
    33  	},
    34  	PageSize:   32,
    35  	ShardCount: 4,
    36  }
    37  
    38  var backfillRetentionKeyFactory = func(_ context.Context, j *dsmapper.Job, _ int) (dsmapper.Mapper, error) {
    39  	return func(ctx context.Context, keys []*datastore.Key) error {
    40  		if len(keys) == 0 {
    41  			return nil
    42  		}
    43  		err := datastore.RunInTransaction(ctx, func(ctx context.Context) error {
    44  			tryjobs := make([]*tryjob.Tryjob, len(keys))
    45  			for i, k := range keys {
    46  				tryjobs[i] = &tryjob.Tryjob{
    47  					ID: common.TryjobID(k.IntID()),
    48  				}
    49  			}
    50  			if err := datastore.Get(ctx, tryjobs); err != nil {
    51  				return errors.Annotate(err, "failed to fetch Tryjobs").Tag(transient.Tag).Err()
    52  			}
    53  			toUpdate := tryjobs[:0] // reuse the slice
    54  			for _, tj := range tryjobs {
    55  				if tj.RetentionKey == "" {
    56  					tj.EVersion++
    57  					toUpdate = append(toUpdate, tj)
    58  				}
    59  			}
    60  			if len(toUpdate) == 0 {
    61  				return nil
    62  			}
    63  			return datastore.Put(ctx, toUpdate)
    64  		}, nil)
    65  		if err != nil {
    66  			return errors.Annotate(err, "failed to update Tryjobs").Tag(transient.Tag).Err()
    67  		}
    68  		return nil
    69  	}, nil
    70  }