Skip to content

Commit

Permalink
Merge 4c22461 into 3c5794f
Browse files Browse the repository at this point in the history
  • Loading branch information
andrey-kashcheev committed May 17, 2023
2 parents 3c5794f + 4c22461 commit dbabae2
Showing 1 changed file with 67 additions and 0 deletions.
67 changes: 67 additions & 0 deletions olp-cpp-sdk-core/src/cache/DiskCache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

#include "DiskCache.h"

#include <sys/statfs.h>
#include <iterator>
#include <memory>
#include <string>
Expand Down Expand Up @@ -144,6 +145,52 @@ int CheckCompactionFinished(leveldb::DB& db) {
return false;
}

uint64_t GetAvailableSpaceBytes(const std::string& path) {
#if defined(_WIN32) && !defined(__MINGW32__)
#error Not implemented!
#else

struct ::statfs64 fs_stat {};

if (::statfs64(path.c_str(), &fs_stat) != 0) {
return 0u;
}

// Check for inodes is missing now but the data is available through
// `statfs64::f_files` and `statfs46::f_ffree`.

const auto available_bytes = fs_stat.f_bavail * fs_stat.f_bsize;
// Here we could adjust available space by reducing it to some extent.
// For example, we could not allow it go lower than 5-10% from the initial
// capacity. This will kinda simulate having a dedicated file but removes all
// bookkeeping stuff.
return available_bytes;

#endif
}

uint64_t GetWriteBatchSize(const leveldb::WriteBatch& batch) {
struct BatchSizeCalculator final : leveldb::WriteBatch::Handler {
void Put(const leveldb::Slice& key, const leveldb::Slice& value) final {
size += key.size() + value.size();
}

void Delete(const leveldb::Slice&) final {
// FIXME: ignore?
}

// It is possible to have some initial size here for any batch, e.g. size of
// a single log file, or even few of them.
uint64_t size{};
};

BatchSizeCalculator batch_size_calculator;

Check warning on line 187 in olp-cpp-sdk-core/src/cache/DiskCache.cpp

View check run for this annotation

Codecov / codecov/patch

olp-cpp-sdk-core/src/cache/DiskCache.cpp#L187

Added line #L187 was not covered by tests
batch.Iterate(&batch_size_calculator);
// And we could tweak the size further here by adding some 'extra overhead',
// e.g. 5-10%
return batch_size_calculator.size;
}

} // anonymous namespace

DiskCache::DiskCache() : env_(DiskCacheEnv::CreateEnv()){};
Expand Down Expand Up @@ -300,6 +347,16 @@ bool DiskCache::Put(const std::string& key, leveldb::Slice slice) {
return false;
}

const auto available_bytes = GetAvailableSpaceBytes(disk_cache_path_);
const auto put_size = key.size() + slice.size();

Check warning on line 351 in olp-cpp-sdk-core/src/cache/DiskCache.cpp

View check run for this annotation

Codecov / codecov/patch

olp-cpp-sdk-core/src/cache/DiskCache.cpp#L350-L351

Added lines #L350 - L351 were not covered by tests
if (put_size >= available_bytes) {
OLP_SDK_LOG_ERROR_F(

Check warning on line 353 in olp-cpp-sdk-core/src/cache/DiskCache.cpp

View check run for this annotation

Codecov / codecov/patch

olp-cpp-sdk-core/src/cache/DiskCache.cpp#L353

Added line #L353 was not covered by tests
kLogTag,
"Put: failed, no space left, put_size=%zu, available_bytes=%zu",
put_size, available_bytes);
return false;

Check warning on line 357 in olp-cpp-sdk-core/src/cache/DiskCache.cpp

View check run for this annotation

Codecov / codecov/patch

olp-cpp-sdk-core/src/cache/DiskCache.cpp#L357

Added line #L357 was not covered by tests
}

leveldb::WriteOptions write_options;
write_options.sync = enforce_immediate_flush_;

Expand Down Expand Up @@ -426,6 +483,16 @@ DiskCache::OperationOutcome DiskCache::ApplyBatch(
}
}

const auto available_bytes = GetAvailableSpaceBytes(disk_cache_path_);
const auto batch_size = GetWriteBatchSize(*batch);
if (batch_size >= available_bytes) {
OLP_SDK_LOG_ERROR_F(kLogTag,
"ApplyBatch: failed, no space left, batch_size=%zu, "
"available_bytes=%zu",
batch_size, available_bytes);
return client::ApiError(client::ErrorCode::CacheIO, "No space left");
}

leveldb::WriteOptions write_options;
write_options.sync = enforce_immediate_flush_;

Expand Down

0 comments on commit dbabae2

Please sign in to comment.