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