256ebe
From 646292b4f73bf1b506d034b85787f794963d7196 Mon Sep 17 00:00:00 2001
256ebe
From: Mohammed Rafi KC <rkavunga@redhat.com>
256ebe
Date: Mon, 6 May 2019 23:35:08 +0530
256ebe
Subject: [PATCH 137/141] shd/glusterd: Serialize shd manager to prevent race
256ebe
 condition
256ebe
256ebe
At the time of a glusterd restart, while doing a handshake
256ebe
there is a possibility that multiple shd manager might get
256ebe
executed. Because of this, there is a chance that multiple
256ebe
shd get spawned during a glusterd restart
256ebe
256ebe
> upstream patch : https://review.gluster.org/#/c/glusterfs/+/22667/
256ebe
256ebe
>Change-Id: Ie20798441e07d7d7a93b7d38dfb924cea178a920
256ebe
>fixes: bz#1707081
256ebe
>Signed-off-by: Mohammed Rafi KC <rkavunga@redhat.com>
256ebe
256ebe
BUG: 1704851
256ebe
Change-Id: Ie20798441e07d7d7a93b7d38dfb924cea178a920
256ebe
Signed-off-by: Mohammed Rafi KC <rkavunga@redhat.com>
256ebe
Reviewed-on: https://code.engineering.redhat.com/gerrit/169947
256ebe
Tested-by: RHGS Build Bot <nigelb@redhat.com>
256ebe
Reviewed-by: Sunil Kumar Heggodu Gopala Acharya <sheggodu@redhat.com>
256ebe
---
256ebe
 .../serialize-shd-manager-glusterd-restart.t       | 54 ++++++++++++++++++++++
256ebe
 xlators/mgmt/glusterd/src/glusterd-shd-svc.c       | 14 ++++++
256ebe
 xlators/mgmt/glusterd/src/glusterd.c               |  1 +
256ebe
 xlators/mgmt/glusterd/src/glusterd.h               |  3 ++
256ebe
 4 files changed, 72 insertions(+)
256ebe
 create mode 100644 tests/bugs/glusterd/serialize-shd-manager-glusterd-restart.t
256ebe
256ebe
diff --git a/tests/bugs/glusterd/serialize-shd-manager-glusterd-restart.t b/tests/bugs/glusterd/serialize-shd-manager-glusterd-restart.t
256ebe
new file mode 100644
256ebe
index 0000000..3a27c2a
256ebe
--- /dev/null
256ebe
+++ b/tests/bugs/glusterd/serialize-shd-manager-glusterd-restart.t
256ebe
@@ -0,0 +1,54 @@
256ebe
+#! /bin/bash
256ebe
+
256ebe
+. $(dirname $0)/../../include.rc
256ebe
+. $(dirname $0)/../../cluster.rc
256ebe
+
256ebe
+function check_peers {
256ebe
+count=`$CLI_1 peer status | grep 'Peer in Cluster (Connected)' | wc -l`
256ebe
+echo $count
256ebe
+}
256ebe
+
256ebe
+function check_shd {
256ebe
+ps aux | grep $1 | grep glustershd | wc -l
256ebe
+}
256ebe
+
256ebe
+cleanup
256ebe
+
256ebe
+
256ebe
+TEST launch_cluster 6
256ebe
+
256ebe
+TESTS_EXPECTED_IN_LOOP=25
256ebe
+for i in $(seq 2 6); do
256ebe
+    hostname="H$i"
256ebe
+    TEST $CLI_1 peer probe ${!hostname}
256ebe
+done
256ebe
+
256ebe
+
256ebe
+EXPECT_WITHIN $PROBE_TIMEOUT 5 check_peers;
256ebe
+for i in $(seq 1 5); do
256ebe
+
256ebe
+    TEST $CLI_1 volume create ${V0}_$i replica 3 $H1:$B1/${V0}_$i $H2:$B2/${V0}_$i $H3:$B3/${V0}_$i $H4:$B4/${V0}_$i $H5:$B5/${V0}_$i $H6:$B6/${V0}_$i
256ebe
+    TEST $CLI_1 volume start ${V0}_$i force
256ebe
+
256ebe
+done
256ebe
+
256ebe
+#kill a node
256ebe
+TEST kill_node 3
256ebe
+
256ebe
+TEST $glusterd_3;
256ebe
+EXPECT_WITHIN $PROBE_TIMEOUT 5 check_peers
256ebe
+
256ebe
+EXPECT_WITHIN $PROCESS_UP_TIMEOUT 1 check_shd $H3
256ebe
+
256ebe
+for i in $(seq 1 5); do
256ebe
+
256ebe
+    TEST $CLI_1 volume stop ${V0}_$i
256ebe
+    TEST $CLI_1 volume delete ${V0}_$i
256ebe
+
256ebe
+done
256ebe
+
256ebe
+for i in $(seq 1 6); do
256ebe
+    hostname="H$i"
256ebe
+    EXPECT_WITHIN $PROCESS_DOWN_TIMEOUT 0 check_shd ${!hostname}
256ebe
+done
256ebe
+cleanup
256ebe
diff --git a/xlators/mgmt/glusterd/src/glusterd-shd-svc.c b/xlators/mgmt/glusterd/src/glusterd-shd-svc.c
256ebe
index a9eab42..75f9a07 100644
256ebe
--- a/xlators/mgmt/glusterd/src/glusterd-shd-svc.c
256ebe
+++ b/xlators/mgmt/glusterd/src/glusterd-shd-svc.c
256ebe
@@ -254,14 +254,26 @@ glusterd_shdsvc_manager(glusterd_svc_t *svc, void *data, int flags)
256ebe
 {
256ebe
     int ret = -1;
256ebe
     glusterd_volinfo_t *volinfo = NULL;
256ebe
+    glusterd_conf_t *conf = NULL;
256ebe
+    gf_boolean_t shd_restart = _gf_false;
256ebe
 
256ebe
+    conf = THIS->private;
256ebe
     volinfo = data;
256ebe
+    GF_VALIDATE_OR_GOTO("glusterd", conf, out);
256ebe
     GF_VALIDATE_OR_GOTO("glusterd", svc, out);
256ebe
     GF_VALIDATE_OR_GOTO("glusterd", volinfo, out);
256ebe
 
256ebe
     if (volinfo)
256ebe
         glusterd_volinfo_ref(volinfo);
256ebe
 
256ebe
+    while (conf->restart_shd) {
256ebe
+        synclock_unlock(&conf->big_lock);
256ebe
+        sleep(2);
256ebe
+        synclock_lock(&conf->big_lock);
256ebe
+    }
256ebe
+    conf->restart_shd = _gf_true;
256ebe
+    shd_restart = _gf_true;
256ebe
+
256ebe
     ret = glusterd_shdsvc_create_volfile(volinfo);
256ebe
     if (ret)
256ebe
         goto out;
256ebe
@@ -310,6 +322,8 @@ glusterd_shdsvc_manager(glusterd_svc_t *svc, void *data, int flags)
256ebe
         }
256ebe
     }
256ebe
 out:
256ebe
+    if (shd_restart)
256ebe
+        conf->restart_shd = _gf_false;
256ebe
     if (volinfo)
256ebe
         glusterd_volinfo_unref(volinfo);
256ebe
     if (ret)
256ebe
diff --git a/xlators/mgmt/glusterd/src/glusterd.c b/xlators/mgmt/glusterd/src/glusterd.c
256ebe
index c0973cb..6d7dd4a 100644
256ebe
--- a/xlators/mgmt/glusterd/src/glusterd.c
256ebe
+++ b/xlators/mgmt/glusterd/src/glusterd.c
256ebe
@@ -1819,6 +1819,7 @@ init(xlator_t *this)
256ebe
     conf->rpc = rpc;
256ebe
     conf->uds_rpc = uds_rpc;
256ebe
     conf->gfs_mgmt = &gd_brick_prog;
256ebe
+    conf->restart_shd = _gf_false;
256ebe
     this->private = conf;
256ebe
     /* conf->workdir and conf->rundir are smaller than PATH_MAX; gcc's
256ebe
      * snprintf checking will throw an error here if sprintf is used.
256ebe
diff --git a/xlators/mgmt/glusterd/src/glusterd.h b/xlators/mgmt/glusterd/src/glusterd.h
256ebe
index bd9f509..2ea8560 100644
256ebe
--- a/xlators/mgmt/glusterd/src/glusterd.h
256ebe
+++ b/xlators/mgmt/glusterd/src/glusterd.h
256ebe
@@ -222,6 +222,9 @@ typedef struct {
256ebe
     gf_atomic_t blockers;
256ebe
     uint32_t mgmt_v3_lock_timeout;
256ebe
     gf_boolean_t restart_bricks;
256ebe
+    gf_boolean_t restart_shd;    /* This flag prevents running two shd manager
256ebe
+                                    simultaneously
256ebe
+                                 */
256ebe
     pthread_mutex_t attach_lock; /* Lock can be per process or a common one */
256ebe
     pthread_mutex_t volume_lock; /* We release the big_lock from lot of places
256ebe
                                     which might lead the modification of volinfo
256ebe
-- 
256ebe
1.8.3.1
256ebe