-
Notifications
You must be signed in to change notification settings - Fork 59
feat(bulk-load): bulk load download part2 - replica download files #471
Changes from all commits
99fe88e
553b1f1
8c64e05
f2e496a
47d4722
c61a5cf
345ec06
78fcca1
4dfd253
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,96 @@ | ||||||||||||||||
// Copyright (c) 2017-present, Xiaomi, Inc. All rights reserved. | ||||||||||||||||
// This source code is licensed under the Apache License Version 2.0, which | ||||||||||||||||
// can be found in the LICENSE file in the root directory of this source tree. | ||||||||||||||||
|
||||||||||||||||
#include "replica_test_base.h" | ||||||||||||||||
|
||||||||||||||||
#include <fstream> | ||||||||||||||||
|
||||||||||||||||
#include <gtest/gtest.h> | ||||||||||||||||
|
||||||||||||||||
namespace dsn { | ||||||||||||||||
namespace replication { | ||||||||||||||||
|
||||||||||||||||
class replica_file_provider_test : public replica_test_base | ||||||||||||||||
{ | ||||||||||||||||
public: | ||||||||||||||||
replica_file_provider_test() | ||||||||||||||||
{ | ||||||||||||||||
_replica = create_mock_replica(stub.get()); | ||||||||||||||||
_fs = stub->get_block_filesystem(); | ||||||||||||||||
utils::filesystem::create_directory(LOCAL_DIR); | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
~replica_file_provider_test() { utils::filesystem::remove_path(LOCAL_DIR); } | ||||||||||||||||
|
||||||||||||||||
public: | ||||||||||||||||
error_code test_do_download() | ||||||||||||||||
{ | ||||||||||||||||
uint64_t download_size = 0; | ||||||||||||||||
return _replica->do_download(PROVIDER, LOCAL_DIR, FILE_NAME, _fs.get(), download_size); | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
void create_local_file(const std::string &file_name) | ||||||||||||||||
{ | ||||||||||||||||
std::string whole_name = utils::filesystem::path_combine(LOCAL_DIR, file_name); | ||||||||||||||||
utils::filesystem::create_file(whole_name); | ||||||||||||||||
std::ofstream test_file; | ||||||||||||||||
test_file.open(whole_name); | ||||||||||||||||
test_file << "write some data.\n"; | ||||||||||||||||
test_file.close(); | ||||||||||||||||
|
||||||||||||||||
_file_meta.name = whole_name; | ||||||||||||||||
utils::filesystem::md5sum(whole_name, _file_meta.md5); | ||||||||||||||||
utils::filesystem::file_size(whole_name, _file_meta.size); | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
void create_remote_file(const std::string &file_name, int64_t size, const std::string &md5) | ||||||||||||||||
{ | ||||||||||||||||
std::string whole_file_name = utils::filesystem::path_combine(PROVIDER, file_name); | ||||||||||||||||
_fs->files[whole_file_name] = std::make_pair(size, md5); | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
public: | ||||||||||||||||
std::unique_ptr<mock_replica> _replica; | ||||||||||||||||
std::unique_ptr<block_service_mock> _fs; | ||||||||||||||||
|
||||||||||||||||
file_meta _file_meta; | ||||||||||||||||
std::string PROVIDER = "local_service"; | ||||||||||||||||
std::string LOCAL_DIR = "test_dir"; | ||||||||||||||||
std::string FILE_NAME = "test_file"; | ||||||||||||||||
}; | ||||||||||||||||
|
||||||||||||||||
// do_download unit tests | ||||||||||||||||
TEST_F(replica_file_provider_test, do_download_remote_file_not_exist) | ||||||||||||||||
{ | ||||||||||||||||
ASSERT_EQ(test_do_download(), ERR_CORRUPTION); | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
TEST_F(replica_file_provider_test, do_download_redownload_file) | ||||||||||||||||
{ | ||||||||||||||||
// local file exists, but md5 not matched with remote file | ||||||||||||||||
// expected to remove old local file and redownload it | ||||||||||||||||
create_local_file(FILE_NAME); | ||||||||||||||||
create_remote_file(FILE_NAME, 2333, "md5_not_match"); | ||||||||||||||||
ASSERT_EQ(test_do_download(), ERR_OK); | ||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. After redownloaded, better to check local file is exist and md5sum matched. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I used rdsn/src/dist/replication/test/replica_test/unit_test/block_service_mock.h Lines 93 to 99 in f5ab808
It will not do actual downloading action, the local file is not existed in unit test. And the real downloading action is hard to mock. |
||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
TEST_F(replica_file_provider_test, do_download_file_exist) | ||||||||||||||||
{ | ||||||||||||||||
create_local_file(FILE_NAME); | ||||||||||||||||
create_remote_file(FILE_NAME, _file_meta.size, _file_meta.md5); | ||||||||||||||||
ASSERT_EQ(test_do_download(), ERR_OK); | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
TEST_F(replica_file_provider_test, do_download_succeed) | ||||||||||||||||
{ | ||||||||||||||||
create_local_file(FILE_NAME); | ||||||||||||||||
create_remote_file(FILE_NAME, _file_meta.size, _file_meta.md5); | ||||||||||||||||
// remove local file to mock condition that file not existed | ||||||||||||||||
std::string file_name = utils::filesystem::path_combine(LOCAL_DIR, FILE_NAME); | ||||||||||||||||
utils::filesystem::remove_path(file_name); | ||||||||||||||||
ASSERT_EQ(test_do_download(), ERR_OK); | ||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. After downloaded, check local file is exist and md5sum matched. |
||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
} // namespace replication | ||||||||||||||||
} // namespace dsn |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I found nothing in your codes relates to the replica except for
ddebug_replica
. It should be moved todist::block_service
module, rather than be in replica.Under this impl you cannt print logs in
do_download
, however, you can return an error with string (error_s) in failure.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think current implementation is during a middle stage, it is okay to leave
do_download
function in replica namespace.If I move download function into block_service module, I should also move unit tests into block_service module. However, cold backup has wrote a
block_file_mock
in replica unit test for backup unit tests. If I move it into block_service, I should update backup unit tests code, which will be refactored in further pull request finally, it is not meaningful to refactor them currently.In my view, we should move all functions related to file provider into block_service module, such as download files, upload files. It can be implemented after refactoring backup and restore code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As we discuss offline, I will refactor code in next pull request.