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