From b9b2813f31b4e86aa6cb54f9341d658402559bec Mon Sep 17 00:00:00 2001 From: Yuan Zhou Date: Fri, 18 Jan 2019 14:26:25 +0800 Subject: [PATCH] common/buffer: fix pread_file to generate local bufferlist Signed-off-by: Yuan Zhou --- src/common/buffer.cc | 8 +- .../immutable_object_cache/CMakeLists.txt | 2 +- .../test_ObjectCacheFile.cc | 120 ++++++++++++++++++ .../immutable_object_cache/test_sync_file.cc | 86 ------------- .../immutable_object_cache/ObjectCacheFile.cc | 2 +- .../immutable_object_cache/ObjectCacheFile.h | 2 +- 6 files changed, 129 insertions(+), 91 deletions(-) create mode 100644 src/test/immutable_object_cache/test_ObjectCacheFile.cc delete mode 100644 src/test/immutable_object_cache/test_sync_file.cc diff --git a/src/common/buffer.cc b/src/common/buffer.cc index 8633ca407b5ed..5f3f948ff9848 100644 --- a/src/common/buffer.cc +++ b/src/common/buffer.cc @@ -1814,8 +1814,12 @@ int buffer::list::pread_file(const char *fn, uint64_t off, uint64_t len, std::st if (len > st.st_size - off) { len = st.st_size - off; } - lseek(fd, off, SEEK_SET); - ssize_t ret = safe_read(fd, (void*)this->c_str(), len); + ssize_t ret = lseek64(fd, off, SEEK_SET); + if (ret != off) { + return -errno; + } + + ret = read_fd(fd, len); if (ret < 0) { std::ostringstream oss; oss << "bufferlist::read_file(" << fn << "): read error:" diff --git a/src/test/immutable_object_cache/CMakeLists.txt b/src/test/immutable_object_cache/CMakeLists.txt index aade49afba88b..7733ca2ddd6a6 100644 --- a/src/test/immutable_object_cache/CMakeLists.txt +++ b/src/test/immutable_object_cache/CMakeLists.txt @@ -2,7 +2,7 @@ add_executable(unittest_ceph_immutable_obj_cache test_main.cc test_SimplePolicy.cc - test_sync_file.cc + test_ObjectCacheFile.cc test_DomainSocket.cc test_multi_session.cc test_object_store.cc diff --git a/src/test/immutable_object_cache/test_ObjectCacheFile.cc b/src/test/immutable_object_cache/test_ObjectCacheFile.cc new file mode 100644 index 0000000000000..29d90b2a026be --- /dev/null +++ b/src/test/immutable_object_cache/test_ObjectCacheFile.cc @@ -0,0 +1,120 @@ +// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- +// vim: ts=8 sw=2 smarttab + +#include "gtest/gtest.h" +#include "include/Context.h" +#include "include/buffer_fwd.h" +#include "common/Mutex.h" +#include "common/Cond.h" +#include "global/global_init.h" +#include "common/ceph_argparse.h" +#include "global/global_context.h" +#include + +#include "tools/immutable_object_cache/ObjectCacheFile.h" + +using namespace ceph::immutable_obj_cache; +namespace efs = std::experimental::filesystem; + +class TestObjectCacheFile :public ::testing::Test { +public: + std::string m_cache_root_dir; + + TestObjectCacheFile(){} + ~TestObjectCacheFile(){} + static void SetUpTestCase() {} + static void TearDownTestCase() {} + + void SetUp() override { + m_cache_root_dir = g_ceph_context->_conf.get_val("immutable_object_cache_path") + + "/ceph_immutable_obj_cache/"; + + if (efs::exists(m_cache_root_dir)) { + efs::remove_all(m_cache_root_dir); + } + efs::create_directories(m_cache_root_dir); + } + + void TearDown() override { + efs::remove_all(m_cache_root_dir); + } + +}; + +TEST_F(TestObjectCacheFile, test_write_object_to_file) { + ObjectCacheFile* m_cache_file_1 = new ObjectCacheFile(g_ceph_context, "test_sync_file_1"); + ObjectCacheFile* m_cache_file_2 = new ObjectCacheFile(g_ceph_context, "test_sync_file_2"); + ObjectCacheFile* m_cache_file_3 = new ObjectCacheFile(g_ceph_context, "test_sync_file_3"); + ASSERT_TRUE(m_cache_file_1->get_file_size() == -1); + ASSERT_TRUE(m_cache_file_2->get_file_size() == -1); + ASSERT_TRUE(m_cache_file_3->get_file_size() == -1); + + bufferlist* buf_1 = new ceph::bufferlist(); + bufferlist* buf_2 = new ceph::bufferlist(); + bufferlist* buf_3 = new ceph::bufferlist(); + buf_1->append(std::string(1024, '0')); + buf_2->append(std::string(4096, '0')); + buf_3->append(std::string(0, '0')); + + ASSERT_TRUE(m_cache_file_1->write_object_to_file(*buf_1, 1024) == 1024); + ASSERT_TRUE(m_cache_file_2->write_object_to_file(*buf_2, 4096) == 4096); + ASSERT_TRUE(m_cache_file_3->write_object_to_file(*buf_3, 0) == 0); + ASSERT_TRUE(m_cache_file_1->get_file_size() == 1024); + ASSERT_TRUE(m_cache_file_2->get_file_size() == 4096); + ASSERT_TRUE(m_cache_file_3->get_file_size() == 0); + + delete m_cache_file_1; + delete m_cache_file_2; + delete m_cache_file_3; + delete buf_1; + delete buf_2; + delete buf_3; +} + +TEST_F(TestObjectCacheFile, test_read_object_from_file) { + ObjectCacheFile* m_cache_file_1 = new ObjectCacheFile(g_ceph_context, "test_sync_file_1"); + ObjectCacheFile* m_cache_file_2 = new ObjectCacheFile(g_ceph_context, "test_sync_file_2"); + bufferlist* buf_1 = new ceph::bufferlist(); + bufferlist* buf_2 = new ceph::bufferlist(); + + ASSERT_TRUE(m_cache_file_1->get_file_size() == -1); + ASSERT_TRUE(m_cache_file_2->get_file_size() == -1); + ASSERT_EQ(m_cache_file_1->read_object_from_file(buf_1, 0, 1024), -1); + ASSERT_EQ(m_cache_file_2->read_object_from_file(buf_2, 0, 1024), -1); + + buf_1->append(std::string("helloworld")); + ASSERT_TRUE(m_cache_file_1->write_object_to_file(*buf_1, 10) == 10); + ASSERT_TRUE(m_cache_file_1->get_file_size() == 10); + + bufferlist* buf_3 = new ceph::bufferlist(); + bufferlist* buf_4 = new ceph::bufferlist(); + bufferlist* buf_5 = new ceph::bufferlist(); + bufferlist* buf_6 = new ceph::bufferlist(); + + ASSERT_EQ(m_cache_file_1->read_object_from_file(buf_3, 0, 10), 10); + ASSERT_EQ(10, buf_3->length()); + ASSERT_EQ(0, (strncmp(buf_1->c_str(), buf_3->c_str(), 10))); + + ASSERT_EQ(m_cache_file_1->read_object_from_file(buf_4, 0, 4096), 10); + ASSERT_EQ(10, buf_4->length()); + ASSERT_EQ(0, (strncmp(buf_1->c_str(), buf_4->c_str(), 10))); + + ASSERT_EQ(m_cache_file_1->read_object_from_file(buf_5, 2, 4), 4); + ASSERT_EQ(4, buf_5->length()); + bufferlist sub_bl; + sub_bl.substr_of(*buf_1, 2, 4); + ASSERT_EQ(0, (strncmp(sub_bl.c_str(), buf_5->c_str(), 4))); + + ASSERT_EQ(m_cache_file_1->read_object_from_file(buf_6, 12, 4), -1); + ASSERT_EQ(0, buf_6->length()); + + + delete m_cache_file_1; + delete m_cache_file_2; + delete buf_1; + delete buf_2; + delete buf_3; + delete buf_4; + delete buf_5; + delete buf_6; +} diff --git a/src/test/immutable_object_cache/test_sync_file.cc b/src/test/immutable_object_cache/test_sync_file.cc deleted file mode 100644 index 1358ac60a448d..0000000000000 --- a/src/test/immutable_object_cache/test_sync_file.cc +++ /dev/null @@ -1,86 +0,0 @@ -// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- -// vim: ts=8 sw=2 smarttab - -#include "gtest/gtest.h" -#include "include/Context.h" -#include "include/buffer_fwd.h" -#include "common/Mutex.h" -#include "common/Cond.h" -#include "global/global_init.h" -#include "common/ceph_argparse.h" -#include "global/global_context.h" -#include - -#include "tools/immutable_object_cache/ObjectCacheFile.h" - -using namespace ceph::immutable_obj_cache; -namespace efs = std::experimental::filesystem; - -class TestObjectCacheFile :public ::testing::Test { -public: - std::string m_cache_root_dir; - - TestObjectCacheFile(){} - ~TestObjectCacheFile(){} - static void SetUpTestCase() {} - static void TearDownTestCase() {} - - void SetUp() override { - m_cache_root_dir = g_ceph_context->_conf.get_val("immutable_object_cache_path") - + "/ceph_immutable_obj_cache/"; - - if (efs::exists(m_cache_root_dir)) { - efs::remove_all(m_cache_root_dir); - } - efs::create_directories(m_cache_root_dir); - } - - void TearDown() override { - efs::remove_all(m_cache_root_dir); - } - -}; - -TEST_F(TestObjectCacheFile, test_write_object_to_file) { - ObjectCacheFile* m_sync_file_1 = new ObjectCacheFile(g_ceph_context, "test_sync_file_1"); - ObjectCacheFile* m_sync_file_2 = new ObjectCacheFile(g_ceph_context, "test_sync_file_2"); - ASSERT_TRUE(m_sync_file_1->get_file_size() == -1); - ASSERT_TRUE(m_sync_file_2->get_file_size() == -1); - bufferlist* buf_1 = new ceph::bufferlist(); - bufferlist* buf_2 = new ceph::bufferlist(); - buf_1->append(std::string(1024, '0')); - buf_2->append(std::string(4096, '0')); - ASSERT_TRUE(m_sync_file_1->write_object_to_file(*buf_1, 1024) == 1024); - ASSERT_TRUE(m_sync_file_2->write_object_to_file(*buf_2, 4096) == 4096); - ASSERT_TRUE(m_sync_file_1->get_file_size() == 1024); - ASSERT_TRUE(m_sync_file_2->get_file_size() == 4096); - delete m_sync_file_1; - delete m_sync_file_2; - delete buf_1; - delete buf_2; -} - -TEST_F(TestObjectCacheFile, test_read_object_from_file) { - ObjectCacheFile* m_sync_file_1 = new ObjectCacheFile(g_ceph_context, "test_sync_file_1"); - ObjectCacheFile* m_sync_file_2 = new ObjectCacheFile(g_ceph_context, "test_sync_file_2"); - bufferlist* buf_1 = new ceph::bufferlist(); - bufferlist* buf_2 = new ceph::bufferlist(); - ASSERT_EQ(m_sync_file_1->read_object_from_file(buf_1, 0, 1024), -1); - ASSERT_EQ(m_sync_file_2->read_object_from_file(buf_2, 0, 1024), -1); - ASSERT_TRUE(m_sync_file_1->get_file_size() == -1); - ASSERT_TRUE(m_sync_file_2->get_file_size() == -1); - ASSERT_EQ(m_sync_file_1->read_object_from_file(buf_1, 0, 1024), -1); - ASSERT_EQ(m_sync_file_2->read_object_from_file(buf_2, 0, 1024), -1); - buf_1->append(std::string(1024, '0')); - buf_2->append(std::string(4096, '2')); - ASSERT_TRUE(m_sync_file_1->write_object_to_file(*buf_1, 1024) == 1024); - ASSERT_TRUE(m_sync_file_2->write_object_to_file(*buf_2, 4096) == 4096); - ASSERT_TRUE(m_sync_file_1->get_file_size() == 1024); - ASSERT_TRUE(m_sync_file_2->get_file_size() == 4096); - ASSERT_EQ(m_sync_file_1->read_object_from_file(buf_1, 0, 1024), 1024); - ASSERT_EQ(m_sync_file_2->read_object_from_file(buf_2, 0, 4096), 4096); - delete m_sync_file_1; - delete m_sync_file_2; - delete buf_1; - delete buf_2; -} diff --git a/src/tools/immutable_object_cache/ObjectCacheFile.cc b/src/tools/immutable_object_cache/ObjectCacheFile.cc index a85d7f5f13c79..1d7f0be9f9ada 100644 --- a/src/tools/immutable_object_cache/ObjectCacheFile.cc +++ b/src/tools/immutable_object_cache/ObjectCacheFile.cc @@ -67,7 +67,7 @@ int ObjectCacheFile::read_object_from_file(ceph::bufferlist* read_buf, uint64_t return read_buf->length(); } -uint64_t ObjectCacheFile::get_file_size() { +int ObjectCacheFile::get_file_size() { struct stat buf; int temp_fd = ::open(m_name.c_str(), O_RDONLY); if(temp_fd < 0) { diff --git a/src/tools/immutable_object_cache/ObjectCacheFile.h b/src/tools/immutable_object_cache/ObjectCacheFile.h index 3bbdaa2d8287a..6357a1f592753 100644 --- a/src/tools/immutable_object_cache/ObjectCacheFile.h +++ b/src/tools/immutable_object_cache/ObjectCacheFile.h @@ -29,7 +29,7 @@ public: int write_object_to_file(ceph::bufferlist read_buf, uint64_t object_len); int read_object_from_file(ceph::bufferlist* read_buf, uint64_t object_off, uint64_t object_len); - uint64_t get_file_size(); + int get_file_size(); private: CephContext *cct; -- 2.39.5