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

Add more options to C api for backups and getting info from them #13270

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
67 changes: 67 additions & 0 deletions db/c.cc
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ using ROCKSDB_NAMESPACE::CompactRangeOptions;
using ROCKSDB_NAMESPACE::Comparator;
using ROCKSDB_NAMESPACE::CompressionType;
using ROCKSDB_NAMESPACE::ConfigOptions;
using ROCKSDB_NAMESPACE::CreateBackupOptions;
using ROCKSDB_NAMESPACE::CuckooTableOptions;
using ROCKSDB_NAMESPACE::DB;
using ROCKSDB_NAMESPACE::DBOptions;
Expand Down Expand Up @@ -145,6 +146,9 @@ struct rocksdb_backup_engine_t {
struct rocksdb_backup_engine_info_t {
std::vector<BackupInfo> rep;
};
struct rocksdb_create_backup_options_t {
CreateBackupOptions rep;
};
struct rocksdb_restore_options_t {
RestoreOptions rep;
};
Expand Down Expand Up @@ -680,12 +684,70 @@ void rocksdb_backup_engine_create_new_backup_flush(
SaveError(errptr, be->rep->CreateNewBackup(db->rep, flush_before_backup));
}

void rocksdb_backup_engine_create_new_backup_with_options_with_metadata(
rocksdb_backup_engine_t* be, rocksdb_t* db,
rocksdb_create_backup_options_t* bo, const char* app_metadata,
uint32_t* new_backup_id, char** errptr) {
std::string metadata_str;
if (app_metadata != NULL) {
metadata_str = std::string(app_metadata);
}

SaveError(errptr, be->rep->CreateNewBackupWithMetadata(
bo->rep, db->rep, metadata_str, new_backup_id));
}

rocksdb_create_backup_options_t* rocksdb_create_backup_options_create() {
return new rocksdb_create_backup_options_t;
}

void rocksdb_create_backup_options_destroy(
rocksdb_create_backup_options_t* opt) {
delete opt;
}

void rocksdb_create_backup_options_set_flush_before_backup(
rocksdb_create_backup_options_t* opt, bool flush) {
opt->rep.flush_before_backup = flush;
}

void rocksdb_backup_engine_purge_old_backups(rocksdb_backup_engine_t* be,
uint32_t num_backups_to_keep,
char** errptr) {
SaveError(errptr, be->rep->PurgeOldBackups(num_backups_to_keep));
}

void rocksdb_backup_engine_delete_backup(rocksdb_backup_engine_t* be,
uint32_t backup_id, char** errptr) {
SaveError(errptr, be->rep->DeleteBackup(backup_id));
}

int* rocksdb_backup_engine_get_corrupted_backups(
const rocksdb_backup_engine_t* be, size_t* num_corrupted_backups) {
std::vector<BackupID> corrupt_backup_ids;

be->rep->GetCorruptedBackups(&corrupt_backup_ids);

if (corrupt_backup_ids.empty()) {
return nullptr;
}

*num_corrupted_backups = corrupt_backup_ids.size();
int* corrupted_backups =
static_cast<int*>(malloc(sizeof(int) * corrupt_backup_ids.size()));

for (size_t i = 0; i < corrupt_backup_ids.size(); ++i) {
corrupted_backups[i] = static_cast<int>(corrupt_backup_ids[i]);
}

return corrupted_backups;
}

void rocksdb_backup_engine_garbage_collect(rocksdb_backup_engine_t* be,
char** errptr) {
SaveError(errptr, be->rep->GarbageCollect());
}

rocksdb_restore_options_t* rocksdb_restore_options_create() {
return new rocksdb_restore_options_t;
}
Expand Down Expand Up @@ -752,6 +814,11 @@ uint32_t rocksdb_backup_engine_info_number_files(
return info->rep[index].number_files;
}

char* rocksdb_backup_engine_info_app_metadata(
const rocksdb_backup_engine_info_t* info, int index) {
return strdup(info->rep[index].app_metadata.c_str());
}

void rocksdb_backup_engine_info_destroy(
const rocksdb_backup_engine_info_t* info) {
delete info;
Expand Down
24 changes: 23 additions & 1 deletion db/c_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -855,9 +855,20 @@ int main(int argc, char** argv) {
rocksdb_delete(db, woptions, "does-not-exist", 14, &err);
CheckNoError(err);

rocksdb_backup_engine_create_new_backup(be, db, &err);
rocksdb_create_backup_options_t* beo =
rocksdb_create_backup_options_create();
rocksdb_create_backup_options_set_flush_before_backup(beo, false);

uint32_t backup_id = 0;

rocksdb_backup_engine_create_new_backup_with_options_with_metadata(
be, db, beo, "a", &backup_id, &err);
CheckNoError(err);

CheckCondition(backup_id > 0);

rocksdb_create_backup_options_destroy(beo);

const rocksdb_backup_engine_info_t* bei =
rocksdb_backup_engine_get_backup_info(be);
CheckCondition(rocksdb_backup_engine_info_count(bei) > 1);
Expand All @@ -870,9 +881,20 @@ int main(int argc, char** argv) {
CheckCondition(rocksdb_backup_engine_info_count(bei) == 1);
rocksdb_backup_engine_info_destroy(bei);

size_t num_corrupted_backups = 0;
int* corrupted_backups =
rocksdb_backup_engine_get_corrupted_backups(be, &num_corrupted_backups);
CheckCondition(num_corrupted_backups == 0);
if (corrupted_backups) {
free(corrupted_backups);
}

rocksdb_delete(db, woptions, "foo", 3, &err);
CheckNoError(err);

rocksdb_backup_engine_garbage_collect(be, &err);
CheckNoError(err);

// get the identity before the backup
size_t before_db_id_len = 0;
char* before_db_id = rocksdb_get_db_identity(db, &before_db_id_len);
Expand Down
29 changes: 29 additions & 0 deletions include/rocksdb/c.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ typedef struct rocksdb_t rocksdb_t;
typedef struct rocksdb_backup_engine_t rocksdb_backup_engine_t;
typedef struct rocksdb_backup_engine_info_t rocksdb_backup_engine_info_t;
typedef struct rocksdb_backup_engine_options_t rocksdb_backup_engine_options_t;
typedef struct rocksdb_create_backup_options_t rocksdb_create_backup_options_t;
typedef struct rocksdb_restore_options_t rocksdb_restore_options_t;
typedef struct rocksdb_memory_allocator_t rocksdb_memory_allocator_t;
typedef struct rocksdb_lru_cache_options_t rocksdb_lru_cache_options_t;
Expand Down Expand Up @@ -172,9 +173,28 @@ extern ROCKSDB_LIBRARY_API void rocksdb_backup_engine_create_new_backup_flush(
rocksdb_backup_engine_t* be, rocksdb_t* db,
unsigned char flush_before_backup, char** errptr);

extern ROCKSDB_LIBRARY_API void
rocksdb_backup_engine_create_new_backup_with_options_with_metadata(
rocksdb_backup_engine_t* be, rocksdb_t* db,
rocksdb_create_backup_options_t* bo, const char* app_metadata,
uint32_t* new_backup_id, char** errptr);

extern ROCKSDB_LIBRARY_API rocksdb_create_backup_options_t*
rocksdb_create_backup_options_create(void);

extern ROCKSDB_LIBRARY_API void rocksdb_create_backup_options_destroy(
rocksdb_create_backup_options_t* opt);

extern ROCKSDB_LIBRARY_API void
rocksdb_create_backup_options_set_flush_before_backup(
rocksdb_create_backup_options_t* opt, bool flush);

extern ROCKSDB_LIBRARY_API void rocksdb_backup_engine_purge_old_backups(
rocksdb_backup_engine_t* be, uint32_t num_backups_to_keep, char** errptr);

extern ROCKSDB_LIBRARY_API void rocksdb_backup_engine_delete_backup(
rocksdb_backup_engine_t* be, uint32_t backup_id, char** errptr);

extern ROCKSDB_LIBRARY_API rocksdb_restore_options_t*
rocksdb_restore_options_create(void);
extern ROCKSDB_LIBRARY_API void rocksdb_restore_options_destroy(
Expand All @@ -195,6 +215,12 @@ extern ROCKSDB_LIBRARY_API void rocksdb_backup_engine_restore_db_from_backup(
const rocksdb_restore_options_t* restore_options, const uint32_t backup_id,
char** errptr);

extern ROCKSDB_LIBRARY_API int* rocksdb_backup_engine_get_corrupted_backups(
const rocksdb_backup_engine_t* be, size_t* num_corrupted_backups);

extern ROCKSDB_LIBRARY_API void rocksdb_backup_engine_garbage_collect(
rocksdb_backup_engine_t* be, char** errptr);

extern ROCKSDB_LIBRARY_API const rocksdb_backup_engine_info_t*
rocksdb_backup_engine_get_backup_info(rocksdb_backup_engine_t* be);

Expand All @@ -213,6 +239,9 @@ extern ROCKSDB_LIBRARY_API uint64_t rocksdb_backup_engine_info_size(
extern ROCKSDB_LIBRARY_API uint32_t rocksdb_backup_engine_info_number_files(
const rocksdb_backup_engine_info_t* info, int index);

extern ROCKSDB_LIBRARY_API char* rocksdb_backup_engine_info_app_metadata(
const rocksdb_backup_engine_info_t* info, int index);

extern ROCKSDB_LIBRARY_API void rocksdb_backup_engine_info_destroy(
const rocksdb_backup_engine_info_t* info);

Expand Down
Loading