7f4c2a
From d77a9d1b2ba6aa582685d14e4301e4ea888f34bd Mon Sep 17 00:00:00 2001
7f4c2a
From: Anuradha Talur <atalur@redhat.com>
7f4c2a
Date: Fri, 26 Jun 2015 11:40:00 +0530
7f4c2a
Subject: [PATCH 146/190] cluster/afr : set pending xattrs for replaced brick
7f4c2a
7f4c2a
       Backport of: http://review.gluster.org/10448/
7f4c2a
7f4c2a
Patch for bug 1140649 was already merged downstream,
7f4c2a
commit-id : a4f226f50a2ef943b5db095877bab5a3eebf7283 .
7f4c2a
7f4c2a
There were a few amendments to be made.
7f4c2a
This patch contains delta changes b/w :
7f4c2a
1) http://review.gluster.org/10448/ &
7f4c2a
2) https://code.engineering.redhat.com/gerrit/#/c/51021/
7f4c2a
7f4c2a
Change-Id: Iad0c89578706b0b7e424b271977908e830c5236b
7f4c2a
BUG: 1140649
7f4c2a
Signed-off-by: Anuradha Talur <atalur@redhat.com>
7f4c2a
Reviewed-on: https://code.engineering.redhat.com/gerrit/51660
7f4c2a
Reviewed-by: Ravishankar Narayanankutty <ravishankar@redhat.com>
7f4c2a
Tested-by: Ravishankar Narayanankutty <ravishankar@redhat.com>
7f4c2a
---
7f4c2a
 tests/basic/afr/replace-brick-self-heal.t     |   33 +++++----
7f4c2a
 xlators/cluster/afr/src/afr-inode-write.c     |  102 ++++++++-----------------
7f4c2a
 xlators/cluster/afr/src/afr-messages.h        |   12 +++-
7f4c2a
 xlators/cluster/afr/src/afr-self-heal-entry.c |    7 ++
7f4c2a
 4 files changed, 69 insertions(+), 85 deletions(-)
7f4c2a
7f4c2a
diff --git a/tests/basic/afr/replace-brick-self-heal.t b/tests/basic/afr/replace-brick-self-heal.t
7f4c2a
index 1901466..8ced7df 100644
7f4c2a
--- a/tests/basic/afr/replace-brick-self-heal.t
7f4c2a
+++ b/tests/basic/afr/replace-brick-self-heal.t
7f4c2a
@@ -3,16 +3,6 @@
7f4c2a
 . $(dirname $0)/../../volume.rc
7f4c2a
 cleanup;
7f4c2a
 
7f4c2a
-function match_dirs {
7f4c2a
-        diff <(ls $1 | sort) <(ls $2 | sort)
7f4c2a
-        if [ $? -eq 0 ];
7f4c2a
-        then
7f4c2a
-                echo "Y"
7f4c2a
-        else
7f4c2a
-                echo "N"
7f4c2a
-        fi
7f4c2a
-}
7f4c2a
-
7f4c2a
 TEST glusterd
7f4c2a
 TEST pidof glusterd
7f4c2a
 TEST $CLI volume create $V0 replica 2 $H0:$B0/${V0}{0,1}
7f4c2a
@@ -24,12 +14,15 @@ TEST $CLI volume set $V0 cluster.entry-self-heal off
7f4c2a
 TEST $CLI volume set $V0 self-heal-daemon off
7f4c2a
 TEST glusterfs --volfile-id=$V0 --volfile-server=$H0 $M0;
7f4c2a
 
7f4c2a
-#Create files
7f4c2a
+# Create files
7f4c2a
 for i in {1..5}
7f4c2a
 do
7f4c2a
         echo $i > $M0/file$i.txt
7f4c2a
 done
7f4c2a
 
7f4c2a
+# Metadata changes
7f4c2a
+TEST setfattr -n user.test -v qwerty $M0/file5.txt
7f4c2a
+
7f4c2a
 # Replace brick1
7f4c2a
 TEST $CLI volume replace-brick $V0 $H0:$B0/${V0}1 $H0:$B0/${V0}1_new commit force
7f4c2a
 
7f4c2a
@@ -45,15 +38,27 @@ EXPECT_WITHIN $PROCESS_UP_TIMEOUT "1" afr_child_up_status $V0 1
7f4c2a
 
7f4c2a
 TEST $CLI volume set $V0 self-heal-daemon on
7f4c2a
 EXPECT_WITHIN $PROCESS_UP_TIMEOUT "Y" glustershd_up_status
7f4c2a
+EXPECT_WITHIN $CHILD_UP_TIMEOUT "1" afr_child_up_status_in_shd $V0 0
7f4c2a
+EXPECT_WITHIN $CHILD_UP_TIMEOUT "1" afr_child_up_status_in_shd $V0 1
7f4c2a
 TEST $CLI volume heal $V0
7f4c2a
 
7f4c2a
-# Check if heal has happened
7f4c2a
-EXPECT_WITHIN $HEAL_TIMEOUT "Y" match_dirs $B0/${V0}0 $B0/${V0}1_new
7f4c2a
+# Wait for heal to complete
7f4c2a
+EXPECT_WITHIN $HEAL_TIMEOUT "0" afr_get_pending_heal_count $V0
7f4c2a
 
7f4c2a
-# To make sure that data was not lost from brick0
7f4c2a
+# Check if entry-heal has happened
7f4c2a
+TEST diff <(ls $B0/${V0}0 | sort) <(ls $B0/${V0}1_new | sort)
7f4c2a
+
7f4c2a
+# To make sure that files were not lost from brick0
7f4c2a
 TEST diff <(ls $B0/${V0}0 | sort) <(ls $B0/${V0}1 | sort)
7f4c2a
 EXPECT "000000000000000000000000" get_hex_xattr trusted.afr.$V0-client-1 $B0/${V0}0
7f4c2a
 
7f4c2a
 # Test if data was healed
7f4c2a
+TEST diff $B0/${V0}0/file1.txt $B0/${V0}1_new/file1.txt
7f4c2a
+# To make sure that data was not lost from brick0
7f4c2a
 TEST diff $B0/${V0}0/file1.txt $B0/${V0}1/file1.txt
7f4c2a
+
7f4c2a
+# Test if metadata was healed and exists on both the bricks
7f4c2a
+EXPECT "qwerty" get_text_xattr user.test $B0/${V0}1_new/file5.txt
7f4c2a
+EXPECT "qwerty" get_text_xattr user.test $B0/${V0}0/file5.txt
7f4c2a
+
7f4c2a
 cleanup;
7f4c2a
diff --git a/xlators/cluster/afr/src/afr-inode-write.c b/xlators/cluster/afr/src/afr-inode-write.c
7f4c2a
index 5d32927..8800b81 100644
7f4c2a
--- a/xlators/cluster/afr/src/afr-inode-write.c
7f4c2a
+++ b/xlators/cluster/afr/src/afr-inode-write.c
7f4c2a
@@ -38,6 +38,7 @@
7f4c2a
 #include "byte-order.h"
7f4c2a
 #include "afr-transaction.h"
7f4c2a
 #include "afr-self-heal.h"
7f4c2a
+#include "afr-messages.h"
7f4c2a
 
7f4c2a
 static void
7f4c2a
 __afr_inode_write_finalize (call_frame_t *frame, xlator_t *this)
7f4c2a
@@ -979,42 +980,26 @@ afr_rb_set_pending_changelog_cbk (call_frame_t *frame, void *cookie,
7f4c2a
 
7f4c2a
 {
7f4c2a
         afr_local_t *local = NULL;
7f4c2a
+        afr_private_t *priv = NULL;
7f4c2a
         int i = 0;
7f4c2a
 
7f4c2a
         local = frame->local;
7f4c2a
+        priv = this->private;
7f4c2a
         i = (long) cookie;
7f4c2a
 
7f4c2a
         local->replies[i].valid = 1;
7f4c2a
         local->replies[i].op_ret = op_ret;
7f4c2a
         local->replies[i].op_errno = op_errno;
7f4c2a
+        gf_msg (this->name, op_ret ? GF_LOG_ERROR : GF_LOG_INFO,
7f4c2a
+                op_ret ? op_errno : 0,
7f4c2a
+                AFR_MSG_REPLACE_BRICK_STATUS, "Set of pending xattr %s on"
7f4c2a
+                " %s.", op_ret ? "failed" : "succeeded",
7f4c2a
+                priv->children[i]->name);
7f4c2a
 
7f4c2a
         syncbarrier_wake (&local->barrier);
7f4c2a
         return 0;
7f4c2a
 }
7f4c2a
 
7f4c2a
-char *
7f4c2a
-afr_opret_matrix_generate (afr_private_t *priv, afr_local_t *local)
7f4c2a
-{
7f4c2a
-        char *matrix = NULL;
7f4c2a
-        char *ptr    = NULL;
7f4c2a
-        int   i      = 0;
7f4c2a
-
7f4c2a
-        /* Allocate max amount of chars required, including -ve values
7f4c2a
-         * and spaces */
7f4c2a
-        matrix = GF_CALLOC (priv->child_count, 3 * sizeof (char),
7f4c2a
-                            gf_afr_mt_char);
7f4c2a
-        if (!matrix)
7f4c2a
-                return NULL;
7f4c2a
-        ptr = matrix;
7f4c2a
-        for (i = 0; i < priv->child_count; i++) {
7f4c2a
-                if (local->replies[i].valid)
7f4c2a
-                        ptr += sprintf (ptr, "%d ", local->replies[i].op_ret);
7f4c2a
-                else
7f4c2a
-                        ptr += sprintf (ptr, "-1 ");
7f4c2a
-        }
7f4c2a
-        return matrix;
7f4c2a
-}
7f4c2a
-
7f4c2a
 int
7f4c2a
 afr_rb_set_pending_changelog (call_frame_t *frame, xlator_t *this,
7f4c2a
                               unsigned char *locked_nodes)
7f4c2a
@@ -1022,7 +1007,6 @@ afr_rb_set_pending_changelog (call_frame_t *frame, xlator_t *this,
7f4c2a
         afr_local_t *local = NULL;
7f4c2a
         afr_private_t *priv = NULL;
7f4c2a
         int ret = 0, i = 0;
7f4c2a
-        char *matrix = NULL;
7f4c2a
 
7f4c2a
         local = frame->local;
7f4c2a
         priv = this->private;
7f4c2a
@@ -1033,22 +1017,18 @@ afr_rb_set_pending_changelog (call_frame_t *frame, xlator_t *this,
7f4c2a
 
7f4c2a
         /* It is sufficient if xattrop was successful on one child */
7f4c2a
         for (i = 0; i < priv->child_count; i++) {
7f4c2a
-                if (local->replies[i].valid &&
7f4c2a
-                    local->replies[i].op_ret == 0) {
7f4c2a
-                        matrix = afr_opret_matrix_generate (priv, local);
7f4c2a
-                        gf_log (this->name, GF_LOG_DEBUG, "Successfully set "
7f4c2a
-                                "pending changelog. op_ret matrix : [ %s].",
7f4c2a
-                                matrix);
7f4c2a
+                if (!local->replies[i].valid)
7f4c2a
+                        continue;
7f4c2a
+
7f4c2a
+                if (local->replies[i].op_ret == 0) {
7f4c2a
                         ret = 0;
7f4c2a
                         goto out;
7f4c2a
+                } else {
7f4c2a
+                        ret = afr_higher_errno (ret,
7f4c2a
+                                                local->replies[i].op_errno);
7f4c2a
                 }
7f4c2a
-                ret = afr_higher_errno (ret, local->replies[i].op_errno);
7f4c2a
         }
7f4c2a
-        gf_log (this->name, GF_LOG_ERROR, "Couldn't set pending xattr "
7f4c2a
-                "on any child. (%s)", strerror (ret));
7f4c2a
 out:
7f4c2a
-        if (matrix)
7f4c2a
-                GF_FREE (matrix);
7f4c2a
         return -ret;
7f4c2a
 }
7f4c2a
 
7f4c2a
@@ -1060,9 +1040,6 @@ _afr_handle_replace_brick_type (xlator_t *this, call_frame_t *frame,
7f4c2a
         afr_local_t     *local            = NULL;
7f4c2a
         afr_private_t   *priv             = NULL;
7f4c2a
         unsigned char   *locked_nodes     = NULL;
7f4c2a
-        struct           gf_flock flock   = {0, };
7f4c2a
-        struct           gf_flock unflock = {0, };
7f4c2a
-        int              i                = 0;
7f4c2a
         int              count            = 0;
7f4c2a
         int              ret              = -ENOMEM;
7f4c2a
         int              idx              = -1;
7f4c2a
@@ -1079,10 +1056,7 @@ _afr_handle_replace_brick_type (xlator_t *this, call_frame_t *frame,
7f4c2a
         if (!local->pending)
7f4c2a
                 goto out;
7f4c2a
 
7f4c2a
-        for (i = 0; i < priv->child_count; i++) {
7f4c2a
-                if (i == rb_index)
7f4c2a
-                        local->pending[i][idx] = hton32 (1);
7f4c2a
-        }
7f4c2a
+        local->pending[rb_index][idx] = hton32 (1);
7f4c2a
 
7f4c2a
         local->xdata_req = dict_new ();
7f4c2a
         if (!local->xdata_req)
7f4c2a
@@ -1093,16 +1067,13 @@ _afr_handle_replace_brick_type (xlator_t *this, call_frame_t *frame,
7f4c2a
                 goto out;
7f4c2a
 
7f4c2a
         if (AFR_ENTRY_TRANSACTION == type) {
7f4c2a
-                AFR_SEQ (frame, afr_selfheal_lock_cbk, entrylk, this->name,
7f4c2a
-                         loc, NULL, ENTRYLK_LOCK, ENTRYLK_WRLCK, NULL);
7f4c2a
+                count = afr_selfheal_entrylk (frame, this, loc->inode,
7f4c2a
+                                              this->name, NULL, locked_nodes);
7f4c2a
         } else {
7f4c2a
-                flock.l_type = F_WRLCK;
7f4c2a
-                flock.l_start = LLONG_MAX - 1;
7f4c2a
-                flock.l_len = 0;
7f4c2a
-                AFR_SEQ (frame, afr_selfheal_lock_cbk, inodelk, this->name,
7f4c2a
-                         loc, F_SETLKW, &flock, NULL);
7f4c2a
+                count = afr_selfheal_inodelk (frame, this, loc->inode,
7f4c2a
+                                              this->name, LLONG_MAX - 1, 0,
7f4c2a
+                                              locked_nodes);
7f4c2a
         }
7f4c2a
-        count = afr_locked_fill (frame, this, locked_nodes);
7f4c2a
 
7f4c2a
         if (!count) {
7f4c2a
                 gf_log (this->name, GF_LOG_ERROR, "Couldn't acquire lock on"
7f4c2a
@@ -1117,15 +1088,11 @@ _afr_handle_replace_brick_type (xlator_t *this, call_frame_t *frame,
7f4c2a
         ret = 0;
7f4c2a
 unlock:
7f4c2a
         if (AFR_ENTRY_TRANSACTION == type) {
7f4c2a
-                AFR_ONLIST (locked_nodes, frame, afr_selfheal_lock_cbk,
7f4c2a
-                            entrylk, this->name, loc, NULL, ENTRYLK_UNLOCK,
7f4c2a
-                            ENTRYLK_WRLCK, NULL);
7f4c2a
+                afr_selfheal_unentrylk (frame, this, loc->inode, this->name,
7f4c2a
+                                        NULL, locked_nodes);
7f4c2a
         } else {
7f4c2a
-                unflock.l_type = F_UNLCK;
7f4c2a
-                unflock.l_start = LLONG_MAX - 1;
7f4c2a
-                unflock.l_len = 0;
7f4c2a
-                AFR_ONLIST (locked_nodes, frame, afr_selfheal_lock_cbk,
7f4c2a
-                            inodelk, this->name, loc, F_SETLK, &unflock, NULL);
7f4c2a
+                afr_selfheal_uninodelk (frame, this, loc->inode, this->name,
7f4c2a
+                                        LLONG_MAX - 1, 0, locked_nodes);
7f4c2a
         }
7f4c2a
 out:
7f4c2a
         return ret;
7f4c2a
@@ -1162,6 +1129,8 @@ _afr_handle_replace_brick (xlator_t *this, call_frame_t *frame, loc_t *loc,
7f4c2a
 
7f4c2a
         dict_unref (local->xdata_req);
7f4c2a
         afr_matrix_cleanup (local->pending, priv->child_count);
7f4c2a
+        local->pending = NULL;
7f4c2a
+        local->xdata_req = NULL;
7f4c2a
 
7f4c2a
         ret = _afr_handle_replace_brick_type (this, frame, loc, rb_index,
7f4c2a
                                               AFR_ENTRY_TRANSACTION);
7f4c2a
@@ -1379,21 +1348,14 @@ int
7f4c2a
 afr_handle_replace_brick (xlator_t *this, call_frame_t *frame, loc_t *loc,
7f4c2a
                           dict_t *dict)
7f4c2a
 {
7f4c2a
-        int             len               = 0;
7f4c2a
         int             ret               = -1;
7f4c2a
         int             rb_index          = -1;
7f4c2a
-        int             op_errno          = EPERM;
7f4c2a
-        void           *value             = NULL;
7f4c2a
         char           *replace_brick     = NULL;
7f4c2a
 
7f4c2a
-        ret =  dict_get_ptr_and_len (dict, GF_AFR_REPLACE_BRICK, &value,
7f4c2a
-                                     &len;;
7f4c2a
-
7f4c2a
-        if (value) {
7f4c2a
-                replace_brick = alloca0 (len + 1);
7f4c2a
-                memcpy (replace_brick, value, len);
7f4c2a
+        ret =  dict_get_str (dict, GF_AFR_REPLACE_BRICK, &replace_brick);
7f4c2a
 
7f4c2a
-                if (!(frame->root->pid == GF_CLIENT_PID_AFR_SELF_HEALD)) {
7f4c2a
+        if (!ret) {
7f4c2a
+                if (frame->root->pid != GF_CLIENT_PID_AFR_SELF_HEALD) {
7f4c2a
                         ret = 1;
7f4c2a
                         goto out;
7f4c2a
                 }
7f4c2a
@@ -1412,8 +1374,8 @@ out:
7f4c2a
         if (ret == 1) {
7f4c2a
                 gf_log (this->name, GF_LOG_ERROR, "'%s' is an internal"
7f4c2a
                         " extended attribute : %s.",
7f4c2a
-                        GF_AFR_REPLACE_BRICK, strerror (op_errno));
7f4c2a
-                AFR_STACK_UNWIND (setxattr, frame, -1, op_errno, NULL);
7f4c2a
+                        GF_AFR_REPLACE_BRICK, strerror (EPERM));
7f4c2a
+                AFR_STACK_UNWIND (setxattr, frame, -1, EPERM, NULL);
7f4c2a
                 ret = 0;
7f4c2a
         }
7f4c2a
         return ret;
7f4c2a
diff --git a/xlators/cluster/afr/src/afr-messages.h b/xlators/cluster/afr/src/afr-messages.h
7f4c2a
index 52bdead..66b8ed1 100644
7f4c2a
--- a/xlators/cluster/afr/src/afr-messages.h
7f4c2a
+++ b/xlators/cluster/afr/src/afr-messages.h
7f4c2a
@@ -45,7 +45,7 @@
7f4c2a
  */
7f4c2a
 
7f4c2a
 #define GLFS_COMP_BASE_AFR GLFS_MSGID_COMP_AFR
7f4c2a
-#define GLFS_NUM_MESSAGES 10
7f4c2a
+#define GLFS_NUM_MESSAGES 11
7f4c2a
 #define GLFS_MSGID_END (GLFS_COMP_BASE_AFR + GLFS_NUM_MESSAGES + 1)
7f4c2a
 
7f4c2a
 #define glfs_msg_start_x GLFS_COMP_BASE_AFR, "Invalid: Start of messages"
7f4c2a
@@ -142,6 +142,16 @@
7f4c2a
 #define AFR_MSG_INODE_UNLOCK_FAIL       (GLFS_COMP_BASE_AFR + 10)
7f4c2a
 
7f4c2a
 
7f4c2a
+/*!
7f4c2a
+ * @messageid 108011
7f4c2a
+ * @diagnosis Setting of pending xattrs succeeded/failed during replace-brick
7f4c2a
+ * operation.
7f4c2a
+ * @recommendedaction In case of failure, error number in the log should give
7f4c2a
+ * the reason why it failed. Also observe brick logs for more information.
7f4c2a
+*/
7f4c2a
+#define AFR_MSG_REPLACE_BRICK_STATUS     (GLFS_COMP_BASE_AFR + 11)
7f4c2a
+
7f4c2a
+
7f4c2a
 #define glfs_msg_end_x GLFS_MSGID_END, "Invalid: End of messages"
7f4c2a
 
7f4c2a
 #endif /* !_AFR_MESSAGES_H_ */
7f4c2a
diff --git a/xlators/cluster/afr/src/afr-self-heal-entry.c b/xlators/cluster/afr/src/afr-self-heal-entry.c
7f4c2a
index e64b6e4..12da920 100644
7f4c2a
--- a/xlators/cluster/afr/src/afr-self-heal-entry.c
7f4c2a
+++ b/xlators/cluster/afr/src/afr-self-heal-entry.c
7f4c2a
@@ -295,6 +295,13 @@ __afr_selfheal_merge_dirent (call_frame_t *frame, xlator_t *this, fd_t *fd,
7f4c2a
 		return 0;
7f4c2a
 	}
7f4c2a
 
7f4c2a
+        /* Set all the sources as 1, otheriwse newentry_mark won't be set */
7f4c2a
+	for (i = 0; i < priv->child_count; i++) {
7f4c2a
+		if (replies[i].valid && replies[i].op_ret == 0) {
7f4c2a
+			sources[i] = 1;
7f4c2a
+		}
7f4c2a
+	}
7f4c2a
+
7f4c2a
         /* In case of a gfid or type mismatch on the entry, return -1.*/
7f4c2a
         ret = afr_selfheal_detect_gfid_and_type_mismatch (this, replies,
7f4c2a
                                                           fd->inode->gfid,
7f4c2a
-- 
7f4c2a
1.7.1
7f4c2a