reduced max open files on levelDB

This commit is contained in:
Brannon King 2019-08-29 08:56:10 -06:00
parent b52f47f273
commit a84c196916
4 changed files with 27 additions and 20 deletions

View file

@ -80,14 +80,14 @@ static void SetMaxOpenFiles(leveldb::Options *options) {
// implementation that does not use extra file descriptors (the fds are
// closed after being mmaped).
//
// Increasing the value beyond the default is dangerous because LevelDB will
// fall back to a non-mmap implementation when the file count is too large.
// Increasing the value beyond the nmap count is dangerous because LevelDB will
// fall back to a non-mmap implementation when the file count is too large (thus contending select()).
// On 32-bit Unix host we should decrease the value because the handles use
// up real fds, and we want to avoid fd exhaustion issues.
//
// See PR #12495 for further discussion.
int default_open_files = options->max_open_files;
int default_open_files = 400;
#ifndef WIN32
if (sizeof(void*) < 8) {
options->max_open_files = 64;

View file

@ -221,6 +221,9 @@ class RandomAccessFile {
// Get a name for the file, only for error reporting
virtual std::string GetName() const = 0;
virtual char* AllocateScratch(std::size_t size) const { return new char[size]; };
virtual void DeallocateScratch(char* pointer) const { delete[] pointer; };
private:
// No copying allowed
RandomAccessFile(const RandomAccessFile&);

View file

@ -73,15 +73,15 @@ Status ReadBlock(RandomAccessFile* file,
// Read the block contents as well as the type/crc footer.
// See table_builder.cc for the code that built this structure.
size_t n = static_cast<size_t>(handle.size());
char* buf = new char[n + kBlockTrailerSize];
char* buf = file->AllocateScratch(n + kBlockTrailerSize);
Slice contents;
Status s = file->Read(handle.offset(), n + kBlockTrailerSize, &contents, buf);
if (!s.ok()) {
delete[] buf;
file->DeallocateScratch(buf);
return s;
}
if (contents.size() != n + kBlockTrailerSize) {
delete[] buf;
file->DeallocateScratch(buf);
return Status::Corruption("truncated block read", file->GetName());
}
@ -91,7 +91,7 @@ Status ReadBlock(RandomAccessFile* file,
const uint32_t crc = crc32c::Unmask(DecodeFixed32(data + n + 1));
const uint32_t actual = crc32c::Value(data, n + 1);
if (actual != crc) {
delete[] buf;
file->DeallocateScratch(buf);
s = Status::Corruption("block checksum mismatch", file->GetName());
return s;
}
@ -103,7 +103,7 @@ Status ReadBlock(RandomAccessFile* file,
// File implementation gave us pointer to some other data.
// Use it directly under the assumption that it will be live
// while the file is open.
delete[] buf;
file->DeallocateScratch(buf);
result->data = Slice(data, n);
result->heap_allocated = false;
result->cachable = false; // Do not double-cache
@ -118,23 +118,23 @@ Status ReadBlock(RandomAccessFile* file,
case kSnappyCompression: {
size_t ulength = 0;
if (!port::Snappy_GetUncompressedLength(data, n, &ulength)) {
delete[] buf;
file->DeallocateScratch(buf);
return Status::Corruption("corrupted compressed block contents", file->GetName());
}
char* ubuf = new char[ulength];
if (!port::Snappy_Uncompress(data, n, ubuf)) {
delete[] buf;
file->DeallocateScratch(buf);
delete[] ubuf;
return Status::Corruption("corrupted compressed block contents", file->GetName());
}
delete[] buf;
file->DeallocateScratch(buf);
result->data = Slice(ubuf, ulength);
result->heap_allocated = true;
result->cachable = true;
break;
}
default:
delete[] buf;
file->DeallocateScratch(buf);
return Status::Corruption("bad block type", file->GetName());
}

View file

@ -162,6 +162,7 @@ class PosixRandomAccessFile: public RandomAccessFile {
}
Status s;
assert(scratch);
ssize_t r = pread(fd, scratch, n, static_cast<off_t>(offset));
*result = Slice(scratch, (r < 0) ? 0 : r);
if (r < 0) {
@ -188,19 +189,19 @@ class PosixMmapReadableFile: public RandomAccessFile {
public:
// base[0,length-1] contains the mmapped contents of the file.
PosixMmapReadableFile(const std::string& fname, void* base, size_t length,
PosixMmapReadableFile(std::string fname, void* base, size_t length,
Limiter* limiter)
: filename_(fname), mmapped_region_(base), length_(length),
: filename_(std::move(fname)), mmapped_region_(base), length_(length),
limiter_(limiter) {
}
virtual ~PosixMmapReadableFile() {
~PosixMmapReadableFile() override {
munmap(mmapped_region_, length_);
limiter_->Release();
}
virtual Status Read(uint64_t offset, size_t n, Slice* result,
char* scratch) const {
Status Read(uint64_t offset, size_t n, Slice* result,
char* scratch) const override {
Status s;
if (offset + n > length_) {
*result = Slice();
@ -211,7 +212,10 @@ class PosixMmapReadableFile: public RandomAccessFile {
return s;
}
virtual std::string GetName() const { return filename_; }
std::string GetName() const override { return filename_; }
char* AllocateScratch(std::size_t size) const override { return nullptr; }
void DeallocateScratch(char* pointer) const override { assert(pointer == nullptr); }
};
class PosixWritableFile : public WritableFile {
@ -585,8 +589,8 @@ static int MaxMmaps() {
if (mmap_limit >= 0) {
return mmap_limit;
}
// Up to 4096 mmaps for 64-bit binaries; none for smaller pointer sizes.
mmap_limit = sizeof(void*) >= 8 ? 4096 : 0;
// Up to 400 mmaps for 64-bit binaries (800MB); none for smaller pointer sizes.
mmap_limit = sizeof(void*) >= 8 ? 400 : 0;
return mmap_limit;
}