Skip to content

Commit

Permalink
[runtime] add runtime configuration support (VKCOM#667)
Browse files Browse the repository at this point in the history
Add the ability to specify a json-config as a server argument to be used inside the script with 'kphp_get_runtime_config()'.
  • Loading branch information
astrophysik authored Nov 10, 2022
1 parent 0980a31 commit f58fe56
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 0 deletions.
1 change: 1 addition & 0 deletions builtin-functions/_functions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ function exit($code = 0) ::: void;
function die($code = 0) ::: void;
function register_kphp_on_warning_callback(callable(string $warning_message, string[] $stacktrace):void $stacktrace) ::: void;
function kphp_set_context_on_error(mixed[] $tags, mixed $extra_info, string $env = "") ::: void;
function kphp_get_runtime_config() ::: mixed;
function kphp_backtrace($pretty ::: bool = true) ::: string[];

function ini_get ($s ::: string) ::: string | false;
Expand Down
6 changes: 6 additions & 0 deletions runtime/interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ bool is_json_log_on_timeout_enabled = true;

static int ignore_level = 0;

mixed runtime_config;

mixed f$kphp_get_runtime_config() {
return runtime_config;
}

void f$ob_clean() {
coub->clean();
}
Expand Down
3 changes: 3 additions & 0 deletions runtime/interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,9 @@ bool f$get_magic_quotes_gpc();

string f$php_sapi_name();

extern mixed runtime_config;

mixed f$kphp_get_runtime_config();

extern mixed v$_SERVER;
extern mixed v$_GET;
Expand Down
19 changes: 19 additions & 0 deletions server/php-engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <cerrno>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <limits>
#include <netdb.h>
#include <netinet/in.h>
Expand Down Expand Up @@ -57,6 +58,7 @@
#include "runtime/interface.h"
#include "runtime/profiler.h"
#include "runtime/rpc.h"
#include "runtime/json-functions.h"
#include "server/cluster-name.h"
#include "server/confdata-binlog-replay.h"
#include "server/database-drivers/adaptor.h"
Expand Down Expand Up @@ -2143,6 +2145,22 @@ int main_args_handler(int i, const char *long_option) {
}
return 0;
}
case 2032: {
std::ifstream file(optarg);
if (!file) {
kprintf("--%s option : file opening failed\n", long_option);
return -1;
}
std::stringstream stringstream;
stringstream << file.rdbuf();
auto [config, success] = json_decode(string(stringstream.str().c_str()));
if (!success) {
kprintf("--%s option : file is not JSON\n", long_option);
return -1;
}
runtime_config = std::move(config);
return 0;
}
default:
return -1;
}
Expand Down Expand Up @@ -2245,6 +2263,7 @@ void parse_main_args(int argc, char *argv[]) {
"memory limit = per_process_memory * processes_count");
parse_option("job-workers-shared-messages-process-multiplier", required_argument, 2031, "Coefficient used to calculate the total count of the shared messages for job workers related communication:\n"
"messages count = coefficient * processes_count");
parse_option("runtime-config", required_argument, 2032, "JSON file path that will be available at runtime as 'mixed' via 'kphp_runtime_config()");
parse_engine_options_long(argc, argv, main_args_handler);
parse_main_args_till_option(argc, argv);
// TODO: remove it after successful migration from kphb.readyV2 to kphb.readyV3
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "simple configuration",
"version": "1.0.0"
}
16 changes: 16 additions & 0 deletions tests/python/tests/http_server/php/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,22 @@ public function work() {
echo "ERROR"; return;
}
echo "OK";
} else if ($_SERVER["PHP_SELF"] === "/test_runtime_config") {
$config = kphp_get_runtime_config();
switch ($_GET["mode"]) {
case "read_only":
echo "name : " . $config["name"] . " ";
echo "version : " . $config["version"] . " ";
break;
case "modify":
$config["version"] = "1.0.1";
echo "modified version : " . $config["version"] . " ";
echo "old version : " . kphp_get_runtime_config()["version"] . " ";
break;
default:
echo "ERROR"; return;
}
echo "OK";
} else if ($_SERVER["PHP_SELF"] === "/test_big_post_data") {
$keys = array_keys($_POST);
if ($keys) {
Expand Down
22 changes: 22 additions & 0 deletions tests/python/tests/http_server/test_runtime_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from python.lib.testcase import KphpServerAutoTestCase

class TestRuntimeConfig(KphpServerAutoTestCase):

@classmethod
def extra_class_setup(cls):
cls.kphp_server.update_options({
"--runtime-config": "./data/runtime_configuration.json"
})


def test_read_only(self):
response = self.kphp_server.http_request(uri="/test_runtime_config?mode=read_only")
self.assertEqual(200, response.status_code)
self.assertEqual(response.reason, "OK")
self.assertEqual(response.content, b'name : simple configuration version : 1.0.0 OK')

def test_modifying(self):
response = self.kphp_server.http_request(uri="/test_runtime_config?mode=modify")
self.assertEqual(200, response.status_code)
self.assertEqual(response.reason, "OK")
self.assertEqual(response.content, b'modified version : 1.0.1 old version : 1.0.0 OK')

0 comments on commit f58fe56

Please sign in to comment.