Skip to content

Commit 418753b

Browse files
authored
[k2] rpc server: part2 (#1301)
* implement store_error, rpc_server_store_response * add some methods to TLBuffer * add TL types for RPC requests and responses
1 parent 64e0fc2 commit 418753b

File tree

23 files changed

+522
-411
lines changed

23 files changed

+522
-411
lines changed

builtin-functions/kphp-light/stdlib/rpc.txt

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,17 @@ function store_string ($v ::: string) ::: bool;
7777
function store_double ($v ::: float) ::: bool;
7878
function store_float ($v ::: float) ::: bool;
7979

80+
/** @kphp-extern-func-info interruptible */
81+
function store_error ($error_code ::: int, $error_text ::: string) ::: bool;
82+
83+
/** @kphp-extern-func-info can_throw */
84+
function rpc_server_fetch_request() ::: @tl\RpcFunction;
85+
86+
/** @kphp-extern-func-info can_throw interruptible */
87+
function rpc_server_store_response(@tl\RpcFunctionReturnResult $response) ::: void;
88+
89+
function rpc_clean() ::: bool;
90+
8091
function rpc_tl_pending_queries_count (): int;
8192
function set_fail_rpc_on_int32_overflow ($fail_rpc ::: bool): bool;
8293

@@ -93,8 +104,6 @@ function extract_kphp_rpc_response_extra_info ($resumable_id ::: int) ::: ?tuple
93104
/** @kphp-extern-func-info stub */
94105
function rpc_parse ($data) ::: bool;
95106

96-
/** @kphp-extern-func-info stub */
97-
function rpc_clean() ::: bool;
98107
/** @kphp-extern-func-info interruptible stub generation-required */
99108
function rpc_queue_create ($request_ids ::: mixed = TODO) ::: int;
100109
/** @kphp-extern-func-info interruptible stub generation-required */
@@ -104,20 +113,12 @@ function rpc_queue_next ($queue_id ::: int, $timeout ::: float = -1.0) ::: int |
104113
/** @kphp-extern-func-info interruptible stub generation-required */
105114
function rpc_queue_push ($queue_id ::: int, $request_ids ::: mixed) ::: int;
106115

107-
/** @kphp-extern-func-info interruptible stub generation-required */
108-
function store_error ($error_code ::: int, $error_text ::: string) ::: bool;
109116
/** @kphp-extern-func-info interruptible stub generation-required */
110117
function store_finish() ::: bool;
111118

112119
/** @kphp-extern-func-info interruptible stub generation-required */
113120
function rpc_tl_query_one (\RpcConnection $rpc_conn, $arr ::: mixed, $timeout ::: float = -1.0) ::: int;
114121

115-
/** @kphp-extern-func-info can_throw interruptible stub generation-required */
116-
function rpc_server_fetch_request() ::: @tl\RpcFunction;
117-
118-
/** @kphp-extern-func-info can_throw interruptible stub generation-required */
119-
function rpc_server_store_response(@tl\RpcFunctionReturnResult $response) ::: void;
120-
121122
/** @kphp-extern-func-info tl_common_h_dep interruptible stub */
122123
function typed_rpc_tl_query_result_synchronously (int[] $query_ids) ::: @tl\RpcResponse[];
123124

compiler/code-gen/files/init-scripts.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -115,11 +115,11 @@ struct RunInterruptedFunction {
115115
void compile(CodeGenerator &W) const {
116116
std::string await_prefix = function->is_interruptible ? "co_await " : "";
117117
std::string try_wrapper = "TRY_CALL_VOID_CORO(void, ";
118-
std::string image_kind = G->is_output_mode_k2_cli() ? "ImageKind::CLI"
119-
: G->is_output_mode_k2_server() ? "ImageKind::Server"
120-
: G->is_output_mode_k2_oneshot() ? "ImageKind::Oneshot"
121-
: G->is_output_mode_k2_multishot() ? "ImageKind::Multishot"
122-
: "ImageKind::Invalid";
118+
std::string image_kind = G->is_output_mode_k2_cli() ? "image_kind::cli"
119+
: G->is_output_mode_k2_server() ? "image_kind::server"
120+
: G->is_output_mode_k2_oneshot() ? "image_kind::oneshot"
121+
: G->is_output_mode_k2_multishot() ? "image_kind::multishot"
122+
: "image_kind::invalid";
123123

124124
std::string script_start = "co_await InstanceState::get().run_instance_prologue<" + image_kind + ">();";
125125
std::string script_finish = "co_await InstanceState::get().run_instance_epilogue();";

compiler/code-gen/files/tl2cpp/tl2cpp.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,6 @@ static void collect_target_objects() {
7171
}
7272

7373
void write_rpc_server_functions(CodeGenerator &W) {
74-
if (G->is_output_mode_k2()) {
75-
return;
76-
}
7774
W << OpenFile("rpc_server_fetch_request.cpp", "tl", false);
7875
W << ExternInclude(G->settings().runtime_headers.get());
7976
std::vector<vk::tlo_parsing::combinator *> kphp_functions;

runtime-light/server/init-functions.h

Lines changed: 0 additions & 46 deletions
This file was deleted.

runtime-light/server/rpc/init-functions.cpp

Lines changed: 12 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,9 @@
55
#include "runtime-light/server/rpc/init-functions.h"
66

77
#include <algorithm>
8-
#include <concepts>
98
#include <cstdint>
109
#include <string_view>
11-
#include <type_traits>
1210
#include <utility>
13-
#include <variant>
1411

1512
#include "runtime-common/core/runtime-core.h"
1613
#include "runtime-light/core/globals/php-script-globals.h"
@@ -38,7 +35,6 @@ constexpr std::string_view RPC_EXTRA_SUPPORTED_COMPRESSION_VERSION = "RPC_EXTRA_
3835
constexpr std::string_view RPC_EXTRA_RANDOM_DELAY = "RPC_EXTRA_RANDOM_DELAY";
3936

4037
void process_rpc_invoke_req_extra(const tl::rpcInvokeReqExtra& extra, PhpScriptBuiltInSuperGlobals& superglobals) noexcept {
41-
superglobals.v$_SERVER.set_value(string{RPC_EXTRA_FLAGS.data(), RPC_EXTRA_FLAGS.size()}, static_cast<int64_t>(extra.flags));
4238
if (extra.opt_wait_binlog_pos.has_value()) {
4339
auto wait_binlog_pos{*extra.opt_wait_binlog_pos};
4440
superglobals.v$_SERVER.set_value(string{RPC_EXTRA_WAIT_BINLOG_POS.data(), RPC_EXTRA_WAIT_BINLOG_POS.size()}, wait_binlog_pos.value);
@@ -84,42 +80,30 @@ void process_rpc_invoke_req_extra(const tl::rpcInvokeReqExtra& extra, PhpScriptB
8480

8581
void process_dest_actor(const tl::rpcDestActor& dest_actor, PhpScriptBuiltInSuperGlobals& superglobals) noexcept {
8682
superglobals.v$_SERVER.set_value(string{RPC_ACTOR_ID.data(), RPC_ACTOR_ID.size()}, dest_actor.actor_id.value);
87-
RpcServerInstanceState::get().buffer.store_bytes(dest_actor.query);
8883
}
8984

9085
void process_dest_flags(const tl::rpcDestFlags& dest_flags, PhpScriptBuiltInSuperGlobals& superglobals) noexcept {
86+
superglobals.v$_SERVER.set_value(string{RPC_EXTRA_FLAGS.data(), RPC_EXTRA_FLAGS.size()}, static_cast<int64_t>(dest_flags.flags.value));
9187
process_rpc_invoke_req_extra(dest_flags.extra, superglobals);
92-
RpcServerInstanceState::get().buffer.store_bytes(dest_flags.query);
9388
}
9489

9590
void process_dest_actor_flags(const tl::rpcDestActorFlags& dest_actor_flags, PhpScriptBuiltInSuperGlobals& superglobals) noexcept {
9691
superglobals.v$_SERVER.set_value(string{RPC_ACTOR_ID.data(), RPC_ACTOR_ID.size()}, dest_actor_flags.actor_id.value);
92+
superglobals.v$_SERVER.set_value(string{RPC_EXTRA_FLAGS.data(), RPC_EXTRA_FLAGS.size()}, static_cast<int64_t>(dest_actor_flags.flags.value));
9793
process_rpc_invoke_req_extra(dest_actor_flags.extra, superglobals);
98-
RpcServerInstanceState::get().buffer.store_bytes(dest_actor_flags.query);
99-
}
100-
101-
void process_no_headers(std::string_view query) noexcept {
102-
RpcServerInstanceState::get().buffer.store_bytes(query);
10394
}
10495

10596
void process_rpc_invoke_req(const tl::rpcInvokeReq& rpc_invoke_req, PhpScriptBuiltInSuperGlobals& superglobals) noexcept {
106-
std::visit(
107-
[&superglobals](const auto& header) noexcept {
108-
using header_t = std::remove_cvref_t<decltype(header)>;
109-
110-
if constexpr (std::same_as<header_t, tl::RpcDestActor>) {
111-
process_dest_actor(header.inner, superglobals);
112-
} else if constexpr (std::same_as<header_t, tl::RpcDestFlags>) {
113-
process_dest_flags(header.inner, superglobals);
114-
} else if constexpr (std::same_as<header_t, tl::RpcDestActorFlags>) {
115-
process_dest_actor_flags(header.inner, superglobals);
116-
} else if constexpr (std::same_as<header_t, std::string_view>) {
117-
process_no_headers(header);
118-
} else {
119-
static_assert(false, "non-exhaustive visitor!");
120-
}
121-
},
122-
rpc_invoke_req.query);
97+
if (rpc_invoke_req.opt_dest_actor) {
98+
process_dest_actor((*rpc_invoke_req.opt_dest_actor).inner, superglobals);
99+
}
100+
if (rpc_invoke_req.opt_dest_flags) {
101+
process_dest_flags((*rpc_invoke_req.opt_dest_flags).inner, superglobals);
102+
}
103+
if (rpc_invoke_req.opt_dest_actor_flags) {
104+
process_dest_actor_flags((*rpc_invoke_req.opt_dest_actor_flags).inner, superglobals);
105+
}
106+
RpcServerInstanceState::get().buffer.store_bytes(rpc_invoke_req.query);
123107
}
124108

125109
} // namespace

runtime-light/server/rpc/rpc-server-state.h

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,16 @@
77
#include <cstdint>
88

99
#include "common/mixin/not_copyable.h"
10+
#include "runtime-light/stdlib/rpc/rpc-constants.h"
1011
#include "runtime-light/stdlib/rpc/rpc-tl-query.h"
1112
#include "runtime-light/tl/tl-core.h"
1213

13-
class RpcServerInstanceState final : vk::not_copyable {
14-
public:
15-
int64_t query_id{};
16-
14+
struct RpcServerInstanceState final : vk::not_copyable {
1715
tl::TLBuffer buffer;
18-
CurrentTlQuery current_client_query{};
19-
CurrentRpcServerQuery current_server_query{};
16+
int64_t query_id{kphp::rpc::INVALID_QUERY_ID};
17+
2018
bool fail_rpc_on_int32_overflow{};
19+
CurrentRpcServerQuery current_server_query{};
2120

2221
RpcServerInstanceState() noexcept = default;
2322

runtime-light/state/init-functions.cpp

Lines changed: 0 additions & 108 deletions
This file was deleted.

runtime-light/state/init-functions.h

Lines changed: 0 additions & 18 deletions
This file was deleted.

0 commit comments

Comments
 (0)