summaryrefslogtreecommitdiffstats
path: root/test/unit
diff options
context:
space:
mode:
authorPrashanth Pai <ppai@redhat.com>2016-03-11 17:56:46 +0530
committerThiago da Silva <thiago@redhat.com>2016-05-03 12:47:05 -0700
commitfddb5d4a918affe7837d523b56e53e33f3ae5408 (patch)
tree919445423bd690ff74a64bf7e96dce0e8ad06eb9 /test/unit
parent933bc5ade145413b0c7307a12b9d0b4084e7d767 (diff)
Open object only if it's going to be read
Open()ing an object is necessarry only in two cases: * Serving a GET request * Recalculating etag when metadata is stale (can be triggered by any type of request) This change ensures that for requests other than GET, a file is not opened if the metadata is valid (size and etag accurate). Note that if metadata is stale, the file is still opened and read to compute etag. This patch does not change the behaviour of triggering metadata validation and regeneration for non-GET requests. Change-Id: Icefa4dec7d715ec9e6dd68ae7fe89a0d90fe71b3 Signed-off-by: Prashanth Pai <ppai@redhat.com> Reviewed-on: http://review.gluster.org/13684 Reviewed-by: Thiago da Silva <thiago@redhat.com> Tested-by: Thiago da Silva <thiago@redhat.com>
Diffstat (limited to 'test/unit')
-rw-r--r--test/unit/obj/test_diskfile.py48
1 files changed, 48 insertions, 0 deletions
diff --git a/test/unit/obj/test_diskfile.py b/test/unit/obj/test_diskfile.py
index 9ef186b..b190526 100644
--- a/test/unit/obj/test_diskfile.py
+++ b/test/unit/obj/test_diskfile.py
@@ -205,6 +205,54 @@ class TestDiskFile(unittest.TestCase):
self.assertRaises(DiskFileNotOpen, gdf.reader)
self.assertRaises(DiskFileNotOpen, gdf.__enter__)
+ def test_read_metadata_optimize_open_close(self):
+ the_path = os.path.join(self.td, "vol0", "bar")
+ the_file = os.path.join(the_path, "z")
+ os.makedirs(the_path)
+ with open(the_file, "wb") as fd:
+ fd.write("1234")
+ init_md = {
+ 'X-Type': 'Object',
+ 'X-Object-Type': 'file',
+ 'Content-Length': 4,
+ 'ETag': md5("1234").hexdigest(),
+ 'X-Timestamp': normalize_timestamp(os.stat(the_file).st_ctime),
+ 'Content-Type': 'application/octet-stream'}
+ _metadata[_mapit(the_file)] = init_md
+ gdf = self._get_diskfile("vol0", "p57", "ufo47", "bar", "z")
+ assert gdf._obj == "z"
+ assert gdf._fd is None
+ assert gdf._disk_file_open is False
+ assert gdf._metadata is None
+ assert not gdf._is_dir
+
+ # Case 1
+ # Ensure that reading metadata for non-GET requests
+ # does not incur opening and closing the file when
+ # metadata is NOT stale.
+ mock_open = Mock()
+ mock_close = Mock()
+ with mock.patch("gluster.swift.obj.diskfile.do_open", mock_open):
+ with mock.patch("gluster.swift.obj.diskfile.do_close", mock_close):
+ md = gdf.read_metadata()
+ self.assertEqual(md, init_md)
+ self.assertFalse(mock_open.called)
+ self.assertFalse(mock_close.called)
+
+ # Case 2
+ # Ensure that reading metadata for non-GET requests
+ # still opens and reads the file when metadata is stale
+ with open(the_file, "a") as fd:
+ # Append to the existing file to make the stored metadata
+ # invalid/stale.
+ fd.write("5678")
+ md = gdf.read_metadata()
+ # Check that the stale metadata is recalculated to account for
+ # change in file content
+ self.assertNotEqual(md, init_md)
+ self.assertEqual(md['Content-Length'], 8)
+ self.assertEqual(md['ETag'], md5("12345678").hexdigest())
+
def test_open_and_close(self):
mock_close = Mock()