Copilot commented on code in PR #7966: URL: https://github.com/apache/ignite-3/pull/7966#discussion_r3070841779
########## modules/platforms/cpp/tests/client-test/partition_awareness_test.cpp: ########## @@ -0,0 +1,737 @@ +// 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. +// + +#include "detail/string_extensions.h" +#include "ignite_runner_suite.h" +#include "tests/test-common/test_utils.h" + +#include "ignite/client/detail/utils.h" +#include "ignite/client/ignite_client.h" +#include "ignite/client/ignite_client_configuration.h" + +#include "proxy/asio_proxy.h" +#include "proxy/message_listener.h" + +#include <gtest/gtest.h> + +namespace ignite { + +using namespace std::chrono_literals; + +using part_id_type = std::size_t; +using id_type = std::int64_t; + +constexpr id_type MIN_ID = 0; +constexpr id_type MAX_ID = 1000; + +static const std::map<std::string, std::string> NODES_TO_ADDR = { + {"org.apache.ignite.internal.runner.app.PlatformTestNodeRunner", "127.0.0.1:10942"}, + {"org.apache.ignite.internal.runner.app.PlatformTestNodeRunner_2", "127.0.0.1:10943"} +}; + +/** + * Parses string-encoded partition distribution. + * Example of string: + * ff979603-fb56-49e9-bc79-7c4487bbbafd=[4];2e14bb82-5cb5-4283-aafd-f2d8552a842a=[5, 8, 9]; + * @param encoded String-encoded distribution + * @return Conveniently structured distribution. + */ +std::map<int64_t, uuid> parse_partition_distribution(const std::string& encoded) { + std::map<int64_t, uuid> res; + + size_t lhs = 0; + size_t rhs = std::string::npos; + while ((rhs = encoded.find(';', lhs)) != std::string::npos) { + std::string chunk = encoded.substr(lhs, rhs - lhs); + + auto eq_pos = chunk.find('='); + + if (eq_pos == std::string::npos) { + throw std::runtime_error("Incorrect partition distribution text:" + encoded); + } + + auto node_id = uuid::from_string(chunk.substr(0, eq_pos)); + + if (!node_id.has_value()) { + throw std::runtime_error("can't parse partition distribution, incorrect input:" + encoded); + } + + std::string num_list = chunk.substr(eq_pos + 1); // e.g. [1,2,3] + + size_t lo = 1; + size_t hi = std::string::npos; + while ((hi = num_list.find_first_of(",]", lo)) != std::string::npos) { + int64_t part_id = std::stoll(num_list.substr(lo, hi - lo)); + res[part_id] = *node_id; + lo = hi + 1; + } + lhs = rhs + 1; + } + + return res; +} + +template<typename T> +class partition_awareness_test : public ignite_runner_suite { +private: + T tab_info{}; +protected: + void SetUp() override { + ignite_client_configuration client_cfg; + client_cfg.set_endpoints(get_node_addrs()); + + m_direct_client = ignite_client::start(client_cfg, 5s); + + auto nodes = m_direct_client.get_cluster_nodes(); + + for (auto& node : nodes) { + if (NODES_TO_ADDR.count(node.get_name())) { + m_endpoint_to_node_id[NODES_TO_ADDR.at(node.get_name())] = node.get_id(); + } + } + + ASSERT_EQ(m_endpoint_to_node_id.size(), get_node_addrs().size()) + << "Incorrect node names or address!" + << "This test should be able to connect to all non-ssl nodes"; + + drop_table_if_exits(); + + create_table(); + + collect_partition_distribution(); + + setup_proxy(); + } + + void TearDown() override { + drop_table_if_exits(); + } + + ignite_tuple key_tup(id_type key) { + return get_tuple(tab_info.key_column_names, tab_info.get_pk_vals(key)); + } + + ignite_tuple main_val_tup(id_type key) { + return get_tuple(tab_info.non_key_column_names, tab_info.get_main_vals(key)); + } + + ignite_tuple alt_val_tup(id_type key) { + return get_tuple(tab_info.non_key_column_names, tab_info.get_alt_vals(key)); + } + + ignite_tuple main_rec(id_type key) { + return get_tuple(tab_info.all_column_names, tab_info.get_main_rec(key)); + } + + ignite_tuple alt_rec(id_type key) { + return get_tuple(tab_info.all_column_names, tab_info.get_main_rec(key)); Review Comment: `alt_rec` returns `get_main_rec` instead of `get_alt_rec`, so tests that rely on an alternate record (e.g. replace / upsert paths) end up using the main record and lose coverage (and can mask bugs). ```suggestion return get_tuple(tab_info.all_column_names, tab_info.get_alt_rec(key)); ``` ########## modules/platforms/cpp/ignite/common/uuid.h: ########## @@ -17,9 +17,12 @@ #pragma once +#include <cerrno> #include <cstdint> +#include <cstdlib> #include <iomanip> #include <istream> +#include <optional> #include <ostream> Review Comment: `uuid::from_string` uses `std::string`, but this header doesn't include `<string>`. Relying on indirect includes can break compilation depending on standard library / build flags; include `<string>` directly here to make the header self-contained. ```suggestion #include <ostream> #include <string> ``` ########## modules/platforms/cpp/ignite/client/detail/table/table_impl.cpp: ########## @@ -472,4 +584,68 @@ std::shared_ptr<table_impl> table_impl::from_facade(table &tb) { return tb.m_impl; } +void table_impl::update_partition_assignment() { + ignite_callback<std::shared_ptr<protocol::partition_assignment>> callback = [self=shared_from_this()](auto pa) { + if (pa.has_error()) { + self->m_connection->get_logger()->log_error("Error while updating partition assignment for table" + + self->get_name() + "error " + pa.error().what_str()); Review Comment: Log message concatenation is missing spaces and reads awkwardly ("... for table<name>error ..."). This makes logs harder to scan and grep; include separators and consider standardizing the wording. ```suggestion self->m_connection->get_logger()->log_error( "Error while updating partition assignment for table '" + self->get_name() + "': " + pa.error().what_str()); ``` ########## modules/platforms/cpp/tests/client-test/partition_awareness_test.cpp: ########## @@ -0,0 +1,737 @@ +// 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. +// + +#include "detail/string_extensions.h" +#include "ignite_runner_suite.h" +#include "tests/test-common/test_utils.h" + +#include "ignite/client/detail/utils.h" +#include "ignite/client/ignite_client.h" +#include "ignite/client/ignite_client_configuration.h" + +#include "proxy/asio_proxy.h" +#include "proxy/message_listener.h" + +#include <gtest/gtest.h> + +namespace ignite { + +using namespace std::chrono_literals; + +using part_id_type = std::size_t; +using id_type = std::int64_t; + +constexpr id_type MIN_ID = 0; +constexpr id_type MAX_ID = 1000; + +static const std::map<std::string, std::string> NODES_TO_ADDR = { + {"org.apache.ignite.internal.runner.app.PlatformTestNodeRunner", "127.0.0.1:10942"}, + {"org.apache.ignite.internal.runner.app.PlatformTestNodeRunner_2", "127.0.0.1:10943"} +}; + +/** + * Parses string-encoded partition distribution. + * Example of string: + * ff979603-fb56-49e9-bc79-7c4487bbbafd=[4];2e14bb82-5cb5-4283-aafd-f2d8552a842a=[5, 8, 9]; + * @param encoded String-encoded distribution + * @return Conveniently structured distribution. + */ +std::map<int64_t, uuid> parse_partition_distribution(const std::string& encoded) { + std::map<int64_t, uuid> res; + + size_t lhs = 0; + size_t rhs = std::string::npos; + while ((rhs = encoded.find(';', lhs)) != std::string::npos) { + std::string chunk = encoded.substr(lhs, rhs - lhs); + + auto eq_pos = chunk.find('='); + + if (eq_pos == std::string::npos) { + throw std::runtime_error("Incorrect partition distribution text:" + encoded); + } + + auto node_id = uuid::from_string(chunk.substr(0, eq_pos)); + + if (!node_id.has_value()) { + throw std::runtime_error("can't parse partition distribution, incorrect input:" + encoded); + } + + std::string num_list = chunk.substr(eq_pos + 1); // e.g. [1,2,3] + + size_t lo = 1; + size_t hi = std::string::npos; + while ((hi = num_list.find_first_of(",]", lo)) != std::string::npos) { + int64_t part_id = std::stoll(num_list.substr(lo, hi - lo)); + res[part_id] = *node_id; + lo = hi + 1; + } + lhs = rhs + 1; + } + + return res; +} + +template<typename T> +class partition_awareness_test : public ignite_runner_suite { +private: + T tab_info{}; +protected: + void SetUp() override { + ignite_client_configuration client_cfg; + client_cfg.set_endpoints(get_node_addrs()); + + m_direct_client = ignite_client::start(client_cfg, 5s); + + auto nodes = m_direct_client.get_cluster_nodes(); + + for (auto& node : nodes) { + if (NODES_TO_ADDR.count(node.get_name())) { + m_endpoint_to_node_id[NODES_TO_ADDR.at(node.get_name())] = node.get_id(); + } + } + + ASSERT_EQ(m_endpoint_to_node_id.size(), get_node_addrs().size()) + << "Incorrect node names or address!" + << "This test should be able to connect to all non-ssl nodes"; + + drop_table_if_exits(); + + create_table(); + + collect_partition_distribution(); + + setup_proxy(); + } + + void TearDown() override { + drop_table_if_exits(); + } + + ignite_tuple key_tup(id_type key) { + return get_tuple(tab_info.key_column_names, tab_info.get_pk_vals(key)); + } + + ignite_tuple main_val_tup(id_type key) { + return get_tuple(tab_info.non_key_column_names, tab_info.get_main_vals(key)); + } + + ignite_tuple alt_val_tup(id_type key) { + return get_tuple(tab_info.non_key_column_names, tab_info.get_alt_vals(key)); + } + + ignite_tuple main_rec(id_type key) { + return get_tuple(tab_info.all_column_names, tab_info.get_main_rec(key)); + } + + ignite_tuple alt_rec(id_type key) { + return get_tuple(tab_info.all_column_names, tab_info.get_main_rec(key)); + } + + void create_table() { + m_direct_client.get_sql().execute(nullptr, nullptr, {tab_info.create_table_ddl()}, {}); + } + + void drop_table_if_exits() { Review Comment: Method name `drop_table_if_exits` is misspelled ("exits" vs "exists"), which makes the test fixture harder to read/search. Consider renaming to `drop_table_if_exists` (and update call sites). ```suggestion void drop_table_if_exists() { ``` ########## modules/platforms/cpp/ignite/client/detail/table/table_impl.cpp: ########## @@ -472,4 +584,68 @@ std::shared_ptr<table_impl> table_impl::from_facade(table &tb) { return tb.m_impl; } +void table_impl::update_partition_assignment() { + ignite_callback<std::shared_ptr<protocol::partition_assignment>> callback = [self=shared_from_this()](auto pa) { + if (pa.has_error()) { + self->m_connection->get_logger()->log_error("Error while updating partition assignment for table" + + self->get_name() + "error " + pa.error().what_str()); + + return; + } + + std::lock_guard lock(self->m_partitions_mutex); + self->m_partition_assignment = std::move(pa).value(); + }; + + load_partition_assignment_async(std::move(callback)); +} + +void table_impl::load_partition_assignment_async(ignite_callback<std::shared_ptr<protocol::partition_assignment>> callback) { + std::int64_t timestamp = m_connection->get_assignment_timestamp(); + + auto pa = get_partition_assignment(); + if (pa && !pa->is_outdated(timestamp)) { + m_connection->get_logger()->log_debug("Partition assignment for table " + get_name() + " is up to date."); + + callback(std::move(pa)); + return; + } + + auto writer_func = [id = m_id, timestamp](protocol::writer &writer, auto&) { + protocol::write_partition_assignment_request(writer, id, timestamp); + }; + + auto reader_func = [timestamp](protocol::reader &reader) -> std::shared_ptr<protocol::partition_assignment> { + return protocol::read_partition_assignment_response(reader, timestamp); + }; + + m_connection->perform_request<std::shared_ptr<protocol::partition_assignment>>( + protocol::client_operation::PARTITION_ASSIGNMENT_GET, + nullptr, + writer_func, + std::move(reader_func), + std::move(callback)); +} + +std::optional<std::string> table_impl::get_preferred_node_name(const ignite_tuple &key_or_rec, const schema &sch) { + auto pa = get_partition_assignment(); + + if (!pa || pa->get_partitions().empty()) { + m_connection->get_logger()->log_debug("No partition distribution available, random node will called"); Review Comment: Log message grammar: "random node will called" is ungrammatical and also missing a word ("be"). Fixing this improves log clarity when partition assignment isn't available. ```suggestion m_connection->get_logger()->log_debug("No partition distribution available, a random node will be called"); ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
