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) 2019 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""" 

16Remote Action Cache 

17=================== 

18 

19Provides an interface to a remote Action Cache. This is used by Execution 

20services to communicate with remote caches. 

21 

22""" 

23 

24import logging 

25 

26from buildgrid._exceptions import NotFoundError 

27from buildgrid.client.actioncache import ActionCacheClient 

28from buildgrid.client.capabilities import CapabilitiesInterface 

29 

30 

31class RemoteActionCache: 

32 

33 def __init__(self, channel, instance_name): 

34 self.__logger = logging.getLogger(__name__) 

35 self.channel = channel 

36 self.instance_name = instance_name 

37 self._allow_updates = None 

38 self._action_cache = ActionCacheClient( 

39 self.channel, instance=self.instance_name) 

40 

41 @property 

42 def allow_updates(self): 

43 # Check if updates are allowed if we haven't already. 

44 # This is done the first time update_action_result is called rather 

45 # than on instantiation because the remote cache may not be running 

46 # when this object is instantiated. 

47 if self._allow_updates is None: 

48 interface = CapabilitiesInterface(self.channel) 

49 capabilities = interface.get_capabilities(self.instance_name) 

50 self._allow_updates = (capabilities 

51 .cache_capabilities 

52 .action_cache_update_capabilities 

53 .update_enabled) 

54 return self._allow_updates 

55 

56 def get_action_result(self, action_digest): 

57 action_result = self._action_cache.get(action_digest) 

58 

59 if action_result is None: 

60 key = self._get_key(action_digest) 

61 raise NotFoundError(f"Key not found: {key}") 

62 return action_result 

63 

64 def update_action_result(self, action_digest, action_result): 

65 if not self.allow_updates: 

66 raise NotImplementedError("Updating cache not allowed") 

67 return self._action_cache.update(action_digest, action_result) 

68 

69 def _get_key(self, action_digest): 

70 return (action_digest.hash, action_digest.size_bytes)