Skip to content

Commit

Permalink
change variant of pointers to base variant
Browse files Browse the repository at this point in the history
  • Loading branch information
Vadim Sadokhov committed Sep 11, 2023
1 parent 1fab058 commit 8a5e683
Show file tree
Hide file tree
Showing 13 changed files with 41 additions and 77 deletions.
25 changes: 14 additions & 11 deletions runtime/interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "common/algorithms/string-algorithms.h"
#include "common/macos-ports.h"
#include "common/tl/constants/common.h"
#include "common/wrappers/overloaded.h"

#include "net/net-connections.h"
#include "runtime/array_functions.h"
Expand Down Expand Up @@ -1801,20 +1802,22 @@ static http_query_data empty_http_data;
static rpc_query_data empty_rpc_data;
static job_query_data empty_job_data;

void init_superglobals(php_query_data_ptr data) {
void init_superglobals(const php_query_data_t &data) {
// init superglobals depending on the request type
std::visit([&](auto && arg) {
using T = std::decay_t<decltype(arg)>;
if constexpr (std::is_same_v<T, rpc_query_data *>) {
std::visit(overloaded{
[](const rpc_query_data &rpc_data) {
query_type = QUERY_TYPE_RPC;
init_superglobals_impl(empty_http_data, *std::get<rpc_query_data *>(data), empty_job_data);
} else if constexpr (std::is_same_v<T, http_query_data *>) {
init_superglobals_impl(empty_http_data, rpc_data, empty_job_data);
},
[](const http_query_data &http_data) {
query_type = QUERY_TYPE_HTTP;
init_superglobals_impl(*std::get<http_query_data *>(data), empty_rpc_data, empty_job_data);
} else if constexpr (std::is_same_v<T, job_query_data *>) {
init_superglobals_impl(http_data, empty_rpc_data, empty_job_data);
},
[](const job_query_data &job_data) {
query_type = QUERY_TYPE_JOB;
init_superglobals_impl(empty_http_data, empty_rpc_data, *std::get<job_query_data *>(data));
} else {
init_superglobals_impl(empty_http_data, empty_rpc_data, job_data);
},
[](const null_query_data &) {
query_type = QUERY_TYPE_CONSOLE;
init_superglobals_impl(empty_http_data, empty_rpc_data, empty_job_data);
}
Expand Down Expand Up @@ -2433,7 +2436,7 @@ void global_init_script_allocator() {
dl::global_init_script_allocator();
}

void init_runtime_environment(php_query_data_ptr data, void *mem, size_t script_mem_size, size_t oom_handling_mem_size) {
void init_runtime_environment(const php_query_data_t &data, void *mem, size_t script_mem_size, size_t oom_handling_mem_size) {
dl::init_script_allocator(mem, script_mem_size, oom_handling_mem_size);
reset_global_interface_vars();
init_runtime_libs();
Expand Down
4 changes: 2 additions & 2 deletions runtime/interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ bool f$is_uploaded_file(const string &filename);
bool f$move_uploaded_file(const string &oldname, const string &newname);


void init_superglobals(php_query_data_ptr data);
void init_superglobals(const php_query_data_t &data);


double f$get_net_time();
Expand Down Expand Up @@ -203,7 +203,7 @@ Optional<array<mixed>> f$getopt(const string &options, array<string> longopts =
void global_init_runtime_libs();
void global_init_script_allocator();

void init_runtime_environment(php_query_data_ptr data, void *mem, size_t script_mem_size, size_t oom_handling_mem_size = 0);
void init_runtime_environment(const php_query_data_t &data, void *mem, size_t script_mem_size, size_t oom_handling_mem_size = 0);

void free_runtime_environment();

Expand Down
5 changes: 2 additions & 3 deletions server/job-workers/job-worker-server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,8 @@ int JobWorkerServer::job_parse_execute(connection *c) noexcept {
job_stat.job_request_max_real_memory_used = job_memory_stats.max_real_memory_used;
job_stat.job_request_max_memory_used = job_memory_stats.max_memory_used;

job_query_data *job_data = job_query_data_create(job, [](JobSharedMessage *job_response) {
return vk::singleton<JobWorkerServer>::get().send_job_reply(job_response);
});
php_query_data_t job_data = job_query_data{job, [](JobSharedMessage *job_response) {
return vk::singleton<JobWorkerServer>::get().send_job_reply(job_response);}};
reinterpret_cast<JobCustomData *>(c->custom_data)->worker = new (&php_worker_storage) PhpWorker(job_worker, c, job_data, job->job_id, left_job_time);

set_connection_timeout(c, left_job_time);
Expand Down
11 changes: 5 additions & 6 deletions server/php-engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -614,10 +614,9 @@ int hts_func_execute(connection *c, int op) {
}

/** save query here **/
http_query_data *http_data = http_query_data_create(qUri, qUriLen, qGet, qGetLen, qHeaders, qHeadersLen, qPost,
qPostLen, query_type_str, strlen(query_type_str), D->query_flags & QF_KEEPALIVE,
inet_sockaddr_address(&c->remote_endpoint),
inet_sockaddr_port(&c->remote_endpoint));
php_query_data_t http_data = http_query_data{qUri, qGet, qHeaders, qPost, query_type_str,
qUriLen, qGetLen, qHeadersLen, qPostLen, static_cast<int>(strlen(query_type_str)),
D->query_flags & QF_KEEPALIVE, inet_sockaddr_address(&c->remote_endpoint), inet_sockaddr_port(&c->remote_endpoint)};

static long long http_script_req_id = 0;
PhpWorker *worker = new (&php_worker_storage) PhpWorker(http_worker, c, http_data, ++http_script_req_id, script_timeout);
Expand Down Expand Up @@ -1019,8 +1018,8 @@ int rpcx_execute(connection *c, int op, raw_message *raw) {
}
assert(fetched_bytes == len);
auto *D = TCP_RPC_DATA(c);
rpc_query_data *rpc_data = rpc_query_data_create(std::move(header), reinterpret_cast<int *>(Rpc_data), len / static_cast<int>(sizeof(int)), D->remote_pid.ip,
D->remote_pid.port, D->remote_pid.pid, D->remote_pid.utime);
php_query_data_t rpc_data = rpc_query_data{std::move(header), reinterpret_cast<int *>(Rpc_data), len / static_cast<int>(sizeof(int)),
D->remote_pid.ip, D->remote_pid.port, D->remote_pid.pid, D->remote_pid.utime};

PhpWorker *worker = new (&php_worker_storage) PhpWorker(run_once ? once_worker : rpc_worker, c, rpc_data, req_id, actual_script_timeout);
D->extra = worker;
Expand Down
30 changes: 0 additions & 30 deletions server/php-query-data.cpp

This file was deleted.

20 changes: 7 additions & 13 deletions server/php-query-data.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

#pragma once

#include "common/tl/query-header.h"

#include <variant>

#include "common/tl/query-header.h"


struct http_query_data {
char const *uri, *get, *headers, *post, *request_method;
Expand Down Expand Up @@ -37,15 +37,9 @@ struct job_query_data {
const char *(*send_reply) (job_workers::JobSharedMessage *job_response);
};

struct null_query_data_ptr {};

using php_query_data_ptr = std::variant<null_query_data_ptr,
http_query_data *,
rpc_query_data *,
job_query_data *>;
using null_query_data = std::monostate;

http_query_data *http_query_data_create(const char *uri, int uri_len, const char *get, int get_len, const char *headers,
int headers_len, const char *post, int post_len, const char *request_method, int request_method_len,
int keep_alive, unsigned int ip, unsigned int port);
rpc_query_data *rpc_query_data_create(tl_query_header_t &&header, const int *data, int len, unsigned int ip, short port, short pid, int utime);
job_query_data *job_query_data_create(job_workers::JobSharedMessage *job_request, const char *(*send_reply) (job_workers::JobSharedMessage *job_response));
using php_query_data_t = std::variant<null_query_data,
http_query_data,
rpc_query_data,
job_query_data>;
6 changes: 3 additions & 3 deletions server/php-runner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ PhpScript::~PhpScript() noexcept {
munmap(run_mem, mem_size);
}

void PhpScript::init(script_t *script, php_query_data_ptr data_to_set) noexcept {
void PhpScript::init(script_t *script, php_query_data_t data_to_set) noexcept {
assert (script != nullptr);
assert_state(run_state_t::empty);

Expand Down Expand Up @@ -328,8 +328,8 @@ void PhpScript::finish() noexcept {
static char buf[buf_size];
buf[0] = 0;
if (disable_access_log < 2) {
if (std::holds_alternative<http_query_data *>(data)) {
http_query_data *http_data = std::get<http_query_data *>(data);
if (std::holds_alternative<http_query_data>(data)) {
http_query_data *http_data = &std::get<http_query_data>(data);
if (http_data != nullptr) {
if (disable_access_log) {
snprintf(buf, buf_size, "[uri = %.*s?<truncated>]", min(http_data->uri_len, 200), http_data->uri);
Expand Down
4 changes: 2 additions & 2 deletions server/php-runner.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ class PhpScript {
sigjmp_buf timeout_handler{};

script_t *run_main{nullptr};
php_query_data_ptr data{null_query_data_ptr{}};
php_query_data_t data{null_query_data{}};
script_result *res{nullptr};

static void script_context_entrypoint() noexcept;
Expand All @@ -126,7 +126,7 @@ class PhpScript {
void try_run_shutdown_functions_on_timeout() noexcept;
void check_net_context_errors() noexcept;

void init(script_t *script, php_query_data_ptr data_to_set) noexcept;
void init(script_t *script, php_query_data_t data_to_set) noexcept;

void pause() noexcept;
void ask_query(php_query_base_t *q) noexcept;
Expand Down
2 changes: 1 addition & 1 deletion server/php-worker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ double PhpWorker::get_timeout() const noexcept {
return time_left;
}

PhpWorker::PhpWorker(php_worker_mode_t mode_, connection *c, php_query_data_ptr query_data,
PhpWorker::PhpWorker(php_worker_mode_t mode_, connection *c, php_query_data_t query_data,
long long int req_id_, double timeout)
: conn(c)
, data(query_data)
Expand Down
4 changes: 2 additions & 2 deletions server/php-worker.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class PhpWorker {
public:
struct connection *conn;

php_query_data_ptr data;
php_query_data_t data;

bool paused;
bool flushed_http_connection;
Expand All @@ -58,7 +58,7 @@ class PhpWorker {
long long req_id;
int target_fd;

PhpWorker(php_worker_mode_t mode_, connection *c, php_query_data_ptr query_data, long long req_id_, double timeout);
PhpWorker(php_worker_mode_t mode_, connection *c, php_query_data_t php_query_data, long long req_id_, double timeout);
~PhpWorker() = default;

double enter_lifecycle() noexcept;
Expand Down
1 change: 0 additions & 1 deletion server/server.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ prepend(KPHP_SERVER_SOURCES ${BASE_DIR}/server/
php-mc-connections.cpp
php-queries.cpp
php-queries-types.cpp
php-query-data.cpp
php-runner.cpp
php-init-scripts.cpp
php-sql-connections.cpp
Expand Down
4 changes: 2 additions & 2 deletions server/signal-handlers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,8 @@ void print_http_data() {
}
if (!PhpScript::current_script) {
write_str(2, "\nPHPScriptBase::current_script is nullptr\n");
} else if (std::holds_alternative<http_query_data *>(PhpScript::current_script->data)) {
const auto & data = std::get<http_query_data *>(PhpScript::current_script->data);
} else if (std::holds_alternative<http_query_data>(PhpScript::current_script->data)) {
http_query_data *data = &std::get<http_query_data>(PhpScript::current_script->data);
write_str(2, "\nuri\n");
write(2, data->uri, data->uri_len);
write_str(2, "\nget\n");
Expand Down
2 changes: 1 addition & 1 deletion tests/cpp/runtime/_runtime-tests-env.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class RuntimeTestsEnvironment final : public testing::Environment {
global_init_runtime_libs();
global_init_script_allocator();

init_runtime_environment(null_query_data_ptr{}, script_memory, script_memory_size);
init_runtime_environment(null_query_data{}, script_memory, script_memory_size);
php_disable_warnings = true;
php_warning_level = 0;
}
Expand Down

0 comments on commit 8a5e683

Please sign in to comment.