Show More
@@ -25,8 +25,9 b' import typing' | |||||
25 | import zlib |
|
25 | import zlib | |
26 | import sqlite3 |
|
26 | import sqlite3 | |
27 |
|
27 | |||
28 |
from |
|
28 | from ...ext_json import json | |
29 | from .lock import GenerationLock |
|
29 | from .lock import GenerationLock | |
|
30 | from .utils import format_size | |||
30 |
|
31 | |||
31 | log = logging.getLogger(__name__) |
|
32 | log = logging.getLogger(__name__) | |
32 |
|
33 | |||
@@ -313,6 +314,9 b' class FanoutCache:' | |||||
313 |
|
314 | |||
314 | select_policy = EVICTION_POLICY[policy]['evict'] |
|
315 | select_policy = EVICTION_POLICY[policy]['evict'] | |
315 |
|
316 | |||
|
317 | log.debug('Running eviction policy \'%s\', and checking for size limit: %s', | |||
|
318 | policy, format_size(size_limit)) | |||
|
319 | ||||
316 | if select_policy is None: |
|
320 | if select_policy is None: | |
317 | return 0 |
|
321 | return 0 | |
318 |
|
322 | |||
@@ -326,21 +330,25 b' class FanoutCache:' | |||||
326 | key_file_path = os.path.join(shard._directory, key_file) |
|
330 | key_file_path = os.path.join(shard._directory, key_file) | |
327 | with open(key_file_path, 'rb') as f: |
|
331 | with open(key_file_path, 'rb') as f: | |
328 | metadata = json.loads(f.read()) |
|
332 | metadata = json.loads(f.read()) | |
329 | # in case we don't have size re-calc it... |
|
333 | ||
330 |
|
|
334 | size = metadata.get('size') | |
331 |
|
|
335 | filename = metadata.get('filename') | |
332 | size = os.stat(fn).st_size |
|
336 | full_path = metadata.get('full_path') | |
|
337 | ||||
|
338 | if not size: | |||
|
339 | # in case we don't have size re-calc it... | |||
|
340 | size = os.stat(full_path).st_size | |||
333 |
|
341 | |||
334 | data.append([ |
|
342 | data.append([ | |
335 | cnt, |
|
343 | cnt, | |
336 | key_file, |
|
344 | key_file, | |
337 | key_file_path, |
|
345 | key_file_path, | |
338 |
|
|
346 | filename, | |
339 |
|
|
347 | full_path, | |
340 | metadata.get('store_time', 0), |
|
348 | metadata.get('store_time', 0), | |
341 | metadata.get('access_time', 0), |
|
349 | metadata.get('access_time', 0), | |
342 | metadata.get('access_count', 0), |
|
350 | metadata.get('access_count', 0), | |
343 |
|
|
351 | size, | |
344 | ]) |
|
352 | ]) | |
345 | cnt += 1 |
|
353 | cnt += 1 | |
346 |
|
354 | |||
@@ -348,20 +356,27 b' class FanoutCache:' | |||||
348 | db.bulk_insert(data) |
|
356 | db.bulk_insert(data) | |
349 |
|
357 | |||
350 | ((total_size,),) = db.sql('SELECT COALESCE(SUM(size), 0) FROM archive_cache').fetchall() |
|
358 | ((total_size,),) = db.sql('SELECT COALESCE(SUM(size), 0) FROM archive_cache').fetchall() | |
351 |
|
359 | log.debug('Analyzed %s keys, occupied: %s', len(data), format_size(total_size)) | ||
352 | select_policy_qry = select_policy.format(fields='key_file_path, full_path, size') |
|
360 | select_policy_qry = select_policy.format(fields='key_file_path, full_path, size') | |
353 | sorted_keys = db.sql(select_policy_qry).fetchall() |
|
361 | sorted_keys = db.sql(select_policy_qry).fetchall() | |
354 |
|
362 | |||
|
363 | removed_items = 0 | |||
|
364 | removed_size = 0 | |||
355 | for key, cached_file, size in sorted_keys: |
|
365 | for key, cached_file, size in sorted_keys: | |
356 | # simulate removal impact BEFORE removal |
|
366 | # simulate removal impact BEFORE removal | |
357 | total_size -= size |
|
367 | total_size -= size | |
|
368 | ||||
358 | if total_size <= size_limit: |
|
369 | if total_size <= size_limit: | |
359 | # we obtained what we wanted... |
|
370 | # we obtained what we wanted... | |
360 | break |
|
371 | break | |
361 |
|
372 | |||
362 | os.remove(cached_file) |
|
373 | os.remove(cached_file) | |
363 | os.remove(key) |
|
374 | os.remove(key) | |
364 | return |
|
375 | removed_items += 1 | |
|
376 | removed_size += size | |||
|
377 | ||||
|
378 | log.debug('Removed %s cache archives, and reduced size: %s', removed_items, format_size(removed_size)) | |||
|
379 | return removed_items | |||
365 |
|
380 | |||
366 |
|
381 | |||
367 | def get_archival_config(config): |
|
382 | def get_archival_config(config): |
@@ -16,8 +16,7 b'' | |||||
16 | # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
|
16 | # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
17 |
|
17 | |||
18 | import redis |
|
18 | import redis | |
19 |
from |
|
19 | from ..._vendor import redis_lock | |
20 |
|
||||
21 | from .utils import ArchiveCacheLock |
|
20 | from .utils import ArchiveCacheLock | |
22 |
|
21 | |||
23 |
|
22 |
General Comments 0
You need to be logged in to leave comments.
Login now