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