From 997171a00e331ed0ee2dc2f69dbb40f5943a020f Mon Sep 17 00:00:00 2001 From: Michael Stetsyuk Date: Fri, 29 Nov 2024 15:29:07 +0000 Subject: [PATCH 1/7] add StartupScriptsExecutionState metric --- programs/server/Server.cpp | 12 ++++++++++++ src/Common/CurrentMetrics.cpp | 2 ++ .../03277_startup_scripts_state_ok.reference | 1 + .../0_stateless/03277_startup_scripts_state_ok.sql | 1 + 4 files changed, 16 insertions(+) create mode 100644 tests/queries/0_stateless/03277_startup_scripts_state_ok.reference create mode 100644 tests/queries/0_stateless/03277_startup_scripts_state_ok.sql diff --git a/programs/server/Server.cpp b/programs/server/Server.cpp index 7d98178e096..a21658b9795 100644 --- a/programs/server/Server.cpp +++ b/programs/server/Server.cpp @@ -295,6 +295,7 @@ namespace CurrentMetrics extern const Metric MergesMutationsMemoryTracking; extern const Metric MaxDDLEntryID; extern const Metric MaxPushedDDLEntryID; + extern const Metric StartupScriptsExecutionState; } namespace ProfileEvents @@ -365,6 +366,14 @@ namespace ErrorCodes } +namespace StartupScriptsExecutionState +{ + [[maybe_unused]] const CurrentMetrics::Value NotFinished = 0; + const CurrentMetrics::Value Success = 1; + const CurrentMetrics::Value Failure = 2; +}; + + static std::string getCanonicalPath(std::string && path) { Poco::trimInPlace(path); @@ -781,9 +790,12 @@ void loadStartupScripts(const Poco::Util::AbstractConfiguration & config, Contex startup_context->makeQueryContext(); executeQuery(read_buffer, write_buffer, true, startup_context, callback, QueryFlags{ .internal = true }, std::nullopt, {}); } + + CurrentMetrics::set(CurrentMetrics::StartupScriptsExecutionState, StartupScriptsExecutionState::Success); } catch (...) { + CurrentMetrics::set(CurrentMetrics::StartupScriptsExecutionState, StartupScriptsExecutionState::Failure); tryLogCurrentException(log, "Failed to parse startup scripts file"); } } diff --git a/src/Common/CurrentMetrics.cpp b/src/Common/CurrentMetrics.cpp index d073e477593..a29ebb2da4c 100644 --- a/src/Common/CurrentMetrics.cpp +++ b/src/Common/CurrentMetrics.cpp @@ -354,6 +354,8 @@ M(SharedCatalogDropZooKeeperThreadsScheduled, "Number of queued or active jobs in the threadpool for drop of object in ZooKeeper in Shared Catalog.") \ \ M(SharedDatabaseCatalogTablesInLocalDropDetachQueue, "Number of tables in the queue for local drop or detach in Shared Catalog.") \ + \ + M(StartupScriptsExecutionState, "State of startup scripts execution: 0 = not finished, 1 = success, 2 = failure.") \ #ifdef APPLY_FOR_EXTERNAL_METRICS #define APPLY_FOR_METRICS(M) APPLY_FOR_BUILTIN_METRICS(M) APPLY_FOR_EXTERNAL_METRICS(M) diff --git a/tests/queries/0_stateless/03277_startup_scripts_state_ok.reference b/tests/queries/0_stateless/03277_startup_scripts_state_ok.reference new file mode 100644 index 00000000000..d00491fd7e5 --- /dev/null +++ b/tests/queries/0_stateless/03277_startup_scripts_state_ok.reference @@ -0,0 +1 @@ +1 diff --git a/tests/queries/0_stateless/03277_startup_scripts_state_ok.sql b/tests/queries/0_stateless/03277_startup_scripts_state_ok.sql new file mode 100644 index 00000000000..b32241d74ab --- /dev/null +++ b/tests/queries/0_stateless/03277_startup_scripts_state_ok.sql @@ -0,0 +1 @@ +SELECT value FROM system.metrics WHERE metric = 'StartupScriptsExecutionState' From b512997e50d60590b6897a84ecfa95632e72bb32 Mon Sep 17 00:00:00 2001 From: Michael Stetsyuk Date: Fri, 29 Nov 2024 17:45:55 +0000 Subject: [PATCH 2/7] better test --- .../__init__.py | 0 .../config/bad_script.xml | 10 ++++ .../config/good_script.xml | 11 ++++ .../config/users.xml | 8 +++ .../test.py | 56 +++++++++++++++++++ .../03277_startup_scripts_state_ok.reference | 1 - .../03277_startup_scripts_state_ok.sql | 1 - 7 files changed, 85 insertions(+), 2 deletions(-) create mode 100644 tests/integration/test_startup_scripts_execution_state/__init__.py create mode 100644 tests/integration/test_startup_scripts_execution_state/config/bad_script.xml create mode 100644 tests/integration/test_startup_scripts_execution_state/config/good_script.xml create mode 100644 tests/integration/test_startup_scripts_execution_state/config/users.xml create mode 100644 tests/integration/test_startup_scripts_execution_state/test.py delete mode 100644 tests/queries/0_stateless/03277_startup_scripts_state_ok.reference delete mode 100644 tests/queries/0_stateless/03277_startup_scripts_state_ok.sql diff --git a/tests/integration/test_startup_scripts_execution_state/__init__.py b/tests/integration/test_startup_scripts_execution_state/__init__.py new file mode 100644 index 00000000000..e69de29bb2d diff --git a/tests/integration/test_startup_scripts_execution_state/config/bad_script.xml b/tests/integration/test_startup_scripts_execution_state/config/bad_script.xml new file mode 100644 index 00000000000..1306cc116c9 --- /dev/null +++ b/tests/integration/test_startup_scripts_execution_state/config/bad_script.xml @@ -0,0 +1,10 @@ + + + + SELECT 42; + + + SELECT * FROM non_existent_table; + + + diff --git a/tests/integration/test_startup_scripts_execution_state/config/good_script.xml b/tests/integration/test_startup_scripts_execution_state/config/good_script.xml new file mode 100644 index 00000000000..f16f800eb6c --- /dev/null +++ b/tests/integration/test_startup_scripts_execution_state/config/good_script.xml @@ -0,0 +1,11 @@ + + + + SELECT 0; + SELECT * FROM non_existent_table; + + + SELECT 42; + + + diff --git a/tests/integration/test_startup_scripts_execution_state/config/users.xml b/tests/integration/test_startup_scripts_execution_state/config/users.xml new file mode 100644 index 00000000000..c5de0b6819c --- /dev/null +++ b/tests/integration/test_startup_scripts_execution_state/config/users.xml @@ -0,0 +1,8 @@ + + + + default + + + + \ No newline at end of file diff --git a/tests/integration/test_startup_scripts_execution_state/test.py b/tests/integration/test_startup_scripts_execution_state/test.py new file mode 100644 index 00000000000..e379a7bf0aa --- /dev/null +++ b/tests/integration/test_startup_scripts_execution_state/test.py @@ -0,0 +1,56 @@ +from enum import Enum +import random +import string +import time + +import pytest + +from helpers.cluster import ClickHouseCluster + +cluster = ClickHouseCluster(__file__) +good = cluster.add_instance( + "good", + main_configs=[ + "config/users.xml", + "config/good_script.xml" + ], + stay_alive=True, +) +bad = cluster.add_instance( + "bad", + main_configs=[ + "config/users.xml", + "config/bad_script.xml" + ], + stay_alive=True, +) + + +@pytest.fixture(scope="module") +def start_cluster(): + try: + cluster.start() + yield cluster + finally: + cluster.shutdown() + + +def test_startup_execution_state(start_cluster): + """ + Making sure that the StartupScriptsExecutionState metric is set correctly. + """ + + STATE_SUCCESS = 1 + STATE_FAILURE = 2 + + assert int( + good.query( + "SELECT value FROM system.metrics WHERE metric = 'StartupScriptsExecutionState'" + ).strip() + ) == STATE_SUCCESS + + assert int( + bad.query( + "SELECT value FROM system.metrics WHERE metric = 'StartupScriptsExecutionState'" + ).strip() + ) == STATE_FAILURE diff --git a/tests/queries/0_stateless/03277_startup_scripts_state_ok.reference b/tests/queries/0_stateless/03277_startup_scripts_state_ok.reference deleted file mode 100644 index d00491fd7e5..00000000000 --- a/tests/queries/0_stateless/03277_startup_scripts_state_ok.reference +++ /dev/null @@ -1 +0,0 @@ -1 diff --git a/tests/queries/0_stateless/03277_startup_scripts_state_ok.sql b/tests/queries/0_stateless/03277_startup_scripts_state_ok.sql deleted file mode 100644 index b32241d74ab..00000000000 --- a/tests/queries/0_stateless/03277_startup_scripts_state_ok.sql +++ /dev/null @@ -1 +0,0 @@ -SELECT value FROM system.metrics WHERE metric = 'StartupScriptsExecutionState' From fad3f2ff0021262915a81faaf517dfe88ca03167 Mon Sep 17 00:00:00 2001 From: robot-clickhouse Date: Fri, 29 Nov 2024 17:53:59 +0000 Subject: [PATCH 3/7] Automatic style fix --- .../test.py | 38 +++++++++---------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/tests/integration/test_startup_scripts_execution_state/test.py b/tests/integration/test_startup_scripts_execution_state/test.py index e379a7bf0aa..f00026df2ff 100644 --- a/tests/integration/test_startup_scripts_execution_state/test.py +++ b/tests/integration/test_startup_scripts_execution_state/test.py @@ -1,7 +1,7 @@ -from enum import Enum import random import string import time +from enum import Enum import pytest @@ -10,18 +10,12 @@ from helpers.cluster import ClickHouseCluster cluster = ClickHouseCluster(__file__) good = cluster.add_instance( "good", - main_configs=[ - "config/users.xml", - "config/good_script.xml" - ], + main_configs=["config/users.xml", "config/good_script.xml"], stay_alive=True, ) bad = cluster.add_instance( "bad", - main_configs=[ - "config/users.xml", - "config/bad_script.xml" - ], + main_configs=["config/users.xml", "config/bad_script.xml"], stay_alive=True, ) @@ -43,14 +37,20 @@ def test_startup_execution_state(start_cluster): STATE_SUCCESS = 1 STATE_FAILURE = 2 - assert int( - good.query( - "SELECT value FROM system.metrics WHERE metric = 'StartupScriptsExecutionState'" - ).strip() - ) == STATE_SUCCESS + assert ( + int( + good.query( + "SELECT value FROM system.metrics WHERE metric = 'StartupScriptsExecutionState'" + ).strip() + ) + == STATE_SUCCESS + ) - assert int( - bad.query( - "SELECT value FROM system.metrics WHERE metric = 'StartupScriptsExecutionState'" - ).strip() - ) == STATE_FAILURE + assert ( + int( + bad.query( + "SELECT value FROM system.metrics WHERE metric = 'StartupScriptsExecutionState'" + ).strip() + ) + == STATE_FAILURE + ) From 4c5caa85036b5eded44f304ddbdff0499429e934 Mon Sep 17 00:00:00 2001 From: Michael Stetsyuk Date: Mon, 2 Dec 2024 13:26:53 +0000 Subject: [PATCH 4/7] empty From 6a4a8926752210a75c33198645e6defada44baef Mon Sep 17 00:00:00 2001 From: Michael Stetsyuk Date: Mon, 2 Dec 2024 17:40:34 +0000 Subject: [PATCH 5/7] empty From 6ce9d45392d08be1e7fc7846e3a9afc77f2d4924 Mon Sep 17 00:00:00 2001 From: Michael Stetsyuk Date: Wed, 4 Dec 2024 13:06:41 +0000 Subject: [PATCH 6/7] better --- programs/server/Server.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/programs/server/Server.cpp b/programs/server/Server.cpp index a21658b9795..cabac3017c4 100644 --- a/programs/server/Server.cpp +++ b/programs/server/Server.cpp @@ -366,11 +366,11 @@ namespace ErrorCodes } -namespace StartupScriptsExecutionState +enum StartupScriptsExecutionState : CurrentMetrics::Value { - [[maybe_unused]] const CurrentMetrics::Value NotFinished = 0; - const CurrentMetrics::Value Success = 1; - const CurrentMetrics::Value Failure = 2; + NotFinished = 0, + Success = 1, + Failure = 2, }; From d77df2b4f278247d30887dd87da6f1b06a6e7840 Mon Sep 17 00:00:00 2001 From: Michael Stetsyuk Date: Wed, 4 Dec 2024 16:05:06 +0000 Subject: [PATCH 7/7] empty