go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/cv/internal/run/rdb/rdb.go (about)

     1  // Copyright 2023 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 rdb
    16  
    17  import (
    18  	"context"
    19  	"net/http"
    20  
    21  	"google.golang.org/grpc/codes"
    22  
    23  	"go.chromium.org/luci/common/errors"
    24  	"go.chromium.org/luci/common/retry/transient"
    25  
    26  	"go.chromium.org/luci/grpc/appstatus"
    27  	"go.chromium.org/luci/grpc/prpc"
    28  	"go.chromium.org/luci/server/auth"
    29  
    30  	rdbpb "go.chromium.org/luci/resultdb/proto/v1"
    31  )
    32  
    33  type RecorderClientFactory interface {
    34  	MakeClient(ctx context.Context, host string) (*RecorderClient, error)
    35  }
    36  
    37  type prodClientFactory struct{}
    38  
    39  // MakeClient implements RecorderClientFactory.
    40  //
    41  // Creates a client to interact with Recorder, acting as LUCI CV to access data.
    42  func (pcf *prodClientFactory) MakeClient(ctx context.Context, host string) (*RecorderClient, error) {
    43  	transport, err := auth.GetRPCTransport(ctx, auth.AsSelf)
    44  	if err != nil {
    45  		return nil, err
    46  	}
    47  
    48  	return &RecorderClient{
    49  		client: rdbpb.NewRecorderPRPCClient(
    50  			&prpc.Client{
    51  				C:                &http.Client{Transport: transport},
    52  				Host:             host,
    53  				Options:          prpc.DefaultOptions(),
    54  				MaxContentLength: prpc.DefaultMaxContentLength,
    55  			}),
    56  	}, nil
    57  }
    58  
    59  func NewRecorderClientFactory() RecorderClientFactory {
    60  	return &prodClientFactory{}
    61  }
    62  
    63  type RecorderClient struct {
    64  	client rdbpb.RecorderClient
    65  }
    66  
    67  func (rc *RecorderClient) MarkInvocationSubmitted(ctx context.Context, invocation string) error {
    68  	req := &rdbpb.MarkInvocationSubmittedRequest{
    69  		Invocation: invocation,
    70  	}
    71  	if _, err := rc.client.MarkInvocationSubmitted(ctx, req); err != nil {
    72  		s, ok := appstatus.Get(err)
    73  		if !ok {
    74  			// no status code attached to the err
    75  			return err
    76  		}
    77  		// Note: If it is already marked submitted the RPC will take no action, so
    78  		// retrying these are safe.
    79  		// PermissionDenied might indicate that either invocation cannot be found
    80  		// or we do not have permission so we avoid retries.
    81  		switch s.Code() {
    82  		case codes.PermissionDenied, codes.InvalidArgument:
    83  			return errors.Annotate(err, "failed to mark %s submitted", invocation).Err()
    84  		default:
    85  			return errors.Annotate(err, "failed to mark %s submitted", invocation).Tag(transient.Tag).Err()
    86  		}
    87  	}
    88  
    89  	return nil
    90  }