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