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