source: trunk/packages/invirt-base/python/invirt/config.py @ 1421

Last change on this file since 1421 was 1421, checked in by price, 16 years ago

factor a bit of code out of the mondo invirt.config.load()

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