go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/logdog/appengine/coordinator/endpoints/services/loadStream.go (about) 1 // Copyright 2015 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 services 16 17 import ( 18 "context" 19 20 "google.golang.org/grpc/codes" 21 "google.golang.org/grpc/status" 22 "google.golang.org/protobuf/types/known/durationpb" 23 24 ds "go.chromium.org/luci/gae/service/datastore" 25 26 logdog "go.chromium.org/luci/logdog/api/endpoints/coordinator/services/v1" 27 "go.chromium.org/luci/logdog/appengine/coordinator" 28 29 "go.chromium.org/luci/common/clock" 30 "go.chromium.org/luci/common/errors" 31 log "go.chromium.org/luci/common/logging" 32 ) 33 34 // LoadStream loads the log stream state. 35 func (s *server) LoadStream(c context.Context, req *logdog.LoadStreamRequest) (*logdog.LoadStreamResponse, error) { 36 id := coordinator.HashID(req.Id) 37 if err := id.Normalize(); err != nil { 38 log.WithError(err).Errorf(c, "Invalid stream ID.") 39 return nil, status.Errorf(codes.InvalidArgument, "Invalid ID (%s): %s", id, err) 40 } 41 42 ls := &coordinator.LogStream{ID: coordinator.HashID(req.Id)} 43 lst := ls.State(c) 44 45 if err := ds.Get(c, lst, ls); err != nil { 46 if anyNoSuchEntity(err) { 47 log.WithError(err).Errorf(c, "No such entity in datastore (log stream).") 48 49 // The state isn't registered, so this stream does not exist. 50 return nil, status.Errorf(codes.NotFound, "Log stream was not found.") 51 } 52 53 log.WithError(err).Errorf(c, "Failed to load log stream.") 54 return nil, status.Error(codes.Internal, "internal server error") 55 } 56 57 // The log stream and state loaded successfully. 58 resp := logdog.LoadStreamResponse{ 59 State: buildLogStreamState(ls, lst), 60 } 61 if req.Desc { 62 resp.Desc = ls.Descriptor 63 } 64 resp.ArchivalKey = lst.ArchivalKey 65 resp.Age = durationpb.New(ds.RoundTime(clock.Now(c)).Sub(lst.Updated)) 66 67 return &resp, nil 68 } 69 70 func anyNoSuchEntity(err error) bool { 71 return errors.Any(err, func(err error) bool { 72 return err == ds.ErrNoSuchEntity 73 }) 74 }