diff --git a/src/cmd/sql_cmd.h b/src/cmd/sql_cmd.h index 5cf1b99cd0c..2d941c65a35 100644 --- a/src/cmd/sql_cmd.h +++ b/src/cmd/sql_cmd.h @@ -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 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; diff --git a/src/sdk/sql_cluster_router.cc b/src/sdk/sql_cluster_router.cc index 296cd3d5755..eea62b508ff 100644 --- a/src/sdk/sql_cluster_router.cc +++ b/src/sdk/sql_cluster_router.cc @@ -4279,7 +4279,7 @@ static const std::initializer_list 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