|
27 | 27 | FileValidationResponse, |
28 | 28 | FileValidationResponseItem, |
29 | 29 | ) |
30 | | -from hera_librarian.utils import compare_checksums, get_hash_function_from_hash |
| 30 | +from hera_librarian.utils import compare_checksums |
31 | 31 |
|
32 | 32 | from ..database import yield_session |
33 | 33 | from ..logger import log |
|
39 | 39 |
|
40 | 40 | router = APIRouter(prefix="/api/v2/validate") |
41 | 41 |
|
42 | | -VALIDATION_TIMEOUT = datetime.timedelta(hours=8) |
43 | | -VALIDATION_CACHE = {} |
44 | | - |
45 | | - |
46 | | -async def cached_calculate_checksum_of_local_copy( |
47 | | - original_checksum: str, |
48 | | - original_size: int, |
49 | | - path_info_function: callable, |
50 | | - path: Path, |
51 | | - store_id: int, |
52 | | - instance_id: int, |
53 | | -): |
54 | | - key = f"{original_checksum}-{instance_id}" |
55 | | - |
56 | | - cached = VALIDATION_CACHE.get(key, None) |
57 | | - |
58 | | - if cached is None or ( |
59 | | - (datetime.datetime.now(datetime.timezone.utc) - cached[1]) > VALIDATION_TIMEOUT |
60 | | - ): |
61 | | - result = await asyncify(calculate_checksum_of_local_copy)( |
62 | | - original_checksum=original_checksum, |
63 | | - original_size=original_size, |
64 | | - path_info_function=path_info_function, |
65 | | - path=path, |
66 | | - store_id=store_id, |
67 | | - instance_id=instance_id, |
68 | | - ) |
69 | | - |
70 | | - VALIDATION_CACHE[key] = (result, datetime.datetime.now(datetime.timezone.utc)) |
71 | | - else: |
72 | | - log.info( |
73 | | - f"Using cached result for instance {instance_id}", instance_id=instance_id |
74 | | - ) |
75 | | - result = cached[0] |
76 | | - |
77 | | - return result |
78 | | - |
79 | 42 |
|
80 | 43 | def calculate_checksum_of_local_copy( |
81 | 44 | original_checksum: str, |
82 | 45 | original_size: int, |
83 | | - path_info_function: callable, |
84 | | - path: Path, |
85 | | - store_id: int, |
86 | | - instance_id: int, |
| 46 | + instance: Instance, |
| 47 | + session: Session, |
87 | 48 | ): |
88 | 49 | start = perf_counter() |
89 | | - hash_function = get_hash_function_from_hash(original_checksum) |
90 | 50 | try: |
91 | | - path_info = path_info_function(path, hash_function=hash_function) |
| 51 | + current_checksum, current_size = instance.calculate_checksum( |
| 52 | + session=session, commit=True |
| 53 | + ) |
92 | 54 | response = FileValidationResponseItem( |
93 | 55 | librarian=server_settings.name, |
94 | | - store=store_id, |
95 | | - instance_id=instance_id, |
| 56 | + store=instance.store_id, |
| 57 | + instance_id=instance.id, |
96 | 58 | original_checksum=original_checksum, |
97 | 59 | original_size=original_size, |
98 | | - current_checksum=path_info.checksum, |
99 | | - current_size=path_info.size, |
| 60 | + current_checksum=current_checksum, |
| 61 | + current_size=current_size, |
100 | 62 | computed_same_checksum=compare_checksums( |
101 | | - original_checksum, path_info.checksum |
| 63 | + original_checksum, current_checksum |
102 | 64 | ), |
103 | 65 | ) |
104 | 66 | end = perf_counter() |
105 | 67 |
|
106 | 68 | log.debug( |
107 | | - f"Calculated path info for {instance_id} ({path_info.size} B) " |
108 | | - f"in {end - start:.2f} seconds." |
| 69 | + f"Calculated path info for {response.instance_id} / {instance.path} " |
| 70 | + f"({response.current_size} B) in {end - start:.2f} seconds" |
109 | 71 | ) |
110 | 72 |
|
111 | 73 | return [response] |
112 | 74 | except FileNotFoundError: |
113 | 75 | # A mistakenly 'available' file that is not actually available. |
114 | 76 | log.error( |
115 | | - f"File {path} in store {store_id} marked as available but does not exist." |
| 77 | + f"File {instance.path} in store {instance.store_id} marked as available but does not exist." |
116 | 78 | ) |
117 | 79 |
|
118 | 80 | return [] |
@@ -224,13 +186,11 @@ async def validate_file( |
224 | 186 | if not instance.available: |
225 | 187 | continue |
226 | 188 |
|
227 | | - this_checksum_info = cached_calculate_checksum_of_local_copy( |
| 189 | + this_checksum_info = asyncify(calculate_checksum_of_local_copy)( |
228 | 190 | original_checksum=file.checksum, |
229 | 191 | original_size=file.size, |
230 | | - path_info_function=instance.store.store_manager.path_info, |
231 | | - path=instance.path, |
232 | | - store_id=instance.store.id, |
233 | | - instance_id=instance.id, |
| 192 | + instance=instance, |
| 193 | + session=session, |
234 | 194 | ) |
235 | 195 |
|
236 | 196 | coroutines.append(this_checksum_info) |
|
0 commit comments