@@ -46,8 +46,6 @@ class BackupEngineImpl : public BackupEngine {
46
46
return RestoreDBFromBackup (latest_backup_id_, db_dir, wal_dir);
47
47
}
48
48
49
- void DeleteBackupsNewerThan (uint64_t sequence_number);
50
-
51
49
private:
52
50
struct FileInfo {
53
51
FileInfo (const std::string& fname, uint64_t sz, uint32_t checksum)
@@ -185,6 +183,12 @@ class BackupEngineImpl : public BackupEngine {
185
183
Env* db_env_;
186
184
Env* backup_env_;
187
185
186
+ // directories
187
+ unique_ptr<Directory> backup_directory_;
188
+ unique_ptr<Directory> shared_directory_;
189
+ unique_ptr<Directory> meta_directory_;
190
+ unique_ptr<Directory> private_directory_;
191
+
188
192
static const size_t copy_file_buffer_size_ = 5 * 1024 * 1024LL ; // 5MB
189
193
};
190
194
@@ -203,11 +207,17 @@ BackupEngineImpl::BackupEngineImpl(Env* db_env,
203
207
204
208
// create all the dirs we need
205
209
backup_env_->CreateDirIfMissing (GetAbsolutePath ());
210
+ backup_env_->NewDirectory (GetAbsolutePath (), &backup_directory_);
206
211
if (options_.share_table_files ) {
207
212
backup_env_->CreateDirIfMissing (GetAbsolutePath (GetSharedFileRel ()));
213
+ backup_env_->NewDirectory (GetAbsolutePath (GetSharedFileRel ()),
214
+ &shared_directory_);
208
215
}
209
216
backup_env_->CreateDirIfMissing (GetAbsolutePath (GetPrivateDirRel ()));
217
+ backup_env_->NewDirectory (GetAbsolutePath (GetPrivateDirRel ()),
218
+ &private_directory_);
210
219
backup_env_->CreateDirIfMissing (GetBackupMetaDir ());
220
+ backup_env_->NewDirectory (GetBackupMetaDir (), &meta_directory_);
211
221
212
222
std::vector<std::string> backup_meta_files;
213
223
backup_env_->GetChildren (GetBackupMetaDir (), &backup_meta_files);
@@ -279,26 +289,6 @@ BackupEngineImpl::BackupEngineImpl(Env* db_env,
279
289
280
290
BackupEngineImpl::~BackupEngineImpl () { LogFlush (options_.info_log ); }
281
291
282
- void BackupEngineImpl::DeleteBackupsNewerThan (uint64_t sequence_number) {
283
- for (auto backup : backups_) {
284
- if (backup.second .GetSequenceNumber () > sequence_number) {
285
- Log (options_.info_log ,
286
- " Deleting backup %u because sequence number (%" PRIu64
287
- " ) is newer than %" PRIu64 " " ,
288
- backup.first , backup.second .GetSequenceNumber (), sequence_number);
289
- backup.second .Delete ();
290
- obsolete_backups_.push_back (backup.first );
291
- }
292
- }
293
- for (auto ob : obsolete_backups_) {
294
- backups_.erase (backups_.find (ob));
295
- }
296
- auto itr = backups_.end ();
297
- latest_backup_id_ = (itr == backups_.begin ()) ? 0 : (--itr)->first ;
298
- PutLatestBackupFileContents (latest_backup_id_); // Ignore errors
299
- GarbageCollection (false );
300
- }
301
-
302
292
Status BackupEngineImpl::CreateNewBackup (DB* db, bool flush_before_backup) {
303
293
Status s;
304
294
std::vector<std::string> live_files;
@@ -348,9 +338,8 @@ Status BackupEngineImpl::CreateNewBackup(DB* db, bool flush_before_backup) {
348
338
return Status::Corruption (" Can't parse file name. This is very bad" );
349
339
}
350
340
// we should only get sst, manifest and current files here
351
- assert (type == kTableFile ||
352
- type == kDescriptorFile ||
353
- type == kCurrentFile );
341
+ assert (type == kTableFile || type == kDescriptorFile ||
342
+ type == kCurrentFile );
354
343
355
344
// rules:
356
345
// * if it's kTableFile, than it's shared
@@ -394,6 +383,28 @@ Status BackupEngineImpl::CreateNewBackup(DB* db, bool flush_before_backup) {
394
383
// install the newly created backup meta! (atomic)
395
384
s = PutLatestBackupFileContents (new_backup_id);
396
385
}
386
+ if (s.ok () && options_.sync ) {
387
+ unique_ptr<Directory> backup_private_directory;
388
+ backup_env_->NewDirectory (
389
+ GetAbsolutePath (GetPrivateFileRel (new_backup_id, false )),
390
+ &backup_private_directory);
391
+ if (backup_private_directory != nullptr ) {
392
+ backup_private_directory->Fsync ();
393
+ }
394
+ if (private_directory_ != nullptr ) {
395
+ private_directory_->Fsync ();
396
+ }
397
+ if (meta_directory_ != nullptr ) {
398
+ meta_directory_->Fsync ();
399
+ }
400
+ if (shared_directory_ != nullptr ) {
401
+ shared_directory_->Fsync ();
402
+ }
403
+ if (backup_directory_ != nullptr ) {
404
+ backup_directory_->Fsync ();
405
+ }
406
+ }
407
+
397
408
if (!s.ok ()) {
398
409
// clean all the files we might have created
399
410
Log (options_.info_log , " Backup failed -- %s" , s.ToString ().c_str ());
@@ -591,6 +602,7 @@ Status BackupEngineImpl::CopyFile(const std::string& src,
591
602
unique_ptr<SequentialFile> src_file;
592
603
EnvOptions env_options;
593
604
env_options.use_mmap_writes = false ;
605
+ env_options.use_os_buffer = false ;
594
606
if (size != nullptr ) {
595
607
*size = 0 ;
596
608
}
@@ -706,6 +718,7 @@ Status BackupEngineImpl::CalculateChecksum(const std::string& src, Env* src_env,
706
718
707
719
EnvOptions env_options;
708
720
env_options.use_mmap_writes = false ;
721
+ env_options.use_os_buffer = false ;
709
722
710
723
std::unique_ptr<SequentialFile> src_file;
711
724
Status s = src_env->NewSequentialFile (src, &src_file, env_options);
@@ -893,6 +906,9 @@ Status BackupEngineImpl::BackupMeta::LoadFromFile(
893
906
894
907
uint64_t size;
895
908
s = env_->GetFileSize (backup_dir + " /" + filename, &size);
909
+ if (!s.ok ()) {
910
+ return s;
911
+ }
896
912
897
913
if (line.empty ()) {
898
914
return Status::Corruption (" File checksum is missing" );
@@ -913,6 +929,11 @@ Status BackupEngineImpl::BackupMeta::LoadFromFile(
913
929
files.emplace_back (filename, size, checksum_value);
914
930
}
915
931
932
+ if (s.ok () && data.size () > 0 ) {
933
+ // file has to be read completely. if not, we count it as corruption
934
+ s = Status::Corruption (" Tailing data in backup meta file" );
935
+ }
936
+
916
937
if (s.ok ()) {
917
938
for (const auto & file_info : files) {
918
939
s = AddFile (file_info);
@@ -968,11 +989,7 @@ Status BackupEngineImpl::BackupMeta::StoreToFile(bool sync) {
968
989
969
990
BackupableDB::BackupableDB (DB* db, const BackupableDBOptions& options)
970
991
: StackableDB(db),
971
- backup_engine_ (new BackupEngineImpl(db->GetEnv (), options)) {
972
- if (options.share_table_files ) {
973
- backup_engine_->DeleteBackupsNewerThan (GetLatestSequenceNumber ());
974
- }
975
- }
992
+ backup_engine_ (new BackupEngineImpl(db->GetEnv (), options)) {}
976
993
977
994
BackupableDB::~BackupableDB () {
978
995
delete backup_engine_;
0 commit comments