-
Notifications
You must be signed in to change notification settings - Fork 1
Implemented recover_file. #209
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jessicayu626
wants to merge
1
commit into
develop
Choose a base branch
from
feature/file-recovery
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,6 +25,7 @@ | |
| #include <google/protobuf/message.h> | ||
| #include <erasurecoding.pb.h> | ||
| #include <zk_dn_client.h> | ||
| #include <zk_nn_client.h> | ||
|
|
||
|
|
||
| using hadoop::hdfs::AddBlockRequestProto; | ||
|
|
@@ -380,6 +381,77 @@ void ZkNnClient::renew_lease(RenewLeaseRequestProto &req, | |
| } | ||
| } | ||
|
|
||
| void ZkNnClient::recover_file(std::string &file_path) { | ||
| int error_code; | ||
| FileZNode znode_data; | ||
| read_file_znode(znode_data, file_path); | ||
| std::uint64_t file_length = znode_data.length; | ||
|
|
||
| std::vector<std::string> children; | ||
| if (!zk->get_children(ZookeeperBlocksPath(file_path), children, error_code)) { | ||
| LOG(FATAL) << "[recover_file] Failed to get children for " << file_path; | ||
| return; | ||
| } | ||
|
|
||
| std::uint64_t size_left = file_length; | ||
| std::sort(children.begin(), children.end()); | ||
| for (auto &child : children) { | ||
| std::vector<std::uint8_t> block_vec; | ||
| std::uint64_t block_id; | ||
| auto child_path = util::concat_path(ZookeeperBlocksPath(file_path), child); | ||
| if (!zk->get(child_path, block_vec, error_code, sizeof(block_id))) { | ||
| LOG(ERROR) << "[recover_file] failed block retrieval"; | ||
| } | ||
| block_id = *reinterpret_cast<std::uint64_t *>(block_vec.data()); | ||
| std::string block_metadata_path = get_block_metadata_path(block_id); | ||
| BlockZNode block_data; | ||
| std::vector<std::uint8_t> block_data_vec(sizeof(block_data)); | ||
| if (!zk->get(block_metadata_path, block_data_vec, error_code, | ||
| sizeof(block_data))) { | ||
| LOG(ERROR) << "[recover_file] Failed getting block size for " | ||
| << block_metadata_path << " with error " << error_code; | ||
| } | ||
| memcpy(&block_data, &block_data_vec[0], sizeof(block_data)); | ||
| uint64_t block_size = block_data.block_size; | ||
|
|
||
| if (size_left < block_size) { | ||
| // Updates the block_size | ||
| block_data.block_size = size_left; | ||
| memcpy(&block_data_vec[0], &block_data, sizeof(block_data)); | ||
| if (!zk->set(block_metadata_path, block_data_vec, error_code, | ||
| sizeof(block_data))) { | ||
| LOG(ERROR) << "[recover_file] Failed setting block size for " | ||
| << block_metadata_path << " with error " << error_code; | ||
| } | ||
| size_left = 0; | ||
| } else if (size_left <= 0) { | ||
| // Removes the block | ||
| std::vector<std::string> datanodes; | ||
|
|
||
| if (!zk->get_children(get_block_metadata_path(block_id), | ||
| datanodes, error_code)) { | ||
| LOG(ERROR) << "[recover_file] Failed getting datanode " | ||
| "locations for block: " | ||
| << block_id | ||
| << " with error: " | ||
| << error_code; | ||
| return; | ||
| } | ||
| // push delete to datanodes' delete queues | ||
| for (auto &dn : datanodes) { | ||
| auto delete_queue = util::concat_path(DELETE_QUEUES, dn); | ||
| auto delete_item = util::concat_path(delete_queue, "block-"); | ||
| LOG(INFO) << "[recover_file] pushed to delete queue " << delete_item; | ||
| if (!zk->create(delete_item, block_vec, error_code, false)) { | ||
| LOG(INFO) << "[recover_file] pushed create " << delete_item; | ||
| } | ||
| blockDeleted(block_id, dn); | ||
|
Contributor
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. This looks good to me, but we should double check that this is a valid delete process with some additional unit tests. |
||
| } | ||
| } | ||
| size_left -= block_size; | ||
| } | ||
| } | ||
|
|
||
| void ZkNnClient::recover_lease(RecoverLeaseRequestProto &req, | ||
| RecoverLeaseResponseProto &res) { | ||
| std::string client_name = req.clientname(); | ||
|
|
@@ -421,9 +493,7 @@ void ZkNnClient::recover_lease(RecoverLeaseRequestProto &req, | |
| } | ||
| std::string lease_holder_client = children[0]; | ||
| if (lease_expired(lease_holder_client)) { | ||
| // TODO(elfyingying): start the lease recovery process | ||
| // that closes the file for the | ||
| // client and make sure the replicas are consistent. | ||
| recover_file(file_path); | ||
| if (!zk->delete_node(LeaseZookeeperPath(file_path) + '/' + | ||
| lease_holder_client, error_code, true)) { | ||
| LOG(ERROR) << "[recover_lease] Failed to delete the dead lease holder " | ||
|
|
@@ -928,7 +998,6 @@ ZkNnClient::DeleteResponse ZkNnClient::destroy_helper(const std::string &path, | |
| } | ||
| for (auto &child : children) { | ||
| auto child_path = util::concat_path(ZookeeperBlocksPath(path), child); | ||
| child_path = child_path; | ||
| ops.push_back(zk->build_delete_op(child_path)); | ||
| std::vector<std::uint8_t> block_vec; | ||
| std::uint64_t block; | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 don't think this case would ever be executed under our current implementation of file appends, but it's probably good to keep if future implementations would use "reserve blocks" for file mutability.