summaryrefslogtreecommitdiff
path: root/nova/virt/storage_users.py
blob: d0dc8048a6689b6370b354a967cab2fef073258b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# Copyright 2012 Michael Still and Canonical Inc
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.


import json
import os
import time

from nova import utils
from oslo.config import cfg


CONF = cfg.CONF
TWENTY_FOUR_HOURS = 3600 * 24


# NOTE(morganfainberg): Due to circular import dependencies, the use of the
# CONF.instances_path needs to be wrapped so that it can be resolved at the
# appropriate time. Because compute.manager imports this file, we end up in
# a rather ugly dependency loop without moving this into a wrapped function.
# This issue mostly stems from the use of a decorator for the lock
# synchronize and the implications of how decorators wrap the wrapped function
# or method.  If this needs to be used outside of compute.manager, it should
# be refactored to eliminate this circular dependency loop.
def register_storage_use(storage_path, hostname):
    """Identify the id of this instance storage."""

    # NOTE(morganfainberg): config option import is avoided here since it is
    # explicitly imported from compute.manager and may cause issues with
    # defining options after config has been processed with the
    # wrapped-function style used here.
    LOCK_PATH = os.path.join(CONF.instances_path, 'locks')

    @utils.synchronized('storage-registry-lock', external=True,
                        lock_path=LOCK_PATH)
    def do_register_storage_use(storage_path, hostname):
        # NOTE(mikal): this is required to determine if the instance storage is
        # shared, which is something that the image cache manager needs to
        # know. I can imagine other uses as well though.

        d = {}
        id_path = os.path.join(storage_path, 'compute_nodes')
        if os.path.exists(id_path):
            with open(id_path) as f:
                d = json.loads(f.read())

        d[hostname] = time.time()

        with open(id_path, 'w') as f:
            f.write(json.dumps(d))

    return do_register_storage_use(storage_path, hostname)


# NOTE(morganfainberg): Due to circular import dependencies, the use of the
# CONF.instances_path needs to be wrapped so that it can be resolved at the
# appropriate time. Because compute.manager imports this file, we end up in
# a rather ugly dependency loop without moving this into a wrapped function.
# This issue mostly stems from the use of a decorator for the lock
# synchronize and the implications of how decorators wrap the wrapped function
# or method.  If this needs to be used outside of compute.manager, it should
# be refactored to eliminate this circular dependency loop.
def get_storage_users(storage_path):
    """Get a list of all the users of this storage path."""

    # NOTE(morganfainberg): config option import is avoided here since it is
    # explicitly imported from compute.manager and may cause issues with
    # defining options after config has been processed with the
    # wrapped-function style used here.
    LOCK_PATH = os.path.join(CONF.instances_path, 'locks')

    @utils.synchronized('storage-registry-lock', external=True,
                        lock_path=LOCK_PATH)
    def do_get_storage_users(storage_path):
        d = {}
        id_path = os.path.join(storage_path, 'compute_nodes')
        if os.path.exists(id_path):
            with open(id_path) as f:
                d = json.loads(f.read())

        recent_users = []
        for node in d:
            if time.time() - d[node] < TWENTY_FOUR_HOURS:
                recent_users.append(node)

        return recent_users

    return do_get_storage_users(storage_path)