74096c
From 175c99dccc47d2b4267a8819404e5cbeb8cfba11 Mon Sep 17 00:00:00 2001
74096c
From: Mohit Agrawal <moagrawal@redhat.com>
74096c
Date: Thu, 12 Mar 2020 21:12:13 +0530
74096c
Subject: [PATCH 448/449] Posix: Use simple approach to close fd
74096c
74096c
Problem: posix_release(dir) functions add the fd's into a ctx->janitor_fds
74096c
         and janitor thread closes the fd's.In brick_mux environment it is
74096c
         difficult to handle race condition in janitor threads because brick
74096c
         spawns a single janitor thread for all bricks.
74096c
74096c
Solution: Use synctask to execute posix_release(dir) functions instead of
74096c
          using background a thread to close fds.
74096c
74096c
> Credits: Pranith Karampuri <pkarampu@redhat.com>
74096c
> Change-Id: Iffb031f0695a7da83d5a2f6bac8863dad225317e
74096c
> Fixes: bz#1811631
74096c
> Signed-off-by: Mohit Agrawal <moagrawal@redhat.com>
74096c
> (Cherry pick from commit fb20713b380e1df8d7f9e9df96563be2f9144fd6)
74096c
> (Reviewed on upstream link https://review.gluster.org/#/c/glusterfs/+/24221/)
74096c
74096c
BUG: 1790336
74096c
Change-Id: Iffb031f0695a7da83d5a2f6bac8863dad225317e
74096c
Signed-off-by: Mohit Agrawal <moagrawal@redhat.com>
74096c
Reviewed-on: https://code.engineering.redhat.com/gerrit/202791
74096c
Tested-by: Mohit Agrawal <moagrawa@redhat.com>
74096c
Reviewed-by: Sunil Kumar Heggodu Gopala Acharya <sheggodu@redhat.com>
74096c
---
74096c
 libglusterfs/src/glusterfs/glusterfs.h         |  6 +-
74096c
 libglusterfs/src/glusterfs/syncop.h            |  7 +-
74096c
 rpc/rpc-lib/src/rpcsvc.c                       |  6 ++
74096c
 run-tests.sh                                   |  2 +-
74096c
 tests/features/ssl-authz.t                     |  7 +-
74096c
 xlators/storage/posix/src/posix-common.c       |  4 --
74096c
 xlators/storage/posix/src/posix-helpers.c      | 98 --------------------------
74096c
 xlators/storage/posix/src/posix-inode-fd-ops.c | 28 ++------
74096c
 xlators/storage/posix/src/posix.h              |  3 -
74096c
 9 files changed, 20 insertions(+), 141 deletions(-)
74096c
74096c
diff --git a/libglusterfs/src/glusterfs/glusterfs.h b/libglusterfs/src/glusterfs/glusterfs.h
74096c
index 584846e..495a4d7 100644
74096c
--- a/libglusterfs/src/glusterfs/glusterfs.h
74096c
+++ b/libglusterfs/src/glusterfs/glusterfs.h
74096c
@@ -734,11 +734,7 @@ struct _glusterfs_ctx {
74096c
 
74096c
     struct list_head volfile_list;
74096c
 
74096c
-    /* Add members to manage janitor threads for cleanup fd */
74096c
-    struct list_head janitor_fds;
74096c
-    pthread_cond_t janitor_cond;
74096c
-    pthread_mutex_t janitor_lock;
74096c
-    pthread_t janitor;
74096c
+    char volume_id[GF_UUID_BUF_SIZE]; /* Used only in protocol/client */
74096c
 };
74096c
 typedef struct _glusterfs_ctx glusterfs_ctx_t;
74096c
 
74096c
diff --git a/libglusterfs/src/glusterfs/syncop.h b/libglusterfs/src/glusterfs/syncop.h
74096c
index 3011b4c..1e4c73b 100644
74096c
--- a/libglusterfs/src/glusterfs/syncop.h
74096c
+++ b/libglusterfs/src/glusterfs/syncop.h
74096c
@@ -254,7 +254,7 @@ struct syncopctx {
74096c
         task = synctask_get();                                                 \
74096c
         stb->task = task;                                                      \
74096c
         if (task)                                                              \
74096c
-            frame = task->opframe;                                             \
74096c
+            frame = copy_frame(task->opframe);                                 \
74096c
         else                                                                   \
74096c
             frame = syncop_create_frame(THIS);                                 \
74096c
                                                                                \
74096c
@@ -269,10 +269,7 @@ struct syncopctx {
74096c
         STACK_WIND_COOKIE(frame, cbk, (void *)stb, subvol, fn_op, params);     \
74096c
                                                                                \
74096c
         __yield(stb);                                                          \
74096c
-        if (task)                                                              \
74096c
-            STACK_RESET(frame->root);                                          \
74096c
-        else                                                                   \
74096c
-            STACK_DESTROY(frame->root);                                        \
74096c
+        STACK_DESTROY(frame->root);                                            \
74096c
     } while (0)
74096c
 
74096c
 /*
74096c
diff --git a/rpc/rpc-lib/src/rpcsvc.c b/rpc/rpc-lib/src/rpcsvc.c
74096c
index 3f184bf..23ca1fd 100644
74096c
--- a/rpc/rpc-lib/src/rpcsvc.c
74096c
+++ b/rpc/rpc-lib/src/rpcsvc.c
74096c
@@ -375,6 +375,12 @@ rpcsvc_program_actor(rpcsvc_request_t *req)
74096c
 
74096c
     req->ownthread = program->ownthread;
74096c
     req->synctask = program->synctask;
74096c
+    if (((req->procnum == GFS3_OP_RELEASE) ||
74096c
+         (req->procnum == GFS3_OP_RELEASEDIR)) &&
74096c
+        (program->prognum == GLUSTER_FOP_PROGRAM)) {
74096c
+        req->ownthread = _gf_false;
74096c
+        req->synctask = _gf_true;
74096c
+    }
74096c
 
74096c
     err = SUCCESS;
74096c
     gf_log(GF_RPCSVC, GF_LOG_TRACE, "Actor found: %s - %s for %s",
74096c
diff --git a/run-tests.sh b/run-tests.sh
74096c
index 5683b21..c835d93 100755
74096c
--- a/run-tests.sh
74096c
+++ b/run-tests.sh
74096c
@@ -356,7 +356,7 @@ function run_tests()
74096c
             selected_tests=$((selected_tests+1))
74096c
             echo
74096c
             echo $section_separator$section_separator
74096c
-            if [[ $(get_test_status $t) == "BAD_TEST" ]] && \
74096c
+            if [[ $(get_test_status $t) =~ "BAD_TEST" ]] && \
74096c
                [[ $skip_bad_tests == "yes" ]]
74096c
             then
74096c
                 skipped_bad_tests=$((skipped_bad_tests+1))
74096c
diff --git a/tests/features/ssl-authz.t b/tests/features/ssl-authz.t
74096c
index 132b598..497083e 100755
74096c
--- a/tests/features/ssl-authz.t
74096c
+++ b/tests/features/ssl-authz.t
74096c
@@ -67,13 +67,14 @@ echo "Memory consumption for glusterfsd process"
74096c
 for i in $(seq 1 100); do
74096c
         gluster v heal $V0 info >/dev/null
74096c
 done
74096c
-
74096c
+#Wait to cleanup memory
74096c
+sleep 10
74096c
 end=`pmap -x $glusterfsd_pid | grep total | awk -F " " '{print $4}'`
74096c
 diff=$((end-start))
74096c
 
74096c
-# If memory consumption is more than 5M some leak in SSL code path
74096c
+# If memory consumption is more than 15M some leak in SSL code path
74096c
 
74096c
-TEST [ $diff -lt 5000 ]
74096c
+TEST [ $diff -lt 15000 ]
74096c
 
74096c
 
74096c
 # Set ssl-allow to a wildcard that includes our identity.
74096c
diff --git a/xlators/storage/posix/src/posix-common.c b/xlators/storage/posix/src/posix-common.c
74096c
index 2cb58ba..ac53796 100644
74096c
--- a/xlators/storage/posix/src/posix-common.c
74096c
+++ b/xlators/storage/posix/src/posix-common.c
74096c
@@ -1041,10 +1041,6 @@ posix_init(xlator_t *this)
74096c
     pthread_mutex_init(&_private->janitor_mutex, NULL);
74096c
     pthread_cond_init(&_private->janitor_cond, NULL);
74096c
     INIT_LIST_HEAD(&_private->fsyncs);
74096c
-    ret = posix_spawn_ctx_janitor_thread(this);
74096c
-    if (ret)
74096c
-        goto out;
74096c
-
74096c
     ret = gf_thread_create(&_private->fsyncer, NULL, posix_fsyncer, this,
74096c
                            "posixfsy");
74096c
     if (ret) {
74096c
diff --git a/xlators/storage/posix/src/posix-helpers.c b/xlators/storage/posix/src/posix-helpers.c
74096c
index 2336add..39dbcce 100644
74096c
--- a/xlators/storage/posix/src/posix-helpers.c
74096c
+++ b/xlators/storage/posix/src/posix-helpers.c
74096c
@@ -1582,104 +1582,6 @@ unlock:
74096c
     return;
74096c
 }
74096c
 
74096c
-static struct posix_fd *
74096c
-janitor_get_next_fd(glusterfs_ctx_t *ctx, int32_t janitor_sleep)
74096c
-{
74096c
-    struct posix_fd *pfd = NULL;
74096c
-
74096c
-    struct timespec timeout;
74096c
-
74096c
-    pthread_mutex_lock(&ctx->janitor_lock);
74096c
-    {
74096c
-        if (list_empty(&ctx->janitor_fds)) {
74096c
-            time(&timeout.tv_sec);
74096c
-            timeout.tv_sec += janitor_sleep;
74096c
-            timeout.tv_nsec = 0;
74096c
-
74096c
-            pthread_cond_timedwait(&ctx->janitor_cond, &ctx->janitor_lock,
74096c
-                                   &timeout);
74096c
-            goto unlock;
74096c
-        }
74096c
-
74096c
-        pfd = list_entry(ctx->janitor_fds.next, struct posix_fd, list);
74096c
-
74096c
-        list_del(ctx->janitor_fds.next);
74096c
-    }
74096c
-unlock:
74096c
-    pthread_mutex_unlock(&ctx->janitor_lock);
74096c
-
74096c
-    return pfd;
74096c
-}
74096c
-
74096c
-static void *
74096c
-posix_ctx_janitor_thread_proc(void *data)
74096c
-{
74096c
-    xlator_t *this = NULL;
74096c
-    struct posix_fd *pfd;
74096c
-    glusterfs_ctx_t *ctx = NULL;
74096c
-    struct posix_private *priv = NULL;
74096c
-    int32_t sleep_duration = 0;
74096c
-
74096c
-    this = data;
74096c
-    ctx = THIS->ctx;
74096c
-    THIS = this;
74096c
-
74096c
-    priv = this->private;
74096c
-    sleep_duration = priv->janitor_sleep_duration;
74096c
-    while (1) {
74096c
-        pfd = janitor_get_next_fd(ctx, sleep_duration);
74096c
-        if (pfd) {
74096c
-            if (pfd->dir == NULL) {
74096c
-                gf_msg_trace(this->name, 0, "janitor: closing file fd=%d",
74096c
-                             pfd->fd);
74096c
-                sys_close(pfd->fd);
74096c
-            } else {
74096c
-                gf_msg_debug(this->name, 0, "janitor: closing dir fd=%p",
74096c
-                             pfd->dir);
74096c
-                sys_closedir(pfd->dir);
74096c
-            }
74096c
-
74096c
-            GF_FREE(pfd);
74096c
-        }
74096c
-    }
74096c
-
74096c
-    return NULL;
74096c
-}
74096c
-
74096c
-int
74096c
-posix_spawn_ctx_janitor_thread(xlator_t *this)
74096c
-{
74096c
-    struct posix_private *priv = NULL;
74096c
-    int ret = 0;
74096c
-    glusterfs_ctx_t *ctx = NULL;
74096c
-
74096c
-    priv = this->private;
74096c
-    ctx = THIS->ctx;
74096c
-
74096c
-    LOCK(&priv->lock);
74096c
-    {
74096c
-        if (!ctx->janitor) {
74096c
-            pthread_mutex_init(&ctx->janitor_lock, NULL);
74096c
-            pthread_cond_init(&ctx->janitor_cond, NULL);
74096c
-            INIT_LIST_HEAD(&ctx->janitor_fds);
74096c
-
74096c
-            ret = gf_thread_create(&ctx->janitor, NULL,
74096c
-                                   posix_ctx_janitor_thread_proc, this,
74096c
-                                   "posixctxjan");
74096c
-
74096c
-            if (ret) {
74096c
-                gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_THREAD_FAILED,
74096c
-                       "spawning janitor "
74096c
-                       "thread failed");
74096c
-                goto unlock;
74096c
-            }
74096c
-        }
74096c
-    }
74096c
-unlock:
74096c
-    UNLOCK(&priv->lock);
74096c
-    return ret;
74096c
-}
74096c
-
74096c
 static int
74096c
 is_fresh_file(int64_t ctime_sec)
74096c
 {
74096c
diff --git a/xlators/storage/posix/src/posix-inode-fd-ops.c b/xlators/storage/posix/src/posix-inode-fd-ops.c
74096c
index 5748b9f..d135d8b 100644
74096c
--- a/xlators/storage/posix/src/posix-inode-fd-ops.c
74096c
+++ b/xlators/storage/posix/src/posix-inode-fd-ops.c
74096c
@@ -1358,7 +1358,6 @@ posix_releasedir(xlator_t *this, fd_t *fd)
74096c
     struct posix_fd *pfd = NULL;
74096c
     uint64_t tmp_pfd = 0;
74096c
     int ret = 0;
74096c
-    glusterfs_ctx_t *ctx = NULL;
74096c
 
74096c
     VALIDATE_OR_GOTO(this, out);
74096c
     VALIDATE_OR_GOTO(fd, out);
74096c
@@ -1376,21 +1375,11 @@ posix_releasedir(xlator_t *this, fd_t *fd)
74096c
         goto out;
74096c
     }
74096c
 
74096c
-    ctx = THIS->ctx;
74096c
-
74096c
-    pthread_mutex_lock(&ctx->janitor_lock);
74096c
-    {
74096c
-        INIT_LIST_HEAD(&pfd->list);
74096c
-        list_add_tail(&pfd->list, &ctx->janitor_fds);
74096c
-        pthread_cond_signal(&ctx->janitor_cond);
74096c
-    }
74096c
-    pthread_mutex_unlock(&ctx->janitor_lock);
74096c
-
74096c
-    /*gf_msg_debug(this->name, 0, "janitor: closing dir fd=%p", pfd->dir);
74096c
+    gf_msg_debug(this->name, 0, "janitor: closing dir fd=%p", pfd->dir);
74096c
 
74096c
     sys_closedir(pfd->dir);
74096c
     GF_FREE(pfd);
74096c
-    */
74096c
+
74096c
 out:
74096c
     return 0;
74096c
 }
74096c
@@ -2510,13 +2499,11 @@ posix_release(xlator_t *this, fd_t *fd)
74096c
     struct posix_fd *pfd = NULL;
74096c
     int ret = -1;
74096c
     uint64_t tmp_pfd = 0;
74096c
-    glusterfs_ctx_t *ctx = NULL;
74096c
 
74096c
     VALIDATE_OR_GOTO(this, out);
74096c
     VALIDATE_OR_GOTO(fd, out);
74096c
 
74096c
     priv = this->private;
74096c
-    ctx = THIS->ctx;
74096c
 
74096c
     ret = fd_ctx_del(fd, this, &tmp_pfd);
74096c
     if (ret < 0) {
74096c
@@ -2531,13 +2518,10 @@ posix_release(xlator_t *this, fd_t *fd)
74096c
                "pfd->dir is %p (not NULL) for file fd=%p", pfd->dir, fd);
74096c
     }
74096c
 
74096c
-    pthread_mutex_lock(&ctx->janitor_lock);
74096c
-    {
74096c
-        INIT_LIST_HEAD(&pfd->list);
74096c
-        list_add_tail(&pfd->list, &ctx->janitor_fds);
74096c
-        pthread_cond_signal(&ctx->janitor_cond);
74096c
-    }
74096c
-    pthread_mutex_unlock(&ctx->janitor_lock);
74096c
+    gf_msg_debug(this->name, 0, "janitor: closing dir fd=%p", pfd->dir);
74096c
+
74096c
+    sys_close(pfd->fd);
74096c
+    GF_FREE(pfd);
74096c
 
74096c
     if (!priv)
74096c
         goto out;
74096c
diff --git a/xlators/storage/posix/src/posix.h b/xlators/storage/posix/src/posix.h
74096c
index ac9d83c..61495a7 100644
74096c
--- a/xlators/storage/posix/src/posix.h
74096c
+++ b/xlators/storage/posix/src/posix.h
74096c
@@ -666,9 +666,6 @@ posix_cs_maintenance(xlator_t *this, fd_t *fd, loc_t *loc, int *pfd,
74096c
 int
74096c
 posix_check_dev_file(xlator_t *this, inode_t *inode, char *fop, int *op_errno);
74096c
 
74096c
-int
74096c
-posix_spawn_ctx_janitor_thread(xlator_t *this);
74096c
-
74096c
 void
74096c
 posix_update_iatt_buf(struct iatt *buf, int fd, char *loc, dict_t *xdata);
74096c
 
74096c
-- 
74096c
1.8.3.1
74096c