d84cf8
From 143b93b230b429cc712353243ed794b68494c040 Mon Sep 17 00:00:00 2001
d84cf8
From: Mohit Agrawal <moagrawa@redhat.com>
d84cf8
Date: Mon, 27 Jul 2020 18:08:00 +0530
d84cf8
Subject: [PATCH 465/465] posix: Implement a janitor thread to close fd
d84cf8
d84cf8
Problem: In the commit fb20713b380e1df8d7f9e9df96563be2f9144fd6 we use
d84cf8
         syntask to close fd but we have found the patch is reducing the
d84cf8
         performance
d84cf8
d84cf8
Solution: Use janitor thread to close fd's and save the pfd ctx into
d84cf8
          ctx janitor list and also save the posix_xlator into pfd object to
d84cf8
          avoid the race condition during cleanup in brick_mux environment
d84cf8
d84cf8
> Change-Id: Ifb3d18a854b267333a3a9e39845bfefb83fbc092
d84cf8
> Fixes: #1396
d84cf8
> Signed-off-by: Mohit Agrawal <moagrawa@redhat.com>
d84cf8
> (Reviewed on upstream link https://review.gluster.org/#/c/glusterfs/+/24755/)
d84cf8
> (Cherry pick from commit 41b9616435cbdf671805856e487e373060c9455b
d84cf8
d84cf8
Change-Id: Ifb3d18a854b267333a3a9e39845bfefb83fbc092
d84cf8
BUG: 1851989
d84cf8
Signed-off-by: Mohit Agrawal <moagrawa@redhat.com>
d84cf8
Reviewed-on: https://code.engineering.redhat.com/gerrit/209448
d84cf8
Tested-by: RHGS Build Bot <nigelb@redhat.com>
d84cf8
Reviewed-by: Sunil Kumar Heggodu Gopala Acharya <sheggodu@redhat.com>
d84cf8
---
d84cf8
 glusterfsd/src/glusterfsd.c                    |  4 ++
d84cf8
 libglusterfs/src/glusterfs/glusterfs.h         |  7 ++
d84cf8
 rpc/rpc-lib/src/rpcsvc.c                       |  6 --
d84cf8
 xlators/storage/posix/src/posix-common.c       | 34 +++++++++-
d84cf8
 xlators/storage/posix/src/posix-helpers.c      | 93 ++++++++++++++++++++++++++
d84cf8
 xlators/storage/posix/src/posix-inode-fd-ops.c | 33 ++++-----
d84cf8
 xlators/storage/posix/src/posix.h              |  7 ++
d84cf8
 7 files changed, 161 insertions(+), 23 deletions(-)
d84cf8
d84cf8
diff --git a/glusterfsd/src/glusterfsd.c b/glusterfsd/src/glusterfsd.c
d84cf8
index 9821180..955bf1d 100644
d84cf8
--- a/glusterfsd/src/glusterfsd.c
d84cf8
+++ b/glusterfsd/src/glusterfsd.c
d84cf8
@@ -1839,6 +1839,10 @@ glusterfs_ctx_defaults_init(glusterfs_ctx_t *ctx)
d84cf8
 
d84cf8
     INIT_LIST_HEAD(&cmd_args->xlator_options);
d84cf8
     INIT_LIST_HEAD(&cmd_args->volfile_servers);
d84cf8
+    ctx->pxl_count = 0;
d84cf8
+    pthread_mutex_init(&ctx->fd_lock, NULL);
d84cf8
+    pthread_cond_init(&ctx->fd_cond, NULL);
d84cf8
+    INIT_LIST_HEAD(&ctx->janitor_fds);
d84cf8
 
d84cf8
     lim.rlim_cur = RLIM_INFINITY;
d84cf8
     lim.rlim_max = RLIM_INFINITY;
d84cf8
diff --git a/libglusterfs/src/glusterfs/glusterfs.h b/libglusterfs/src/glusterfs/glusterfs.h
d84cf8
index 495a4d7..bf6a987 100644
d84cf8
--- a/libglusterfs/src/glusterfs/glusterfs.h
d84cf8
+++ b/libglusterfs/src/glusterfs/glusterfs.h
d84cf8
@@ -733,6 +733,13 @@ struct _glusterfs_ctx {
d84cf8
     } stats;
d84cf8
 
d84cf8
     struct list_head volfile_list;
d84cf8
+    /* Add members to manage janitor threads for cleanup fd */
d84cf8
+    struct list_head janitor_fds;
d84cf8
+    pthread_cond_t fd_cond;
d84cf8
+    pthread_mutex_t fd_lock;
d84cf8
+    pthread_t janitor;
d84cf8
+    /* The variable is use to save total posix xlator count */
d84cf8
+    uint32_t pxl_count;
d84cf8
 
d84cf8
     char volume_id[GF_UUID_BUF_SIZE]; /* Used only in protocol/client */
d84cf8
 };
d84cf8
diff --git a/rpc/rpc-lib/src/rpcsvc.c b/rpc/rpc-lib/src/rpcsvc.c
d84cf8
index 23ca1fd..3f184bf 100644
d84cf8
--- a/rpc/rpc-lib/src/rpcsvc.c
d84cf8
+++ b/rpc/rpc-lib/src/rpcsvc.c
d84cf8
@@ -375,12 +375,6 @@ rpcsvc_program_actor(rpcsvc_request_t *req)
d84cf8
 
d84cf8
     req->ownthread = program->ownthread;
d84cf8
     req->synctask = program->synctask;
d84cf8
-    if (((req->procnum == GFS3_OP_RELEASE) ||
d84cf8
-         (req->procnum == GFS3_OP_RELEASEDIR)) &&
d84cf8
-        (program->prognum == GLUSTER_FOP_PROGRAM)) {
d84cf8
-        req->ownthread = _gf_false;
d84cf8
-        req->synctask = _gf_true;
d84cf8
-    }
d84cf8
 
d84cf8
     err = SUCCESS;
d84cf8
     gf_log(GF_RPCSVC, GF_LOG_TRACE, "Actor found: %s - %s for %s",
d84cf8
diff --git a/xlators/storage/posix/src/posix-common.c b/xlators/storage/posix/src/posix-common.c
d84cf8
index b317627..c5a43a1 100644
d84cf8
--- a/xlators/storage/posix/src/posix-common.c
d84cf8
+++ b/xlators/storage/posix/src/posix-common.c
d84cf8
@@ -150,6 +150,7 @@ posix_notify(xlator_t *this, int32_t event, void *data, ...)
d84cf8
     struct timespec sleep_till = {
d84cf8
         0,
d84cf8
     };
d84cf8
+    glusterfs_ctx_t *ctx = this->ctx;
d84cf8
 
d84cf8
     switch (event) {
d84cf8
         case GF_EVENT_PARENT_UP: {
d84cf8
@@ -160,8 +161,6 @@ posix_notify(xlator_t *this, int32_t event, void *data, ...)
d84cf8
         case GF_EVENT_PARENT_DOWN: {
d84cf8
             if (!victim->cleanup_starting)
d84cf8
                 break;
d84cf8
-            gf_log(this->name, GF_LOG_INFO, "Sending CHILD_DOWN for brick %s",
d84cf8
-                   victim->name);
d84cf8
 
d84cf8
             if (priv->janitor) {
d84cf8
                 pthread_mutex_lock(&priv->janitor_mutex);
d84cf8
@@ -187,6 +186,16 @@ posix_notify(xlator_t *this, int32_t event, void *data, ...)
d84cf8
                 GF_FREE(priv->janitor);
d84cf8
             }
d84cf8
             priv->janitor = NULL;
d84cf8
+            pthread_mutex_lock(&ctx->fd_lock);
d84cf8
+            {
d84cf8
+                while (priv->rel_fdcount > 0) {
d84cf8
+                    pthread_cond_wait(&priv->fd_cond, &ctx->fd_lock);
d84cf8
+                }
d84cf8
+            }
d84cf8
+            pthread_mutex_unlock(&ctx->fd_lock);
d84cf8
+
d84cf8
+            gf_log(this->name, GF_LOG_INFO, "Sending CHILD_DOWN for brick %s",
d84cf8
+                   victim->name);
d84cf8
             default_notify(this->parents->xlator, GF_EVENT_CHILD_DOWN, data);
d84cf8
         } break;
d84cf8
         default:
d84cf8
@@ -1038,7 +1047,13 @@ posix_init(xlator_t *this)
d84cf8
     pthread_cond_init(&_private->fsync_cond, NULL);
d84cf8
     pthread_mutex_init(&_private->janitor_mutex, NULL);
d84cf8
     pthread_cond_init(&_private->janitor_cond, NULL);
d84cf8
+    pthread_cond_init(&_private->fd_cond, NULL);
d84cf8
     INIT_LIST_HEAD(&_private->fsyncs);
d84cf8
+    _private->rel_fdcount = 0;
d84cf8
+    ret = posix_spawn_ctx_janitor_thread(this);
d84cf8
+    if (ret)
d84cf8
+        goto out;
d84cf8
+
d84cf8
     ret = gf_thread_create(&_private->fsyncer, NULL, posix_fsyncer, this,
d84cf8
                            "posixfsy");
d84cf8
     if (ret) {
d84cf8
@@ -1133,6 +1148,8 @@ posix_fini(xlator_t *this)
d84cf8
 {
d84cf8
     struct posix_private *priv = this->private;
d84cf8
     gf_boolean_t health_check = _gf_false;
d84cf8
+    glusterfs_ctx_t *ctx = this->ctx;
d84cf8
+    uint32_t count;
d84cf8
     int ret = 0;
d84cf8
 
d84cf8
     if (!priv)
d84cf8
@@ -1166,6 +1183,19 @@ posix_fini(xlator_t *this)
d84cf8
         priv->janitor = NULL;
d84cf8
     }
d84cf8
 
d84cf8
+    pthread_mutex_lock(&ctx->fd_lock);
d84cf8
+    {
d84cf8
+        count = --ctx->pxl_count;
d84cf8
+        if (count == 0) {
d84cf8
+            pthread_cond_signal(&ctx->fd_cond);
d84cf8
+        }
d84cf8
+    }
d84cf8
+    pthread_mutex_unlock(&ctx->fd_lock);
d84cf8
+
d84cf8
+    if (count == 0) {
d84cf8
+        pthread_join(ctx->janitor, NULL);
d84cf8
+    }
d84cf8
+
d84cf8
     if (priv->fsyncer) {
d84cf8
         (void)gf_thread_cleanup_xint(priv->fsyncer);
d84cf8
         priv->fsyncer = 0;
d84cf8
diff --git a/xlators/storage/posix/src/posix-helpers.c b/xlators/storage/posix/src/posix-helpers.c
d84cf8
index 39dbcce..73a44be 100644
d84cf8
--- a/xlators/storage/posix/src/posix-helpers.c
d84cf8
+++ b/xlators/storage/posix/src/posix-helpers.c
d84cf8
@@ -1582,6 +1582,99 @@ unlock:
d84cf8
     return;
d84cf8
 }
d84cf8
 
d84cf8
+static struct posix_fd *
d84cf8
+janitor_get_next_fd(glusterfs_ctx_t *ctx)
d84cf8
+{
d84cf8
+    struct posix_fd *pfd = NULL;
d84cf8
+
d84cf8
+    while (list_empty(&ctx->janitor_fds)) {
d84cf8
+        if (ctx->pxl_count == 0) {
d84cf8
+            return NULL;
d84cf8
+        }
d84cf8
+
d84cf8
+        pthread_cond_wait(&ctx->fd_cond, &ctx->fd_lock);
d84cf8
+    }
d84cf8
+
d84cf8
+    pfd = list_first_entry(&ctx->janitor_fds, struct posix_fd, list);
d84cf8
+    list_del_init(&pfd->list);
d84cf8
+
d84cf8
+    return pfd;
d84cf8
+}
d84cf8
+
d84cf8
+static void
d84cf8
+posix_close_pfd(xlator_t *xl, struct posix_fd *pfd)
d84cf8
+{
d84cf8
+    THIS = xl;
d84cf8
+
d84cf8
+    if (pfd->dir == NULL) {
d84cf8
+        gf_msg_trace(xl->name, 0, "janitor: closing file fd=%d", pfd->fd);
d84cf8
+        sys_close(pfd->fd);
d84cf8
+    } else {
d84cf8
+        gf_msg_debug(xl->name, 0, "janitor: closing dir fd=%p", pfd->dir);
d84cf8
+        sys_closedir(pfd->dir);
d84cf8
+    }
d84cf8
+
d84cf8
+    GF_FREE(pfd);
d84cf8
+}
d84cf8
+
d84cf8
+static void *
d84cf8
+posix_ctx_janitor_thread_proc(void *data)
d84cf8
+{
d84cf8
+    xlator_t *xl;
d84cf8
+    struct posix_fd *pfd;
d84cf8
+    glusterfs_ctx_t *ctx = NULL;
d84cf8
+    struct posix_private *priv_fd;
d84cf8
+
d84cf8
+    ctx = data;
d84cf8
+
d84cf8
+    pthread_mutex_lock(&ctx->fd_lock);
d84cf8
+
d84cf8
+    while ((pfd = janitor_get_next_fd(ctx)) != NULL) {
d84cf8
+        pthread_mutex_unlock(&ctx->fd_lock);
d84cf8
+
d84cf8
+        xl = pfd->xl;
d84cf8
+        posix_close_pfd(xl, pfd);
d84cf8
+
d84cf8
+        pthread_mutex_lock(&ctx->fd_lock);
d84cf8
+
d84cf8
+        priv_fd = xl->private;
d84cf8
+        priv_fd->rel_fdcount--;
d84cf8
+        if (!priv_fd->rel_fdcount)
d84cf8
+            pthread_cond_signal(&priv_fd->fd_cond);
d84cf8
+    }
d84cf8
+
d84cf8
+    pthread_mutex_unlock(&ctx->fd_lock);
d84cf8
+
d84cf8
+    return NULL;
d84cf8
+}
d84cf8
+
d84cf8
+int
d84cf8
+posix_spawn_ctx_janitor_thread(xlator_t *this)
d84cf8
+{
d84cf8
+    int ret = 0;
d84cf8
+    glusterfs_ctx_t *ctx = NULL;
d84cf8
+
d84cf8
+    ctx = this->ctx;
d84cf8
+
d84cf8
+    pthread_mutex_lock(&ctx->fd_lock);
d84cf8
+    {
d84cf8
+        if (ctx->pxl_count++ == 0) {
d84cf8
+            ret = gf_thread_create(&ctx->janitor, NULL,
d84cf8
+                                   posix_ctx_janitor_thread_proc, ctx,
d84cf8
+                                   "posixctxjan");
d84cf8
+
d84cf8
+            if (ret) {
d84cf8
+                gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_THREAD_FAILED,
d84cf8
+                       "spawning janitor thread failed");
d84cf8
+                ctx->pxl_count--;
d84cf8
+            }
d84cf8
+        }
d84cf8
+    }
d84cf8
+    pthread_mutex_unlock(&ctx->fd_lock);
d84cf8
+
d84cf8
+    return ret;
d84cf8
+}
d84cf8
+
d84cf8
 static int
d84cf8
 is_fresh_file(int64_t ctime_sec)
d84cf8
 {
d84cf8
diff --git a/xlators/storage/posix/src/posix-inode-fd-ops.c b/xlators/storage/posix/src/posix-inode-fd-ops.c
d84cf8
index 81f4a6b..21119ea 100644
d84cf8
--- a/xlators/storage/posix/src/posix-inode-fd-ops.c
d84cf8
+++ b/xlators/storage/posix/src/posix-inode-fd-ops.c
d84cf8
@@ -1352,6 +1352,22 @@ out:
d84cf8
     return 0;
d84cf8
 }
d84cf8
 
d84cf8
+static void
d84cf8
+posix_add_fd_to_cleanup(xlator_t *this, struct posix_fd *pfd)
d84cf8
+{
d84cf8
+    glusterfs_ctx_t *ctx = this->ctx;
d84cf8
+    struct posix_private *priv = this->private;
d84cf8
+
d84cf8
+    pfd->xl = this;
d84cf8
+    pthread_mutex_lock(&ctx->fd_lock);
d84cf8
+    {
d84cf8
+        list_add_tail(&pfd->list, &ctx->janitor_fds);
d84cf8
+        priv->rel_fdcount++;
d84cf8
+        pthread_cond_signal(&ctx->fd_cond);
d84cf8
+    }
d84cf8
+    pthread_mutex_unlock(&ctx->fd_lock);
d84cf8
+}
d84cf8
+
d84cf8
 int32_t
d84cf8
 posix_releasedir(xlator_t *this, fd_t *fd)
d84cf8
 {
d84cf8
@@ -1374,11 +1390,7 @@ posix_releasedir(xlator_t *this, fd_t *fd)
d84cf8
                "pfd->dir is NULL for fd=%p", fd);
d84cf8
         goto out;
d84cf8
     }
d84cf8
-
d84cf8
-    gf_msg_debug(this->name, 0, "janitor: closing dir fd=%p", pfd->dir);
d84cf8
-
d84cf8
-    sys_closedir(pfd->dir);
d84cf8
-    GF_FREE(pfd);
d84cf8
+    posix_add_fd_to_cleanup(this, pfd);
d84cf8
 
d84cf8
 out:
d84cf8
     return 0;
d84cf8
@@ -2494,7 +2506,6 @@ out:
d84cf8
 int32_t
d84cf8
 posix_release(xlator_t *this, fd_t *fd)
d84cf8
 {
d84cf8
-    struct posix_private *priv = NULL;
d84cf8
     struct posix_fd *pfd = NULL;
d84cf8
     int ret = -1;
d84cf8
     uint64_t tmp_pfd = 0;
d84cf8
@@ -2502,8 +2513,6 @@ posix_release(xlator_t *this, fd_t *fd)
d84cf8
     VALIDATE_OR_GOTO(this, out);
d84cf8
     VALIDATE_OR_GOTO(fd, out);
d84cf8
 
d84cf8
-    priv = this->private;
d84cf8
-
d84cf8
     ret = fd_ctx_del(fd, this, &tmp_pfd);
d84cf8
     if (ret < 0) {
d84cf8
         gf_msg(this->name, GF_LOG_WARNING, 0, P_MSG_PFD_NULL,
d84cf8
@@ -2517,13 +2526,7 @@ posix_release(xlator_t *this, fd_t *fd)
d84cf8
                "pfd->dir is %p (not NULL) for file fd=%p", pfd->dir, fd);
d84cf8
     }
d84cf8
 
d84cf8
-    gf_msg_debug(this->name, 0, "janitor: closing dir fd=%p", pfd->dir);
d84cf8
-
d84cf8
-    sys_close(pfd->fd);
d84cf8
-    GF_FREE(pfd);
d84cf8
-
d84cf8
-    if (!priv)
d84cf8
-        goto out;
d84cf8
+    posix_add_fd_to_cleanup(this, pfd);
d84cf8
 
d84cf8
 out:
d84cf8
     return 0;
d84cf8
diff --git a/xlators/storage/posix/src/posix.h b/xlators/storage/posix/src/posix.h
d84cf8
index 124dbb4..07f367b 100644
d84cf8
--- a/xlators/storage/posix/src/posix.h
d84cf8
+++ b/xlators/storage/posix/src/posix.h
d84cf8
@@ -134,6 +134,8 @@ struct posix_fd {
d84cf8
     off_t dir_eof; /* offset at dir EOF */
d84cf8
     int odirect;
d84cf8
     struct list_head list; /* to add to the janitor list */
d84cf8
+    xlator_t *xl;
d84cf8
+    char _pad[4]; /* manual padding */
d84cf8
 };
d84cf8
 
d84cf8
 struct posix_private {
d84cf8
@@ -204,6 +206,7 @@ struct posix_private {
d84cf8
     pthread_cond_t fsync_cond;
d84cf8
     pthread_mutex_t janitor_mutex;
d84cf8
     pthread_cond_t janitor_cond;
d84cf8
+    pthread_cond_t fd_cond;
d84cf8
     int fsync_queue_count;
d84cf8
 
d84cf8
     enum {
d84cf8
@@ -259,6 +262,7 @@ struct posix_private {
d84cf8
     gf_boolean_t fips_mode_rchecksum;
d84cf8
     gf_boolean_t ctime;
d84cf8
     gf_boolean_t janitor_task_stop;
d84cf8
+    uint32_t rel_fdcount;
d84cf8
 };
d84cf8
 
d84cf8
 typedef struct {
d84cf8
@@ -665,6 +669,9 @@ posix_cs_maintenance(xlator_t *this, fd_t *fd, loc_t *loc, int *pfd,
d84cf8
 int
d84cf8
 posix_check_dev_file(xlator_t *this, inode_t *inode, char *fop, int *op_errno);
d84cf8
 
d84cf8
+int
d84cf8
+posix_spawn_ctx_janitor_thread(xlator_t *this);
d84cf8
+
d84cf8
 void
d84cf8
 posix_update_iatt_buf(struct iatt *buf, int fd, char *loc, dict_t *xdata);
d84cf8
 
d84cf8
-- 
d84cf8
1.8.3.1
d84cf8