Coverage for /builds/BuildGrid/buildgrid/buildgrid/server/referencestorage/service.py: 68.06%

72 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2023-06-05 15:37 +0000

1# Copyright (C) 2018 Bloomberg LP 

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 

16import logging 

17 

18import grpc 

19 

20from buildgrid._exceptions import InvalidArgumentError, NotFoundError 

21from buildgrid._protos.buildstream.v2 import buildstream_pb2, buildstream_pb2_grpc 

22from buildgrid.server._authentication import AuthContext, authorize 

23from buildgrid.server.referencestorage.storage import ReferenceCache 

24from buildgrid.server.servicer import InstancedServicer 

25from buildgrid.server.utils.decorators import track_request_id 

26 

27LOGGER = logging.getLogger(__name__) 

28 

29 

30class ReferenceStorageService( 

31 buildstream_pb2_grpc.ReferenceStorageServicer, 

32 InstancedServicer[ReferenceCache], 

33): 

34 REGISTER_METHOD = buildstream_pb2_grpc.add_ReferenceStorageServicer_to_server 

35 FULL_NAME = buildstream_pb2.DESCRIPTOR.services_by_name["ReferenceStorage"].full_name 

36 

37 @track_request_id 

38 @authorize(AuthContext) 

39 def GetReference( 

40 self, request: buildstream_pb2.GetReferenceRequest, context: grpc.ServicerContext 

41 ) -> buildstream_pb2.GetReferenceResponse: 

42 LOGGER.debug(f"GetReference request from [{context.peer()}]") 

43 

44 try: 

45 instance = self.get_instance(request.instance_name) 

46 digest = instance.get_digest_reference(request.key) 

47 response = buildstream_pb2.GetReferenceResponse() 

48 response.digest.CopyFrom(digest) 

49 return response 

50 

51 except InvalidArgumentError as e: 

52 LOGGER.info(e) 

53 context.set_details(str(e)) 

54 context.set_code(grpc.StatusCode.INVALID_ARGUMENT) 

55 

56 except NotFoundError as e: 

57 LOGGER.debug(e) 

58 context.set_code(grpc.StatusCode.NOT_FOUND) 

59 

60 except Exception as e: 

61 LOGGER.exception(f"Unexpected error in GetReference; request=[{request}]") 

62 context.set_details(str(e)) 

63 context.set_code(grpc.StatusCode.INTERNAL) 

64 

65 return buildstream_pb2.GetReferenceResponse() 

66 

67 @track_request_id 

68 @authorize(AuthContext) 

69 def UpdateReference( 

70 self, request: buildstream_pb2.UpdateReferenceRequest, context: grpc.ServicerContext 

71 ) -> buildstream_pb2.UpdateReferenceResponse: 

72 LOGGER.debug(f"UpdateReference request from [{context.peer()}]") 

73 

74 try: 

75 instance = self.get_instance(request.instance_name) 

76 digest = request.digest 

77 

78 for key in request.keys: 

79 instance.update_reference(key, digest) 

80 

81 except InvalidArgumentError as e: 

82 LOGGER.info(e) 

83 context.set_details(str(e)) 

84 context.set_code(grpc.StatusCode.INVALID_ARGUMENT) 

85 

86 except NotImplementedError as e: 

87 LOGGER.info(e) 

88 context.set_code(grpc.StatusCode.UNIMPLEMENTED) 

89 

90 except Exception as e: 

91 LOGGER.exception(f"Unexpected error in UpdateReference; request=[{request}]") 

92 context.set_details(str(e)) 

93 context.set_code(grpc.StatusCode.INTERNAL) 

94 

95 return buildstream_pb2.UpdateReferenceResponse() 

96 

97 @track_request_id 

98 @authorize(AuthContext) 

99 def Status( 

100 self, request: buildstream_pb2.StatusRequest, context: grpc.ServicerContext 

101 ) -> buildstream_pb2.StatusResponse: 

102 LOGGER.debug(f"Status request from [{context.peer()}]") 

103 

104 try: 

105 instance = self.get_instance(request.instance_name) 

106 allow_updates = instance.allow_updates 

107 return buildstream_pb2.StatusResponse(allow_updates=allow_updates) 

108 

109 except InvalidArgumentError as e: 

110 LOGGER.info(e) 

111 context.set_details(str(e)) 

112 context.set_code(grpc.StatusCode.INVALID_ARGUMENT) 

113 

114 except Exception as e: 

115 LOGGER.exception(f"Unexpected error in Status; request=[{request}]") 

116 context.set_details(str(e)) 

117 context.set_code(grpc.StatusCode.INTERNAL) 

118 

119 return buildstream_pb2.StatusResponse()