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

feat: clean LOG.old* files on db start #2475

Merged
merged 2 commits into from
May 16, 2023
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
4 changes: 4 additions & 0 deletions libraries/core_libs/storage/include/storage/storage.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,10 @@ class DbStorage : public std::enable_shared_from_this<DbStorage> {
void updateDbVersions();
void deleteColumnData(const Column& c);

// For removal of LOG.old.* files in the database
void removeOldLogFiles() const;
void removeFilesWithPattern(const std::string& directory, const std::regex& pattern) const;

uint32_t getMajorVersion() const;
std::unique_ptr<rocksdb::Iterator> getColumnIterator(const Column& c);

Expand Down
30 changes: 27 additions & 3 deletions libraries/core_libs/storage/src/storage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <boost/algorithm/string/split.hpp>
#include <cstdint>
#include <memory>
#include <regex>

#include "config/version.hpp"
#include "dag/sortition_params_manager.hpp"
Expand Down Expand Up @@ -42,6 +43,8 @@ DbStorage::DbStorage(fs::path const& path, uint32_t db_snapshot_each_n_pbft_bloc
}

fs::create_directories(db_path_);
removeOldLogFiles();

rocksdb::Options options;
options.create_missing_column_families = true;
options.create_if_missing = true;
Expand Down Expand Up @@ -86,6 +89,26 @@ DbStorage::DbStorage(fs::path const& path, uint32_t db_snapshot_each_n_pbft_bloc
}
}

void DbStorage::removeOldLogFiles() const {
const std::regex filePattern("LOG\\.old\\.\\d+");
removeFilesWithPattern(db_path_, filePattern);
removeFilesWithPattern(state_db_path_, filePattern);
}

void DbStorage::removeFilesWithPattern(const std::string& directory, const std::regex& pattern) const {
try {
for (const auto& entry : std::filesystem::directory_iterator(directory)) {
const std::string& filename = entry.path().filename().string();
if (std::regex_match(filename, pattern)) {
std::filesystem::remove(entry.path());
LOG(log_dg_) << "Removed file: " << filename << std::endl;
}
}
} catch (const std::filesystem::filesystem_error& e) {
LOG(log_dg_) << "Error accessing directory: " << e.what() << std::endl;
}
}

void DbStorage::updateDbVersions() {
saveStatusField(StatusDbField::DbMajorVersion, TARAXA_DB_MAJOR_VERSION);
saveStatusField(StatusDbField::DbMinorVersion, TARAXA_DB_MINOR_VERSION);
Expand Down Expand Up @@ -464,7 +487,8 @@ void DbStorage::clearPeriodDataHistory(PbftPeriod end_period) {
auto start_slice = toSlice(start_period);
auto end_slice = toSlice(end_period);
for (auto period = start_period; period < end_period; period++) {
// Find transactions included in the old blocks and delete data related to these transactions to free disk space
// Find transactions included in the old blocks and delete data related to these transactions to free disk
// space
auto trx_hashes_raw = lookup(period, DB::Columns::final_chain_transaction_hashes_by_blk_number);
auto hashes_count = trx_hashes_raw.size() / trx_hash_t::size;
for (uint32_t i = 0; i < hashes_count; i++) {
Expand All @@ -481,8 +505,8 @@ void DbStorage::clearPeriodDataHistory(PbftPeriod end_period) {
commitWriteBatch(write_batch);

db_->DeleteRange(write_options_, handle(Columns::period_data), start_slice, end_slice);
// Deletion alone does not guarantee that the disk space is freed, these CompactRange methods actually compact the
// data in the database and free disk space
// Deletion alone does not guarantee that the disk space is freed, these CompactRange methods actually compact
// the data in the database and free disk space
db_->CompactRange({}, handle(Columns::period_data), &start_slice, &end_slice);
db_->CompactRange({}, handle(Columns::final_chain_receipt_by_trx_hash), nullptr, nullptr);
db_->CompactRange({}, handle(Columns::final_chain_transaction_hashes_by_blk_number), nullptr, nullptr);
Expand Down