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

chore: delete tmp folders on startup #2824

Merged
merged 1 commit into from
Aug 9, 2024
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
3 changes: 2 additions & 1 deletion libraries/core_libs/storage/include/storage/storage.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,9 @@ class DbStorage : public std::enable_shared_from_this<DbStorage> {
const std::string& new_col_name, bool move_data = false);

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

uint32_t getMajorVersion() const;
std::unique_ptr<rocksdb::Iterator> getColumnIterator(const Column& c);
Expand Down
21 changes: 19 additions & 2 deletions libraries/core_libs/storage/src/storage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ DbStorage::DbStorage(fs::path const& path, uint32_t db_snapshot_each_n_pbft_bloc
LOG_OBJECTS_CREATE("DBS");

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

rocksdb::Options options;
options.create_missing_column_families = true;
Expand Down Expand Up @@ -94,10 +94,11 @@ DbStorage::DbStorage(fs::path const& path, uint32_t db_snapshot_each_n_pbft_bloc
}
}

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

void DbStorage::removeFilesWithPattern(const std::string& directory, const std::regex& pattern) const {
Expand All @@ -118,6 +119,22 @@ void DbStorage::removeFilesWithPattern(const std::string& directory, const std::
}
}

void DbStorage::deleteTmpDirectories(const std::string& path) const {
try {
for (const auto& entry : fs::directory_iterator(path)) {
if (entry.is_directory()) {
std::string dirName = entry.path().filename().string();
if (dirName.size() >= 4 && dirName.substr(dirName.size() - 4) == ".tmp") {
fs::remove_all(entry.path());
LOG(log_dg_) << "Deleted: " << entry.path() << std::endl;
}
}
}
} catch (const fs::filesystem_error& e) {
LOG(log_er_) << "Error: " << e.what() << std::endl;
}
}

void DbStorage::updateDbVersions() {
saveStatusField(StatusDbField::DbMajorVersion, TARAXA_DB_MAJOR_VERSION);
saveStatusField(StatusDbField::DbMinorVersion, TARAXA_DB_MINOR_VERSION);
Expand Down
Loading