Skip to content

Commit

Permalink
Guarantee kvrocks doesn't delete checkpoint that is creating
Browse files Browse the repository at this point in the history
  • Loading branch information
ShooterIT committed Mar 19, 2021
1 parent a358ddd commit ff4187b
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
5 changes: 4 additions & 1 deletion src/server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,10 @@ void Server::cron() {
time_t create_time = storage_->GetCheckpointCreateTime();
time_t access_time = storage_->GetCheckpointAccessTime();

if (storage_->ExistCheckpoint()) {
// Maybe creating checkpoint costs much time if target dir is on another
// disk partition, so when we want to clean up checkpoint, we should guarantee
// that kvrocks is not creating checkpoint even if there is a checkpoint.
if (storage_->ExistCheckpoint() && storage_->IsCreatingCheckpoint() == false) {
// TODO(shooterit): support to config the alive time of checkpoint
if ((GetFetchFileThreadNum() == 0 && std::time(nullptr) - access_time > 30) ||
(std::time(nullptr) - create_time > 24 * 60 * 60)) {
Expand Down
11 changes: 6 additions & 5 deletions src/storage.cc
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ Storage::Storage(Config *config)
lock_mgr_(16) {
InitCRC32Table();
Metadata::InitVersionCounter();
SetCreatingCheckpoint(false);
SetCheckpointCreateTime(0);
SetCheckpointAccessTime(0);
}
Expand Down Expand Up @@ -608,14 +609,14 @@ Status Storage::ReplDataManager::GetFullReplDataInfo(Storage *storage, std::stri
LOG(WARNING) << "Fail to create checkpoint, error:" << s.ToString();
return Status(Status::NotOK, s.ToString());
}

// Set checkpoint time
storage->SetCheckpointCreateTime(std::time(nullptr));
storage->SetCheckpointAccessTime(std::time(nullptr));
std::unique_ptr<rocksdb::Checkpoint> checkpoint_guard(checkpoint);

// Create checkpoint of rocksdb
std::unique_ptr<rocksdb::Checkpoint> checkpoint_guard(checkpoint);
storage->SetCreatingCheckpoint(true);
s = checkpoint->CreateCheckpoint(data_files_dir);
storage->SetCheckpointCreateTime(std::time(nullptr));
storage->SetCheckpointAccessTime(std::time(nullptr));
storage->SetCreatingCheckpoint(false);
if (!s.ok()) {
LOG(WARNING) << "Fail to create checkpoint, error:" << s.ToString();
return Status(Status::NotOK, s.ToString());
Expand Down
3 changes: 3 additions & 0 deletions src/storage.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ class Storage {
static Status CleanInvalidFiles(Storage *storage,
const std::string &dir, std::vector<std::string> valid_files);
struct CheckpointInfo {
std::atomic<bool> is_creating;
std::atomic<time_t> create_time;
std::atomic<time_t> access_time;
};
Expand All @@ -118,6 +119,8 @@ class Storage {
};

bool ExistCheckpoint() { return backup_env_->FileExists(config_->checkpoint_dir).ok(); }
void SetCreatingCheckpoint(bool yes_or_no) { checkpoint_info_.is_creating = yes_or_no; }
bool IsCreatingCheckpoint() { return checkpoint_info_.is_creating; }
void SetCheckpointCreateTime(time_t t) { checkpoint_info_.create_time = t; }
time_t GetCheckpointCreateTime() { return checkpoint_info_.create_time; }
void SetCheckpointAccessTime(time_t t) { checkpoint_info_.access_time = t; }
Expand Down

0 comments on commit ff4187b

Please sign in to comment.