Skip to content

Commit e41d40b

Browse files
committed
rpcReqResultExtra::fetch
1 parent cf15be3 commit e41d40b

5 files changed

Lines changed: 75 additions & 61 deletions

File tree

runtime-light/stdlib/rpc/rpc-api.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,10 @@ mixed mixed_array_get_value(const mixed& arr, const string& str_key, int64_t num
7474
// THROWING
7575
array<mixed> fetch_function_untyped(const class_instance<RpcTlQuery>& rpc_query) noexcept {
7676
kphp::log::assertion(!rpc_query.is_null());
77-
if (TlRpcError err{}; TRY_CALL(bool, array<mixed>, err.try_fetch())) {
77+
TlRpcError err{};
78+
if (auto is_error_fetched{err.try_fetch()}; !is_error_fetched.has_value()) [[unlikely]] {
79+
return {};
80+
} else if (*is_error_fetched) {
7881
return err.make_error();
7982
}
8083

@@ -95,7 +98,10 @@ class_instance<C$VK$TL$RpcResponse> fetch_function_typed(const class_instance<Rp
9598
auto& cur_query{CurrentTlQuery::get()};
9699
const vk::final_action finalizer{[&cur_query] noexcept { cur_query.reset(); }};
97100
cur_query.set_current_tl_function(rpc_query);
98-
if (TlRpcError err{}; TRY_CALL(bool, class_instance<C$VK$TL$RpcResponse>, err.try_fetch())) {
101+
TlRpcError err{};
102+
if (auto is_error_fetched{err.try_fetch()}; !is_error_fetched.has_value()) [[unlikely]] {
103+
return {};
104+
} else if (*is_error_fetched) {
99105
return error_factory.make_error(std::move(err));
100106
}
101107

runtime-light/stdlib/rpc/rpc-tl-error.cpp

Lines changed: 8 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include "runtime-light/stdlib/rpc/rpc-tl-error.h"
66

7+
#include <optional>
78
#include <tuple>
89

910
#include "common/tl/constants/common.h"
@@ -26,11 +27,14 @@ bool fetch_and_skip_n(int32_t n, tl::fetcher& fetcher) noexcept {
2627

2728
} // namespace
2829

29-
bool TlRpcError::try_fetch() noexcept {
30+
std::optional<bool> TlRpcError::try_fetch() noexcept {
3031
const auto backup_pos{tl_parse_save_pos()};
3132
auto op{TRY_CALL(decltype(f$fetch_int()), bool, f$fetch_int())};
3233
if (op == TL_REQ_RESULT_HEADER) {
33-
fetch_and_skip_header();
34+
if (!fetch_and_skip_header()) [[unlikely]] {
35+
tl_parse_restore_pos(backup_pos);
36+
return std::nullopt;
37+
}
3438
op = TRY_CALL(decltype(f$fetch_int()), bool, f$fetch_int());
3539
}
3640
if (op != TL_RPC_REQ_ERROR) {
@@ -44,59 +48,6 @@ bool TlRpcError::try_fetch() noexcept {
4448
return true;
4549
}
4650

47-
void TlRpcError::fetch_and_skip_header() const noexcept {
48-
auto& fetcher{RpcServerInstanceState::get().tl_fetcher};
49-
tl::i32 flags{};
50-
if (!flags.fetch(fetcher)) [[unlikely]] {
51-
THROW_EXCEPTION(kphp::rpc::exception::not_enough_data_to_fetch::make());
52-
return;
53-
}
54-
55-
if (flags.value & vk::tl::common::rpc_req_result_extra_flags::binlog_pos && !tl::i64{}.fetch(fetcher)) [[unlikely]] {
56-
THROW_EXCEPTION(kphp::rpc::exception::not_enough_data_to_fetch::make());
57-
return;
58-
}
59-
if (flags.value & vk::tl::common::rpc_req_result_extra_flags::binlog_time && !tl::i64{}.fetch(fetcher)) [[unlikely]] {
60-
THROW_EXCEPTION(kphp::rpc::exception::not_enough_data_to_fetch::make());
61-
return;
62-
}
63-
if (flags.value & vk::tl::common::rpc_req_result_extra_flags::engine_pid && !fetch_and_skip_n<tl::i32>(3, fetcher)) [[unlikely]] {
64-
THROW_EXCEPTION(kphp::rpc::exception::not_enough_data_to_fetch::make());
65-
return;
66-
}
67-
if (flags.value & vk::tl::common::rpc_req_result_extra_flags::request_size && !tl::i32{}.fetch(fetcher)) [[unlikely]] {
68-
THROW_EXCEPTION(kphp::rpc::exception::not_enough_data_to_fetch::make());
69-
return;
70-
}
71-
if (flags.value & vk::tl::common::rpc_req_result_extra_flags::response_size && !tl::i32{}.fetch(fetcher)) [[unlikely]] {
72-
THROW_EXCEPTION(kphp::rpc::exception::not_enough_data_to_fetch::make());
73-
return;
74-
}
75-
if (flags.value & vk::tl::common::rpc_req_result_extra_flags::failed_subqueries && !tl::i32{}.fetch(fetcher)) [[unlikely]] {
76-
THROW_EXCEPTION(kphp::rpc::exception::not_enough_data_to_fetch::make());
77-
return;
78-
}
79-
if (flags.value & vk::tl::common::rpc_req_result_extra_flags::compression_version && !tl::i32{}.fetch(fetcher)) [[unlikely]] {
80-
THROW_EXCEPTION(kphp::rpc::exception::not_enough_data_to_fetch::make());
81-
return;
82-
}
83-
if (flags.value & vk::tl::common::rpc_req_result_extra_flags::stats) {
84-
tl::i32 size{};
85-
if (!size.fetch(fetcher)) [[unlikely]] {
86-
THROW_EXCEPTION(kphp::rpc::exception::not_enough_data_to_fetch::make());
87-
return;
88-
}
89-
if (!fetch_and_skip_n<tl::string>(size.value * 2, fetcher)) [[unlikely]] {
90-
THROW_EXCEPTION(kphp::rpc::exception::cant_fetch_string::make());
91-
return;
92-
}
93-
}
94-
if (flags.value & vk::tl::common::rpc_req_result_extra_flags::epoch_number && !tl::i64{}.fetch(fetcher)) [[unlikely]] {
95-
THROW_EXCEPTION(kphp::rpc::exception::not_enough_data_to_fetch::make());
96-
return;
97-
}
98-
if (flags.value & vk::tl::common::rpc_req_result_extra_flags::view_number && !tl::i64{}.fetch(fetcher)) [[unlikely]] {
99-
THROW_EXCEPTION(kphp::rpc::exception::not_enough_data_to_fetch::make());
100-
return;
101-
}
51+
bool TlRpcError::fetch_and_skip_header() const noexcept {
52+
return tl::rpcReqResultExtra{}.fetch(RpcServerInstanceState::get().tl_fetcher);
10253
}

runtime-light/stdlib/rpc/rpc-tl-error.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#pragma once
66

77
#include <cstdint>
8+
#include <optional>
89

910
#include "common/rpc-error-codes.h"
1011
#include "runtime-light/stdlib/diagnostics/exception-types.h"
@@ -37,10 +38,10 @@ struct TlRpcError {
3738
return {};
3839
}
3940

40-
bool try_fetch() noexcept;
41+
std::optional<bool> try_fetch() noexcept;
4142

4243
private:
43-
void fetch_and_skip_header() const noexcept;
44+
bool fetch_and_skip_header() const noexcept;
4445
};
4546

4647
class RpcErrorFactory {

runtime-light/tl/tl-types.cpp

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,61 @@ tl::mask rpcInvokeReqExtra::get_flags() const noexcept {
219219
return flags;
220220
}
221221

222+
bool rpcReqResultExtra::fetch(tl::fetcher& tlf) noexcept {
223+
if (!flags.fetch(tlf)) [[unlikely]] {
224+
return false;
225+
}
226+
if (static_cast<bool>(flags.value & BINLOG_POS_FLAG)) {
227+
if (!binlog_pos.fetch(tlf)) [[unlikely]] {
228+
return false;
229+
}
230+
}
231+
if (static_cast<bool>(flags.value & BINLOG_TIME_FLAG)) {
232+
if (!binlog_time.fetch(tlf)) [[unlikely]] {
233+
return false;
234+
}
235+
}
236+
if (static_cast<bool>(flags.value) & ENGINE_PID_FLAG) {
237+
if (!engine_pid.fetch(tlf)) [[unlikely]] {
238+
return false;
239+
}
240+
}
241+
if (static_cast<bool>(flags.value & REQUEST_SIZE_FLAG)) {
242+
kphp::log::assertion(static_cast<bool>(flags.value & RESPONSE_SIZE_FLAG));
243+
if (!request_size.fetch(tlf)) [[unlikely]] {
244+
return false;
245+
}
246+
if (!response_size.fetch(tlf)) [[unlikely]] {
247+
return false;
248+
}
249+
}
250+
if (static_cast<bool>(flags.value & FAILED_SUBQUERIES_FLAG)) {
251+
if (!failed_subqueries.fetch(tlf)) [[unlikely]] {
252+
return false;
253+
}
254+
}
255+
if (static_cast<bool>(flags.value & COMPRESSION_VERSION_FLAG)) {
256+
if (!compression_version.fetch(tlf)) [[unlikely]] {
257+
return false;
258+
}
259+
}
260+
if (static_cast<bool>(flags.value & STATS_FLAG)) {
261+
if (!stats.fetch(tlf)) [[unlikely]] {
262+
return false;
263+
}
264+
}
265+
if (static_cast<bool>(flags.value & EPOCH_NUMBER_FLAG)) {
266+
kphp::log::assertion(static_cast<bool>(flags.value & VIEW_NUMBER_FLAG));
267+
if (!epoch_number.fetch(tlf)) [[unlikely]] {
268+
return false;
269+
}
270+
if (!view_number.fetch(tlf)) [[unlikely]] {
271+
return false;
272+
}
273+
}
274+
return true;
275+
}
276+
222277
void rpcReqResultExtra::store(tl::storer& tls) const noexcept {
223278
flags.store(tls);
224279
if (static_cast<bool>(flags.value & BINLOG_POS_FLAG)) {

runtime-light/tl/tl-types.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1153,6 +1153,7 @@ class rpcReqResultExtra final {
11531153
tl::i64 epoch_number{};
11541154
tl::i64 view_number{};
11551155

1156+
bool fetch(tl::fetcher& tlf) noexcept;
11561157
void store(tl::storer& tls) const noexcept;
11571158

11581159
size_t footprint() const noexcept;

0 commit comments

Comments
 (0)