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