1 from __future__ import with_statement
4 from invirt.common import *
6 from os.path import getmtime
7 from contextlib import closing
9 src_path = '/etc/invirt/master.yaml'
10 cache_path = '/var/lib/invirt/cache.json'
11 lock_path = '/var/lib/invirt/cache.lock'
13 def load(force_refresh = False):
15 Try loading the configuration from the faster-to-load JSON cache at
16 cache_path. If it doesn't exist or is outdated, load the configuration
17 instead from the original YAML file at src_path and regenerate the cache.
18 I assume I have the permissions to write to the cache directory.
21 # Namespace container for state variables, so that they can be updated by
28 src_mtime = getmtime(src_path)
29 try: cache_mtime = getmtime(cache_path)
30 except OSError: do_refresh = True
31 else: do_refresh = src_mtime + 1 >= cache_mtime
33 # We chose not to simply say
35 # do_refresh = src_mtime >= cache_time
37 # because between the getmtime(src_path) and the time the cache is
38 # rewritten, the master configuration may have been updated, so future
39 # checks here would find a cache with a newer mtime than the master
40 # (and thus treat the cache as containing the latest version of the
41 # master). The +1 means that for at least a full second following the
42 # update to the master, this function will refresh the cache, giving us
43 # 1 second to write the cache. Note that if it takes longer than 1
44 # second to write the cache, then this situation could still arise.
46 # The getmtime calls should logically be part of the same transaction
47 # as the rest of this function (cache read + conditional cache
48 # refresh), but to wrap everything in an flock would cause the
49 # following cache read to be less streamlined.
52 # Try reading from the cache first. This must be transactionally
53 # isolated from concurrent writes to prevent reading an incomplete
54 # (changing) version of the data (but the transaction can share the
55 # lock with other concurrent reads). This isolation is accomplished
56 # using an atomic filesystem rename in the refreshing stage.
58 with closing(file(cache_path)) as f:
59 ns.cfg = json.read(f.read())
60 except: do_refresh = True
63 # Atomically reload the source and regenerate the cache. The read and
64 # write must be a single transaction, or a stale version may be
65 # written (if another read/write of a more recent configuration
66 # is interleaved). The final atomic rename is to keep this
67 # transactionally isolated from the above cache read. If we fail to
68 # acquire the lock, just try to load the master configuration.
70 try: loader = yaml.CSafeLoader
71 except: loader = yaml.SafeLoader
73 with lock_file(lock_path):
74 with closing(file(src_path)) as f:
75 ns.cfg = yaml.load(f, loader)
77 with closing(file(cache_path + '.tmp', 'w')) as f:
78 f.write(json.write(ns.cfg))
79 except: pass # silent failure
80 else: rename(cache_path + '.tmp', cache_path)
82 with closing(file(src_path)) as f:
83 ns.cfg = yaml.load(f, loader)
87 structs = dicts2struct(dicts)