50dc83
From faaaa3452ceec6afcc18cffc9beca3fe19841cce Mon Sep 17 00:00:00 2001
50dc83
From: Mohammed Rafi KC <rkavunga@redhat.com>
50dc83
Date: Thu, 3 Jan 2019 17:44:18 +0530
50dc83
Subject: [PATCH 104/124] afr/shd: Cleanup self heal daemon resources during
50dc83
 afr fini
50dc83
50dc83
We were not properly cleaning self-heal daemon resources
50dc83
during afr fini. This patch will clean the same.
50dc83
50dc83
Backport of: https://review.gluster.org/#/c/glusterfs/+/22151/
50dc83
50dc83
>Change-Id: I597860be6f781b195449e695d871b8667a418d5a
50dc83
>updates: bz#1659708
50dc83
>Signed-off-by: Mohammed Rafi KC <rkavunga@redhat.com>
50dc83
50dc83
Change-Id: I7be981b9c2476c8cacadea6b14d74234f67b714f
50dc83
BUG: 1471742
50dc83
Signed-off-by: Mohammed Rafi KC <rkavunga@redhat.com>
50dc83
Reviewed-on: https://code.engineering.redhat.com/gerrit/167845
50dc83
Tested-by: RHGS Build Bot <nigelb@redhat.com>
50dc83
Reviewed-by: Atin Mukherjee <amukherj@redhat.com>
50dc83
---
50dc83
 libglusterfs/src/syncop-utils.c          |  8 +++++
50dc83
 xlators/cluster/afr/src/afr-self-heald.c |  2 ++
50dc83
 xlators/cluster/afr/src/afr.c            | 57 ++++++++++++++++++++++++++++++++
50dc83
 3 files changed, 67 insertions(+)
50dc83
50dc83
diff --git a/libglusterfs/src/syncop-utils.c b/libglusterfs/src/syncop-utils.c
50dc83
index be03527..b842142 100644
50dc83
--- a/libglusterfs/src/syncop-utils.c
50dc83
+++ b/libglusterfs/src/syncop-utils.c
50dc83
@@ -350,6 +350,11 @@ syncop_mt_dir_scan(call_frame_t *frame, xlator_t *subvol, loc_t *loc, int pid,
50dc83
     gf_boolean_t cond_init = _gf_false;
50dc83
     gf_boolean_t mut_init = _gf_false;
50dc83
     gf_dirent_t entries;
50dc83
+    xlator_t *this = NULL;
50dc83
+
50dc83
+    if (frame) {
50dc83
+        this = frame->this;
50dc83
+    }
50dc83
 
50dc83
     /*For this functionality to be implemented in general, we need
50dc83
      * synccond_t infra which doesn't block the executing thread. Until then
50dc83
@@ -397,6 +402,9 @@ syncop_mt_dir_scan(call_frame_t *frame, xlator_t *subvol, loc_t *loc, int pid,
50dc83
 
50dc83
         list_for_each_entry_safe(entry, tmp, &entries.list, list)
50dc83
         {
50dc83
+            if (this && this->cleanup_starting)
50dc83
+                goto out;
50dc83
+
50dc83
             list_del_init(&entry->list);
50dc83
             if (!strcmp(entry->d_name, ".") || !strcmp(entry->d_name, "..")) {
50dc83
                 gf_dirent_entry_free(entry);
50dc83
diff --git a/xlators/cluster/afr/src/afr-self-heald.c b/xlators/cluster/afr/src/afr-self-heald.c
50dc83
index 7eb1207..8bc4720 100644
50dc83
--- a/xlators/cluster/afr/src/afr-self-heald.c
50dc83
+++ b/xlators/cluster/afr/src/afr-self-heald.c
50dc83
@@ -373,6 +373,7 @@ afr_shd_sweep_prepare(struct subvol_healer *healer)
50dc83
 
50dc83
     time(&event->start_time);
50dc83
     event->end_time = 0;
50dc83
+    _mask_cancellation();
50dc83
 }
50dc83
 
50dc83
 void
50dc83
@@ -394,6 +395,7 @@ afr_shd_sweep_done(struct subvol_healer *healer)
50dc83
 
50dc83
     if (eh_save_history(shd->statistics[healer->subvol], history) < 0)
50dc83
         GF_FREE(history);
50dc83
+    _unmask_cancellation();
50dc83
 }
50dc83
 
50dc83
 int
50dc83
diff --git a/xlators/cluster/afr/src/afr.c b/xlators/cluster/afr/src/afr.c
50dc83
index 33258a0..a0a7551 100644
50dc83
--- a/xlators/cluster/afr/src/afr.c
50dc83
+++ b/xlators/cluster/afr/src/afr.c
50dc83
@@ -611,13 +611,70 @@ init(xlator_t *this)
50dc83
 out:
50dc83
     return ret;
50dc83
 }
50dc83
+void
50dc83
+afr_destroy_healer_object(xlator_t *this, struct subvol_healer *healer)
50dc83
+{
50dc83
+    int ret = -1;
50dc83
+
50dc83
+    if (!healer)
50dc83
+        return;
50dc83
+
50dc83
+    if (healer->running) {
50dc83
+        /*
50dc83
+         * If there are any resources to cleanup, We need
50dc83
+         * to do that gracefully using pthread_cleanup_push
50dc83
+         */
50dc83
+        ret = gf_thread_cleanup_xint(healer->thread);
50dc83
+        if (ret)
50dc83
+            gf_msg(this->name, GF_LOG_WARNING, 0, AFR_MSG_SELF_HEAL_FAILED,
50dc83
+                   "Failed to clean up healer threads.");
50dc83
+        healer->thread = 0;
50dc83
+    }
50dc83
+    pthread_cond_destroy(&healer->cond);
50dc83
+    pthread_mutex_destroy(&healer->mutex);
50dc83
+}
50dc83
+
50dc83
+void
50dc83
+afr_selfheal_daemon_fini(xlator_t *this)
50dc83
+{
50dc83
+    struct subvol_healer *healer = NULL;
50dc83
+    afr_self_heald_t *shd = NULL;
50dc83
+    afr_private_t *priv = NULL;
50dc83
+    int i = 0;
50dc83
+
50dc83
+    priv = this->private;
50dc83
+    if (!priv)
50dc83
+        return;
50dc83
+
50dc83
+    shd = &priv->shd;
50dc83
+    if (!shd->iamshd)
50dc83
+        return;
50dc83
+
50dc83
+    for (i = 0; i < priv->child_count; i++) {
50dc83
+        healer = &shd->index_healers[i];
50dc83
+        afr_destroy_healer_object(this, healer);
50dc83
 
50dc83
+        healer = &shd->full_healers[i];
50dc83
+        afr_destroy_healer_object(this, healer);
50dc83
+
50dc83
+        if (shd->statistics[i])
50dc83
+            eh_destroy(shd->statistics[i]);
50dc83
+    }
50dc83
+    GF_FREE(shd->index_healers);
50dc83
+    GF_FREE(shd->full_healers);
50dc83
+    GF_FREE(shd->statistics);
50dc83
+    if (shd->split_brain)
50dc83
+        eh_destroy(shd->split_brain);
50dc83
+}
50dc83
 void
50dc83
 fini(xlator_t *this)
50dc83
 {
50dc83
     afr_private_t *priv = NULL;
50dc83
 
50dc83
     priv = this->private;
50dc83
+
50dc83
+    afr_selfheal_daemon_fini(this);
50dc83
+
50dc83
     LOCK(&priv->lock);
50dc83
     if (priv->timer != NULL) {
50dc83
         gf_timer_call_cancel(this->ctx, priv->timer);
50dc83
-- 
50dc83
1.8.3.1
50dc83