##// END OF EJS Templates
chore(code-sync): synced code from ce for archive_cache
super-admin -
r1244:ecae6663 default
parent child Browse files
Show More
@@ -25,8 +25,9 b' import typing'
25 25 import zlib
26 26 import sqlite3
27 27
28 from vcsserver.lib.rc_json import json
28 from ...ext_json import json
29 29 from .lock import GenerationLock
30 from .utils import format_size
30 31
31 32 log = logging.getLogger(__name__)
32 33
@@ -313,6 +314,9 b' class FanoutCache:'
313 314
314 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 320 if select_policy is None:
317 321 return 0
318 322
@@ -326,21 +330,25 b' class FanoutCache:'
326 330 key_file_path = os.path.join(shard._directory, key_file)
327 331 with open(key_file_path, 'rb') as f:
328 332 metadata = json.loads(f.read())
329 # in case we don't have size re-calc it...
330 if not metadata.get('size'):
331 fn = metadata.get('full_path')
332 size = os.stat(fn).st_size
333
334 size = metadata.get('size')
335 filename = metadata.get('filename')
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 342 data.append([
335 343 cnt,
336 344 key_file,
337 345 key_file_path,
338 metadata.get('filename'),
339 metadata.get('full_path'),
346 filename,
347 full_path,
340 348 metadata.get('store_time', 0),
341 349 metadata.get('access_time', 0),
342 350 metadata.get('access_count', 0),
343 metadata.get('size', size),
351 size,
344 352 ])
345 353 cnt += 1
346 354
@@ -348,20 +356,27 b' class FanoutCache:'
348 356 db.bulk_insert(data)
349 357
350 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 360 select_policy_qry = select_policy.format(fields='key_file_path, full_path, size')
353 361 sorted_keys = db.sql(select_policy_qry).fetchall()
354 362
363 removed_items = 0
364 removed_size = 0
355 365 for key, cached_file, size in sorted_keys:
356 366 # simulate removal impact BEFORE removal
357 367 total_size -= size
368
358 369 if total_size <= size_limit:
359 370 # we obtained what we wanted...
360 371 break
361 372
362 373 os.remove(cached_file)
363 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 382 def get_archival_config(config):
@@ -16,8 +16,7 b''
16 16 # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 17
18 18 import redis
19 from vcsserver.lib._vendor import redis_lock
20
19 from ..._vendor import redis_lock
21 20 from .utils import ArchiveCacheLock
22 21
23 22
General Comments 0
You need to be logged in to leave comments. Login now