From cd952a6d98d5d9273e37d7927ea8998d2799c724 Mon Sep 17 00:00:00 2001
From: Petr Shumilov
Date: Tue, 10 Mar 2026 11:24:06 +0300
Subject: [PATCH 1/5] Add get_kphp_cluster_name support
Signed-off-by: Petr Shumilov
---
runtime-light/state/component-state.cpp | 13 +++++++++++++
runtime-light/state/component-state.h | 7 +++++++
runtime-light/stdlib/system/system-functions.h | 6 +++++-
3 files changed, 25 insertions(+), 1 deletion(-)
diff --git a/runtime-light/state/component-state.cpp b/runtime-light/state/component-state.cpp
index e0e5b2c9e1..88633a23e3 100644
--- a/runtime-light/state/component-state.cpp
+++ b/runtime-light/state/component-state.cpp
@@ -8,6 +8,7 @@
#include
#include
#include
+#include
#include
#include
#include
@@ -93,3 +94,15 @@ void ComponentState::parse_args() noexcept {
}
}
}
+
+void ComponentState::parse_cluster_name() noexcept {
+ const string key{CLUSTER_NAME_ENV.data(), CLUSTER_NAME_ENV.size()};
+ if (!env.has_key(key) ) {
+ return;
+ }
+ const auto& value{env.get_value(key)};
+ if (!value.is_string()) {
+ return;
+ }
+ cluster_name.emplace(value.as_string());
+}
diff --git a/runtime-light/state/component-state.h b/runtime-light/state/component-state.h
index b48fe3bd3b..0868efb9e4 100644
--- a/runtime-light/state/component-state.h
+++ b/runtime-light/state/component-state.h
@@ -8,6 +8,7 @@
#include
#include
#include
+#include
#include "common/mixin/not_copyable.h"
#include "common/php-functions.h"
@@ -27,10 +28,12 @@ struct ComponentState final : private vk::not_copyable {
array ini_opts{array_size{argc, false}};
array env{array_size{envc, false}};
mixed runtime_config;
+ std::optional cluster_name;
ComponentState() noexcept {
parse_env();
parse_args();
+ parse_cluster_name();
kphp::log::assertion((kphp::core::set_reference_counter_recursive(ini_opts, ExtraRefCnt::for_global_const),
kphp::core::is_reference_counter_recursive(ini_opts, ExtraRefCnt::for_global_const)));
@@ -38,6 +41,7 @@ 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(cluster_name.has_value() ? cluster_name->is_reference_counter(ExtraRefCnt::for_global_const) : true);
}
static const ComponentState& get() noexcept {
@@ -53,6 +57,7 @@ struct ComponentState final : private vk::not_copyable {
static constexpr std::string_view KML_DIR_ARG = "kml-dir";
static constexpr std::string_view RUNTIME_CONFIG_ARG = "runtime-config";
static constexpr auto INIT_COMPONENT_ALLOCATOR_SIZE = static_cast(1024U * 1024U); // 1MB
+ static constexpr std::string_view CLUSTER_NAME_ENV = "CLUSTER_NAME";
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() noexcept;
};
diff --git a/runtime-light/stdlib/system/system-functions.h b/runtime-light/stdlib/system/system-functions.h
index 65927e1c15..2f78a912fa 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,10 @@ inline string f$get_engine_version() noexcept {
}
inline string f$get_kphp_cluster_name() noexcept {
- return string{"adm512"};
+ if (const auto& state{ComponentState::get()}; state.cluster_name.has_value()) {
+ return state.cluster_name.value();
+ }
+ return string{"default"};
}
inline void f$kphp_turn_on_host_tag_in_inner_statshouse_metrics_toggle() noexcept {}
From b5a3caa7b453dac4300b65ee9783b81c85105dfb Mon Sep 17 00:00:00 2001
From: Petr Shumilov
Date: Tue, 10 Mar 2026 11:24:38 +0300
Subject: [PATCH 2/5] Add tests
Signed-off-by: Petr Shumilov
---
tests/python/tests/http_server/php/index.php | 2 ++
.../http_server/test_get_cluster_name.py | 24 +++++++++++++++++++
2 files changed, 26 insertions(+)
create mode 100644 tests/python/tests/http_server/test_get_cluster_name.py
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..f888d9c043
--- /dev/null
+++ b/tests/python/tests/http_server/test_get_cluster_name.py
@@ -0,0 +1,24 @@
+import os
+import pytest
+
+from python.lib.testcase import WebServerAutoTestCase
+
+class TestClusterName(WebServerAutoTestCase):
+ @classmethod
+ def extra_class_setup(cls):
+ if cls.should_use_k2():
+ os.environ["CLUSTER_NAME"] = "custom_cluster_name"
+ else:
+ cls.web_server.update_options({
+ "--server-config": "data/server-config.yml"
+ })
+
+ @classmethod
+ def extra_class_teardown(cls):
+ if cls.should_use_k2():
+ os.environ.pop("CLUSTER_NAME")
+
+ 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")
From 50c4fc53d2f30e69479bc5edfaa7535a6b509732 Mon Sep 17 00:00:00 2001
From: Petr Shumilov
Date: Tue, 10 Mar 2026 11:30:48 +0300
Subject: [PATCH 3/5] Fix code style
Signed-off-by: Petr Shumilov
---
runtime-light/state/component-state.cpp | 2 +-
runtime-light/state/component-state.h | 2 +-
2 files changed, 2 insertions(+), 2 deletions(-)
diff --git a/runtime-light/state/component-state.cpp b/runtime-light/state/component-state.cpp
index 88633a23e3..94e805c94e 100644
--- a/runtime-light/state/component-state.cpp
+++ b/runtime-light/state/component-state.cpp
@@ -97,7 +97,7 @@ void ComponentState::parse_args() noexcept {
void ComponentState::parse_cluster_name() noexcept {
const string key{CLUSTER_NAME_ENV.data(), CLUSTER_NAME_ENV.size()};
- if (!env.has_key(key) ) {
+ if (!env.has_key(key)) {
return;
}
const auto& value{env.get_value(key)};
diff --git a/runtime-light/state/component-state.h b/runtime-light/state/component-state.h
index 0868efb9e4..c2648956ec 100644
--- a/runtime-light/state/component-state.h
+++ b/runtime-light/state/component-state.h
@@ -7,8 +7,8 @@
#include
#include
#include
-#include
#include
+#include
#include "common/mixin/not_copyable.h"
#include "common/php-functions.h"
From 310962606d0d0d459ca6ecade70855a27c88507e Mon Sep 17 00:00:00 2001
From: Petr Shumilov
Date: Tue, 10 Mar 2026 16:15:01 +0300
Subject: [PATCH 4/5] Move default cluster name into component state
Signed-off-by: Petr Shumilov
---
runtime-light/state/component-state.cpp | 2 +-
runtime-light/state/component-state.h | 8 +++++---
runtime-light/stdlib/system/system-functions.h | 5 +----
3 files changed, 7 insertions(+), 8 deletions(-)
diff --git a/runtime-light/state/component-state.cpp b/runtime-light/state/component-state.cpp
index 94e805c94e..e0763752a6 100644
--- a/runtime-light/state/component-state.cpp
+++ b/runtime-light/state/component-state.cpp
@@ -104,5 +104,5 @@ void ComponentState::parse_cluster_name() noexcept {
if (!value.is_string()) {
return;
}
- cluster_name.emplace(value.as_string());
+ cluster_name = value.as_string();
}
diff --git a/runtime-light/state/component-state.h b/runtime-light/state/component-state.h
index c2648956ec..692018b604 100644
--- a/runtime-light/state/component-state.h
+++ b/runtime-light/state/component-state.h
@@ -28,7 +28,7 @@ struct ComponentState final : private vk::not_copyable {
array ini_opts{array_size{argc, false}};
array env{array_size{envc, false}};
mixed runtime_config;
- std::optional cluster_name;
+ string cluster_name{DEFAULT_CLUSTER_NAME.data(), DEFAULT_CLUSTER_NAME.size()};
ComponentState() noexcept {
parse_env();
@@ -41,7 +41,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(cluster_name.has_value() ? cluster_name->is_reference_counter(ExtraRefCnt::for_global_const) : true);
+ 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 {
@@ -56,8 +57,9 @@ 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 auto INIT_COMPONENT_ALLOCATOR_SIZE = static_cast(1024U * 1024U); // 1MB
static constexpr std::string_view CLUSTER_NAME_ENV = "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;
diff --git a/runtime-light/stdlib/system/system-functions.h b/runtime-light/stdlib/system/system-functions.h
index 2f78a912fa..1650a5138e 100644
--- a/runtime-light/stdlib/system/system-functions.h
+++ b/runtime-light/stdlib/system/system-functions.h
@@ -252,10 +252,7 @@ inline string f$get_engine_version() noexcept {
}
inline string f$get_kphp_cluster_name() noexcept {
- if (const auto& state{ComponentState::get()}; state.cluster_name.has_value()) {
- return state.cluster_name.value();
- }
- return string{"default"};
+ return ComponentState::get().cluster_name;
}
inline void f$kphp_turn_on_host_tag_in_inner_statshouse_metrics_toggle() noexcept {}
From 7e597d5420b48de3a26348425e4ec84990061eb6 Mon Sep 17 00:00:00 2001
From: Petr Shumilov
Date: Tue, 10 Mar 2026 16:45:23 +0300
Subject: [PATCH 5/5] Extract cluster name from args instead of env
Signed-off-by: Petr Shumilov
---
runtime-light/state/component-state.cpp | 19 ++++++-------------
runtime-light/state/component-state.h | 6 ++----
.../php/data/component-config.yaml | 1 +
.../http_server/test_get_cluster_name.py | 12 +-----------
4 files changed, 10 insertions(+), 28 deletions(-)
diff --git a/runtime-light/state/component-state.cpp b/runtime-light/state/component-state.cpp
index e0763752a6..c69605abda 100644
--- a/runtime-light/state/component-state.cpp
+++ b/runtime-light/state/component-state.cpp
@@ -8,7 +8,6 @@
#include
#include
#include
-#include
#include
#include
#include
@@ -77,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)};
@@ -89,20 +92,10 @@ 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);
}
}
}
-
-void ComponentState::parse_cluster_name() noexcept {
- const string key{CLUSTER_NAME_ENV.data(), CLUSTER_NAME_ENV.size()};
- if (!env.has_key(key)) {
- return;
- }
- const auto& value{env.get_value(key)};
- if (!value.is_string()) {
- return;
- }
- cluster_name = value.as_string();
-}
diff --git a/runtime-light/state/component-state.h b/runtime-light/state/component-state.h
index 692018b604..96834ce2fb 100644
--- a/runtime-light/state/component-state.h
+++ b/runtime-light/state/component-state.h
@@ -7,7 +7,6 @@
#include
#include
#include
-#include
#include
#include "common/mixin/not_copyable.h"
@@ -33,7 +32,6 @@ struct ComponentState final : private vk::not_copyable {
ComponentState() noexcept {
parse_env();
parse_args();
- parse_cluster_name();
kphp::log::assertion((kphp::core::set_reference_counter_recursive(ini_opts, ExtraRefCnt::for_global_const),
kphp::core::is_reference_counter_recursive(ini_opts, ExtraRefCnt::for_global_const)));
@@ -57,7 +55,7 @@ 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_ENV = "CLUSTER_NAME";
+ 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
@@ -71,5 +69,5 @@ struct ComponentState final : private vk::not_copyable {
void parse_runtime_config_arg(std::string_view) noexcept;
- void parse_cluster_name() noexcept;
+ void parse_cluster_name(std::string_view value_view) 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/test_get_cluster_name.py b/tests/python/tests/http_server/test_get_cluster_name.py
index f888d9c043..efcc4d6232 100644
--- a/tests/python/tests/http_server/test_get_cluster_name.py
+++ b/tests/python/tests/http_server/test_get_cluster_name.py
@@ -1,23 +1,13 @@
-import os
-import pytest
-
from python.lib.testcase import WebServerAutoTestCase
class TestClusterName(WebServerAutoTestCase):
@classmethod
def extra_class_setup(cls):
- if cls.should_use_k2():
- os.environ["CLUSTER_NAME"] = "custom_cluster_name"
- else:
+ if not cls.should_use_k2():
cls.web_server.update_options({
"--server-config": "data/server-config.yml"
})
- @classmethod
- def extra_class_teardown(cls):
- if cls.should_use_k2():
- os.environ.pop("CLUSTER_NAME")
-
def test_get_cluster_name(self):
resp = self.web_server.http_request(uri="/test_get_cluster_name")
self.assertEqual(resp.status_code, 200)