Skip to content

Commit f019896

Browse files
LiuRuoyu01yuhaijun999
authored andcommitted
[feat][coordinator]Add register restore info and status
1 parent bce368a commit f019896

File tree

7 files changed

+434
-10
lines changed

7 files changed

+434
-10
lines changed

src/client_v2/coordinator.cc

Lines changed: 64 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "common/logging.h"
2525
#include "common/version.h"
2626
#include "document/codec.h"
27+
#include "proto/coordinator.pb.h"
2728

2829
namespace client_v2 {
2930

@@ -52,6 +53,10 @@ void SetUpCoordinatorSubCommands(CLI::App &app) {
5253
SetUpUpdateTenantGCSafePoint(app);
5354
SetUpUpdateGCFlag(app);
5455
SetUpGetGCSafePoint(app);
56+
57+
// br
58+
SetUpGetBackUpStatus(app);
59+
SetUpGetRestoreStatus(app);
5560
}
5661

5762
bool GetBrpcChannel(const std::string &location, brpc::Channel &channel) {
@@ -305,8 +310,7 @@ void RunRaftAddPeer(RaftAddPeerCommandOptions const &opt) {
305310
DINGO_LOG(WARNING) << "Fail to send request to : " << cntl.ErrorText();
306311
// bthread_usleep(FLAGS_timeout_ms * 1000L);
307312
}
308-
DINGO_LOG(INFO) << "Received response"
309-
<< " request_attachment=" << cntl.request_attachment().size()
313+
DINGO_LOG(INFO) << "Received response" << " request_attachment=" << cntl.request_attachment().size()
310314
<< " response_attachment=" << cntl.response_attachment().size() << " latency=" << cntl.latency_us();
311315
DINGO_LOG(INFO) << response.DebugString();
312316
std::cout << "response:" << response.DebugString();
@@ -449,8 +453,7 @@ void RunRaftSnapshot(RaftSnapshotOption const &opt) {
449453
// bthread_usleep(timeout_ms * 1000L);
450454
}
451455
std::cout << "response:" << response.DebugString();
452-
DINGO_LOG(INFO) << "Received response"
453-
<< " request_attachment=" << cntl.request_attachment().size()
456+
DINGO_LOG(INFO) << "Received response" << " request_attachment=" << cntl.request_attachment().size()
454457
<< " response_attachment=" << cntl.response_attachment().size() << " latency=" << cntl.latency_us();
455458
}
456459

@@ -514,8 +517,7 @@ void RunRaftResetPeer(RaftResetPeerOption const &opt) {
514517
// bthread_usleep(timeout_ms * 1000L);
515518
}
516519
std::cout << "response:" << response.DebugString();
517-
DINGO_LOG(INFO) << "Received response"
518-
<< " request_attachment=" << cntl.request_attachment().size()
520+
DINGO_LOG(INFO) << "Received response" << " request_attachment=" << cntl.request_attachment().size()
519521
<< " response_attachment=" << cntl.response_attachment().size() << " latency=" << cntl.latency_us();
520522
}
521523

@@ -551,8 +553,7 @@ void RunGetNodeInfo(GetNodeInfoOption const &opt) {
551553
// bthread_usleep(timeout_ms * 1000L);
552554
}
553555

554-
DINGO_LOG(INFO) << "Received response"
555-
<< " cluster_id=" << request.cluster_id()
556+
DINGO_LOG(INFO) << "Received response" << " cluster_id=" << request.cluster_id()
556557
<< " request_attachment=" << cntl.request_attachment().size()
557558
<< " response_attachment=" << cntl.response_attachment().size() << " latency=" << cntl.latency_us();
558559
DINGO_LOG(INFO) << response.DebugString();
@@ -2727,5 +2728,60 @@ void RunUpdateForceReadOnly(UpdateForceReadOnlyOptions const &opt) {
27272728
DINGO_LOG(INFO) << response.DebugString();
27282729
}
27292730

2731+
void SetUpGetBackUpStatus(CLI::App &app) {
2732+
auto opt = std::make_shared<GetBackUpStatusOptions>();
2733+
auto *cmd = app.add_subcommand("GetBackUpStatus", "Get backup status ")->group("Coordinator Command");
2734+
cmd->add_option("--coor_url", opt->coor_url, "Coordinator url, default:file://./coor_list");
2735+
cmd->callback([opt]() { RunGetBackUpStatus(*opt); });
2736+
}
2737+
void RunGetBackUpStatus(GetBackUpStatusOptions const &opt) {
2738+
if (Helper::SetUp(opt.coor_url) < 0) {
2739+
exit(-1);
2740+
}
2741+
dingodb::pb::coordinator::RegisterBackupStatusRequest request;
2742+
dingodb::pb::coordinator::RegisterBackupStatusResponse response;
2743+
2744+
auto status = CoordinatorInteraction::GetInstance().GetCoorinatorInteraction()->SendRequest("RegisterBackupStatus",
2745+
request, response);
2746+
if (!status.ok()) {
2747+
DINGO_LOG(INFO) << "SendRequest status=" << status;
2748+
} else {
2749+
DINGO_LOG(INFO) << response.DebugString();
2750+
if (response.is_backing_up()) {
2751+
std::cout << "is backing_up , backing_up name : " << response.backup_name() << std::endl;
2752+
} else {
2753+
std::cout << "is not backing_up" << std::endl;
2754+
}
2755+
}
2756+
}
2757+
2758+
void SetUpGetRestoreStatus(CLI::App &app) {
2759+
auto opt = std::make_shared<GetRestoreStatusOptions>();
2760+
auto *cmd = app.add_subcommand("GetRestoreStatus", "Get restore status ")->group("Coordinator Command");
2761+
cmd->add_option("--coor_url", opt->coor_url, "Coordinator url, default:file://./coor_list");
2762+
cmd->callback([opt]() { RunGetRestoreStatus(*opt); });
2763+
}
2764+
2765+
void RunGetRestoreStatus(GetRestoreStatusOptions const &opt) {
2766+
if (Helper::SetUp(opt.coor_url) < 0) {
2767+
exit(-1);
2768+
}
2769+
dingodb::pb::coordinator::RegisterRestoreStatusRequest request;
2770+
dingodb::pb::coordinator::RegisterRestoreStatusResponse response;
2771+
2772+
auto status = CoordinatorInteraction::GetInstance().GetCoorinatorInteraction()->SendRequest("RegisterRestoreStatus",
2773+
request, response);
2774+
if (!status.ok()) {
2775+
DINGO_LOG(INFO) << "SendRequest status=" << status;
2776+
} else {
2777+
DINGO_LOG(INFO) << response.DebugString();
2778+
if (response.is_restoring()) {
2779+
std::cout << "is restoring , restore name : " << response.restore_name() << std::endl;
2780+
} else {
2781+
std::cout << "is not restoring" << std::endl;
2782+
}
2783+
}
2784+
}
2785+
27302786
// refactor
27312787
} // namespace client_v2

src/client_v2/coordinator.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -557,6 +557,18 @@ struct UpdateForceReadOnlyOptions {
557557
void SetUpUpdateForceReadOnly(CLI::App &app);
558558
void RunUpdateForceReadOnly(UpdateForceReadOnlyOptions const &opt);
559559

560+
struct GetBackUpStatusOptions {
561+
std::string coor_url;
562+
};
563+
void SetUpGetBackUpStatus(CLI::App &app);
564+
void RunGetBackUpStatus(GetBackUpStatusOptions const &opt);
565+
566+
struct GetRestoreStatusOptions {
567+
std::string coor_url;
568+
};
569+
void SetUpGetRestoreStatus(CLI::App &app);
570+
void RunGetRestoreStatus(GetRestoreStatusOptions const &opt);
571+
560572
} // namespace client_v2
561573

562574
#endif // DINGODB_CLIENT_COORDINATOR_H_

src/client_v2/meta.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2832,7 +2832,7 @@ void RunCreateIds(CreateIdsOptions const &opt) {
28322832

28332833
void SetUpImportMeta(CLI::App &app) {
28342834
auto opt = std::make_shared<ImportMetaOptions>();
2835-
auto *cmd = app.add_subcommand("ImportMeta", "Import meta ")->group("Coordinator Command");
2835+
auto *cmd = app.add_subcommand("ImportMeta", "Import meta ")->group("Meta Command");
28362836
cmd->add_option("--coor_url", opt->coor_url, "Coordinator url, default:file://./coor_list");
28372837
cmd->add_option("--meta_file_dir", opt->dir_meta_file, "Request parameter meta_file_dir")->required();
28382838
cmd->callback([opt]() { RunImportMeta(*opt); });
@@ -2894,7 +2894,7 @@ void RunImportMeta(const ImportMetaOptions &opt) {
28942894

28952895
void SetUpExportMeta(CLI::App &app) {
28962896
auto opt = std::make_shared<ExportMetaOptions>();
2897-
auto *cmd = app.add_subcommand("ExportMeta", "Export meta ")->group("Coordinator Command");
2897+
auto *cmd = app.add_subcommand("ExportMeta", "Export meta ")->group("Meta Command");
28982898
cmd->add_option("--coor_url", opt->coor_url, "Coordinator url, default:file://./coor_list");
28992899
cmd->add_option("--meta_file_dir", opt->dir_meta_file, "Request parameter meta_file_dir")->required();
29002900
cmd->callback([opt]() { RunExportMeta(*opt); });

src/coordinator/coordinator_control.h

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -984,6 +984,16 @@ class CoordinatorControl : public MetaControl {
984984
int64_t backup_timeout_s);
985985
static butil::Status UnRegisterBackup(const std::string &backup_name);
986986

987+
static butil::Status RegisterRestore(const std::string &restore_name, const std::string &restore_path,
988+
int64_t restore_start_timestamp, int64_t restore_current_timestamp,
989+
int64_t restore_timeout_s);
990+
991+
static butil::Status UnRegisterRestore(const std::string &restore_name);
992+
993+
static butil::Status GetBackupStatus(bool &is_backing_up, std::string &backup_name);
994+
995+
static butil::Status GetRestoreStatus(bool &is_restoring, std::string &restore_name);
996+
987997
butil::Status GetAllPresentId(std::vector<std::pair<pb::coordinator::IdEpochType, int64_t>> &id_epoch_type_values);
988998

989999
private:
@@ -1206,6 +1216,25 @@ struct BrBackupWatchDogInfo {
12061216

12071217
~BrBackupWatchDogInfo() = default;
12081218
};
1219+
1220+
struct BrRestoreWatchDogInfo {
1221+
std::string restore_name;
1222+
std::string restore_path;
1223+
int64_t restore_start_timestamp;
1224+
int64_t restore_current_timestamp;
1225+
int64_t restore_timeout_s;
1226+
1227+
BrRestoreWatchDogInfo(const std::string &restore_name, const std::string &restore_path,
1228+
int64_t restore_start_timestamp, int64_t restore_current_timestamp, int64_t restore_timeout_s)
1229+
: restore_name(restore_name),
1230+
restore_path(restore_path),
1231+
restore_start_timestamp(restore_start_timestamp),
1232+
restore_current_timestamp(restore_current_timestamp),
1233+
restore_timeout_s(restore_timeout_s) {}
1234+
1235+
~BrRestoreWatchDogInfo() = default;
1236+
};
1237+
12091238
class BrWatchDogManager {
12101239
public:
12111240
static BrWatchDogManager *Instance();
@@ -1216,10 +1245,21 @@ class BrWatchDogManager {
12161245

12171246
butil::Status UnRegisterBackup(const std::string &backup_name);
12181247

1248+
butil::Status RegisterRestore(const std::string &restore_name, const std::string &restore_path,
1249+
int64_t restore_start_timestamp, int64_t restore_current_timestamp,
1250+
int64_t restore_timeout_s);
1251+
1252+
butil::Status UnRegisterRestore(const std::string &restore_name);
1253+
1254+
butil::Status GetBackupStatus(bool &is_backing_up, std::string &backup_name);
1255+
1256+
butil::Status GetRestoreStatus(bool &is_restoring, std::string &restore_name);
1257+
12191258
private:
12201259
BrWatchDogManager();
12211260
~BrWatchDogManager();
12221261
std::shared_ptr<BrBackupWatchDogInfo> br_backup_watch_dog_info_;
1262+
std::shared_ptr<BrRestoreWatchDogInfo> br_restore_watch_dog_info_;
12231263
bthread_mutex_t mutex_;
12241264
};
12251265

src/coordinator/coordinator_control_coor.cc

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7000,6 +7000,103 @@ butil::Status BrWatchDogManager::UnRegisterBackup(const std::string& backup_name
70007000
return butil::Status::OK();
70017001
}
70027002

7003+
butil::Status BrWatchDogManager::RegisterRestore(const std::string& restore_name, const std::string& restore_path,
7004+
int64_t restore_start_timestamp, int64_t restore_current_timestamp,
7005+
int64_t restore_timeout_s) {
7006+
BAIDU_SCOPED_LOCK(mutex_);
7007+
// check exist
7008+
if (!br_restore_watch_dog_info_) {
7009+
br_restore_watch_dog_info_ = std::make_shared<BrRestoreWatchDogInfo>(
7010+
restore_name, restore_path, restore_start_timestamp, restore_current_timestamp, restore_timeout_s);
7011+
} else {
7012+
// get current timestamp
7013+
int64_t current_timestamp = Helper::Timestamp();
7014+
// update
7015+
if (br_restore_watch_dog_info_->restore_name == restore_name) {
7016+
br_restore_watch_dog_info_->restore_current_timestamp = restore_current_timestamp;
7017+
} else { // restore name not match
7018+
if (current_timestamp >
7019+
(br_restore_watch_dog_info_->restore_current_timestamp + br_restore_watch_dog_info_->restore_timeout_s)) {
7020+
// timeout, reset
7021+
br_restore_watch_dog_info_ = std::make_shared<BrRestoreWatchDogInfo>(
7022+
restore_name, restore_path, restore_start_timestamp, restore_current_timestamp, restore_timeout_s);
7023+
} else {
7024+
// not timeout, update restore_current_timestamp
7025+
std::string s = fmt::format(
7026+
"register restore failed, restore exist. restore name not match, input restore_name={} not "
7027+
"match "
7028+
"current "
7029+
"already exist restore_name={}",
7030+
restore_name, br_restore_watch_dog_info_->restore_name);
7031+
DINGO_LOG(ERROR) << s;
7032+
return butil::Status(pb::error::ERESTORE_TASK_EXIST, s);
7033+
}
7034+
}
7035+
}
7036+
return butil::Status::OK();
7037+
}
7038+
7039+
butil::Status BrWatchDogManager::UnRegisterRestore(const std::string& restore_name) {
7040+
BAIDU_SCOPED_LOCK(mutex_);
7041+
// check exist
7042+
if (!br_restore_watch_dog_info_) {
7043+
return butil::Status::OK();
7044+
} else {
7045+
// restore name equal
7046+
if (br_restore_watch_dog_info_->restore_name == restore_name) {
7047+
br_restore_watch_dog_info_.reset();
7048+
} else { // restore name not match
7049+
std::string s = fmt::format(
7050+
"unregister restore failed. restore name not match, input restore_name={} not match current "
7051+
"already "
7052+
"exist "
7053+
"restore_name={}",
7054+
restore_name, br_restore_watch_dog_info_->restore_name);
7055+
DINGO_LOG(ERROR) << s;
7056+
return butil::Status(pb::error::ERESTORE_TASK_NAME_NOT_MATCH, s);
7057+
}
7058+
}
7059+
return butil::Status::OK();
7060+
}
7061+
7062+
butil::Status BrWatchDogManager::GetBackupStatus(bool& is_backing_up, std::string& backup_name) {
7063+
BAIDU_SCOPED_LOCK(mutex_);
7064+
if (!br_backup_watch_dog_info_) {
7065+
is_backing_up = false;
7066+
} else {
7067+
// get current timestamp
7068+
int64_t current_timestamp = Helper::Timestamp();
7069+
if (current_timestamp >
7070+
(br_restore_watch_dog_info_->restore_current_timestamp + br_restore_watch_dog_info_->restore_timeout_s)) {
7071+
// timeout
7072+
is_backing_up = false;
7073+
} else {
7074+
is_backing_up = true;
7075+
backup_name = br_backup_watch_dog_info_->backup_name;
7076+
}
7077+
}
7078+
return butil::Status::OK();
7079+
}
7080+
7081+
butil::Status BrWatchDogManager::GetRestoreStatus(bool& is_restoring, std::string& restore_name) {
7082+
BAIDU_SCOPED_LOCK(mutex_);
7083+
if (!br_restore_watch_dog_info_) {
7084+
is_restoring = false;
7085+
} else {
7086+
// get current timestamp
7087+
int64_t current_timestamp = Helper::Timestamp();
7088+
if (current_timestamp >
7089+
(br_restore_watch_dog_info_->restore_current_timestamp + br_restore_watch_dog_info_->restore_timeout_s)) {
7090+
// timeout
7091+
is_restoring = false;
7092+
} else {
7093+
is_restoring = true;
7094+
restore_name = br_restore_watch_dog_info_->restore_name;
7095+
}
7096+
}
7097+
return butil::Status::OK();
7098+
}
7099+
70037100
butil::Status CoordinatorControl::RegisterBackup(const std::string& backup_name, const std::string& backup_path,
70047101
int64_t backup_start_timestamp, int64_t backup_current_timestamp,
70057102
int64_t backup_timeout_s) {
@@ -7011,4 +7108,23 @@ butil::Status CoordinatorControl::UnRegisterBackup(const std::string& backup_nam
70117108
return BrWatchDogManager::Instance()->UnRegisterBackup(backup_name);
70127109
}
70137110

7111+
butil::Status CoordinatorControl::RegisterRestore(const std::string& restore_name, const std::string& restore_path,
7112+
int64_t restore_start_timestamp, int64_t restore_current_timestamp,
7113+
int64_t restore_timeout_s) {
7114+
return BrWatchDogManager::Instance()->RegisterRestore(restore_name, restore_path, restore_start_timestamp,
7115+
restore_current_timestamp, restore_timeout_s);
7116+
}
7117+
7118+
butil::Status CoordinatorControl::UnRegisterRestore(const std::string& restore_name) {
7119+
return BrWatchDogManager::Instance()->UnRegisterRestore(restore_name);
7120+
}
7121+
7122+
butil::Status CoordinatorControl::GetBackupStatus(bool& is_backing_up, std::string& backup_name) {
7123+
return BrWatchDogManager::Instance()->GetBackupStatus(is_backing_up, backup_name);
7124+
}
7125+
7126+
butil::Status CoordinatorControl::GetRestoreStatus(bool& is_restoring, std::string& restore_name) {
7127+
return BrWatchDogManager::Instance()->GetRestoreStatus(is_restoring, restore_name);
7128+
}
7129+
70147130
} // namespace dingodb

0 commit comments

Comments
 (0)