Skip to content

Commit

Permalink
feat: check table status when CLI login (#3506)
Browse files Browse the repository at this point in the history
* feat: check table status when CLI login

* fix
  • Loading branch information
vagetablechicken authored Oct 11, 2023
1 parent 2224a6c commit 1386632
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 3 deletions.
49 changes: 47 additions & 2 deletions src/cmd/sql_cmd.h
Original file line number Diff line number Diff line change
Expand Up @@ -141,14 +141,59 @@ std::string ExecFetch(const std::string& sql) {
return ss.str();
}

void HandleSQL(const std::string& sql) {
std::cout << ExecFetch(sql);
void HandleSQL(const std::string& sql) { std::cout << ExecFetch(sql); }

std::string SafeGetString(std::shared_ptr<hybridse::sdk::ResultSet> rs, int idx) {
std::string tmp;
if (!rs->GetString(idx, &tmp)) {
LOG(WARNING) << "fail to get string in col " << idx;
return "";
}
return tmp;
}

bool CheckAllTableStatus() {
hybridse::sdk::Status status;
auto rs = sr->ExecuteSQL("show table status like '%'", &status);
bool is_ok = true;
if (status.IsOK()) {
// ref GetTableStatusSchema, just use idx to get column
while (rs->Next()) {
auto table = SafeGetString(rs, 1);
auto db = SafeGetString(rs, 2);
auto pu = SafeGetString(rs, 8);
auto warnings = SafeGetString(rs, 13);
std::string msg = absl::StrCat("table ", db, ".", table, " is broken, `show table status` to check detail");
bool is_broken = false;
if (pu != "0") {
is_broken = true;
msg.append(", unalive partition: ").append(pu);
}
if (!warnings.empty()) {
is_broken = true;
msg.append(", warning preview: ").append(warnings.substr(0, 100));
}
if (is_broken) {
is_ok = false;
std::cout << "ERROR: " << msg << std::endl;
}
}
} else {
std::cout << "ERROR: fail to get all table status, " << status.ToString() << std::endl;
is_ok = false;
}
return is_ok;
}

// cluster mode if zk_cluster is not empty, otherwise standalone mode
void Shell() {
DCHECK(cs);
DCHECK(sr);
// before all, check all table status
if (!CheckAllTableStatus()) {
std::cout << "HINT: Use `openmldb_tool inspect` to get full report." << std::endl;
}

// If use FLAGS_cmd, non-interactive. No Logo and make sure router interactive is false
if (!FLAGS_cmd.empty()) {
std::string db = FLAGS_database;
Expand Down
2 changes: 1 addition & 1 deletion src/sdk/sql_cluster_router.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4279,7 +4279,7 @@ static const std::initializer_list<std::string> GetTableStatusSchema() {
// 1. memory: binlog + snapshot
// 2. SSD/HDD: binlog + rocksdb data (sst files), wal files and checkpoints are not included
// - Partition: partition number
// - partition_unalive: partition number that is unalive
// - Partition_unalive: partition number that is unalive
// - Replica: replica number
// - Offline_path: data path for offline data
// - Offline_format: format for offline data
Expand Down

0 comments on commit 1386632

Please sign in to comment.