Coverage for /builds/BuildGrid/buildgrid/buildgrid/server/cas/storage/index/index_abc.py: 81.82%

22 statements  

« prev     ^ index     » next       coverage.py v7.4.1, created at 2024-06-11 15:37 +0000

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 

16""" 

17IndexABC 

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

19 

20The abstract base class for storage indices. An index is a special type of 

21Storage that facilitates storing blob metadata. It must wrap another Storage. 

22 

23Derived classes must implement all methods of both this interface and the 

24StorageABC interface. 

25""" 

26 

27import abc 

28from datetime import datetime 

29from typing import Iterator, Optional 

30 

31from buildgrid._protos.build.bazel.remote.execution.v2.remote_execution_pb2 import Digest 

32 

33from ..storage_abc import StorageABC 

34 

35 

36class IndexABC(StorageABC): 

37 @abc.abstractmethod 

38 def __init__(self, *, fallback_on_get: bool = False) -> None: 

39 # If fallback is enabled, the index is required to fetch blobs from 

40 # storage on each get_blob and bulk_read_blobs request and update 

41 # itself accordingly. 

42 self._fallback_on_get = fallback_on_get 

43 

44 @abc.abstractmethod 

45 def delete_blob(self, digest: Digest) -> None: 

46 """Delete a blob from the index. Return True if the blob was deleted, 

47 or False otherwise. 

48 

49 TODO: This method will be promoted to StorageABC in a future commit.""" 

50 raise NotImplementedError() 

51 

52 @abc.abstractmethod 

53 def least_recent_digests(self) -> Iterator[Digest]: 

54 """Generator to iterate through the digests in LRU order""" 

55 raise NotImplementedError() 

56 

57 @abc.abstractmethod 

58 def get_total_size(self, include_marked: bool = True) -> int: 

59 """ 

60 Return the sum of the size of all blobs within the index. 

61 

62 If include_marked is True, it will also include the size of blobs that have been marked deleted. 

63 """ 

64 raise NotImplementedError() 

65 

66 @abc.abstractmethod 

67 def delete_n_bytes( 

68 self, 

69 n_bytes: int, 

70 dry_run: bool = False, 

71 protect_blobs_after: Optional[datetime] = None, 

72 ) -> int: 

73 """Delete around n bytes of data from the index. 

74 

75 The ordering in which digests are deleted is up to the specific implementations 

76 and may provide different semantics such as LRU or random deletion. ALL implementations 

77 must respect the protect_blobs_after parameter to limit the age of deleted blobs 

78 

79 Implementations should generate delete around n_bytes on each call, but may delete more or less 

80 depending on the state of the storage, index, and value of protect_blobs_after. 

81 

82 Args: 

83 n_bytes (int): The number of bytes to be deleted 

84 dry_run (bool): Don't actually delete any data, just return the number of bytes which would be deleted 

85 protect_blobs_after: Don't delete any digests which have been accessed after this time 

86 

87 Returns: 

88 Number of bytes deleted 

89 

90 """ 

91 raise NotImplementedError()