Merge pull request #72637 from ClickHouse/startup-scripts-execution-state

add `StartupScriptsExecutionState` metric
This commit is contained in:
Miсhael Stetsyuk 2024-12-05 10:43:04 +00:00 committed by GitHub
commit 4346ccb434
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 99 additions and 0 deletions

View File

@ -296,6 +296,7 @@ namespace CurrentMetrics
extern const Metric MergesMutationsMemoryTracking;
extern const Metric MaxDDLEntryID;
extern const Metric MaxPushedDDLEntryID;
extern const Metric StartupScriptsExecutionState;
}
namespace ProfileEvents
@ -366,6 +367,14 @@ namespace ErrorCodes
}
enum StartupScriptsExecutionState : CurrentMetrics::Value
{
NotFinished = 0,
Success = 1,
Failure = 2,
};
static std::string getCanonicalPath(std::string && path)
{
Poco::trimInPlace(path);
@ -782,9 +791,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");
}
}

View File

@ -356,6 +356,8 @@
M(SharedDatabaseCatalogTablesInLocalDropDetachQueue, "Number of tables in the queue for local drop or detach in Shared Catalog.") \
\
M(MergeTreeIndexGranularityInternalArraysTotalSize, "The total size of all internal arrays in Merge Tree index granularity objects in bytes.") \
\
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)

View File

@ -0,0 +1,10 @@
<clickhouse>
<startup_scripts>
<scripts>
<query>SELECT 42;</query>
</scripts>
<scripts>
<query>SELECT * FROM non_existent_table;</query>
</scripts>
</startup_scripts>
</clickhouse>

View File

@ -0,0 +1,11 @@
<clickhouse>
<startup_scripts>
<scripts>
<condition>SELECT 0;</condition>
<query>SELECT * FROM non_existent_table;</query>
</scripts>
<scripts>
<query>SELECT 42;</query>
</scripts>
</startup_scripts>
</clickhouse>

View File

@ -0,0 +1,8 @@
<clickhouse>
<users>
<default>
<profile>default</profile>
<no_password></no_password>
</default>
</users>
</clickhouse>

View File

@ -0,0 +1,56 @@
import random
import string
import time
from enum import Enum
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
)