@@ -119,42 +119,7 @@ void DbCheckpointManager::init() {
119119 // if any dbcheckpoints are present in the file system.
120120 if (isMetadataErased_) {
121121 // update metadata for dbcheckpoints from file system
122- const auto & checkpointDir = dbClient_->getCheckpointPath ();
123- _fs::path path (checkpointDir);
124- std::vector<_fs::directory_entry> allDBCheckpoints;
125-
126- try {
127- if (_fs::exists (path)) {
128- uint64_t lastCreatedDbCheckpoint = 0 ;
129- for (const auto & entry : _fs::directory_iterator (checkpointDir)) {
130- const auto filenameStr = entry.path ().filename ().string ();
131- if (_fs::is_directory (entry)) {
132- allDBCheckpoints.push_back (entry);
133- const CheckpointId& checkPointId = std::stoi (filenameStr);
134- const auto blockId = checkPointId;
135-
136- _fs::file_time_type ftime = _fs::last_write_time (entry.path ());
137- std::time_t cftime = decltype (ftime)::clock::to_time_t (ftime);
138- const std::chrono::seconds creationTime = std::chrono::seconds (cftime);
139- {
140- std::scoped_lock lock (lockLastDbCheckpointDesc_);
141- lastCreatedCheckpointMetadata_.emplace (DbCheckpointMetadata::DbCheckPointDescriptor{
142- checkPointId, creationTime, blockId, lastCheckpointSeqNum_});
143- dbCheckptMetadata_.dbCheckPoints_ .insert ({checkPointId, lastCreatedCheckpointMetadata_.value ()});
144- }
145-
146- // extract the last created BlockId from all created dbCheckpoints.
147- lastCreatedDbCheckpoint = std::max (lastCreatedDbCheckpoint, checkPointId);
148- numOfDbCheckpointsCreated_++;
149- }
150- }
151- lastDbCheckpointBlockId_.Get ().Set (lastCreatedDbCheckpoint);
152- metrics_.UpdateAggregator ();
153- }
154- updateMetrics ();
155- } catch (std::exception& e) {
156- LOG_WARN (getLogger (), " Failed to update checkpoints metadata from checkpoint directory" << e.what ());
157- }
122+ builMetadataFromFileSystem ();
158123 } else {
159124 // check if there is chkpt data in persistence
160125 loadCheckpointDataFromPersistence ();
@@ -473,4 +438,48 @@ std::map<uint64_t, uint64_t> DbCheckpointManager::getDbSize() {
473438 }
474439 return dbSizeMap;
475440}
441+ void DbCheckpointManager::builMetadataFromFileSystem () {
442+ // update metadata for dbcheckpoints from file system
443+ const auto & checkpointDir = dbClient_->getCheckpointPath ();
444+ _fs::path path (checkpointDir);
445+
446+ try {
447+ if (_fs::exists (path)) {
448+ for (const auto & entry : _fs::directory_iterator (checkpointDir)) {
449+ const auto filenameStr = entry.path ().filename ().string ();
450+ // directory name is last block id in the db checkpoint
451+ // and must be a valid numeric string
452+ if (filenameStr.find_first_not_of (" 0123456789" ) != string::npos) {
453+ LOG_WARN (getLogger (), " Invalid file or directory:" << filenameStr << " found in checkpoint folder" );
454+ continue ;
455+ }
456+ if (_fs::is_directory (entry)) {
457+ const CheckpointId& checkPointId = std::stoi (filenameStr);
458+ const auto blockId = checkPointId;
459+ _fs::file_time_type ftime = _fs::last_write_time (entry.path ());
460+ std::time_t cftime = decltype (ftime)::clock::to_time_t (ftime);
461+ const std::chrono::seconds creationTime = std::chrono::seconds (cftime);
462+ {
463+ std::scoped_lock lock (lockLastDbCheckpointDesc_);
464+ const auto checkPointDescriptor = DbCheckpointMetadata::DbCheckPointDescriptor{
465+ checkPointId, creationTime, blockId, lastCheckpointSeqNum_};
466+ dbCheckptMetadata_.dbCheckPoints_ .insert ({checkPointId, checkPointDescriptor});
467+ }
468+ }
469+ }
470+
471+ if (auto it = dbCheckptMetadata_.dbCheckPoints_ .rbegin (); it != dbCheckptMetadata_.dbCheckPoints_ .rend ()) {
472+ lastCreatedCheckpointMetadata_ = it->second ;
473+ dbCheckptMetadata_.lastCmdTimestamp_ = it->second .creationTimeSinceEpoch_ ;
474+ lastCheckpointCreationTime_ = dbCheckptMetadata_.lastCmdTimestamp_ ;
475+ lastDbCheckpointBlockId_.Get ().Set (it->second .lastBlockId_ );
476+ metrics_.UpdateAggregator ();
477+ updateMetrics ();
478+ }
479+ updateDbCheckpointMetadata ();
480+ }
481+ } catch (std::exception& e) {
482+ LOG_WARN (getLogger (), " Failed to update checkpoints metadata from checkpoint directory" << e.what ());
483+ }
484+ }
476485} // namespace bftEngine::impl
0 commit comments