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