Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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 

16""" 

17ActionCacheService 

18================== 

19 

20Allows clients to manually query/update the action cache. 

21""" 

22 

23import logging 

24 

25import grpc 

26 

27from buildgrid._exceptions import InvalidArgumentError, NotFoundError 

28from buildgrid._protos.build.bazel.remote.execution.v2 import remote_execution_pb2 

29from buildgrid._protos.build.bazel.remote.execution.v2 import remote_execution_pb2_grpc 

30from buildgrid.server._authentication import AuthContext, authorize 

31from buildgrid.server.request_metadata_utils import printable_request_metadata 

32 

33 

34class ActionCacheService(remote_execution_pb2_grpc.ActionCacheServicer): 

35 

36 def __init__(self, server): 

37 self.__logger = logging.getLogger(__name__) 

38 

39 self._instances = {} 

40 

41 remote_execution_pb2_grpc.add_ActionCacheServicer_to_server(self, server) 

42 

43 # --- Public API --- 

44 

45 def add_instance(self, name, instance): 

46 self._instances[name] = instance 

47 

48 # --- Public API: Servicer --- 

49 

50 @authorize(AuthContext) 

51 def GetActionResult(self, request, context): 

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

53 f"([{printable_request_metadata(context)}])") 

54 

55 try: 

56 instance = self._get_instance(request.instance_name) 

57 return instance.get_action_result(request.action_digest) 

58 

59 except InvalidArgumentError as e: 

60 self.__logger.error(e) 

61 context.set_details(str(e)) 

62 context.set_code(grpc.StatusCode.INVALID_ARGUMENT) 

63 

64 except NotFoundError as e: 

65 self.__logger.debug(e) 

66 context.set_code(grpc.StatusCode.NOT_FOUND) 

67 

68 return remote_execution_pb2.ActionResult() 

69 

70 @authorize(AuthContext) 

71 def UpdateActionResult(self, request, context): 

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

73 f"([{printable_request_metadata(context)}])") 

74 

75 try: 

76 instance = self._get_instance(request.instance_name) 

77 instance.update_action_result(request.action_digest, request.action_result) 

78 return request.action_result 

79 

80 except InvalidArgumentError as e: 

81 self.__logger.error(e) 

82 context.set_details(str(e)) 

83 context.set_code(grpc.StatusCode.INVALID_ARGUMENT) 

84 

85 except NotImplementedError as e: 

86 self.__logger.error(e) 

87 context.set_code(grpc.StatusCode.UNIMPLEMENTED) 

88 

89 return remote_execution_pb2.ActionResult() 

90 

91 # --- Private API --- 

92 

93 def _get_instance(self, instance_name): 

94 try: 

95 return self._instances[instance_name] 

96 

97 except KeyError: 

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