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