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