Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(19)

Side by Side Diff: client/named_cache.py

Issue 2752293002: named_caches: make more resilient to corrupted state (Closed)
Patch Set: lint Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | client/tests/named_cache_test.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 # Copyright 2016 The LUCI Authors. All rights reserved. 1 # Copyright 2016 The LUCI Authors. All rights reserved.
2 # Use of this source code is governed under the Apache License, Version 2.0 2 # Use of this source code is governed under the Apache License, Version 2.0
3 # that can be found in the LICENSE file. 3 # that can be found in the LICENSE file.
4 4
5 """This file implements Named Caches.""" 5 """This file implements Named Caches."""
6 6
7 import contextlib 7 import contextlib
8 import logging 8 import logging
9 import optparse 9 import optparse
10 import os 10 import os
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
56 """Opens NamedCaches for mutation operations, such as request or trim. 56 """Opens NamedCaches for mutation operations, such as request or trim.
57 57
58 Only on caller can open the cache manager at a time. If the same thread 58 Only on caller can open the cache manager at a time. If the same thread
59 calls this function after opening it earlier, the call will deadlock. 59 calls this function after opening it earlier, the call will deadlock.
60 60
61 time_fn is a function that returns timestamp (float) and used to take 61 time_fn is a function that returns timestamp (float) and used to take
62 timestamps when new caches are requested. 62 timestamps when new caches are requested.
63 63
64 Returns a context manager that must be closed as soon as possible. 64 Returns a context manager that must be closed as soon as possible.
65 """ 65 """
66 state_path = os.path.join(self.root_dir, u'state.json')
67 with self._lock: 66 with self._lock:
67 state_path = os.path.join(self.root_dir, u'state.json')
68 assert self._lru is None, 'acquired lock, but self._lru is not None'
68 if os.path.isfile(state_path): 69 if os.path.isfile(state_path):
69 self._lru = lru.LRUDict.load(state_path) 70 try:
70 else: 71 self._lru = lru.LRUDict.load(state_path)
71 self._lru = lru.LRUDict() 72 except ValueError:
73 logging.exception('failed to load named cache state file')
74 logging.warning('deleting named caches')
75 file_path.rmtree(self.root_dir)
76 self._lru = self._lru or lru.LRUDict()
72 if time_fn: 77 if time_fn:
73 self._lru.time_fn = time_fn 78 self._lru.time_fn = time_fn
74 try: 79 try:
75 yield 80 yield
76 finally: 81 finally:
77 file_path.ensure_tree(self.root_dir) 82 file_path.ensure_tree(self.root_dir)
78 self._lru.save(state_path) 83 self._lru.save(state_path)
79 self._lru = None 84 self._lru = None
80 85
81 def __len__(self): 86 def __len__(self):
(...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after
276 if options.named_cache_root: 281 if options.named_cache_root:
277 return CacheManager(os.path.abspath(options.named_cache_root)) 282 return CacheManager(os.path.abspath(options.named_cache_root))
278 return None 283 return None
279 284
280 285
281 def _validate_named_cache_path(path): 286 def _validate_named_cache_path(path):
282 if os.path.isabs(path): 287 if os.path.isabs(path):
283 raise Error('named cache path must not be absolute') 288 raise Error('named cache path must not be absolute')
284 if '..' in path.split(os.path.sep): 289 if '..' in path.split(os.path.sep):
285 raise Error('named cache path must not contain ".."') 290 raise Error('named cache path must not contain ".."')
OLDNEW
« no previous file with comments | « no previous file | client/tests/named_cache_test.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698