Skip to content

Commit

Permalink
add dynamic allocation for rpc query
Browse files Browse the repository at this point in the history
  • Loading branch information
Vadim Sadokhov committed Sep 11, 2023
1 parent 8a5e683 commit 4d4b5dc
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 12 deletions.
2 changes: 1 addition & 1 deletion server/job-workers/job-worker-server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ int JobWorkerServer::job_parse_execute(connection *c) noexcept {

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);
reinterpret_cast<JobCustomData *>(c->custom_data)->worker = new (&php_worker_storage) PhpWorker(job_worker, c, std::move(job_data), job->job_id, left_job_time);

set_connection_timeout(c, left_job_time);
c->status = conn_wait_net;
Expand Down
10 changes: 5 additions & 5 deletions server/php-engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -619,7 +619,7 @@ int hts_func_execute(connection *c, int op) {
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);
PhpWorker *worker = new (&php_worker_storage) PhpWorker(http_worker, c, std::move(http_data), ++http_script_req_id, script_timeout);
D->extra = worker;

set_connection_timeout(c, script_timeout);
Expand Down Expand Up @@ -1010,18 +1010,18 @@ int rpcx_execute(connection *c, int op, raw_message *raw) {
double actual_script_timeout = custom_settings.has_timeout() ? normalize_script_timeout(custom_settings.php_timeout_ms / 1000.0) : script_timeout;
set_connection_timeout(c, actual_script_timeout);

static char Rpc_data[MAX_RPC_REQUEST_SIZE];
auto fetched_bytes = tl_fetch_data(Rpc_data, len);
char *buffer = static_cast<char *>(malloc(len + 1));
auto fetched_bytes = tl_fetch_data(buffer, len);
if (fetched_bytes == -1) {
client_rpc_error(c, req_id, tl_fetch_error_code(), tl_fetch_error_string());
return 0;
}
assert(fetched_bytes == len);
auto *D = TCP_RPC_DATA(c);
php_query_data_t rpc_data = rpc_query_data{std::move(header), reinterpret_cast<int *>(Rpc_data), len / static_cast<int>(sizeof(int)),
php_query_data_t rpc_data = rpc_query_data{std::move(header), reinterpret_cast<int *>(buffer), 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);
PhpWorker *worker = new (&php_worker_storage) PhpWorker(run_once ? once_worker : rpc_worker, c, std::move(rpc_data), req_id, actual_script_timeout);
D->extra = worker;

c->status = conn_wait_net;
Expand Down
8 changes: 4 additions & 4 deletions server/php-query-data.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ struct http_query_data {
struct rpc_query_data {
tl_query_header_t header;

const int *data;
int *data;
int len;
unsigned ip;
short port;
Expand All @@ -40,6 +40,6 @@ struct job_query_data {
using null_query_data = std::monostate;

using php_query_data_t = std::variant<null_query_data,
http_query_data,
rpc_query_data,
job_query_data>;
http_query_data,
rpc_query_data,
job_query_data>;
7 changes: 6 additions & 1 deletion server/php-runner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <cstdlib>
#include <cstring>
#include <exception>
#include <utility>
#include <sys/mman.h>
#include <sys/time.h>
#include <unistd.h>
Expand Down Expand Up @@ -155,6 +156,10 @@ PhpScript::PhpScript(size_t mem_size, double oom_handling_memory_ratio, size_t s

PhpScript::~PhpScript() noexcept {
munmap(run_mem, mem_size);
// free dynamic allocated buffer for rpc @see rpcx_execute
if (std::holds_alternative<rpc_query_data>(data)) {
free(std::get<rpc_query_data>(data).data);
}
}

void PhpScript::init(script_t *script, php_query_data_t data_to_set) noexcept {
Expand All @@ -173,7 +178,7 @@ void PhpScript::init(script_t *script, php_query_data_t data_to_set) noexcept {
makecontext_portable(&run_context, &script_context_entrypoint, 0);

run_main = script;
data = data_to_set;
data = std::move(data_to_set);

state = run_state_t::ready;

Expand Down
4 changes: 3 additions & 1 deletion server/php-worker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// Distributed under the GPL v3 License, see LICENSE.notice.txt

#include <cassert>
#include <utility>
#include <poll.h>

#include "common/precise-time.h"
Expand Down Expand Up @@ -161,6 +162,7 @@ void PhpWorker::state_init_script() noexcept {
}
dl::init_critical_section();
php_script->init(script, data);
data = null_query_data{};
php_script->set_timeout(timeout);
state = phpq_run;
}
Expand Down Expand Up @@ -447,7 +449,7 @@ double PhpWorker::get_timeout() const noexcept {
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)
, data(std::move(query_data))
, paused(false)
, flushed_http_connection(false)
, terminate_flag(false)
Expand Down

0 comments on commit 4d4b5dc

Please sign in to comment.