summaryrefslogtreecommitdiffstats
path: root/glustolibs-gluster/glustolibs/gluster/heal_libs.py
blob: e4f2b2fdf22c3d74ae28db5c01ee48b1aa1573e3 (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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
#!/usr/bin/env python
#  Copyright (C) 2016 Red Hat, Inc. <http://www.redhat.com>
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License along
#  with this program; if not, write to the Free Software Foundation, Inc.,
#  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#

"""
    Description: Module for gluster heal related helper functions.
"""

import time
from glusto.core import Glusto as g
from glustolibs.gluster.volume_ops import get_volume_status
try:
    import xml.etree.cElementTree as etree
except ImportError:
    import xml.etree.ElementTree as etree


def is_heal_enabled(mnode, volname):
    """Check if heal is enabled for a volume.

    Args:
        mnode : Node on which commands are executed
        volname : Name of the volume

    Returns:
        bool : True if heal is enabled on volume. False otherwise.
        NoneType: None if unable to get the volume status.
    """
    enabled = True
    vol_status_dict = get_volume_status(mnode, volname, service='shd')
    if vol_status_dict is None:
        g.log.error("Failed to check if heal is enabled on volume %s or not" %
                    volname)
        return None
    for node in vol_status_dict[volname].keys():
        if not ('Self-heal Daemon' in vol_status_dict[volname][node]):
            enabled = False
    return enabled


def is_heal_disabled(mnode, volname):
    """Check if heal is disabled for a volume.

    Args:
        mnode : Node on which commands are executed
        volname : Name of the volume

    Returns:
        bool : True if heal is disabled on volume. False otherwise.
        NoneType: None if unable to get the volume status shd or parse error.
    """
    cmd = "gluster volume status %s shd --xml" % volname
    ret, out, _ = g.run(mnode, cmd, log_level='DEBUG')
    if ret != 0:
        g.log.error("Failed to get the self-heal-daemon status for the "
                    "volume" % volname)
        return None

    try:
        root = etree.XML(out)
    except etree.ParseError:
        g.log.error("Failed to parse the volume status shd xml output.")
        return None

    operr = root.find("opErrstr")
    if operr:
        if "Self-heal Daemon is disabled for volume" in operr.text:
            return True
    return False


def are_all_self_heal_daemons_are_online(mnode, volname):
    """Verifies whether all the self-heal-daemons are online for the specified
        volume.

    Args:
        mnode (str): Node on which cmd has to be executed.
        volname (str): volume name

    Returns:
        bool : True if all the self-heal-daemons are online for the volume.
            False otherwise.
        NoneType: None if unable to get the volume status
    """
    from glustolibs.gluster.volume_libs import is_distribute_volume
    if is_distribute_volume(mnode, volname):
        g.log.info("Volume %s is a distribute volume. "
                   "Hence not checking for self-heal daemons "
                   "to be online", volname)
        return True

    service = 'shd'
    failure_msg = ("Verifying all self-heal-daemons are online failed for "
                   "volume %s" % volname)
    # Get volume status
    vol_status = get_volume_status(mnode=mnode, volname=volname,
                                   service=service)
    if vol_status is None:
        g.log.error(failure_msg)
        return None

    # Get all nodes from pool list
    from glustolibs.gluster.peer_ops import nodes_from_pool_list
    all_nodes = nodes_from_pool_list(mnode)
    if not all_nodes:
        g.log.error(failure_msg)
        return False

    online_status = True
    for node in all_nodes:
        node_shd_status_value = (vol_status[volname][node]['Self-heal Daemon']
                                 ['status'])
        if node_shd_status_value != '1':
            online_status = False
    g.run(mnode, ("gluster volume status %s shd" % volname))
    if online_status is True:
        g.log.info("All self-heal Daemons are online")
        return True
    else:
        g.log.error("Some of the self-heal Daemons are offline")
        return False


def monitor_heal_completion(mnode, volname, timeout_period=1200):
    """Monitors heal completion by looking into .glusterfs/indices/xattrop
        directory of every brick for certain time. When there are no entries
        in all the brick directories then heal is successful. Otherwise heal is
        pending on the volume.

    Args:
        mnode : Node on which commands are executed
        volname : Name of the volume
        heal_monitor_timeout : time until which the heal monitoring to be done.
                               Default: 1200 i.e 20 minutes.

    Return:
        bool: True if heal is complete within timeout_period. False otherwise
    """
    if timeout_period != 0:
        heal_monitor_timeout = timeout_period
    time_counter = heal_monitor_timeout
    g.log.info("The heal monitoring timeout is : %d minutes" %
               (heal_monitor_timeout / 60))

    # Get all bricks
    from glustolibs.gluster.brick_libs import get_all_bricks
    bricks_list = get_all_bricks(mnode, volname)
    if bricks_list is None:
        g.log.error("Unable to get the bricks list. Hence unable to verify "
                    "whether self-heal-daemon process is running or not "
                    "on the volume %s" % volname)
        return False

    while time_counter > 0:
        heal_complete = True
        for brick in bricks_list:
            brick_node, brick_path = brick.split(":")
            cmd = ("ls -1 %s/.glusterfs/indices/xattrop/ | "
                   "grep -ve \"xattrop-\" | wc -l" % brick_path)
            ret, out, err = g.run(brick_node, cmd)
            if out.strip('\n') != "0":
                heal_complete = False
        if heal_complete:
            break
        else:
            time.sleep(120)
            time_counter = time_counter - 120

    if heal_complete:
        heal_completion_status = is_heal_complete(mnode, volname)
        if heal_completion_status is True:
            g.log.info("Heal has successfully completed on volume %s" %
                       volname)
            return True

    g.log.info("Heal has not yet completed on volume %s" % volname)
    for brick in bricks_list:
        brick_node, brick_path = brick.split(":")
        cmd = ("ls -1 %s/.glusterfs/indices/xattrop/ " % brick_path)
        g.run(brick_node, cmd)
    return False


def is_heal_complete(mnode, volname):
    """Verifies there are no pending heals on the volume.
        The 'number of entries' in the output of heal info
        for all the bricks should be 0 for heal to be completed.

    Args:
        mnode : Node on which commands are executed
        volname : Name of the volume

    Return:
        bool: True if heal is complete. False otherwise
    """
    from glustolibs.gluster.heal_ops import get_heal_info
    heal_info_data = get_heal_info(mnode, volname)
    if heal_info_data is None:
        g.log.error("Unable to verify whether heal is successful or not on "
                    "volume %s" % volname)
        return False

    heal_complete = True
    for brick_heal_info_data in heal_info_data:
        if brick_heal_info_data['numberOfEntries'] != '0':
            heal_complete = False

    if not heal_complete:
        g.log.error("Heal is not complete on some of the bricks for the "
                    "volume %s" % volname)
        return False
    g.log.info("Heal is complete on all the bricks for the volume %s" %
               volname)
    return True


def is_volume_in_split_brain(mnode, volname):
    """Verifies there are no split-brain on the volume.
        The 'number of entries' in the output of heal info split-brain
        for all the bricks should be 0 for volume not to be in split-brain.

    Args:
        mnode : Node on which commands are executed
        volname : Name of the volume

    Return:
        bool: True if volume is not in split-brain. False otherwise
    """
    from glustolibs.gluster.heal_ops import get_heal_info_split_brain
    heal_info_split_brain_data = get_heal_info_split_brain(mnode, volname)
    if heal_info_split_brain_data is None:
        g.log.error("Unable to verify whether volume %s is not in split-brain "
                    "or not" % volname)
        return False

    split_brain = False
    for brick_heal_info_split_brain_data in heal_info_split_brain_data:
        if brick_heal_info_split_brain_data['numberOfEntries'] == '-':
            continue
        if brick_heal_info_split_brain_data['numberOfEntries'] != '0':
            split_brain = True

    if split_brain:
        g.log.error("Volume %s is in split-brain state." % volname)
        return True

    g.log.info("Volume %s is not in split-brain state." % volname)
    return False


def get_unhealed_entries_info(volname, mnode=''):
    """Get the information of all gfid's on which heal is pending. The
        information includes - stat of gfid, getfattr output for all the dirs/
        files for a given gfid

    Args:
        mnode : Node on which commands are executed
        volname : Name of the volume

    Return:
        bool: True if getting unhealed entries info is successful.
            False otherwise
    """
    return True


def wait_for_self_heal_daemons_to_be_online(mnode, volname, timeout=300):
    """Waits for the volume self-heal-daemons to be online until timeout

    Args:
        mnode (str): Node on which commands will be executed.
        volname (str): Name of the volume.

    Kwargs:
        timeout (int): timeout value in seconds to wait for self-heal-daemons
        to be online.

    Returns:
        True if all self-heal-daemons are online within timeout,
        False otherwise
    """
    from glustolibs.gluster.volume_libs import is_distribute_volume
    if is_distribute_volume(mnode, volname):
        g.log.info("Volume %s is a distribute volume. "
                   "Hence not waiting for self-heal daemons "
                   "to be online", volname)
        return True

    counter = 0
    flag = 0
    while counter < timeout:
        status = are_all_self_heal_daemons_are_online(mnode, volname)
        if status:
            flag = 1
            break
        if not status:
            time.sleep(10)
            counter = counter + 10

    if not flag:
        g.log.error("All self-heal-daemons of the volume '%s' are not online "
                    "even after %d minutes", (volname, timeout/60.0))
        return False
    else:
        g.log.info("All self-heal-daemons of the volume '%s' are online ",
                   volname)
    return True


def get_self_heal_daemon_pid(nodes):
    """
    Checks if self-heal daemon process is running and
    return the process id's in dictionary format

    Args:
        nodes ( str|list ) : Node/Nodes of the cluster

    Returns:
        tuple : Tuple containing two elements (ret, glustershd_pids).
        The first element 'ret' is of type 'bool', True if and only if
        glustershd is running on all the nodes in the list and each
        node contains only one instance of glustershd running.
        False otherwise.

        The second element 'glustershd_pids' is of type dictonary and it
        contains the process ID's for glustershd
    """
    glustershd_pids = {}
    _rc = True
    if isinstance(nodes, str):
        nodes = [nodes]
    cmd = r"pgrep -f glustershd | grep -v ^$$\$"
    g.log.info("Executing cmd: %s on node %s" % (cmd, nodes))
    results = g.run_parallel(nodes, cmd)
    for node in results:
        ret, out, err = results[node]
        if ret == 0:
            if len(out.strip().split("\n")) == 1:
                if not out.strip():
                    g.log.error("NO self heal daemon process found "
                                "on node %s" % node)
                    _rc = False
                    glustershd_pids[node] = [-1]
                else:
                    g.log.info("Single Self Heal Daemon process with "
                               "pid %s found on %s",
                               out.strip().split("\n"), node)
                    glustershd_pids[node] = (out.strip().split("\n"))
            else:
                g.log.error("More than One self heal daemon process "
                            "found on node %s" % node)
                _rc = False
                glustershd_pids[node] = [-1]
        else:
            g.log.error("Not able to get self heal daemon process "
                        "from node %s" % node)
            _rc = False
            glustershd_pids[node] = [-1]

    return _rc, glustershd_pids


def do_bricks_exist_in_shd_volfile(mnode, volname, brick_list):
    """
    Checks whether the given brick list is present in glustershd
    server volume file

    Args:
        mnode (str)         : Node on which commands will be executed.
        volname (str)       : Name of the volume.
        brick_list ( list ) : brick list of a volume which needs to
                              compare in glustershd server volume file

    Returns:
        bool : True if brick exists in glustershd server volume file.
               False Otherwise
    """
    GLUSTERSHD = "/var/lib/glusterd/glustershd/glustershd-server.vol"
    brick_list_server_vol = []
    volume_clients = "volume " + volname + "-client-"
    host = brick = None
    parse = False

    # Establish connection to mnode
    conn = g.rpyc_get_connection(mnode)
    if conn is None:
        g.log.info("Not able to establish connection to node %s" % mnode)
        return False
    try:
        fd = conn.builtins.open(GLUSTERSHD)
        for each_line in fd:
            each_line = each_line.strip()
            if volume_clients in each_line:
                parse = True
            elif "end-volume" in each_line:
                if parse:
                    brick_list_server_vol.append("%s:%s" % (host, brick))
                parse = False
            elif parse:
                if "option remote-subvolume" in each_line:
                    brick = each_line.split(" ")[2]
                if "option remote-host" in each_line:
                    host = each_line.split(" ")[2]

    except IOError as e:
        g.log.info("I/O error ({0}): {1}".format(e.errno, e.strerror))
        return False

    g.log.info("Brick List from volume info : %s" % brick_list)
    g.log.info("Brick List from glustershd server volume "
               "file : %s" % brick_list_server_vol)

    if set(brick_list) != set(brick_list_server_vol):
        return False
    return True


def is_shd_daemonized(nodes, timeout=120):
    """
    wait for the glustershd process to release parent process.

    Args:
        nodes ( str|list ) : Node/Nodes of the cluster

    Kwargs:
        timeout (int): timeout value in seconds to wait for self-heal-daemons
        to be online.

    Returns:
        bool : True if glustershd releases its parent.
               False Otherwise

    """
    counter = 0
    flag = 0
    if isinstance(nodes, str):
        nodes = [nodes]
    while counter < timeout:
        ret, pids = get_self_heal_daemon_pid(nodes)
        if not ret:
            g.log.info("Retry after 3 sec to get self heal "
                       "daemon process....")
            time.sleep(3)
            counter = counter + 3
        else:
            flag = 1
            break

    if not flag:
        g.log.error("Either No self heal daemon process found or more than"
                    "One self heal daemon process found even "
                    "after %d minutes", (timeout/60.0))
        return False
    else:
        g.log.info("Single self heal daemon process on all nodes %s",
                   nodes)
    return True


def bring_self_heal_daemon_process_offline(nodes):
    """
    Bring the self-heal daemon process offline for the nodes

    Args:
        nodes ( str|list ) : Node/Nodes of the cluster to bring
                             self-heal daemon process offline

    Returns:
        bool : True on successfully bringing self-heal daemon process offline.
               False otherwise
    """
    if isinstance(nodes, str):
        nodes = [nodes]

    failed_nodes = []
    _rc = True

    g.log.info("Starting to get self heal daemon process on nodes %s" % nodes)
    ret, pids = get_self_heal_daemon_pid(nodes)
    if not ret:
        g.log.error("Either no self heal daemon process found or more than"
                    " one self heal daemon process found : %s" % pids)
        return False
    g.log.info("Successful in getting single self heal daemon process"
               " on all nodes %s", nodes)

    for node in pids:
        pid = pids[node][0]
        kill_cmd = "kill -SIGKILL %s" % pid
        ret, _, _ = g.run(node, kill_cmd)
        if ret != 0:
            g.log.error("Unable to kill the self heal daemon "
                        "process on %s" % node)
            failed_nodes.append(node)

    if failed_nodes:
        g.log.info("Unable to kill the self heal daemon "
                   "process on nodes %s" % nodes)
        _rc = False

    return _rc