diff --git a/runtime-light/state/component-state.cpp b/runtime-light/state/component-state.cpp index e0e5b2c9e1..c69605abda 100644 --- a/runtime-light/state/component-state.cpp +++ b/runtime-light/state/component-state.cpp @@ -76,6 +76,10 @@ void ComponentState::parse_runtime_config_arg(std::string_view value_view) noexc runtime_config = *std::move(opt_config); } +void ComponentState::parse_cluster_name(std::string_view value_view) noexcept { + cluster_name = string{value_view.data(), static_cast(value_view.size())}; +} + void ComponentState::parse_args() noexcept { for (auto i = 0; i < argc; ++i) { const auto [arg_key, arg_value]{k2::arg_fetch(i)}; @@ -88,6 +92,8 @@ void ComponentState::parse_args() noexcept { parse_kml_arg(value_view); } else if (key_view == RUNTIME_CONFIG_ARG) [[likely]] { parse_runtime_config_arg(value_view); + } else if (key_view == CLUSTER_NAME_ARG) [[likely]] { + parse_cluster_name(value_view); } else { kphp::log::warning("unexpected argument format: {}", key_view); } diff --git a/runtime-light/state/component-state.h b/runtime-light/state/component-state.h index b48fe3bd3b..96834ce2fb 100644 --- a/runtime-light/state/component-state.h +++ b/runtime-light/state/component-state.h @@ -27,6 +27,7 @@ struct ComponentState final : private vk::not_copyable { array ini_opts{array_size{argc, false}}; array env{array_size{envc, false}}; mixed runtime_config; + string cluster_name{DEFAULT_CLUSTER_NAME.data(), DEFAULT_CLUSTER_NAME.size()}; ComponentState() noexcept { parse_env(); @@ -38,6 +39,8 @@ struct ComponentState final : private vk::not_copyable { kphp::core::is_reference_counter_recursive(env, ExtraRefCnt::for_global_const))); kphp::log::assertion((kphp::core::set_reference_counter_recursive(runtime_config, ExtraRefCnt::for_global_const), kphp::core::is_reference_counter_recursive(runtime_config, ExtraRefCnt::for_global_const))); + kphp::log::assertion((kphp::core::set_reference_counter_recursive(cluster_name, ExtraRefCnt::for_global_const), + kphp::core::is_reference_counter_recursive(cluster_name, ExtraRefCnt::for_global_const))); } static const ComponentState& get() noexcept { @@ -52,6 +55,8 @@ struct ComponentState final : private vk::not_copyable { static constexpr std::string_view INI_ARG_PREFIX = "ini "; static constexpr std::string_view KML_DIR_ARG = "kml-dir"; static constexpr std::string_view RUNTIME_CONFIG_ARG = "runtime-config"; + static constexpr std::string_view CLUSTER_NAME_ARG = "cluster-name"; + static constexpr std::string_view DEFAULT_CLUSTER_NAME = "default"; static constexpr auto INIT_COMPONENT_ALLOCATOR_SIZE = static_cast(1024U * 1024U); // 1MB void parse_env() noexcept; @@ -63,4 +68,6 @@ struct ComponentState final : private vk::not_copyable { void parse_kml_arg(std::string_view) noexcept; void parse_runtime_config_arg(std::string_view) noexcept; + + void parse_cluster_name(std::string_view value_view) noexcept; }; diff --git a/runtime-light/stdlib/system/system-functions.h b/runtime-light/stdlib/system/system-functions.h index 65927e1c15..1650a5138e 100644 --- a/runtime-light/stdlib/system/system-functions.h +++ b/runtime-light/stdlib/system/system-functions.h @@ -30,6 +30,7 @@ #include "runtime-light/coroutine/io-scheduler.h" #include "runtime-light/coroutine/task.h" #include "runtime-light/k2-platform/k2-api.h" +#include "runtime-light/state/component-state.h" #include "runtime-light/state/image-state.h" #include "runtime-light/stdlib/diagnostics/contextual-logger.h" #include "runtime-light/stdlib/diagnostics/logs.h" @@ -251,7 +252,7 @@ inline string f$get_engine_version() noexcept { } inline string f$get_kphp_cluster_name() noexcept { - return string{"adm512"}; + return ComponentState::get().cluster_name; } inline void f$kphp_turn_on_host_tag_in_inner_statshouse_metrics_toggle() noexcept {} diff --git a/tests/python/tests/http_server/php/data/component-config.yaml b/tests/python/tests/http_server/php/data/component-config.yaml index 5683788426..26e55c8438 100644 --- a/tests/python/tests/http_server/php/data/component-config.yaml +++ b/tests/python/tests/http_server/php/data/component-config.yaml @@ -6,4 +6,5 @@ components: args: ini hello: "world" runtime-config: ${RUNTIME_CONFIG_PATH} + cluster-name: "custom_cluster_name" links: {} diff --git a/tests/python/tests/http_server/php/index.php b/tests/python/tests/http_server/php/index.php index d6ad319bfc..ab511bd186 100644 --- a/tests/python/tests/http_server/php/index.php +++ b/tests/python/tests/http_server/php/index.php @@ -331,6 +331,8 @@ public function work(string $output) { return; } }); +} else if ($_SERVER["PHP_SELF"] === "/test_get_cluster_name") { + echo get_kphp_cluster_name(); } else { if ($_GET["hints"] === "yes") { send_http_103_early_hints(["Content-Type: text/plain or application/json", "Link: ; rel=preload; as=script"]); diff --git a/tests/python/tests/http_server/test_get_cluster_name.py b/tests/python/tests/http_server/test_get_cluster_name.py new file mode 100644 index 0000000000..efcc4d6232 --- /dev/null +++ b/tests/python/tests/http_server/test_get_cluster_name.py @@ -0,0 +1,14 @@ +from python.lib.testcase import WebServerAutoTestCase + +class TestClusterName(WebServerAutoTestCase): + @classmethod + def extra_class_setup(cls): + if not cls.should_use_k2(): + cls.web_server.update_options({ + "--server-config": "data/server-config.yml" + }) + + def test_get_cluster_name(self): + resp = self.web_server.http_request(uri="/test_get_cluster_name") + self.assertEqual(resp.status_code, 200) + self.assertEqual(resp.content.decode("utf-8"), "custom_cluster_name")