Skip to content

Commit 27bf56f

Browse files
authored
[k2] add get_kphp_cluster_name support (#1547)
Signed-off-by: Petr Shumilov <p.shumilov@vkteam.ru>
1 parent fc8f88a commit 27bf56f

6 files changed

Lines changed: 32 additions & 1 deletion

File tree

runtime-light/state/component-state.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ void ComponentState::parse_runtime_config_arg(std::string_view value_view) noexc
7676
runtime_config = *std::move(opt_config);
7777
}
7878

79+
void ComponentState::parse_cluster_name(std::string_view value_view) noexcept {
80+
cluster_name = string{value_view.data(), static_cast<string::size_type>(value_view.size())};
81+
}
82+
7983
void ComponentState::parse_args() noexcept {
8084
for (auto i = 0; i < argc; ++i) {
8185
const auto [arg_key, arg_value]{k2::arg_fetch(i)};
@@ -88,6 +92,8 @@ void ComponentState::parse_args() noexcept {
8892
parse_kml_arg(value_view);
8993
} else if (key_view == RUNTIME_CONFIG_ARG) [[likely]] {
9094
parse_runtime_config_arg(value_view);
95+
} else if (key_view == CLUSTER_NAME_ARG) [[likely]] {
96+
parse_cluster_name(value_view);
9197
} else {
9298
kphp::log::warning("unexpected argument format: {}", key_view);
9399
}

runtime-light/state/component-state.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ struct ComponentState final : private vk::not_copyable {
2727
array<string> ini_opts{array_size{argc, false}};
2828
array<mixed> env{array_size{envc, false}};
2929
mixed runtime_config;
30+
string cluster_name{DEFAULT_CLUSTER_NAME.data(), DEFAULT_CLUSTER_NAME.size()};
3031

3132
ComponentState() noexcept {
3233
parse_env();
@@ -38,6 +39,8 @@ struct ComponentState final : private vk::not_copyable {
3839
kphp::core::is_reference_counter_recursive(env, ExtraRefCnt::for_global_const)));
3940
kphp::log::assertion((kphp::core::set_reference_counter_recursive(runtime_config, ExtraRefCnt::for_global_const),
4041
kphp::core::is_reference_counter_recursive(runtime_config, ExtraRefCnt::for_global_const)));
42+
kphp::log::assertion((kphp::core::set_reference_counter_recursive(cluster_name, ExtraRefCnt::for_global_const),
43+
kphp::core::is_reference_counter_recursive(cluster_name, ExtraRefCnt::for_global_const)));
4144
}
4245

4346
static const ComponentState& get() noexcept {
@@ -52,6 +55,8 @@ struct ComponentState final : private vk::not_copyable {
5255
static constexpr std::string_view INI_ARG_PREFIX = "ini ";
5356
static constexpr std::string_view KML_DIR_ARG = "kml-dir";
5457
static constexpr std::string_view RUNTIME_CONFIG_ARG = "runtime-config";
58+
static constexpr std::string_view CLUSTER_NAME_ARG = "cluster-name";
59+
static constexpr std::string_view DEFAULT_CLUSTER_NAME = "default";
5560
static constexpr auto INIT_COMPONENT_ALLOCATOR_SIZE = static_cast<size_t>(1024U * 1024U); // 1MB
5661

5762
void parse_env() noexcept;
@@ -63,4 +68,6 @@ struct ComponentState final : private vk::not_copyable {
6368
void parse_kml_arg(std::string_view) noexcept;
6469

6570
void parse_runtime_config_arg(std::string_view) noexcept;
71+
72+
void parse_cluster_name(std::string_view value_view) noexcept;
6673
};

runtime-light/stdlib/system/system-functions.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include "runtime-light/coroutine/io-scheduler.h"
3131
#include "runtime-light/coroutine/task.h"
3232
#include "runtime-light/k2-platform/k2-api.h"
33+
#include "runtime-light/state/component-state.h"
3334
#include "runtime-light/state/image-state.h"
3435
#include "runtime-light/stdlib/diagnostics/contextual-logger.h"
3536
#include "runtime-light/stdlib/diagnostics/logs.h"
@@ -251,7 +252,7 @@ inline string f$get_engine_version() noexcept {
251252
}
252253

253254
inline string f$get_kphp_cluster_name() noexcept {
254-
return string{"adm512"};
255+
return ComponentState::get().cluster_name;
255256
}
256257

257258
inline void f$kphp_turn_on_host_tag_in_inner_statshouse_metrics_toggle() noexcept {}

tests/python/tests/http_server/php/data/component-config.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ components:
66
args:
77
ini hello: "world"
88
runtime-config: ${RUNTIME_CONFIG_PATH}
9+
cluster-name: "custom_cluster_name"
910
links: {}

tests/python/tests/http_server/php/index.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,8 @@ public function work(string $output) {
331331
return;
332332
}
333333
});
334+
} else if ($_SERVER["PHP_SELF"] === "/test_get_cluster_name") {
335+
echo get_kphp_cluster_name();
334336
} else {
335337
if ($_GET["hints"] === "yes") {
336338
send_http_103_early_hints(["Content-Type: text/plain or application/json", "Link: </script.js>; rel=preload; as=script"]);
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from python.lib.testcase import WebServerAutoTestCase
2+
3+
class TestClusterName(WebServerAutoTestCase):
4+
@classmethod
5+
def extra_class_setup(cls):
6+
if not cls.should_use_k2():
7+
cls.web_server.update_options({
8+
"--server-config": "data/server-config.yml"
9+
})
10+
11+
def test_get_cluster_name(self):
12+
resp = self.web_server.http_request(uri="/test_get_cluster_name")
13+
self.assertEqual(resp.status_code, 200)
14+
self.assertEqual(resp.content.decode("utf-8"), "custom_cluster_name")

0 commit comments

Comments
 (0)