Skip to content
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

close unused file-handles to avoid exceeding rlimit #133

Merged
merged 1 commit into from
Dec 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 14 additions & 14 deletions src/disk.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,22 +97,29 @@ struct FileDisk {
explicit FileDisk(const fs::path &filename)
{
filename_ = filename;
Open(false);
}

void Open(bool read_only = true)
{
// if the file is already open, don't do anything
if (f_) return;

// Opens the file for reading and writing
#ifdef _WIN32
f_ = ::_wfopen(filename.c_str(), L"w+b");
f_ = ::_wfopen(filename_.c_str(), read_only ? L"r+b" : L"w+b");
#else
f_ = ::fopen(filename.c_str(), "w+b");
f_ = ::fopen(filename_.c_str(), read_only ? "r+b" : "w+b");
#endif
if (f_ == nullptr) {
throw InvalidValueException(
"Could not open " + filename.string() + ": " + ::strerror(errno));
"Could not open " + filename_.string() + ": " + ::strerror(errno));
}
}

FileDisk(FileDisk &&fd)
{
filename_ = fd.filename_;
filename_ = std::move(fd.filename_);
f_ = fd.f_;
fd.f_ = nullptr;
}
Expand All @@ -131,6 +138,7 @@ struct FileDisk {

void Read(uint64_t begin, uint8_t *memcache, uint64_t length)
{
Open();
#if ENABLE_LOGGING
disk_log(filename_, op_t::read, begin, length);
#endif
Expand Down Expand Up @@ -160,6 +168,7 @@ struct FileDisk {

void Write(uint64_t begin, const uint8_t *memcache, uint64_t length)
{
Open();
#if ENABLE_LOGGING
disk_log(filename_, op_t::write, begin, length);
#endif
Expand Down Expand Up @@ -198,15 +207,6 @@ struct FileDisk {
{
Close();
fs::resize_file(filename_, new_size);
#ifdef _WIN32
f_ = ::_wfopen(filename_.c_str(), L"r+b");
#else
f_ = ::fopen(filename_.c_str(), "r+b");
#endif
if (f_ == nullptr) {
throw InvalidValueException(
"Could not open " + filename_.string() + ": " + ::strerror(errno));
}
}

private:
Expand All @@ -217,7 +217,7 @@ struct FileDisk {
bool bReading = true;

fs::path filename_;
FILE *f_;
FILE *f_ = nullptr;
};

struct BufferedDisk : Disk
Expand Down
4 changes: 3 additions & 1 deletion src/sort_manager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ class SortManager : public Disk {
{
for (auto& b : buckets_) {
b.file.FreeMemory();
// the underlying file will be re-opened again on-demand
b.underlying_file.Close();
}
prev_bucket_buf_.reset();
memory_start_.reset();
Expand Down Expand Up @@ -229,7 +231,7 @@ class SortManager : public Disk {
BufferedDisk file;
};

// Start of the whole memory array. This will be diveded into buckets
// The buffer we use to sort buckets in-memory
std::unique_ptr<uint8_t[]> memory_start_;
// Size of the whole memory array
uint64_t memory_size_;
Expand Down