14f8ab
From 51090a4b3cb000d601083f12d1875547819fc03f Mon Sep 17 00:00:00 2001
14f8ab
From: Mohit Agrawal <moagrawal@redhat.com>
14f8ab
Date: Wed, 4 Mar 2020 09:17:26 +0530
14f8ab
Subject: [PATCH 447/449] core[brick_mux]: brick crashed when creating and
14f8ab
 deleting volumes over time
14f8ab
14f8ab
Problem: In brick_mux environment, while volumes are created/stopped in a loop
14f8ab
         after running a long time the main brick is crashed.The brick is crashed
14f8ab
         because the main brick process was not cleaned up memory for all objects
14f8ab
         at the time of detaching a volume.
14f8ab
         Below are the objects that are missed at the time of detaching a volume
14f8ab
         1) xlator object for a brick graph
14f8ab
         2) local_pool for posix_lock xlator
14f8ab
         3) rpc object cleanup at quota xlator
14f8ab
         4) inode leak at brick xlator
14f8ab
14f8ab
Solution: To avoid the crash resolve all leak at the time of detaching a brick
14f8ab
> Change-Id: Ibb6e46c5fba22b9441a88cbaf6b3278823235913
14f8ab
> updates: #977
14f8ab
> Signed-off-by: Mohit Agrawal <moagrawa@redhat.com>
14f8ab
> (Cherry pick from commit e589d8de66d3325da8fbbbe44d1a5bd6335e08ab)
14f8ab
> (Reviewed on upstream link https://review.gluster.org/#/c/glusterfs/+/24209/)
14f8ab
14f8ab
BUG: 1790336
14f8ab
Change-Id: Ibb6e46c5fba22b9441a88cbaf6b3278823235913
14f8ab
Signed-off-by: Mohit Agrawal <moagrawa@redhat.com>
14f8ab
Reviewed-on: https://code.engineering.redhat.com/gerrit/202782
14f8ab
Tested-by: RHGS Build Bot <nigelb@redhat.com>
14f8ab
Reviewed-by: Xavi Hernandez Juan <xhernandez@redhat.com>
14f8ab
---
14f8ab
 libglusterfs/src/glusterfs/glusterfs.h             |  1 +
14f8ab
 libglusterfs/src/graph.c                           |  1 +
14f8ab
 libglusterfs/src/graph.y                           |  2 +-
14f8ab
 libglusterfs/src/xlator.c                          | 29 ++++++++----
14f8ab
 xlators/features/changelog/src/changelog.c         |  1 +
14f8ab
 xlators/features/locks/src/posix.c                 |  4 ++
14f8ab
 xlators/features/quota/src/quota-enforcer-client.c | 14 +++++-
14f8ab
 xlators/features/quota/src/quota.c                 | 54 ++++++++++++++++++++--
14f8ab
 xlators/features/quota/src/quota.h                 |  3 ++
14f8ab
 xlators/protocol/server/src/server.c               | 12 +++--
14f8ab
 10 files changed, 103 insertions(+), 18 deletions(-)
14f8ab
14f8ab
diff --git a/libglusterfs/src/glusterfs/glusterfs.h b/libglusterfs/src/glusterfs/glusterfs.h
14f8ab
index 177a020..584846e 100644
14f8ab
--- a/libglusterfs/src/glusterfs/glusterfs.h
14f8ab
+++ b/libglusterfs/src/glusterfs/glusterfs.h
14f8ab
@@ -603,6 +603,7 @@ struct _glusterfs_graph {
14f8ab
     int used; /* Should be set when fuse gets
14f8ab
                         first CHILD_UP */
14f8ab
     uint32_t volfile_checksum;
14f8ab
+    pthread_mutex_t mutex;
14f8ab
 };
14f8ab
 typedef struct _glusterfs_graph glusterfs_graph_t;
14f8ab
 
14f8ab
diff --git a/libglusterfs/src/graph.c b/libglusterfs/src/graph.c
14f8ab
index bb5e67a..1cd92db 100644
14f8ab
--- a/libglusterfs/src/graph.c
14f8ab
+++ b/libglusterfs/src/graph.c
14f8ab
@@ -1092,6 +1092,7 @@ glusterfs_graph_destroy_residual(glusterfs_graph_t *graph)
14f8ab
     ret = xlator_tree_free_memacct(graph->first);
14f8ab
 
14f8ab
     list_del_init(&graph->list);
14f8ab
+    pthread_mutex_destroy(&graph->mutex);
14f8ab
     GF_FREE(graph);
14f8ab
 
14f8ab
     return ret;
14f8ab
diff --git a/libglusterfs/src/graph.y b/libglusterfs/src/graph.y
14f8ab
index 5b92985..5733515 100644
14f8ab
--- a/libglusterfs/src/graph.y
14f8ab
+++ b/libglusterfs/src/graph.y
14f8ab
@@ -541,7 +541,7 @@ glusterfs_graph_new ()
14f8ab
                 return NULL;
14f8ab
 
14f8ab
         INIT_LIST_HEAD (&graph->list);
14f8ab
-
14f8ab
+        pthread_mutex_init(&graph->mutex, NULL);
14f8ab
         gettimeofday (&graph->dob, NULL);
14f8ab
 
14f8ab
         return graph;
14f8ab
diff --git a/libglusterfs/src/xlator.c b/libglusterfs/src/xlator.c
14f8ab
index 108b96a..36cc32c 100644
14f8ab
--- a/libglusterfs/src/xlator.c
14f8ab
+++ b/libglusterfs/src/xlator.c
14f8ab
@@ -938,6 +938,8 @@ xlator_mem_cleanup(xlator_t *this)
14f8ab
     xlator_list_t **trav_p = NULL;
14f8ab
     xlator_t *top = NULL;
14f8ab
     xlator_t *victim = NULL;
14f8ab
+    glusterfs_graph_t *graph = NULL;
14f8ab
+    gf_boolean_t graph_cleanup = _gf_false;
14f8ab
 
14f8ab
     if (this->call_cleanup || !this->ctx)
14f8ab
         return;
14f8ab
@@ -945,6 +947,12 @@ xlator_mem_cleanup(xlator_t *this)
14f8ab
     this->call_cleanup = 1;
14f8ab
     ctx = this->ctx;
14f8ab
 
14f8ab
+    inode_table = this->itable;
14f8ab
+    if (inode_table) {
14f8ab
+        inode_table_destroy(inode_table);
14f8ab
+        this->itable = NULL;
14f8ab
+    }
14f8ab
+
14f8ab
     xlator_call_fini(trav);
14f8ab
 
14f8ab
     while (prev) {
14f8ab
@@ -953,12 +961,6 @@ xlator_mem_cleanup(xlator_t *this)
14f8ab
         prev = trav;
14f8ab
     }
14f8ab
 
14f8ab
-    inode_table = this->itable;
14f8ab
-    if (inode_table) {
14f8ab
-        inode_table_destroy(inode_table);
14f8ab
-        this->itable = NULL;
14f8ab
-    }
14f8ab
-
14f8ab
     if (this->fini) {
14f8ab
         this->fini(this);
14f8ab
     }
14f8ab
@@ -968,17 +970,28 @@ xlator_mem_cleanup(xlator_t *this)
14f8ab
     if (ctx->active) {
14f8ab
         top = ctx->active->first;
14f8ab
         LOCK(&ctx->volfile_lock);
14f8ab
-        /* TODO here we have leak for xlator node in a graph */
14f8ab
-        /* Need to move only top xlator from a graph */
14f8ab
         for (trav_p = &top->children; *trav_p; trav_p = &(*trav_p)->next) {
14f8ab
             victim = (*trav_p)->xlator;
14f8ab
             if (victim->call_cleanup && !strcmp(victim->name, this->name)) {
14f8ab
+                graph_cleanup = _gf_true;
14f8ab
                 (*trav_p) = (*trav_p)->next;
14f8ab
                 break;
14f8ab
             }
14f8ab
         }
14f8ab
         UNLOCK(&ctx->volfile_lock);
14f8ab
     }
14f8ab
+
14f8ab
+    if (graph_cleanup) {
14f8ab
+        prev = this;
14f8ab
+        graph = ctx->active;
14f8ab
+        pthread_mutex_lock(&graph->mutex);
14f8ab
+        while (prev) {
14f8ab
+            trav = prev->next;
14f8ab
+            GF_FREE(prev);
14f8ab
+            prev = trav;
14f8ab
+        }
14f8ab
+        pthread_mutex_unlock(&graph->mutex);
14f8ab
+    }
14f8ab
 }
14f8ab
 
14f8ab
 void
14f8ab
diff --git a/xlators/features/changelog/src/changelog.c b/xlators/features/changelog/src/changelog.c
14f8ab
index ff06c09..b54112c 100644
14f8ab
--- a/xlators/features/changelog/src/changelog.c
14f8ab
+++ b/xlators/features/changelog/src/changelog.c
14f8ab
@@ -2872,6 +2872,7 @@ fini(xlator_t *this)
14f8ab
         if (priv->active || priv->rpc_active) {
14f8ab
             /* terminate RPC server/threads */
14f8ab
             changelog_cleanup_rpc(this, priv);
14f8ab
+            GF_FREE(priv->ev_dispatcher);
14f8ab
         }
14f8ab
         /* call barrier_disable to cancel timer */
14f8ab
         if (priv->barrier_enabled)
14f8ab
diff --git a/xlators/features/locks/src/posix.c b/xlators/features/locks/src/posix.c
14f8ab
index 9a14c64..50f1265 100644
14f8ab
--- a/xlators/features/locks/src/posix.c
14f8ab
+++ b/xlators/features/locks/src/posix.c
14f8ab
@@ -4102,6 +4102,10 @@ fini(xlator_t *this)
14f8ab
     if (!priv)
14f8ab
         return;
14f8ab
     this->private = NULL;
14f8ab
+    if (this->local_pool) {
14f8ab
+        mem_pool_destroy(this->local_pool);
14f8ab
+        this->local_pool = NULL;
14f8ab
+    }
14f8ab
     GF_FREE(priv->brickname);
14f8ab
     GF_FREE(priv);
14f8ab
 
14f8ab
diff --git a/xlators/features/quota/src/quota-enforcer-client.c b/xlators/features/quota/src/quota-enforcer-client.c
14f8ab
index 1a4c2e3..097439d 100644
14f8ab
--- a/xlators/features/quota/src/quota-enforcer-client.c
14f8ab
+++ b/xlators/features/quota/src/quota-enforcer-client.c
14f8ab
@@ -362,16 +362,28 @@ quota_enforcer_notify(struct rpc_clnt *rpc, void *mydata,
14f8ab
 {
14f8ab
     xlator_t *this = NULL;
14f8ab
     int ret = 0;
14f8ab
+    quota_priv_t *priv = NULL;
14f8ab
 
14f8ab
     this = mydata;
14f8ab
-
14f8ab
+    priv = this->private;
14f8ab
     switch (event) {
14f8ab
         case RPC_CLNT_CONNECT: {
14f8ab
+            pthread_mutex_lock(&priv->conn_mutex);
14f8ab
+            {
14f8ab
+                priv->conn_status = _gf_true;
14f8ab
+            }
14f8ab
+            pthread_mutex_unlock(&priv->conn_mutex);
14f8ab
             gf_msg_trace(this->name, 0, "got RPC_CLNT_CONNECT");
14f8ab
             break;
14f8ab
         }
14f8ab
 
14f8ab
         case RPC_CLNT_DISCONNECT: {
14f8ab
+            pthread_mutex_lock(&priv->conn_mutex);
14f8ab
+            {
14f8ab
+                priv->conn_status = _gf_false;
14f8ab
+                pthread_cond_signal(&priv->conn_cond);
14f8ab
+            }
14f8ab
+            pthread_mutex_unlock(&priv->conn_mutex);
14f8ab
             gf_msg_trace(this->name, 0, "got RPC_CLNT_DISCONNECT");
14f8ab
             break;
14f8ab
         }
14f8ab
diff --git a/xlators/features/quota/src/quota.c b/xlators/features/quota/src/quota.c
14f8ab
index a0c236d..d1123ce 100644
14f8ab
--- a/xlators/features/quota/src/quota.c
14f8ab
+++ b/xlators/features/quota/src/quota.c
14f8ab
@@ -5014,6 +5014,43 @@ quota_forget(xlator_t *this, inode_t *inode)
14f8ab
     return 0;
14f8ab
 }
14f8ab
 
14f8ab
+int
14f8ab
+notify(xlator_t *this, int event, void *data, ...)
14f8ab
+{
14f8ab
+    quota_priv_t *priv = NULL;
14f8ab
+    int ret = 0;
14f8ab
+    rpc_clnt_t *rpc = NULL;
14f8ab
+    gf_boolean_t conn_status = _gf_true;
14f8ab
+    xlator_t *victim = data;
14f8ab
+
14f8ab
+    priv = this->private;
14f8ab
+    if (!priv || !priv->is_quota_on)
14f8ab
+        goto out;
14f8ab
+
14f8ab
+    if (event == GF_EVENT_PARENT_DOWN) {
14f8ab
+        rpc = priv->rpc_clnt;
14f8ab
+        if (rpc) {
14f8ab
+            rpc_clnt_disable(rpc);
14f8ab
+            pthread_mutex_lock(&priv->conn_mutex);
14f8ab
+            {
14f8ab
+                conn_status = priv->conn_status;
14f8ab
+                while (conn_status) {
14f8ab
+                    (void)pthread_cond_wait(&priv->conn_cond,
14f8ab
+                                            &priv->conn_mutex);
14f8ab
+                    conn_status = priv->conn_status;
14f8ab
+                }
14f8ab
+            }
14f8ab
+            pthread_mutex_unlock(&priv->conn_mutex);
14f8ab
+            gf_log(this->name, GF_LOG_INFO,
14f8ab
+                   "Notify GF_EVENT_PARENT_DOWN for brick %s", victim->name);
14f8ab
+        }
14f8ab
+    }
14f8ab
+
14f8ab
+out:
14f8ab
+    ret = default_notify(this, event, data);
14f8ab
+    return ret;
14f8ab
+}
14f8ab
+
14f8ab
 int32_t
14f8ab
 init(xlator_t *this)
14f8ab
 {
14f8ab
@@ -5056,6 +5093,10 @@ init(xlator_t *this)
14f8ab
         goto err;
14f8ab
     }
14f8ab
 
14f8ab
+    pthread_mutex_init(&priv->conn_mutex, NULL);
14f8ab
+    pthread_cond_init(&priv->conn_cond, NULL);
14f8ab
+    priv->conn_status = _gf_false;
14f8ab
+
14f8ab
     if (priv->is_quota_on) {
14f8ab
         rpc = quota_enforcer_init(this, this->options);
14f8ab
         if (rpc == NULL) {
14f8ab
@@ -5169,20 +5210,22 @@ fini(xlator_t *this)
14f8ab
 {
14f8ab
     quota_priv_t *priv = NULL;
14f8ab
     rpc_clnt_t *rpc = NULL;
14f8ab
-    int i = 0, cnt = 0;
14f8ab
 
14f8ab
     priv = this->private;
14f8ab
     if (!priv)
14f8ab
         return;
14f8ab
     rpc = priv->rpc_clnt;
14f8ab
     priv->rpc_clnt = NULL;
14f8ab
-    this->private = NULL;
14f8ab
     if (rpc) {
14f8ab
-        cnt = GF_ATOMIC_GET(rpc->refcount);
14f8ab
-        for (i = 0; i < cnt; i++)
14f8ab
-            rpc_clnt_unref(rpc);
14f8ab
+        rpc_clnt_connection_cleanup(&rpc->conn);
14f8ab
+        rpc_clnt_unref(rpc);
14f8ab
     }
14f8ab
+
14f8ab
+    this->private = NULL;
14f8ab
     LOCK_DESTROY(&priv->lock);
14f8ab
+    pthread_mutex_destroy(&priv->conn_mutex);
14f8ab
+    pthread_cond_destroy(&priv->conn_cond);
14f8ab
+
14f8ab
     GF_FREE(priv);
14f8ab
     if (this->local_pool) {
14f8ab
         mem_pool_destroy(this->local_pool);
14f8ab
@@ -5314,6 +5357,7 @@ struct volume_options options[] = {
14f8ab
 xlator_api_t xlator_api = {
14f8ab
     .init = init,
14f8ab
     .fini = fini,
14f8ab
+    .notify = notify,
14f8ab
     .reconfigure = reconfigure,
14f8ab
     .mem_acct_init = mem_acct_init,
14f8ab
     .op_version = {1}, /* Present from the initial version */
14f8ab
diff --git a/xlators/features/quota/src/quota.h b/xlators/features/quota/src/quota.h
14f8ab
index a5a99ca..e51ffd4 100644
14f8ab
--- a/xlators/features/quota/src/quota.h
14f8ab
+++ b/xlators/features/quota/src/quota.h
14f8ab
@@ -217,6 +217,9 @@ struct quota_priv {
14f8ab
     char *volume_uuid;
14f8ab
     uint64_t validation_count;
14f8ab
     int32_t quotad_conn_status;
14f8ab
+    pthread_mutex_t conn_mutex;
14f8ab
+    pthread_cond_t conn_cond;
14f8ab
+    gf_boolean_t conn_status;
14f8ab
 };
14f8ab
 typedef struct quota_priv quota_priv_t;
14f8ab
 
14f8ab
diff --git a/xlators/protocol/server/src/server.c b/xlators/protocol/server/src/server.c
14f8ab
index a5f09fe..54d9c0f 100644
14f8ab
--- a/xlators/protocol/server/src/server.c
14f8ab
+++ b/xlators/protocol/server/src/server.c
14f8ab
@@ -409,7 +409,13 @@ server_call_xlator_mem_cleanup(xlator_t *this, char *victim_name)
14f8ab
 
14f8ab
     arg = calloc(1, sizeof(*arg));
14f8ab
     arg->this = this;
14f8ab
-    arg->victim_name = gf_strdup(victim_name);
14f8ab
+    arg->victim_name = strdup(victim_name);
14f8ab
+    if (!arg->victim_name) {
14f8ab
+        gf_smsg(this->name, GF_LOG_CRITICAL, ENOMEM, LG_MSG_NO_MEMORY,
14f8ab
+                "Memory allocation is failed");
14f8ab
+        return;
14f8ab
+    }
14f8ab
+
14f8ab
     th_ret = gf_thread_create_detached(&th_id, server_graph_janitor_threads,
14f8ab
                                        arg, "graphjanitor");
14f8ab
     if (th_ret) {
14f8ab
@@ -417,7 +423,7 @@ server_call_xlator_mem_cleanup(xlator_t *this, char *victim_name)
14f8ab
                "graph janitor Thread"
14f8ab
                " creation is failed for brick %s",
14f8ab
                victim_name);
14f8ab
-        GF_FREE(arg->victim_name);
14f8ab
+        free(arg->victim_name);
14f8ab
         free(arg);
14f8ab
     }
14f8ab
 }
14f8ab
@@ -628,7 +634,7 @@ server_graph_janitor_threads(void *data)
14f8ab
     }
14f8ab
 
14f8ab
 out:
14f8ab
-    GF_FREE(arg->victim_name);
14f8ab
+    free(arg->victim_name);
14f8ab
     free(arg);
14f8ab
     return NULL;
14f8ab
 }
14f8ab
-- 
14f8ab
1.8.3.1
14f8ab