summaryrefslogtreecommitdiffstats
path: root/src/pybind/mgr/dashboard/services/tcmu_service.py
blob: 1f3e4d8d78b4967836cf140ac912b7d9379c05c6 (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
from mgr_util import get_most_recent_rate

from dashboard.services.ceph_service import CephService
from .. import mgr

SERVICE_TYPE = 'tcmu-runner'


class TcmuService(object):
    # pylint: disable=too-many-nested-blocks
    @staticmethod
    def get_iscsi_info():
        daemons = {}
        images = {}
        for service in CephService.get_service_list(SERVICE_TYPE):
            metadata = service['metadata']
            if metadata is None:
                continue
            status = service['status']
            hostname = service['hostname']

            daemon = daemons.get(hostname, None)
            if daemon is None:
                daemon = {
                    'server_hostname': hostname,
                    'version': metadata['ceph_version'],
                    'optimized_paths': 0,
                    'non_optimized_paths': 0
                }
                daemons[hostname] = daemon

            service_id = service['id']
            device_id = service_id.split(':')[-1]
            image = images.get(device_id)
            if image is None:
                image = {
                    'device_id': device_id,
                    'pool_name': metadata['pool_name'],
                    'name': metadata['image_name'],
                    'id': metadata.get('image_id', None),
                    'optimized_paths': [],
                    'non_optimized_paths': []
                }
                images[device_id] = image

            if status.get('lock_owner', 'false') == 'true':
                daemon['optimized_paths'] += 1
                image['optimized_paths'].append(hostname)

                perf_key_prefix = "librbd-{id}-{pool}-{name}.".format(
                    id=metadata.get('image_id', ''),
                    pool=metadata['pool_name'],
                    name=metadata['image_name'])
                perf_key = "{}lock_acquired_time".format(perf_key_prefix)
                lock_acquired_time = (mgr.get_counter(
                    'tcmu-runner', service_id, perf_key)[perf_key]
                                      or [[0, 0]])[-1][1] / 1000000000
                if lock_acquired_time > image.get('optimized_since', 0):
                    image['optimized_daemon'] = hostname
                    image['optimized_since'] = lock_acquired_time
                    image['stats'] = {}
                    image['stats_history'] = {}
                    for s in ['rd', 'wr', 'rd_bytes', 'wr_bytes']:
                        perf_key = "{}{}".format(perf_key_prefix, s)
                        rates = CephService.get_rates('tcmu-runner', service_id, perf_key)
                        image['stats'][s] = get_most_recent_rate(rates)
                        image['stats_history'][s] = rates
            else:
                daemon['non_optimized_paths'] += 1
                image['non_optimized_paths'].append(hostname)

        # clear up races w/ tcmu-runner clients that haven't detected
        # loss of optimized path
        for image in images.values():
            optimized_daemon = image.get('optimized_daemon', None)
            if optimized_daemon:
                for daemon_name in image['optimized_paths']:
                    if daemon_name != optimized_daemon:
                        daemon = daemons[daemon_name]
                        daemon['optimized_paths'] -= 1
                        daemon['non_optimized_paths'] += 1
                        image['non_optimized_paths'].append(daemon_name)
                image['optimized_paths'] = [optimized_daemon]

        return {
            'daemons': sorted(daemons.values(),
                              key=lambda d: d['server_hostname']),
            'images': sorted(images.values(), key=lambda i: ['id']),
        }

    @staticmethod
    def get_image_info(pool_name, image_name, get_iscsi_info):
        for image in get_iscsi_info['images']:
            if image['pool_name'] == pool_name and image['name'] == image_name:
                return image
        return None