-
Notifications
You must be signed in to change notification settings - Fork 211
Persistent file handles in VFS write path #5764
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
kounelisagis
wants to merge
5
commits into
main
Choose a base branch
from
agis/buffered-io
base: main
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.
+291
−55
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a049612
Posix support
kounelisagis c3c9d20
Init win support
kounelisagis 991e515
Merge remote-tracking branch 'origin/main' into agis/buffered-io
kounelisagis e46113f
Windows
kounelisagis 096960d
Evicts cached file handles on remove and move
kounelisagis 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 | ||
|---|---|---|---|---|
|
|
@@ -123,6 +123,14 @@ class PosixDIR { | |||
| optional<DIR*> dir_; | ||||
| }; | ||||
|
|
||||
| Posix::~Posix() { | ||||
| std::lock_guard<std::mutex> lock(open_files_mtx_); | ||||
| for (auto& [path, of] : open_files_) { | ||||
| ::close(of.fd); | ||||
| } | ||||
| open_files_.clear(); | ||||
|
Member
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.
Suggested change
No need to clear the map; it's going to be freed very soon either way. |
||||
| } | ||||
|
|
||||
| Posix::Posix(const Config& config) { | ||||
| // Initialize member variables with posix config parameters. | ||||
|
|
||||
|
|
@@ -199,8 +207,22 @@ bool Posix::is_file(const URI& uri) const { | |||
| return (stat(uri.to_path().c_str(), &st) == 0) && !S_ISDIR(st.st_mode); | ||||
| } | ||||
|
|
||||
| void Posix::evict_cached_fds(const std::string& path_prefix) const { | ||||
| std::lock_guard<std::mutex> lock(open_files_mtx_); | ||||
| for (auto it = open_files_.begin(); it != open_files_.end();) { | ||||
| if (it->first == path_prefix || | ||||
| it->first.compare(0, path_prefix.size() + 1, path_prefix + "/") == 0) { | ||||
| ::close(it->second.fd); | ||||
| it = open_files_.erase(it); | ||||
| } else { | ||||
| ++it; | ||||
| } | ||||
| } | ||||
| } | ||||
|
|
||||
| void Posix::remove_dir(const URI& uri) const { | ||||
| auto path = uri.to_path(); | ||||
| evict_cached_fds(path); | ||||
| int rc = nftw(path.c_str(), unlink_cb, 64, FTW_DEPTH | FTW_PHYS); | ||||
| if (rc) { | ||||
| throw IOError( | ||||
|
|
@@ -223,6 +245,7 @@ bool Posix::remove_dir_if_empty(const std::string& path) const { | |||
|
|
||||
| void Posix::remove_file(const URI& uri) const { | ||||
| auto path = uri.to_path(); | ||||
| evict_cached_fds(path); | ||||
| if (remove(path.c_str()) != 0) { | ||||
| throw IOError( | ||||
| std::string("Cannot delete file '") + path + "'; " + strerror(errno)); | ||||
|
|
@@ -245,6 +268,7 @@ uint64_t Posix::file_size(const URI& uri) const { | |||
| } | ||||
|
|
||||
| void Posix::move_file(const URI& old_path, const URI& new_path) const { | ||||
| evict_cached_fds(old_path.to_path()); | ||||
| auto new_uri_path = new_path.to_path(); | ||||
| throw_if_not_ok(ensure_directory(new_uri_path)); | ||||
| if (rename(old_path.to_path().c_str(), new_path.to_path().c_str()) != 0) { | ||||
|
|
@@ -296,7 +320,34 @@ uint64_t Posix::read( | |||
| } | ||||
|
|
||||
| void Posix::flush(const URI& uri, bool) { | ||||
| sync(uri); | ||||
| auto path = uri.to_path(); | ||||
| int fd = -1; | ||||
|
|
||||
| { | ||||
| std::lock_guard<std::mutex> lock(open_files_mtx_); | ||||
| auto it = open_files_.find(path); | ||||
| if (it != open_files_.end()) { | ||||
| fd = it->second.fd; | ||||
| open_files_.erase(it); | ||||
| } | ||||
| } | ||||
|
|
||||
| if (fd != -1) { | ||||
| // fsync and close the cached file descriptor. | ||||
| if (::fsync(fd) != 0) { | ||||
| auto err = errno; | ||||
| ::close(fd); | ||||
| throw IOError( | ||||
| std::string("Cannot sync file '") + path + "'; " + strerror(err)); | ||||
| } | ||||
| if (::close(fd) != 0) { | ||||
| throw IOError( | ||||
| std::string("Cannot close file '") + path + "'; " + strerror(errno)); | ||||
| } | ||||
| } else { | ||||
| // No cached fd (e.g. directory sync or file not written through us). | ||||
| sync(uri); | ||||
| } | ||||
| } | ||||
|
|
||||
| void Posix::sync(const URI& uri) const { | ||||
|
|
@@ -350,32 +401,42 @@ void Posix::write( | |||
| } | ||||
| } | ||||
|
|
||||
| // Get file offset (equal to file size) | ||||
| Status st; | ||||
| uint64_t file_offset = 0; | ||||
| if (is_file(URI(path))) { | ||||
| file_offset = file_size(URI(path)); | ||||
| } else { | ||||
| throw_if_not_ok(ensure_directory(path)); | ||||
| } | ||||
| int fd; | ||||
| uint64_t file_offset; | ||||
|
|
||||
| // Open or create file. | ||||
| int fd = open(path.c_str(), O_WRONLY | O_CREAT, file_permissions_); | ||||
| if (fd == -1) { | ||||
| throw IOError( | ||||
| std::string("Cannot open file '") + path + "'; " + strerror(errno)); | ||||
| { | ||||
| std::lock_guard<std::mutex> lock(open_files_mtx_); | ||||
| auto it = open_files_.find(path); | ||||
| if (it != open_files_.end()) { | ||||
| // Reuse the cached file descriptor. | ||||
| fd = it->second.fd; | ||||
| file_offset = it->second.offset; | ||||
| } else { | ||||
| // First write to this path: open fd and determine current size. | ||||
| if (is_file(URI(path))) { | ||||
| file_offset = file_size(URI(path)); | ||||
| } else { | ||||
| throw_if_not_ok(ensure_directory(path)); | ||||
| file_offset = 0; | ||||
| } | ||||
| fd = ::open(path.c_str(), O_WRONLY | O_CREAT, file_permissions_); | ||||
| if (fd == -1) { | ||||
| throw IOError( | ||||
| std::string("Cannot open file '") + path + "'; " + strerror(errno)); | ||||
| } | ||||
| open_files_.emplace(path, OpenFile{fd, file_offset}); | ||||
| } | ||||
| } | ||||
|
|
||||
| st = write_at(fd, file_offset, buffer, buffer_size); | ||||
| auto st = write_at(fd, file_offset, buffer, buffer_size); | ||||
| if (!st.ok()) { | ||||
| close(fd); | ||||
| std::stringstream errmsg; | ||||
| errmsg << "Cannot write to file '" << path << "'; " << st.message(); | ||||
| throw IOError(errmsg.str()); | ||||
| } | ||||
| if (close(fd) != 0) { | ||||
| throw IOError( | ||||
| std::string("Cannot close file '") + path + "'; " + strerror(errno)); | ||||
| std::string("Cannot write to file '") + path + "'; " + st.message()); | ||||
| } | ||||
|
|
||||
| { | ||||
| std::lock_guard<std::mutex> lock(open_files_mtx_); | ||||
| open_files_[path].offset = file_offset + buffer_size; | ||||
| } | ||||
| } | ||||
|
|
||||
|
|
||||
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 |
|---|---|---|
|
|
@@ -41,7 +41,9 @@ | |
| #include <unistd.h> | ||
|
|
||
| #include <functional> | ||
| #include <mutex> | ||
| #include <string> | ||
| #include <unordered_map> | ||
| #include <vector> | ||
|
|
||
| #include "tiledb/common/status.h" | ||
|
|
@@ -79,8 +81,8 @@ class Posix : public LocalFilesystem { | |
| /** Constructor. */ | ||
| explicit Posix(const Config& config); | ||
|
|
||
| /** Destructor. */ | ||
| ~Posix() override = default; | ||
| /** Destructor. Closes any cached file descriptors. */ | ||
| ~Posix() override; | ||
|
|
||
| /* ********************************* */ | ||
| /* API */ | ||
|
|
@@ -299,6 +301,25 @@ class Posix : public LocalFilesystem { | |
|
|
||
| private: | ||
| uint32_t file_permissions_, directory_permissions_; | ||
|
|
||
| /** | ||
| * Closes and removes cached fds whose path equals or is under the given | ||
| * prefix. Called from const removal/move methods to keep the cache | ||
| * consistent. | ||
| */ | ||
| void evict_cached_fds(const std::string& path_prefix) const; | ||
|
|
||
| /** State for a file descriptor kept open between write() and flush(). */ | ||
| struct OpenFile { | ||
| int fd; | ||
| uint64_t offset; | ||
| }; | ||
|
Comment on lines
+312
to
+316
Member
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. The struct should have a destructor (and be un-copiable and immovable). |
||
|
|
||
| /** Maps path -> cached file descriptor and current write offset. */ | ||
| mutable std::unordered_map<std::string, OpenFile> open_files_; | ||
|
|
||
| /** Protects open_files_. */ | ||
| mutable std::mutex open_files_mtx_; | ||
| }; | ||
|
|
||
| } // namespace sm | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
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.
The newly added logic should be moved to
local.ccas much as currently possible, to reduce duplications across POSIX and Windows.