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

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

69 statements  

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 

22from buildgrid._protos.buildstream.v2 import buildstream_pb2_grpc 

23from buildgrid.server._authentication import AuthContext, authorize 

24 

25 

26class ReferenceStorageService(buildstream_pb2_grpc.ReferenceStorageServicer): 

27 

28 def __init__(self, server): 

29 self.__logger = logging.getLogger(__name__) 

30 

31 self._instances = {} 

32 

33 buildstream_pb2_grpc.add_ReferenceStorageServicer_to_server(self, server) 

34 

35 # --- Public API --- 

36 

37 def add_instance(self, name, instance): 

38 self._instances[name] = instance 

39 

40 # --- Public API: Servicer --- 

41 

42 @authorize(AuthContext) 

43 def GetReference(self, request, context): 

44 self.__logger.debug(f"GetReference request from [{context.peer()}]") 

45 

46 try: 

47 instance = self._get_instance(request.instance_name) 

48 digest = instance.get_digest_reference(request.key) 

49 response = buildstream_pb2.GetReferenceResponse() 

50 response.digest.CopyFrom(digest) 

51 return response 

52 

53 except InvalidArgumentError as e: 

54 self.__logger.info(e) 

55 context.set_details(str(e)) 

56 context.set_code(grpc.StatusCode.INVALID_ARGUMENT) 

57 

58 except NotFoundError as e: 

59 self.__logger.debug(e) 

60 context.set_code(grpc.StatusCode.NOT_FOUND) 

61 

62 except Exception: 

63 self.__logger.exception( 

64 f"Unexpected error in GetReference; request=[{request}]" 

65 ) 

66 context.set_code(grpc.StatusCode.INTERNAL) 

67 

68 return buildstream_pb2.GetReferenceResponse() 

69 

70 @authorize(AuthContext) 

71 def UpdateReference(self, request, context): 

72 self.__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 self.__logger.info(e) 

83 context.set_details(str(e)) 

84 context.set_code(grpc.StatusCode.INVALID_ARGUMENT) 

85 

86 except NotImplementedError as e: 

87 self.__logger.info(e) 

88 context.set_code(grpc.StatusCode.UNIMPLEMENTED) 

89 

90 except Exception: 

91 self.__logger.exception( 

92 f"Unexpected error in UpdateReference; request=[{request}]" 

93 ) 

94 context.set_code(grpc.StatusCode.INTERNAL) 

95 

96 return buildstream_pb2.UpdateReferenceResponse() 

97 

98 @authorize(AuthContext) 

99 def Status(self, request, context): 

100 self.__logger.debug(f"Status request from [{context.peer()}]") 

101 

102 try: 

103 instance = self._get_instance(request.instance_name) 

104 allow_updates = instance.allow_updates 

105 return buildstream_pb2.StatusResponse(allow_updates=allow_updates) 

106 

107 except InvalidArgumentError as e: 

108 self.__logger.info(e) 

109 context.set_details(str(e)) 

110 context.set_code(grpc.StatusCode.INVALID_ARGUMENT) 

111 

112 except Exception: 

113 self.__logger.exception( 

114 f"Unexpected error in Status; request=[{request}]" 

115 ) 

116 context.set_code(grpc.StatusCode.INTERNAL) 

117 

118 return buildstream_pb2.StatusResponse() 

119 

120 # --- Private API --- 

121 

122 def _get_instance(self, instance_name): 

123 try: 

124 return self._instances[instance_name] 

125 

126 except KeyError: 

127 raise InvalidArgumentError(f"Invalid instance name: [{instance_name}]")