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

16 statements  

« prev     ^ index     » next       coverage.py v7.4.1, created at 2024-10-04 17:48 +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 least_recent_digests(self) -> Iterator[Digest]: 

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

47 

48 @abc.abstractmethod 

49 def get_total_size(self) -> int: 

50 """ 

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

52 """ 

53 

54 @abc.abstractmethod 

55 def delete_n_bytes( 

56 self, 

57 n_bytes: int, 

58 dry_run: bool = False, 

59 protect_blobs_after: Optional[datetime] = None, 

60 ) -> int: 

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

62 

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

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

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

66 

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

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

69 

70 Args: 

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

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

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

74 

75 Returns: 

76 Number of bytes deleted 

77 """