c5d8c8
From 2cb90b7798fa469f2d7d938ae88733eb1962d63d Mon Sep 17 00:00:00 2001
c5d8c8
From: Xavi Hernandez <xhernandez@gmail.com>
c5d8c8
Date: Fri, 9 Apr 2021 18:13:30 +0200
c5d8c8
Subject: [PATCH 554/584] dht: fix rebalance of sparse files
c5d8c8
c5d8c8
Current implementation of rebalance for sparse files has a bug that,
c5d8c8
in some cases, causes a read of 0 bytes from the source subvolume.
c5d8c8
Posix xlator doesn't allow 0 byte reads and fails them with EINVAL,
c5d8c8
which causes rebalance to abort the migration.
c5d8c8
c5d8c8
This patch implements a more robust way of finding data segments in
c5d8c8
a sparse file that avoids 0 byte reads, allowing the file to be
c5d8c8
migrated successfully.
c5d8c8
c5d8c8
Backport of:
c5d8c8
> Upstream-patch: https://github.com/gluster/glusterfs/pull/2318
c5d8c8
> Fixes: #2317
c5d8c8
> Change-Id: Iff168dda2fb0f2edf716b21eb04cc2cc8ac3915c
c5d8c8
> Signed-off-by: Xavi Hernandez <xhernandez@redhat.com>
c5d8c8
c5d8c8
BUG: 1957641
c5d8c8
Change-Id: Iff168dda2fb0f2edf716b21eb04cc2cc8ac3915c
c5d8c8
Signed-off-by: Xavi Hernandez <xhernandez@redhat.com>
c5d8c8
Reviewed-on: https://code.engineering.redhat.com/gerrit/c/rhs-glusterfs/+/244551
c5d8c8
Tested-by: RHGS Build Bot <nigelb@redhat.com>
c5d8c8
Reviewed-by: Sunil Kumar Heggodu Gopala Acharya <sheggodu@redhat.com>
c5d8c8
---
c5d8c8
 tests/bugs/distribute/issue-2317.t      |  29 ++++++++
c5d8c8
 tests/volume.rc                         |   4 ++
c5d8c8
 xlators/cluster/dht/src/dht-rebalance.c | 116 +++++++++++++++++---------------
c5d8c8
 3 files changed, 93 insertions(+), 56 deletions(-)
c5d8c8
 create mode 100755 tests/bugs/distribute/issue-2317.t
c5d8c8
c5d8c8
diff --git a/tests/bugs/distribute/issue-2317.t b/tests/bugs/distribute/issue-2317.t
c5d8c8
new file mode 100755
c5d8c8
index 0000000..e29d003
c5d8c8
--- /dev/null
c5d8c8
+++ b/tests/bugs/distribute/issue-2317.t
c5d8c8
@@ -0,0 +1,29 @@
c5d8c8
+#!/bin/bash
c5d8c8
+
c5d8c8
+. $(dirname $0)/../../include.rc
c5d8c8
+. $(dirname $0)/../../volume.rc
c5d8c8
+
c5d8c8
+TESTS_EXPECTED_IN_LOOP=126
c5d8c8
+
c5d8c8
+cleanup
c5d8c8
+
c5d8c8
+TEST glusterd
c5d8c8
+TEST ${CLI} volume create ${V0} replica 3 ${H0}:/$B0/${V0}_{0..2}
c5d8c8
+TEST ${CLI} volume start ${V0}
c5d8c8
+
c5d8c8
+TEST ${GFS} --volfile-server ${H0} --volfile-id ${V0} ${M0}
c5d8c8
+
c5d8c8
+# Create several files to make sure that at least some of them should be
c5d8c8
+# migrated by rebalance.
c5d8c8
+for i in {0..63}; do
c5d8c8
+    TEST dd if=/dev/urandom of=${M0}/file.${i} bs=4k count=1
c5d8c8
+    TEST dd if=/dev/urandom of=${M0}/file.${i} bs=4k count=1 seek=128
c5d8c8
+done
c5d8c8
+
c5d8c8
+TEST ${CLI} volume add-brick ${V0} ${H0}:${B0}/${V0}_{3..5}
c5d8c8
+TEST ${CLI} volume rebalance ${V0} start force
c5d8c8
+EXPECT_WITHIN ${REBALANCE_TIMEOUT} "completed" rebalance_status_field "${V0}"
c5d8c8
+
c5d8c8
+EXPECT "^0$" rebalance_failed_field "${V0}"
c5d8c8
+
c5d8c8
+cleanup
c5d8c8
diff --git a/tests/volume.rc b/tests/volume.rc
c5d8c8
index 9a002d9..f5dd0b1 100644
c5d8c8
--- a/tests/volume.rc
c5d8c8
+++ b/tests/volume.rc
c5d8c8
@@ -75,6 +75,10 @@ function rebalance_status_field {
c5d8c8
         $CLI volume rebalance $1 status | awk '{print $7}' | sed -n 3p
c5d8c8
 }
c5d8c8
 
c5d8c8
+function rebalance_failed_field {
c5d8c8
+        $CLI volume rebalance $1 status | awk '{print $5}' | sed -n 3p
c5d8c8
+}
c5d8c8
+
c5d8c8
 function fix-layout_status_field {
c5d8c8
         #The fix-layout status can be up to 3 words, (ex:'fix-layout in progress'), hence the awk-print $2 thru $4.
c5d8c8
         #But if the status is less than 3 words, it also prints the next field i.e the run_time_in_secs.(ex:'completed 3.00').
c5d8c8
diff --git a/xlators/cluster/dht/src/dht-rebalance.c b/xlators/cluster/dht/src/dht-rebalance.c
c5d8c8
index 072896d..eab7558 100644
c5d8c8
--- a/xlators/cluster/dht/src/dht-rebalance.c
c5d8c8
+++ b/xlators/cluster/dht/src/dht-rebalance.c
c5d8c8
@@ -1024,6 +1024,46 @@ out:
c5d8c8
     return ret;
c5d8c8
 }
c5d8c8
 
c5d8c8
+static int32_t
c5d8c8
+dht_rebalance_sparse_segment(xlator_t *subvol, fd_t *fd, off_t *offset,
c5d8c8
+                             size_t *size)
c5d8c8
+{
c5d8c8
+    off_t hole;
c5d8c8
+    int32_t ret;
c5d8c8
+
c5d8c8
+    do {
c5d8c8
+        ret = syncop_seek(subvol, fd, *offset, GF_SEEK_DATA, NULL, offset);
c5d8c8
+        if (ret >= 0) {
c5d8c8
+            /* Starting at the offset of the last data segment, find the
c5d8c8
+             * next hole. After a data segment there should always be a
c5d8c8
+             * hole, since EOF is considered a hole. */
c5d8c8
+            ret = syncop_seek(subvol, fd, *offset, GF_SEEK_HOLE, NULL, &hole);
c5d8c8
+        }
c5d8c8
+
c5d8c8
+        if (ret < 0) {
c5d8c8
+            if (ret == -ENXIO) {
c5d8c8
+                /* This can happen if there are no more data segments (i.e.
c5d8c8
+                 * the offset is at EOF), or there was a data segment but the
c5d8c8
+                 * file has been truncated to a smaller size between both
c5d8c8
+                 * seek requests. In both cases we are done. The file doesn't
c5d8c8
+                 * contain more data. */
c5d8c8
+                ret = 0;
c5d8c8
+            }
c5d8c8
+            return ret;
c5d8c8
+        }
c5d8c8
+
c5d8c8
+        /* It could happen that at the same offset we detected data in the
c5d8c8
+         * first seek, there could be a hole in the second seek if user is
c5d8c8
+         * modifying the file concurrently. In this case we need to find a
c5d8c8
+         * new data segment to migrate. */
c5d8c8
+    } while (hole <= *offset);
c5d8c8
+
c5d8c8
+    /* Calculate the total size of the current data block */
c5d8c8
+    *size = hole - *offset;
c5d8c8
+
c5d8c8
+    return 1;
c5d8c8
+}
c5d8c8
+
c5d8c8
 static int
c5d8c8
 __dht_rebalance_migrate_data(xlator_t *this, gf_defrag_info_t *defrag,
c5d8c8
                              xlator_t *from, xlator_t *to, fd_t *src, fd_t *dst,
c5d8c8
@@ -1032,8 +1072,6 @@ __dht_rebalance_migrate_data(xlator_t *this, gf_defrag_info_t *defrag,
c5d8c8
     int ret = 0;
c5d8c8
     int count = 0;
c5d8c8
     off_t offset = 0;
c5d8c8
-    off_t data_offset = 0;
c5d8c8
-    off_t hole_offset = 0;
c5d8c8
     struct iovec *vector = NULL;
c5d8c8
     struct iobref *iobref = NULL;
c5d8c8
     uint64_t total = 0;
c5d8c8
@@ -1048,71 +1086,36 @@ __dht_rebalance_migrate_data(xlator_t *this, gf_defrag_info_t *defrag,
c5d8c8
     while (total < ia_size) {
c5d8c8
         /* This is a regular file - read it sequentially */
c5d8c8
         if (!hole_exists) {
c5d8c8
-            read_size = (((ia_size - total) > DHT_REBALANCE_BLKSIZE)
c5d8c8
-                             ? DHT_REBALANCE_BLKSIZE
c5d8c8
-                             : (ia_size - total));
c5d8c8
+            data_block_size = ia_size - total;
c5d8c8
         } else {
c5d8c8
             /* This is a sparse file - read only the data segments in the file
c5d8c8
              */
c5d8c8
 
c5d8c8
             /* If the previous data block is fully copied, find the next data
c5d8c8
-             * segment
c5d8c8
-             * starting at the offset of the last read and written byte,  */
c5d8c8
+             * segment starting at the offset of the last read and written
c5d8c8
+             * byte. */
c5d8c8
             if (data_block_size <= 0) {
c5d8c8
-                ret = syncop_seek(from, src, offset, GF_SEEK_DATA, NULL,
c5d8c8
-                                  &data_offset);
c5d8c8
-                if (ret) {
c5d8c8
-                    if (ret == -ENXIO)
c5d8c8
-                        ret = 0; /* No more data segments */
c5d8c8
-                    else
c5d8c8
-                        *fop_errno = -ret; /* Error occurred */
c5d8c8
-
c5d8c8
+                ret = dht_rebalance_sparse_segment(from, src, &offset,
c5d8c8
+                                                   &data_block_size);
c5d8c8
+                if (ret <= 0) {
c5d8c8
+                    *fop_errno = -ret;
c5d8c8
                     break;
c5d8c8
                 }
c5d8c8
-
c5d8c8
-                /* If the position of the current data segment is greater than
c5d8c8
-                 * the position of the next hole, find the next hole in order to
c5d8c8
-                 * calculate the length of the new data segment */
c5d8c8
-                if (data_offset > hole_offset) {
c5d8c8
-                    /* Starting at the offset of the last data segment, find the
c5d8c8
-                     * next hole */
c5d8c8
-                    ret = syncop_seek(from, src, data_offset, GF_SEEK_HOLE,
c5d8c8
-                                      NULL, &hole_offset);
c5d8c8
-                    if (ret) {
c5d8c8
-                        /* If an error occurred here it's a real error because
c5d8c8
-                         * if the seek for a data segment was successful then
c5d8c8
-                         * necessarily another hole must exist (EOF is a hole)
c5d8c8
-                         */
c5d8c8
-                        *fop_errno = -ret;
c5d8c8
-                        break;
c5d8c8
-                    }
c5d8c8
-
c5d8c8
-                    /* Calculate the total size of the current data block */
c5d8c8
-                    data_block_size = hole_offset - data_offset;
c5d8c8
-                }
c5d8c8
-            } else {
c5d8c8
-                /* There is still data in the current segment, move the
c5d8c8
-                 * data_offset to the position of the last written byte */
c5d8c8
-                data_offset = offset;
c5d8c8
             }
c5d8c8
-
c5d8c8
-            /* Calculate how much data needs to be read and written. If the data
c5d8c8
-             * segment's length is bigger than DHT_REBALANCE_BLKSIZE, read and
c5d8c8
-             * write DHT_REBALANCE_BLKSIZE data length and the rest in the
c5d8c8
-             * next iteration(s) */
c5d8c8
-            read_size = ((data_block_size > DHT_REBALANCE_BLKSIZE)
c5d8c8
-                             ? DHT_REBALANCE_BLKSIZE
c5d8c8
-                             : data_block_size);
c5d8c8
-
c5d8c8
-            /* Calculate the remaining size of the data block - maybe there's no
c5d8c8
-             * need to seek for data in the next iteration */
c5d8c8
-            data_block_size -= read_size;
c5d8c8
-
c5d8c8
-            /* Set offset to the offset of the data segment so read and write
c5d8c8
-             * will have the correct position */
c5d8c8
-            offset = data_offset;
c5d8c8
         }
c5d8c8
 
c5d8c8
+        /* Calculate how much data needs to be read and written. If the data
c5d8c8
+         * segment's length is bigger than DHT_REBALANCE_BLKSIZE, read and
c5d8c8
+         * write DHT_REBALANCE_BLKSIZE data length and the rest in the
c5d8c8
+         * next iteration(s) */
c5d8c8
+        read_size = ((data_block_size > DHT_REBALANCE_BLKSIZE)
c5d8c8
+                         ? DHT_REBALANCE_BLKSIZE
c5d8c8
+                         : data_block_size);
c5d8c8
+
c5d8c8
+        /* Calculate the remaining size of the data block - maybe there's no
c5d8c8
+         * need to seek for data in the next iteration */
c5d8c8
+        data_block_size -= read_size;
c5d8c8
+
c5d8c8
         ret = syncop_readv(from, src, read_size, offset, 0, &vector, &count,
c5d8c8
                            &iobref, NULL, NULL, NULL);
c5d8c8
 
c5d8c8
@@ -1177,6 +1180,7 @@ __dht_rebalance_migrate_data(xlator_t *this, gf_defrag_info_t *defrag,
c5d8c8
         iobref = NULL;
c5d8c8
         vector = NULL;
c5d8c8
     }
c5d8c8
+
c5d8c8
     if (iobref)
c5d8c8
         iobref_unref(iobref);
c5d8c8
     GF_FREE(vector);
c5d8c8
-- 
c5d8c8
1.8.3.1
c5d8c8