From 1ad02d8a3298f5898851dbc330f9ab02a19c30e5 Mon Sep 17 00:00:00 2001 From: aceforeverd Date: Tue, 26 Mar 2024 12:17:52 +0000 Subject: [PATCH] fix(tests): sql_cmd_test 1. recycle_bin_root_path does not setted up randomly and get cleaned up after test down. 2. UTs should ensure cleanup even test fails, missed cleanup might affect subsequent tests 3. cleanup tmp dirs even on signal 4. workaround: standalone & cluster env require distinct db_root_path 5. fix sql_cluster_test --- hybridse/src/planv2/planner_v2.cc | 1 - src/apiserver/api_server_test.cc | 2 + src/catalog/distribute_iterator_test.cc | 1 + src/cmd/single_tablet_test.cc | 2 + src/cmd/sql_cmd_test.cc | 77 +++++++++---------- .../name_server_create_remote_test.cc | 1 + src/nameserver/name_server_test.cc | 5 +- src/nameserver/new_server_env_test.cc | 1 + src/nameserver/standalone_test.cc | 4 +- src/nameserver/system_table_test.cc | 4 +- src/replica/binlog_test.cc | 1 + src/replica/snapshot_replica_test.cc | 4 +- src/sdk/db_sdk_test.cc | 2 + src/sdk/mini_cluster.h | 12 +-- src/sdk/mini_cluster_batch_bm.cc | 2 + src/sdk/mini_cluster_request_bm.cc | 2 + src/sdk/sql_cluster_test.cc | 10 ++- src/sdk/sql_router_test.cc | 2 + src/sdk/sql_sdk_test.cc | 1 + src/sdk/sql_standalone_sdk_test.cc | 3 +- src/storage/aggregator_test.cc | 1 + src/storage/disk_table_snapshot_test.cc | 3 +- src/storage/disk_table_test.cc | 4 +- src/storage/snapshot_test.cc | 1 + src/tablet/sql_cluster_availability_test.cc | 1 + src/tablet/tablet_impl_func_test.cc | 5 +- src/tablet/tablet_impl_keep_alive_test.cc | 3 +- src/tablet/tablet_impl_mem_test.cc | 3 +- src/tablet/tablet_impl_multi_path_test.cc | 34 +------- src/tablet/tablet_impl_projection_test.cc | 38 +-------- src/tablet/tablet_impl_test.cc | 18 +---- src/test/util.cc | 77 ++++++++++++++++++- src/test/util.h | 7 +- 33 files changed, 162 insertions(+), 170 deletions(-) diff --git a/hybridse/src/planv2/planner_v2.cc b/hybridse/src/planv2/planner_v2.cc index bc18c262a00..81c8ac8cfc0 100644 --- a/hybridse/src/planv2/planner_v2.cc +++ b/hybridse/src/planv2/planner_v2.cc @@ -33,7 +33,6 @@ base::Status SimplePlannerV2::CreateASTScriptPlan(const zetasql::ASTScript *scri CHECK_STATUS(ConvertASTScript(script, node_manager_, &resolved_trees)); CHECK_TRUE(nullptr != resolved_trees && resolved_trees->GetSize() > 0, common::kPlanError, "fail to create plan, sql trees is null or empty"); - DLOG(INFO) << "parsed SqlNode:\n" << resolved_trees->GetTreeString(); CHECK_STATUS(CreatePlanTree(resolved_trees->GetList(), plan_trees)); DLOG(INFO) << "PlanNode:"; for (decltype(plan_trees.size()) i = 0; i < plan_trees.size(); ++i) { diff --git a/src/apiserver/api_server_test.cc b/src/apiserver/api_server_test.cc index 45435b3b0e0..c0a8e581603 100644 --- a/src/apiserver/api_server_test.cc +++ b/src/apiserver/api_server_test.cc @@ -27,6 +27,7 @@ #include "rapidjson/error/en.h" #include "rapidjson/rapidjson.h" #include "sdk/mini_cluster.h" +#include "test/util.h" DEFINE_int32(zk_port, 6181, "zk port"); DEFINE_string(api_server_port, "8084", "api server port"); @@ -1412,5 +1413,6 @@ int main(int argc, char* argv[]) { ::testing::InitGoogleTest(&argc, argv); ::google::ParseCommandLineFlags(&argc, &argv, true); ::openmldb::base::SetupGlog(true); + ::openmldb::test::InitRandomDiskFlags("api_server_test"); return RUN_ALL_TESTS(); } diff --git a/src/catalog/distribute_iterator_test.cc b/src/catalog/distribute_iterator_test.cc index 49e4958101f..d6e7a88b5ec 100644 --- a/src/catalog/distribute_iterator_test.cc +++ b/src/catalog/distribute_iterator_test.cc @@ -1011,5 +1011,6 @@ TEST_F(DistributeIteratorTest, IteratorZero) { int main(int argc, char **argv) { ::testing::InitGoogleTest(&argc, argv); + ::openmldb::test::InitRandomDiskFlags("distribute_iterator_test"); return RUN_ALL_TESTS(); } diff --git a/src/cmd/single_tablet_test.cc b/src/cmd/single_tablet_test.cc index bfe3ccedba4..52a3b80bb26 100644 --- a/src/cmd/single_tablet_test.cc +++ b/src/cmd/single_tablet_test.cc @@ -227,6 +227,8 @@ int main(int argc, char** argv) { ::hybridse::vm::Engine::InitializeGlobalLLVM(); ::testing::InitGoogleTest(&argc, argv); ::google::ParseCommandLineFlags(&argc, &argv, true); + ::openmldb::test::InitRandomDiskFlags("single_tablet_test"); + FLAGS_zk_session_timeout = 100000; FLAGS_enable_distsql = true; mc.SetUp(1); diff --git a/src/cmd/sql_cmd_test.cc b/src/cmd/sql_cmd_test.cc index 7741b14a1f4..149fdf9ab2b 100644 --- a/src/cmd/sql_cmd_test.cc +++ b/src/cmd/sql_cmd_test.cc @@ -43,10 +43,6 @@ DECLARE_string(host); DECLARE_int32(port); DECLARE_uint32(traverse_cnt_limit); -DECLARE_string(ssd_root_path); -DECLARE_string(hdd_root_path); -DECLARE_string(recycle_bin_ssd_root_path); -DECLARE_string(recycle_bin_hdd_root_path); DECLARE_uint32(get_table_status_interval); ::openmldb::sdk::StandaloneEnv env; @@ -1129,15 +1125,25 @@ TEST_P(DBSDKTest, DeployWithBias) { TEST_P(DBSDKTest, Truncate) { auto cli = GetParam(); sr = cli->sr; - std::string db_name = "test2"; - std::string table_name = "test1"; - std::string ddl = "create table test1 (c1 string, c2 int, c3 bigint, INDEX(KEY=c1, ts=c3));"; + std::string db_name = absl::StrCat("db_", GenRand()); + std::string table_name = absl::StrCat("tb_", GenRand()); + std::string ddl = + absl::Substitute("create table $0 (c1 string, c2 int, c3 bigint, INDEX(KEY=c1, ts=c3))", table_name); ProcessSQLs(sr, { "set @@execute_mode = 'online'", absl::StrCat("create database ", db_name, ";"), absl::StrCat("use ", db_name, ";"), ddl, }); + absl::Cleanup c = [&]() { + ProcessSQLs(sr, { + absl::StrCat("use ", db_name, ";"), + absl::StrCat("drop table ", table_name), + absl::StrCat("drop database ", db_name), + }); + }; + + absl::SleepFor(absl::Seconds(5)); hybridse::sdk::Status status; sr->ExecuteSQL(absl::StrCat("truncate table ", table_name, ";"), &status); ASSERT_TRUE(status.IsOK()) << status.ToString(); @@ -1150,6 +1156,7 @@ TEST_P(DBSDKTest, Truncate) { sr->ExecuteSQL(absl::StrCat("insert into ", table_name, " values ('", key, "', 11, ", ts, ");"), &status); } } + absl::SleepFor(absl::Seconds(5)); res = sr->ExecuteSQL(absl::StrCat("select * from ", table_name, ";"), &status); ASSERT_EQ(res->Size(), 100); @@ -1160,11 +1167,6 @@ TEST_P(DBSDKTest, Truncate) { sr->ExecuteSQL(absl::StrCat("insert into ", table_name, " values ('aa', 11, 100);"), &status); res = sr->ExecuteSQL(absl::StrCat("select * from ", table_name, ";"), &status); ASSERT_EQ(res->Size(), 1); - ProcessSQLs(sr, { - absl::StrCat("use ", db_name, ";"), - absl::StrCat("drop table ", table_name), - absl::StrCat("drop database ", db_name), - }); } TEST_P(DBSDKTest, DeletetRange) { @@ -3458,6 +3460,7 @@ TEST_P(DBSDKTest, MapTypeTable) { }); absl::Cleanup clean = [&]() { ProcessSQLs(sr, { + absl::StrCat("use ", db), absl::Substitute("drop table $0", table), absl::Substitute("drop database $0", db), }); @@ -3636,6 +3639,11 @@ TEST_P(DBSDKTest, ShowTableStatusUnderRoot) { }); // reset to empty db sr->SetDatabase(""); + absl::Cleanup c = [&]() { + ProcessSQLs(sr, {absl::StrCat("use ", db_name), absl::StrCat("drop table ", tb_name), + absl::StrCat("drop database ", db_name)}); + sr->SetDatabase(""); + }; // sleep for 4s, name server should updated TableInfo in schedule absl::SleepFor(absl::Seconds(4)); @@ -3656,11 +3664,6 @@ TEST_P(DBSDKTest, ShowTableStatusUnderRoot) { } // runs HandleSQL only for the purpose of pretty print result in console HandleSQL("show table status"); - - // teardown - ProcessSQLs(sr, {absl::StrCat("use ", db_name), absl::StrCat("drop table ", tb_name), - absl::StrCat("drop database ", db_name)}); - sr->SetDatabase(""); } // show table status with patterns when no database is selected @@ -3683,6 +3686,11 @@ TEST_P(DBSDKTest, ShowTableStatusLike) { }); // reset to empty db sr->SetDatabase(""); + absl::Cleanup c = [&]() { + ProcessSQLs(sr, {absl::StrCat("use ", db_name), absl::StrCat("drop table ", tb_name), + absl::StrCat("drop database ", db_name)}); + sr->SetDatabase(""); + }; // sleep for 4s, name server should updated TableInfo in schedule absl::SleepFor(absl::Seconds(4)); @@ -3753,11 +3761,6 @@ TEST_P(DBSDKTest, ShowTableStatusLike) { {{{}, tb_name, db_name, "memory", "1", {{}, "0"}, {{}, "0"}, "1", "0", "1", "NULL", "NULL", "NULL", ""}}, rs.get(), true, false); } - - // teardown - ProcessSQLs(sr, {absl::StrCat("use ", db_name), absl::StrCat("drop table ", tb_name), - absl::StrCat("drop database ", db_name)}); - sr->SetDatabase(""); } TEST_P(DBSDKTest, ShowTableStatusForHddTable) { @@ -4003,6 +4006,17 @@ TEST_F(SqlCmdTest, SelectWithAddNewIndex) { absl::StrCat("CREATE INDEX index1 ON ", db1_name, ".", tb1_name, " (c2) OPTIONS (ttl=10m, ttl_type=absolute);"), }); + absl::Cleanup clean = [&]() { + ProcessSQLs(sr, { + absl::StrCat("use ", db1_name, ";"), + absl::StrCat("drop table ", tb1_name), + absl::StrCat("drop database ", db1_name), + absl::StrCat("drop database ", db2_name), + }); + + sr->SetDatabase(""); + }; + absl::SleepFor(absl::Seconds(10)); hybridse::sdk::Status status; auto res = sr->ExecuteSQL(absl::StrCat("use ", db1_name, ";"), &status); @@ -4016,14 +4030,6 @@ TEST_F(SqlCmdTest, SelectWithAddNewIndex) { absl::SleepFor(absl::Seconds(2)); res = sr->ExecuteSQL(absl::StrCat("select id,c1,c2,c3 from ", tb1_name, " where c2=1;"), &status); ASSERT_EQ(res->Size(), 3); - - ProcessSQLs(sr, { - absl::StrCat("use ", db1_name, ";"), - absl::StrCat("drop table ", tb1_name), - absl::StrCat("drop database ", db1_name), - }); - - sr->SetDatabase(""); } // -------------------------------------------------------------------------------------- @@ -4205,17 +4211,8 @@ int main(int argc, char** argv) { FLAGS_traverse_cnt_limit = 500; FLAGS_zk_session_timeout = 100000; FLAGS_get_table_status_interval = 1000; - // enable disk table flags - std::filesystem::path tmp_path = std::filesystem::temp_directory_path() / "openmldb"; - absl::Cleanup clean = [&tmp_path]() { std::filesystem::remove_all(tmp_path); }; - const std::string& tmp_path_str = tmp_path.string(); - FLAGS_ssd_root_path = absl::StrCat(tmp_path_str, "/ssd_root_random_", ::openmldb::test::GenRand()); - FLAGS_hdd_root_path = absl::StrCat(tmp_path_str, "/hdd_root_random_", ::openmldb::test::GenRand()); - FLAGS_recycle_bin_hdd_root_path = - absl::StrCat(tmp_path_str, "/recycle_hdd_root_random_", ::openmldb::test::GenRand()); - FLAGS_recycle_bin_ssd_root_path = - absl::StrCat(tmp_path_str, "/recycle_ssd_root_random_", ::openmldb::test::GenRand()); + ::openmldb::test::InitRandomDiskFlags("sql_cmd_test"); ::openmldb::sdk::MiniCluster mc(6181); ::openmldb::cmd::mc_ = &mc; diff --git a/src/nameserver/name_server_create_remote_test.cc b/src/nameserver/name_server_create_remote_test.cc index def3d1d0a07..5b6d1a7a05d 100644 --- a/src/nameserver/name_server_create_remote_test.cc +++ b/src/nameserver/name_server_create_remote_test.cc @@ -1359,6 +1359,7 @@ int main(int argc, char** argv) { srand(time(NULL)); ::openmldb::base::SetLogLevel(INFO); ::google::ParseCommandLineFlags(&argc, &argv, true); + ::openmldb::test::InitRandomDiskFlags("name_server_create_remote_test"); FLAGS_system_table_replica_num = 0; return RUN_ALL_TESTS(); } diff --git a/src/nameserver/name_server_test.cc b/src/nameserver/name_server_test.cc index e01f2f5c792..3799873d597 100644 --- a/src/nameserver/name_server_test.cc +++ b/src/nameserver/name_server_test.cc @@ -1291,10 +1291,7 @@ int main(int argc, char** argv) { ::openmldb::base::SetLogLevel(INFO); ::google::ParseCommandLineFlags(&argc, &argv, true); FLAGS_zk_cluster = "127.0.0.1:6181"; - ::openmldb::test::TempPath tmp_path; - FLAGS_db_root_path = tmp_path.GetTempPath("memory"); - FLAGS_ssd_root_path = tmp_path.GetTempPath("ssd"); - FLAGS_hdd_root_path = tmp_path.GetTempPath("hdd"); + ::openmldb::test::InitRandomDiskFlags("name_server_test"); FLAGS_system_table_replica_num = 0; return RUN_ALL_TESTS(); } diff --git a/src/nameserver/new_server_env_test.cc b/src/nameserver/new_server_env_test.cc index 405e3f436e0..30a83f268b2 100644 --- a/src/nameserver/new_server_env_test.cc +++ b/src/nameserver/new_server_env_test.cc @@ -358,6 +358,7 @@ int main(int argc, char** argv) { srand(time(NULL)); ::openmldb::base::SetLogLevel(INFO); ::google::ParseCommandLineFlags(&argc, &argv, true); + ::openmldb::test::InitRandomDiskFlags("new_server_env_test"); FLAGS_system_table_replica_num = 0; return RUN_ALL_TESTS(); } diff --git a/src/nameserver/standalone_test.cc b/src/nameserver/standalone_test.cc index cb37293cea9..34c0244ed9c 100644 --- a/src/nameserver/standalone_test.cc +++ b/src/nameserver/standalone_test.cc @@ -115,8 +115,6 @@ int main(int argc, char** argv) { ::testing::InitGoogleTest(&argc, argv); ::openmldb::base::SetLogLevel(INFO); ::google::ParseCommandLineFlags(&argc, &argv, true); - ::openmldb::test::TempPath tmp_path; - FLAGS_db_root_path = tmp_path.GetTempPath(); - FLAGS_hdd_root_path = tmp_path.GetTempPath(); + ::openmldb::test::InitRandomDiskFlags("standalone_test"); return RUN_ALL_TESTS(); } diff --git a/src/nameserver/system_table_test.cc b/src/nameserver/system_table_test.cc index eadca079c93..07839adcc05 100644 --- a/src/nameserver/system_table_test.cc +++ b/src/nameserver/system_table_test.cc @@ -35,7 +35,6 @@ DECLARE_string(zk_cluster); DECLARE_string(zk_root_path); -DECLARE_string(db_root_path); DECLARE_int32(zk_session_timeout); namespace openmldb { @@ -107,7 +106,6 @@ int main(int argc, char** argv) { srand(time(NULL)); ::openmldb::base::SetLogLevel(INFO); ::google::ParseCommandLineFlags(&argc, &argv, true); - ::openmldb::test::TempPath tmp_path; - FLAGS_db_root_path = tmp_path.GetTempPath(); + ::openmldb::test::InitRandomDiskFlags("system_table_test"); return RUN_ALL_TESTS(); } diff --git a/src/replica/binlog_test.cc b/src/replica/binlog_test.cc index 797514c76bd..e24cc6c511e 100644 --- a/src/replica/binlog_test.cc +++ b/src/replica/binlog_test.cc @@ -126,6 +126,7 @@ int main(int argc, char** argv) { srand(time(NULL)); ::openmldb::base::SetLogLevel(DEBUG); ::google::ParseCommandLineFlags(&argc, &argv, true); + ::openmldb::test::InitRandomDiskFlags("binlog_test"); int ret = 0; std::vector vec{"off", "zlib", "snappy"}; ::openmldb::test::TempPath tmp_path; diff --git a/src/replica/snapshot_replica_test.cc b/src/replica/snapshot_replica_test.cc index 64b8c2565ff..a9f3712a335 100644 --- a/src/replica/snapshot_replica_test.cc +++ b/src/replica/snapshot_replica_test.cc @@ -642,8 +642,6 @@ int main(int argc, char** argv) { srand(time(NULL)); ::openmldb::base::SetLogLevel(INFO); ::google::ParseCommandLineFlags(&argc, &argv, true); - ::openmldb::test::TempPath temp_path; - FLAGS_db_root_path = temp_path.GetTempPath(); - FLAGS_hdd_root_path = temp_path.GetTempPath(); + ::openmldb::test::InitRandomDiskFlags("snapshot_replica_test"); return RUN_ALL_TESTS(); } diff --git a/src/sdk/db_sdk_test.cc b/src/sdk/db_sdk_test.cc index bdd76cec47f..eac29948bda 100644 --- a/src/sdk/db_sdk_test.cc +++ b/src/sdk/db_sdk_test.cc @@ -29,6 +29,7 @@ #include "proto/tablet.pb.h" #include "proto/type.pb.h" #include "sdk/mini_cluster.h" +#include "test/util.h" namespace openmldb::sdk { @@ -148,5 +149,6 @@ int main(int argc, char** argv) { FLAGS_zk_session_timeout = 100000; srand(time(nullptr)); ::openmldb::base::SetupGlog(true); + ::openmldb::test::InitRandomDiskFlags("db_sdk_test"); return RUN_ALL_TESTS(); } diff --git a/src/sdk/mini_cluster.h b/src/sdk/mini_cluster.h index 439a311f243..f6d0bbc950f 100644 --- a/src/sdk/mini_cluster.h +++ b/src/sdk/mini_cluster.h @@ -38,6 +38,7 @@ #include "rpc/rpc_client.h" #include "sdk/db_sdk.h" #include "tablet/tablet_impl.h" +#include "test/util.h" DECLARE_string(endpoint); DECLARE_string(tablet); @@ -75,7 +76,6 @@ class MiniCluster { if (ns_client_) { delete ns_client_; } - base::RemoveDirRecursive(db_root_path_); } bool SetUp(int tablet_num = 2) { @@ -88,8 +88,6 @@ class MiniCluster { FLAGS_get_table_diskused_interval = 2000; FLAGS_sync_deploy_stats_timeout = 2000; srand(time(NULL)); - db_root_path_ = "/tmp/mini_cluster" + GenRand(); - FLAGS_db_root_path = db_root_path_; zk_cluster_ = "127.0.0.1:" + std::to_string(zk_port_); FLAGS_zk_cluster = zk_cluster_; std::string ns_endpoint = "127.0.0.1:" + GenRand(); @@ -220,7 +218,6 @@ class MiniCluster { ::openmldb::client::NsClient* ns_client_; std::map tablets_; std::map tb_clients_; - std::string db_root_path_; }; class StandaloneEnv { @@ -233,13 +230,13 @@ class StandaloneEnv { if (ns_client_) { delete ns_client_; } - base::RemoveDirRecursive(db_root_path_); } bool SetUp() { srand(time(nullptr)); - db_root_path_ = "/tmp/standalone_env" + std::to_string(GenRand()); - FLAGS_db_root_path = db_root_path_; + // shit happens, cluster & standalone require distinct db_root_path + test::TempPath tmp; + FLAGS_db_root_path = tmp.GetTempPath(); if (!StartTablet(&tb_server_)) { LOG(WARNING) << "fail to start tablet"; return false; @@ -333,7 +330,6 @@ class StandaloneEnv { uint64_t ns_port_ = 0; ::openmldb::client::NsClient* ns_client_; ::openmldb::client::TabletClient* tb_client_; - std::string db_root_path_; }; } // namespace sdk diff --git a/src/sdk/mini_cluster_batch_bm.cc b/src/sdk/mini_cluster_batch_bm.cc index 296fd739021..97c143f62fb 100644 --- a/src/sdk/mini_cluster_batch_bm.cc +++ b/src/sdk/mini_cluster_batch_bm.cc @@ -27,6 +27,7 @@ #include "sdk/sql_router.h" #include "sdk/table_reader.h" #include "test/base_test.h" +#include "test/util.h" #include "vm/catalog.h" DECLARE_bool(enable_distsql); @@ -895,6 +896,7 @@ int main(int argc, char** argv) { ::google::ParseCommandLineFlags(&argc, &argv, true); ::openmldb::base::SetupGlog(true); ::hybridse::vm::Engine::InitializeGlobalLLVM(); + ::openmldb::test::InitRandomDiskFlags("mini_cluster_batch_bm"); FLAGS_enable_distsql = hybridse::sqlcase::SqlCase::IsCluster(); FLAGS_enable_localtablet = !hybridse::sqlcase::SqlCase::IsDisableLocalTablet(); ::benchmark::Initialize(&argc, argv); diff --git a/src/sdk/mini_cluster_request_bm.cc b/src/sdk/mini_cluster_request_bm.cc index a0628f3b96d..2bba15ee75e 100644 --- a/src/sdk/mini_cluster_request_bm.cc +++ b/src/sdk/mini_cluster_request_bm.cc @@ -19,6 +19,7 @@ #include "benchmark/benchmark.h" #include "sdk/mini_cluster.h" #include "sdk/mini_cluster_bm.h" +#include "test/util.h" DECLARE_bool(enable_distsql); DECLARE_bool(enable_localtablet); ::openmldb::sdk::MiniCluster* mc; @@ -56,6 +57,7 @@ int main(int argc, char** argv) { ::google::ParseCommandLineFlags(&argc, &argv, true); ::openmldb::base::SetupGlog(true); ::hybridse::vm::Engine::InitializeGlobalLLVM(); + ::openmldb::test::InitRandomDiskFlags("min_cluster_request_bm"); FLAGS_enable_distsql = hybridse::sqlcase::SqlCase::IsCluster(); FLAGS_enable_localtablet = !hybridse::sqlcase::SqlCase::IsDisableLocalTablet(); ::benchmark::Initialize(&argc, argv); diff --git a/src/sdk/sql_cluster_test.cc b/src/sdk/sql_cluster_test.cc index ee421a27ecb..d32841bb461 100644 --- a/src/sdk/sql_cluster_test.cc +++ b/src/sdk/sql_cluster_test.cc @@ -15,6 +15,7 @@ */ #include + #include #include #include @@ -22,6 +23,7 @@ #include "absl/strings/match.h" #include "absl/strings/str_cat.h" +#include "absl/time/clock.h" #include "base/glog_wrapper.h" #include "codec/fe_row_codec.h" #include "gflags/gflags.h" @@ -31,9 +33,9 @@ #include "sdk/sql_cluster_router.h" #include "sdk/sql_router.h" #include "sdk/sql_sdk_test.h" +#include "test/util.h" #include "vm/catalog.h" - DECLARE_uint32(max_traverse_cnt); DECLARE_uint32(traverse_cnt_limit); @@ -446,7 +448,7 @@ TEST_F(SQLClusterTest, ClusterInsert) { std::string ddl = "create table " + name + "(" "col1 string, col2 bigint," - "index(key=col1, ts=col2)) options(partitionnum=8);"; + "index(key=col1, ts=col2)) options(partitionnum=8, replicanum = 2);"; ok = router->ExecuteDDL(db, ddl, &status); ASSERT_TRUE(ok); ASSERT_TRUE(router->RefreshCatalog()); @@ -459,6 +461,7 @@ TEST_F(SQLClusterTest, ClusterInsert) { uint32_t pid = static_cast(::openmldb::base::hash64(key) % 8); key_map[pid].push_back(key); } + absl::SleepFor(absl::Seconds(5)); std::vector<::openmldb::nameserver::TableInfo> tables; auto ns = mc_->GetNsClient(); auto ret = ns->ShowDBTable(db, &tables); @@ -482,7 +485,7 @@ TEST_F(SQLClusterTest, ClusterInsert) { } } } - ASSERT_EQ(100u, count); + ASSERT_EQ(2* 100u, count); ok = router->ExecuteDDL(db, "drop table " + name + ";", &status); ASSERT_TRUE(ok); ok = router->DropDB(db, &status); @@ -1398,6 +1401,7 @@ int main(int argc, char** argv) { ::testing::InitGoogleTest(&argc, argv); ::google::ParseCommandLineFlags(&argc, &argv, true); ::hybridse::vm::Engine::InitializeGlobalLLVM(); + ::openmldb::test::InitRandomDiskFlags("sql_cluster_test"); FLAGS_zk_session_timeout = 100000; ::openmldb::base::SetupGlog(true); diff --git a/src/sdk/sql_router_test.cc b/src/sdk/sql_router_test.cc index daa2a1ed059..34f6f24b688 100644 --- a/src/sdk/sql_router_test.cc +++ b/src/sdk/sql_router_test.cc @@ -29,6 +29,7 @@ #include "gflags/gflags.h" #include "gtest/gtest.h" #include "sdk/mini_cluster.h" +#include "test/util.h" #include "vm/catalog.h" namespace openmldb::sdk { @@ -1295,6 +1296,7 @@ int main(int argc, char** argv) { ::testing::InitGoogleTest(&argc, argv); ::google::ParseCommandLineFlags(&argc, &argv, true); ::hybridse::vm::Engine::InitializeGlobalLLVM(); + ::openmldb::test::InitRandomDiskFlags("sql_router_test"); ::openmldb::base::SetupGlog(true); FLAGS_zk_session_timeout = 100000; diff --git a/src/sdk/sql_sdk_test.cc b/src/sdk/sql_sdk_test.cc index 4b6a67de11f..70d33a1fed1 100644 --- a/src/sdk/sql_sdk_test.cc +++ b/src/sdk/sql_sdk_test.cc @@ -927,6 +927,7 @@ int main(int argc, char** argv) { ::google::ParseCommandLineFlags(&argc, &argv, true); ::hybridse::vm::Engine::InitializeGlobalLLVM(); ::openmldb::base::SetupGlog(true); + ::openmldb::test::InitRandomDiskFlags("sql_sdk_test"); srand(time(NULL)); FLAGS_zk_session_timeout = 100000; diff --git a/src/sdk/sql_standalone_sdk_test.cc b/src/sdk/sql_standalone_sdk_test.cc index 2b2a4cc2be8..b7f56b76055 100644 --- a/src/sdk/sql_standalone_sdk_test.cc +++ b/src/sdk/sql_standalone_sdk_test.cc @@ -33,12 +33,12 @@ #include "sdk/sql_cluster_router.h" #include "sdk/db_sdk.h" #include "test/base_test.h" +#include "test/util.h" #include "vm/catalog.h" namespace openmldb { namespace sdk { -MiniCluster* mc_ = nullptr; std::shared_ptr router_ = std::shared_ptr(); ::openmldb::sdk::DBSDK * cs_; /// TODO(cj): replace rtidb-unsupport with performance-sensitive-unsupport @@ -876,6 +876,7 @@ int main(int argc, char** argv) { ::testing::InitGoogleTest(&argc, argv); ::google::ParseCommandLineFlags(&argc, &argv, true); ::hybridse::vm::Engine::InitializeGlobalLLVM(); + ::openmldb::test::InitRandomDiskFlags("sql_standalone_sdk_test"); srand(time(NULL)); ::openmldb::base::SetupGlog(true); diff --git a/src/storage/aggregator_test.cc b/src/storage/aggregator_test.cc index 2fa9299c6f2..7eb4b15d800 100644 --- a/src/storage/aggregator_test.cc +++ b/src/storage/aggregator_test.cc @@ -1145,5 +1145,6 @@ TEST_F(AggregatorTest, FlushAll) { int main(int argc, char** argv) { ::testing::InitGoogleTest(&argc, argv); + ::openmldb::test::InitRandomDiskFlags("aggregator_test"); return RUN_ALL_TESTS(); } diff --git a/src/storage/disk_table_snapshot_test.cc b/src/storage/disk_table_snapshot_test.cc index f093f222b11..c8cd052141d 100644 --- a/src/storage/disk_table_snapshot_test.cc +++ b/src/storage/disk_table_snapshot_test.cc @@ -112,7 +112,6 @@ TEST_F(SnapshotTest, MakeSnapshot) { int main(int argc, char** argv) { ::testing::InitGoogleTest(&argc, argv); ::google::ParseCommandLineFlags(&argc, &argv, true); - ::openmldb::test::TempPath tmp_path; - FLAGS_hdd_root_path = tmp_path.GetTempPath(); + ::openmldb::test::InitRandomDiskFlags("disk_table_snapshot_test"); return RUN_ALL_TESTS(); } diff --git a/src/storage/disk_table_test.cc b/src/storage/disk_table_test.cc index 39c3feb6034..f78518b1b37 100644 --- a/src/storage/disk_table_test.cc +++ b/src/storage/disk_table_test.cc @@ -1341,8 +1341,6 @@ TEST_F(DiskTableTest, CheckPoint) { int main(int argc, char** argv) { ::testing::InitGoogleTest(&argc, argv); ::openmldb::base::SetLogLevel(INFO); - ::openmldb::test::TempPath tmp_path; - FLAGS_hdd_root_path = tmp_path.GetTempPath(); - FLAGS_ssd_root_path = tmp_path.GetTempPath(); + ::openmldb::test::InitRandomDiskFlags("disk_table_test"); return RUN_ALL_TESTS(); } diff --git a/src/storage/snapshot_test.cc b/src/storage/snapshot_test.cc index e9dd679eafc..7b40bbf0e43 100644 --- a/src/storage/snapshot_test.cc +++ b/src/storage/snapshot_test.cc @@ -1847,6 +1847,7 @@ int main(int argc, char** argv) { srand(time(NULL)); ::google::ParseCommandLineFlags(&argc, &argv, true); ::openmldb::base::SetLogLevel(DEBUG); + ::openmldb::test::InitRandomDiskFlags("snapshot_test"); int ret = 0; std::vector vec{"off", "zlib", "snappy"}; ::openmldb::test::TempPath tmp_path; diff --git a/src/tablet/sql_cluster_availability_test.cc b/src/tablet/sql_cluster_availability_test.cc index 267b9144a3d..aef00e4c851 100644 --- a/src/tablet/sql_cluster_availability_test.cc +++ b/src/tablet/sql_cluster_availability_test.cc @@ -421,6 +421,7 @@ int main(int argc, char** argv) { srand(time(NULL)); ::openmldb::base::SetLogLevel(INFO); ::google::ParseCommandLineFlags(&argc, &argv, true); + ::openmldb::test::InitRandomDiskFlags("sql_cluster_availability_test"); FLAGS_system_table_replica_num = 0; return RUN_ALL_TESTS(); } diff --git a/src/tablet/tablet_impl_func_test.cc b/src/tablet/tablet_impl_func_test.cc index c07084a396d..634c99a867f 100644 --- a/src/tablet/tablet_impl_func_test.cc +++ b/src/tablet/tablet_impl_func_test.cc @@ -399,9 +399,6 @@ INSTANTIATE_TEST_SUITE_P(TabletMemAndHDD, TabletFuncTest, int main(int argc, char** argv) { ::testing::InitGoogleTest(&argc, argv); srand(time(NULL)); - ::openmldb::test::TempPath tmp_path; - FLAGS_db_root_path = tmp_path.GetTempPath(); - FLAGS_ssd_root_path = tmp_path.GetTempPath("ssd"); - FLAGS_hdd_root_path = tmp_path.GetTempPath("hdd"); + ::openmldb::test::InitRandomDiskFlags("tablet_impl_func_test"); return RUN_ALL_TESTS(); } diff --git a/src/tablet/tablet_impl_keep_alive_test.cc b/src/tablet/tablet_impl_keep_alive_test.cc index 7339ca80607..a6e0fd1f5ec 100644 --- a/src/tablet/tablet_impl_keep_alive_test.cc +++ b/src/tablet/tablet_impl_keep_alive_test.cc @@ -91,7 +91,6 @@ int main(int argc, char** argv) { srand(time(NULL)); ::openmldb::base::SetLogLevel(DEBUG); ::google::ParseCommandLineFlags(&argc, &argv, true); - ::openmldb::test::TempPath tmp_path; - FLAGS_db_root_path = tmp_path.GetTempPath(); + ::openmldb::test::InitRandomDiskFlags("tablet_impl_keep_alive_test"); return RUN_ALL_TESTS(); } diff --git a/src/tablet/tablet_impl_mem_test.cc b/src/tablet/tablet_impl_mem_test.cc index 1423fc95263..c153d29f1b8 100644 --- a/src/tablet/tablet_impl_mem_test.cc +++ b/src/tablet/tablet_impl_mem_test.cc @@ -184,7 +184,6 @@ int main(int argc, char** argv) { srand(time(NULL)); ::google::ParseCommandLineFlags(&argc, &argv, true); ::openmldb::base::SetLogLevel(INFO); - ::openmldb::test::TempPath tmp_path; - FLAGS_db_root_path = tmp_path.GetTempPath(); + ::openmldb::test::InitRandomDiskFlags("tablet_impl_mem_test"); return RUN_ALL_TESTS(); } diff --git a/src/tablet/tablet_impl_multi_path_test.cc b/src/tablet/tablet_impl_multi_path_test.cc index 425d400f6fa..ed9f5bff4ce 100644 --- a/src/tablet/tablet_impl_multi_path_test.cc +++ b/src/tablet/tablet_impl_multi_path_test.cc @@ -58,31 +58,6 @@ class MockClosure : public ::google::protobuf::Closure { using ::openmldb::api::TableStatus; -class DiskTestEnvironment : public ::testing::Environment{ - virtual void SetUp() { - std::vector file_path; - ::openmldb::base::SplitString(FLAGS_hdd_root_path, ",", file_path); - for (uint32_t i = 0; i < file_path.size(); i++) { - ::openmldb::base::RemoveDirRecursive(file_path[i]); - } - ::openmldb::base::SplitString(FLAGS_recycle_bin_hdd_root_path, ",", file_path); - for (uint32_t i = 0; i < file_path.size(); i++) { - ::openmldb::base::RemoveDirRecursive(file_path[i]); - } - } - virtual void TearDown() { - std::vector file_path; - ::openmldb::base::SplitString(FLAGS_hdd_root_path, ",", file_path); - for (uint32_t i = 0; i < file_path.size(); i++) { - ::openmldb::base::RemoveDirRecursive(file_path[i]); - } - ::openmldb::base::SplitString(FLAGS_recycle_bin_hdd_root_path, ",", file_path); - for (uint32_t i = 0; i < file_path.size(); i++) { - ::openmldb::base::RemoveDirRecursive(file_path[i]); - } - } -}; - void CreateBaseTablet(::openmldb::tablet::TabletImpl& tablet, // NOLINT const ::openmldb::type::TTLType& ttl_type, uint64_t ttl, uint64_t start_ts, uint32_t tid, uint32_t pid, openmldb::common::StorageMode storage_mode) { @@ -295,15 +270,8 @@ TEST_F(TabletMultiPathTest, SSDTestReadWriteAbsolute) { } // namespace openmldb int main(int argc, char** argv) { - ::testing::AddGlobalTestEnvironment(new ::openmldb::tablet::DiskTestEnvironment); ::testing::InitGoogleTest(&argc, argv); srand(time(NULL)); - ::openmldb::test::TempPath tmp_path; - FLAGS_db_root_path = absl::StrCat(tmp_path.GetTempPath(), ",", tmp_path.GetTempPath()); - FLAGS_ssd_root_path = absl::StrCat(tmp_path.GetTempPath("ssd"), ",", tmp_path.GetTempPath("ssd")); - FLAGS_hdd_root_path = absl::StrCat(tmp_path.GetTempPath("hdd"), ",", tmp_path.GetTempPath("hdd")); - FLAGS_recycle_bin_root_path = absl::StrCat(tmp_path.GetTempPath(), ",", tmp_path.GetTempPath()); - FLAGS_recycle_bin_ssd_root_path = absl::StrCat(tmp_path.GetTempPath("ssd"), ",", tmp_path.GetTempPath("ssd")); - FLAGS_recycle_bin_hdd_root_path = absl::StrCat(tmp_path.GetTempPath("hdd"), ",", tmp_path.GetTempPath("hdd")); + ::openmldb::test::InitRandomDiskFlags("tablet_impl_multi_path_test"); return RUN_ALL_TESTS(); } diff --git a/src/tablet/tablet_impl_projection_test.cc b/src/tablet/tablet_impl_projection_test.cc index 1315777515b..5e634f041e4 100644 --- a/src/tablet/tablet_impl_projection_test.cc +++ b/src/tablet/tablet_impl_projection_test.cc @@ -33,12 +33,6 @@ #include "tablet/tablet_impl.h" #include "test/util.h" -DECLARE_string(db_root_path); -DECLARE_string(ssd_root_path); -DECLARE_string(hdd_root_path); -DECLARE_string(recycle_bin_root_path); -DECLARE_string(recycle_bin_ssd_root_path); -DECLARE_string(recycle_bin_hdd_root_path); DECLARE_string(zk_cluster); DECLARE_string(zk_root_path); DECLARE_int32(gc_interval); @@ -73,31 +67,6 @@ struct TestArgs { ~TestArgs() {} }; -class DiskTestEnvironment : public ::testing::Environment{ - virtual void SetUp() { - std::vector file_path; - ::openmldb::base::SplitString(FLAGS_hdd_root_path, ",", file_path); - for (uint32_t i = 0; i < file_path.size(); i++) { - ::openmldb::base::RemoveDirRecursive(file_path[i]); - } - ::openmldb::base::SplitString(FLAGS_recycle_bin_hdd_root_path, ",", file_path); - for (uint32_t i = 0; i < file_path.size(); i++) { - ::openmldb::base::RemoveDirRecursive(file_path[i]); - } - } - virtual void TearDown() { - std::vector file_path; - ::openmldb::base::SplitString(FLAGS_hdd_root_path, ",", file_path); - for (uint32_t i = 0; i < file_path.size(); i++) { - ::openmldb::base::RemoveDirRecursive(file_path[i]); - } - ::openmldb::base::SplitString(FLAGS_recycle_bin_hdd_root_path, ",", file_path); - for (uint32_t i = 0; i < file_path.size(); i++) { - ::openmldb::base::RemoveDirRecursive(file_path[i]); - } - } -}; - class TabletProjectTest : public ::testing::TestWithParam { public: TabletProjectTest() {} @@ -675,14 +644,9 @@ INSTANTIATE_TEST_SUITE_P(TabletProjectPrefix, TabletProjectTest, testing::Values } // namespace openmldb int main(int argc, char** argv) { - ::testing::AddGlobalTestEnvironment(new ::openmldb::tablet::DiskTestEnvironment); ::testing::InitGoogleTest(&argc, argv); + ::openmldb::test::InitRandomDiskFlags("tablet_impl_projection_test"); srand(time(NULL)); - ::openmldb::test::TempPath tmp_path; - FLAGS_db_root_path = absl::StrCat(tmp_path.GetTempPath(), ",", tmp_path.GetTempPath()); - FLAGS_hdd_root_path = absl::StrCat(tmp_path.GetTempPath(), ",", tmp_path.GetTempPath());; - FLAGS_recycle_bin_root_path = absl::StrCat(tmp_path.GetTempPath(), ",", tmp_path.GetTempPath()); - FLAGS_recycle_bin_hdd_root_path = absl::StrCat(tmp_path.GetTempPath(), ",", tmp_path.GetTempPath()); ::hybridse::vm::Engine::InitializeGlobalLLVM(); return RUN_ALL_TESTS(); } diff --git a/src/tablet/tablet_impl_test.cc b/src/tablet/tablet_impl_test.cc index 3b6284ea938..28128dad32c 100644 --- a/src/tablet/tablet_impl_test.cc +++ b/src/tablet/tablet_impl_test.cc @@ -89,15 +89,6 @@ void RemoveData(const std::string& path) { ::openmldb::base::RemoveDir(FLAGS_ssd_root_path); } -class DiskTestEnvironment : public ::testing::Environment{ - virtual void SetUp() { - ::openmldb::base::RemoveDirRecursive(FLAGS_hdd_root_path); - } - virtual void TearDown() { - ::openmldb::base::RemoveDirRecursive(FLAGS_hdd_root_path); - } -}; - class TabletImplTest : public ::testing::TestWithParam<::openmldb::common::StorageMode> { public: TabletImplTest() {} @@ -6319,19 +6310,12 @@ TEST_F(TabletImplTest, DeleteRange) { } // namespace openmldb int main(int argc, char** argv) { - ::testing::AddGlobalTestEnvironment(new ::openmldb::tablet::DiskTestEnvironment); ::testing::InitGoogleTest(&argc, argv); ::hybridse::vm::Engine::InitializeGlobalLLVM(); srand(time(NULL)); ::openmldb::base::SetLogLevel(INFO); ::google::ParseCommandLineFlags(&argc, &argv, true); - ::openmldb::test::TempPath tmp_path; - FLAGS_db_root_path = tmp_path.GetTempPath(); - FLAGS_ssd_root_path = tmp_path.GetTempPath("ssd"); - FLAGS_hdd_root_path = tmp_path.GetTempPath("hdd"); - FLAGS_recycle_bin_root_path = tmp_path.GetTempPath("recycle"); - FLAGS_recycle_bin_ssd_root_path = tmp_path.GetTempPath("recycle"); - FLAGS_recycle_bin_hdd_root_path = tmp_path.GetTempPath("recycle"); + ::openmldb::test::InitRandomDiskFlags("tablet_impl_test"); FLAGS_recycle_bin_enabled = true; return RUN_ALL_TESTS(); } diff --git a/src/test/util.cc b/src/test/util.cc index ed07eb642d4..4378e82f111 100644 --- a/src/test/util.cc +++ b/src/test/util.cc @@ -22,6 +22,8 @@ #include #include +#include "absl/cleanup/cleanup.h" +#include "absl/status/status.h" #include "absl/strings/str_cat.h" #include "brpc/server.h" #include "codec/sdk_codec.h" @@ -32,12 +34,45 @@ DECLARE_string(endpoint); DECLARE_string(tablet); DECLARE_string(zk_cluster); +DECLARE_string(db_root_path); +DECLARE_string(ssd_root_path); +DECLARE_string(hdd_root_path); +DECLARE_string(recycle_bin_ssd_root_path); +DECLARE_string(recycle_bin_root_path); +DECLARE_string(recycle_bin_hdd_root_path); + namespace openmldb { namespace test { using ::openmldb::codec::SchemaCodec; +struct DiskFlagsRAII { + // no copy + DiskFlagsRAII(const DiskFlagsRAII&) = delete; + DiskFlagsRAII(DiskFlagsRAII&&) = default; + DiskFlagsRAII& operator=(const DiskFlagsRAII&) = delete; + DiskFlagsRAII& operator=(DiskFlagsRAII&&) = default; + + explicit DiskFlagsRAII(const std::filesystem::path& p) : prefix(p) { + LOG(INFO) << "setting temp path for test in " << prefix; + FLAGS_db_root_path = prefix / "db_root"; + FLAGS_ssd_root_path = prefix / "ssd_root"; + FLAGS_hdd_root_path = prefix / "hdd_root"; + FLAGS_recycle_bin_root_path = prefix / "recycle_root"; + FLAGS_recycle_bin_hdd_root_path = prefix / "recycle_hdd_root"; + FLAGS_recycle_bin_ssd_root_path = prefix / "recycle_ssd_root"; + } + + ~DiskFlagsRAII() { + LOG(INFO) << "removing temp path: " << prefix; + std::filesystem::remove_all(prefix); + } + + std::filesystem::path prefix; +}; +static const DiskFlagsRAII* flags_raii_ptr = nullptr; + std::string GenRand() { return std::to_string(rand() % 10000000 + 1); // NOLINT } @@ -261,12 +296,15 @@ std::string GetParentDir(const std::string& path) { return dir; } +// TempPath requires initialize random DiskFlagsRAII explicitly, +// or abort with error message TempPath::TempPath() { - std::filesystem::path tmp_path = - std::filesystem::temp_directory_path() / "openmldb_test" / ::openmldb::test::GenRand(); + assert(flags_raii_ptr != nullptr && "use of TempPath must explicitly initialize with InitRandomDiskFlags()"); + auto& global_tmp_prefix = flags_raii_ptr->prefix; + std::filesystem::path tmp_path = global_tmp_prefix / absl::StrCat("tmppath", ::openmldb::test::GenRand()); base_path_ = tmp_path.string(); } -TempPath::~TempPath() { std::filesystem::remove_all(base_path_); } +TempPath::~TempPath() {} std::string TempPath::GetTempPath() { return absl::StrCat(base_path_, "/", ::openmldb::test::GenRand()); } @@ -315,5 +353,38 @@ api::TableMeta CreateTableMeta(const std::string& name, uint32_t tid, uint32_t p return table_meta; } + +static std::filesystem::path GenerateTmpDirPrefix(absl::string_view tag) { + std::filesystem::path tmp_path = std::filesystem::temp_directory_path() / "openmldb"; + const auto& rand = ::openmldb::test::GenRand(); + return tmp_path / absl::StrCat(tag, rand); +} + +static DiskFlagsRAII GenerateRandomDiskFlags(absl::string_view tag) { + return DiskFlagsRAII(GenerateTmpDirPrefix(tag)); +} + +static bool InitRandomDiskFlagsImpl(absl::string_view tag) { + static DiskFlagsRAII flags_raii = GenerateRandomDiskFlags(tag); + auto sig_handler = [](int sig) { + flags_raii.~DiskFlagsRAII(); + _exit(1); + }; + signal(SIGINT, sig_handler); + signal(SIGKILL, sig_handler); + signal(SIGTERM, sig_handler); + signal(SIGABRT, sig_handler); + + flags_raii_ptr = &flags_raii; + return true; +} + +// DiskFlagsRAII instance that initialize exact once: +// - flags_raii ensure global gflags define and set once +// - flags_raii_flag also ensure signal handler set once +bool InitRandomDiskFlags(std::string_view tag) { + static bool flags_raii_flag = InitRandomDiskFlagsImpl(tag); + return flags_raii_flag; +} } // namespace test } // namespace openmldb diff --git a/src/test/util.h b/src/test/util.h index 02bd3fd2a04..24d45ab80c1 100644 --- a/src/test/util.h +++ b/src/test/util.h @@ -107,7 +107,12 @@ api::TableMeta CreateTableMeta(const std::string& name, uint32_t tid, uint32_t p ::openmldb::type::CompressType compress_type, common::StorageMode storage_mode = ::openmldb::common::kMemory); - +// generate a set of disk flags we used for any disk storage in a temporary directory, +// those directories are ensured to be deleted gracefully, regardless of normal or abnormal exit +// +// Since glfags are global, the function does take effect only once +[[maybe_unused]] +bool InitRandomDiskFlags(std::string_view tag = "test"); } // namespace test } // namespace openmldb #endif // SRC_TEST_UTIL_H_