-
Notifications
You must be signed in to change notification settings - Fork 133
IGNITE-27056 added request timeouts to C++ client #7080
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
7171d63
86f96ba
ab4a207
a28548f
2cf4277
5c7d71a
859ac61
e5dec2e
02cd62d
b333e6e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -35,9 +35,10 @@ node_connection::node_connection(std::uint64_t id, std::shared_ptr<network::asyn | |||||||||
| , m_timer_thread(std::move(timer_thread)){} | ||||||||||
|
|
||||||||||
| node_connection::~node_connection() { | ||||||||||
| for (auto &handler : m_request_handlers) { | ||||||||||
| for (auto& req : m_request_handlers) { | ||||||||||
| auto handling_res = result_of_operation<void>([&]() { | ||||||||||
| auto res = handler.second->set_error(ignite_error("Connection closed before response was received")); | ||||||||||
| auto handler = req.second.handler; | ||||||||||
| auto res = handler->set_error(ignite_error("Connection closed before response was received")); | ||||||||||
| if (res.has_error()) | ||||||||||
| m_logger->log_error( | ||||||||||
| "Uncaught user callback exception while handling operation error: " + res.error().what_str()); | ||||||||||
|
|
@@ -182,6 +183,10 @@ ignite_result<void> node_connection::process_handshake_rsp(bytes_view msg) { | |||||||||
| plan_heartbeat(m_heartbeat_interval); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| if (m_configuration.get_operation_timeout().count() > 0) { | ||||||||||
| handle_timeouts(); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| return {}; | ||||||||||
| } | ||||||||||
|
|
||||||||||
|
|
@@ -192,7 +197,7 @@ std::shared_ptr<response_handler> node_connection::get_and_remove_handler(std::i | |||||||||
| if (it == m_request_handlers.end()) | ||||||||||
| return {}; | ||||||||||
|
|
||||||||||
| auto res = std::move(it->second); | ||||||||||
| auto res = std::move(it->second.handler); | ||||||||||
| m_request_handlers.erase(it); | ||||||||||
|
|
||||||||||
| return res; | ||||||||||
|
|
@@ -203,7 +208,46 @@ std::shared_ptr<response_handler> node_connection::find_handler_unsafe(std::int6 | |||||||||
| if (it == m_request_handlers.end()) | ||||||||||
| return {}; | ||||||||||
|
|
||||||||||
| return it->second; | ||||||||||
| return it->second.handler; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| void node_connection::handle_timeouts() { | ||||||||||
| auto now = std::chrono::steady_clock::now(); | ||||||||||
|
|
||||||||||
| { | ||||||||||
| std::lock_guard lock(m_request_handlers_mutex); | ||||||||||
|
|
||||||||||
| std::vector<int64_t> keys_for_erasure; | ||||||||||
|
|
||||||||||
| for (auto& [id, req] : m_request_handlers) { | ||||||||||
| if (req.timeouts_at > now) { | ||||||||||
|
||||||||||
| if (req.timeouts_at > now) { | |
| if (req.timeouts_at < now) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems wrong. Do tests pass?
| if (req.timeouts_at > now) { | |
| if (req.timeouts_at < now) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will add a test which would fail with suc incorrect code. Thats left as artifact of refactoring.
Copilot
AI
Nov 26, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing space after comma in function call. Should be ss.str()) → ss.str()) with proper spacing: CLIENT_OPERATION_TIMEOUT, ss.str().
| auto res = req.handler->set_error(ignite_error(error::code::CLIENT_OPERATION_TIMEOUT,ss.str())); | |
| auto res = req.handler->set_error(ignite_error(error::code::CLIENT_OPERATION_TIMEOUT, ss.str())); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should also log it as a warning here.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -49,6 +49,23 @@ class node_connection : public std::enable_shared_from_this<node_connection> { | |
| public: | ||
| typedef std::function<void(protocol::writer&, const protocol::protocol_context&)> writer_function_type; | ||
|
|
||
| struct pending_request { | ||
| /** Handler function for request */ | ||
| std::shared_ptr<response_handler> handler = nullptr; | ||
|
|
||
| /** | ||
| * Optional request timeout. | ||
| * When provided contains time point after which request would be considered as timed out. | ||
| */ | ||
| std::optional<std::chrono::time_point<std::chrono::steady_clock>> timeouts_at = std::nullopt; | ||
|
|
||
| explicit pending_request( | ||
| std::shared_ptr<response_handler> handler, | ||
| std::optional<std::chrono::time_point<std::chrono::steady_clock>> timeouts_at) | ||
| : handler(std::move(handler)) | ||
| , timeouts_at(timeouts_at) {} | ||
| }; | ||
|
|
||
| // Deleted | ||
| node_connection() = delete; | ||
| node_connection(node_connection &&) = delete; | ||
|
|
@@ -103,8 +120,11 @@ class node_connection : public std::enable_shared_from_this<node_connection> { | |
| * @param handler response handler. | ||
| * @return A request ID on success and @c std::nullopt otherwise. | ||
| */ | ||
| [[nodiscard]] std::optional<std::int64_t> perform_request(protocol::client_operation op, const writer_function_type &wr, | ||
| std::shared_ptr<response_handler> handler) { | ||
| [[nodiscard]] std::optional<std::int64_t> perform_request( | ||
| protocol::client_operation op, | ||
| const writer_function_type &wr, | ||
| std::shared_ptr<response_handler> handler) | ||
| { | ||
| auto req_id = generate_request_id(); | ||
| std::vector<std::byte> message; | ||
| { | ||
|
|
@@ -119,9 +139,14 @@ class node_connection : public std::enable_shared_from_this<node_connection> { | |
| buffer.write_length_header(); | ||
| } | ||
|
|
||
| std::optional<std::chrono::time_point<std::chrono::steady_clock>> timeout{}; | ||
| if (m_configuration.get_operation_timeout().count() > 0) { | ||
| timeout = std::chrono::steady_clock::now() + m_configuration.get_operation_timeout(); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In java client there is an optimization for calling analog of ::now(). I am not aware of performance of this method. Should we research if similiar optimisation is required?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why? What exactly should be optimized here?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TLDR: as I said I don't know any performance issues of this method. In java case there were some issues with analogous System.currentTimeMillis() on windows platform. That could be a reason why in ignite codebase we have what optimization could be (if calling std::chrono::steady_clock::now() would be slower than we can afford). We spawn a thread which updates atomic value every X seconds. Instead of calling ::now() we read from atomic variable. As I googled a bit calling steady_clock::now() doesn't switch context from userspace to kernelspace (on opposing of calling system_clock::now()). I am pretty sure that we don't want to do it right now, but maybe small task to research performance impact of calling ::now on each request. |
||
| } | ||
|
|
||
| { | ||
| std::lock_guard<std::recursive_mutex> lock(m_request_handlers_mutex); | ||
| m_request_handlers[req_id] = std::move(handler); | ||
| m_request_handlers.emplace(req_id, pending_request(std::move(handler), timeout)); | ||
| } | ||
|
|
||
| if (m_logger->is_debug_enabled()) { | ||
|
|
@@ -134,6 +159,7 @@ class node_connection : public std::enable_shared_from_this<node_connection> { | |
| get_and_remove_handler(req_id); | ||
| return {}; | ||
| } | ||
|
|
||
| return {req_id}; | ||
| } | ||
|
|
||
|
|
@@ -148,8 +174,11 @@ class node_connection : public std::enable_shared_from_this<node_connection> { | |
| * @return Channel used for the request. | ||
| */ | ||
| template<typename T> | ||
| [[nodiscard]] std::optional<std::int64_t> perform_request(protocol::client_operation op, const writer_function_type &wr, | ||
| std::function<T(protocol::reader &)> rd, ignite_callback<T> callback) { | ||
| [[nodiscard]] std::optional<std::int64_t> perform_request( | ||
| protocol::client_operation op, | ||
| const writer_function_type &wr, | ||
| std::function<T(protocol::reader &)> rd, | ||
| ignite_callback<T> callback) { | ||
| auto handler = std::make_shared<response_handler_reader<T>>(std::move(rd), std::move(callback)); | ||
| return perform_request(op, wr, std::move(handler)); | ||
| } | ||
|
|
@@ -203,6 +232,8 @@ class node_connection : public std::enable_shared_from_this<node_connection> { | |
| */ | ||
| std::shared_ptr<ignite_logger> get_logger() const { return m_logger; } | ||
|
|
||
| void handle_timeouts(); | ||
|
|
||
| private: | ||
| /** | ||
| * Constructor. | ||
|
|
@@ -290,7 +321,7 @@ class node_connection : public std::enable_shared_from_this<node_connection> { | |
| std::atomic_int64_t m_req_id_gen{0}; | ||
|
|
||
| /** Pending request handlers. */ | ||
| std::unordered_map<std::int64_t, std::shared_ptr<response_handler>> m_request_handlers; | ||
| std::unordered_map<std::int64_t, pending_request> m_request_handlers; | ||
|
|
||
| /** Handlers map mutex. */ | ||
| std::recursive_mutex m_request_handlers_mutex; | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -47,6 +47,11 @@ class ignite_client_configuration { | |||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||
| static constexpr std::chrono::milliseconds DEFAULT_HEARTBEAT_INTERVAL = std::chrono::seconds(30); | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||
| * Default operation | ||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||
| static constexpr std::chrono::milliseconds DEFAULT_OPERATION_TIMEOUT = std::chrono::milliseconds(0); | ||||||||||||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| // Default | ||||||||||||||||||||||||||||||||||
| ignite_client_configuration() = default; | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
|
|
@@ -187,6 +192,28 @@ class ignite_client_configuration { | |||||||||||||||||||||||||||||||||
| m_heartbeat_interval = heartbeat_interval; | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||
| * Gets the operation timeout, in milliseconds. Default is 0 (no timeout). | ||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||
| * An "operation" is a single client request to the server. Some public API calls may involve multiple operations, | ||||||||||||||||||||||||||||||||||
| * in which case the operation timeout is applied to each individual network call. | ||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||
| * @return Operation timeout, in milliseconds. | ||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||
|
Comment on lines
+195
to
+202
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can see from the type it's in ms. Also, when std::chrono duration types are used it's not really important if it in milliseconds or in something else as this value can be converted freely.
Suggested change
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, let's clarify the "single request" part. I think we should clarify that the timeout is for the interval between sending a request and receiving a response to it. We can also clarify here that it's not applicable for the long running compute jobs execution, as the response is received as soon as the job is started by the cluster, and when it's done we receive a notification from the server which is a different mechanism. |
||||||||||||||||||||||||||||||||||
| [[nodiscard]] std::chrono::milliseconds get_operation_timeout() const { return m_operation_timeout; } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||
| * Sets the operation timeout. | ||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||
|
Comment on lines
+205
to
+207
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add some description of the parameter.
Suggested change
|
||||||||||||||||||||||||||||||||||
| void set_operation_timeout(std::chrono::milliseconds operation_timeout) { | ||||||||||||||||||||||||||||||||||
| if (operation_timeout.count() < 0) { | ||||||||||||||||||||||||||||||||||
| throw ignite_error(error::code::ILLEGAL_ARGUMENT, "Operation timeout can't be negative: " | ||||||||||||||||||||||||||||||||||
|
Comment on lines
+209
to
+210
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's add a test for this. |
||||||||||||||||||||||||||||||||||
| + std::to_string(operation_timeout.count()) + " microseconds"); | ||||||||||||||||||||||||||||||||||
Erixonich marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| m_operation_timeout = operation_timeout; | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||
| * Gets the authenticator. | ||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||
|
|
@@ -306,6 +333,9 @@ class ignite_client_configuration { | |||||||||||||||||||||||||||||||||
| /** Heartbeat interval. */ | ||||||||||||||||||||||||||||||||||
| std::chrono::milliseconds m_heartbeat_interval{DEFAULT_HEARTBEAT_INTERVAL}; | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| /** Operation timeout. */ | ||||||||||||||||||||||||||||||||||
| std::chrono::milliseconds m_operation_timeout{DEFAULT_OPERATION_TIMEOUT}; | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| /** SSL Mode. */ | ||||||||||||||||||||||||||||||||||
| ssl_mode m_ssl_mode{ssl_mode::DISABLE}; | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -72,7 +72,7 @@ int main(int argc, char **argv) { | |
| runner.stop(); | ||
| }); | ||
|
|
||
| if (!check_test_node_connectable(std::chrono::seconds(5))) { | ||
| if (!check_test_node_connectable(std::chrono::seconds(5)) && false) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Revert. |
||
| runner.start(); | ||
| ensure_node_connectable(std::chrono::seconds(60)); | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -58,7 +58,7 @@ void set_process_abort_handler(std::function<void(int)> handler) { | |
|
|
||
| using namespace ignite; | ||
|
|
||
| const std::vector<std::string> DEFAULT_VERSIONS = {"9.1.0", "9.1.1", "9.1.2", "9.1.3", "9.1.4"}; | ||
| const std::vector<std::string> DEFAULT_VERSIONS = {"3.0.0"}; | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. unrelated to this ticket but mistake I made in previous PR.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What about 3.1.0?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed |
||
|
|
||
| /** | ||
| * Structure to store argument values for automatic memory management. | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| # | ||
| # Licensed to the Apache Software Foundation (ASF) under one or more | ||
| # contributor license agreements. See the NOTICE file distributed with | ||
| # this work for additional information regarding copyright ownership. | ||
| # The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| # (the "License"); you may not use this file except in compliance with | ||
| # the License. You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| # | ||
|
|
||
| project(ignite-fake-server) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is it in a separate package? |
||
|
|
||
| set(TARGET ${PROJECT_NAME}) | ||
|
|
||
| set(SOURCES | ||
| main.cpp | ||
| fake_server.cpp | ||
| tcp_client_channel.cpp | ||
| response_action.cpp | ||
| connection_test.cpp | ||
| ) | ||
|
|
||
| ignite_test(${TARGET} SOURCES ${SOURCES} LIBS ignite-test-common ignite3-client) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| // | ||
| // Created by Ed on 21.11.2025. | ||
| // | ||
|
Comment on lines
+1
to
+3
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. License |
||
|
|
||
| #include "tests/client-test/ignite_runner_suite.h" | ||
| #include "ignite/client/ignite_client.h" | ||
| #include "fake_server.h" | ||
|
|
||
| #include <gtest/gtest.h> | ||
| #include <thread> | ||
|
|
||
| using namespace ignite; | ||
|
|
||
| class connection_test : public ignite_runner_suite { | ||
|
|
||
| }; | ||
|
|
||
|
|
||
| TEST_F(connection_test, handshake_with_fake_server) { | ||
| using namespace std::chrono_literals; | ||
| fake_server fs{}; | ||
|
|
||
| fs.start(); | ||
|
|
||
| ignite_client_configuration cfg; | ||
| cfg.set_logger(get_logger()); | ||
| cfg.set_endpoints({"127.0.0.1:10800"}); | ||
|
|
||
| auto cl = ignite_client::start(cfg, 5s); | ||
| } | ||
|
|
||
| TEST_F(connection_test, request_timeout) { | ||
| using namespace std::chrono_literals; | ||
| fake_server fs{ | ||
| 10800, | ||
| [](protocol::client_operation op) -> std::unique_ptr<response_action> { | ||
| switch (op) { | ||
| case protocol::client_operation::CLUSTER_GET_NODES: | ||
| return std::make_unique<drop_action>(); | ||
| default: | ||
| return nullptr; | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| fs.start(); | ||
|
|
||
| ignite_client_configuration cfg; | ||
| cfg.set_logger(get_logger()); | ||
| cfg.set_endpoints({"127.0.0.1:10800"}); | ||
| cfg.set_operation_timeout(std::chrono::milliseconds{100}); | ||
|
|
||
| auto cl = ignite_client::start(cfg, 5s); | ||
|
|
||
| try { | ||
| auto cluster_nodes = cl.get_cluster_nodes(); | ||
| } catch (ignite_error& err) { | ||
| EXPECT_EQ(error::code::CLIENT_OPERATION_TIMEOUT, err.get_status_code()); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| // | ||
| // Created by Ed on 21.11.2025. | ||
| // | ||
|
|
||
| #include "fake_server.h" | ||
|
Comment on lines
+1
to
+5
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Empty file, let's remove. |
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's try avoiding additional memory allocation here. Let's erase values as we move through the map. We hold the lock anyway. It can be done with iterator: