diff --git a/src/Processors/Executors/ExecutingGraph.cpp b/src/Processors/Executors/ExecutingGraph.cpp index 69277bdeca80..20f12635905d 100644 --- a/src/Processors/Executors/ExecutingGraph.cpp +++ b/src/Processors/Executors/ExecutingGraph.cpp @@ -34,6 +34,9 @@ ExecutingGraph::ExecutingGraph(std::shared_ptr processors_, bool pro } /// Create edges. + for (uint64_t node = 0; node < num_processors; ++node) + addPortNumbers(node); + for (uint64_t node = 0; node < num_processors; ++node) addEdges(node); } @@ -55,6 +58,34 @@ ExecutingGraph::Edge & ExecutingGraph::addEdge(Edges & edges, Edge edge, const I return added_edge; } +void ExecutingGraph::addPortNumbers(uint64_t node) +{ + IProcessor * processor = nodes[node]->processor; + + auto & inputs = processor->getInputs(); + uint64_t input_number = nodes[node]->back_edges.size(); + for (auto it = std::next(inputs.begin(), input_number); it != inputs.end(); ++it, ++input_number) + port_numbers[&*it] = input_number; + + auto & outputs = processor->getOutputs(); + uint64_t output_number = nodes[node]->direct_edges.size(); + for (auto it = std::next(outputs.begin(), output_number); it != outputs.end(); ++it, ++output_number) + port_numbers[&*it] = output_number; +} + +uint64_t ExecutingGraph::getPortNumber(const Port & port, const IProcessor * from) const +{ + auto it = port_numbers.find(&port); + if (it == port_numbers.end()) + throw Exception( + ErrorCodes::LOGICAL_ERROR, + "Port of processor {} connected to processor {} was not found in pipeline", + port.getProcessor().getName(), + from->getName()); + + return it->second; +} + bool ExecutingGraph::addEdges(uint64_t node) { IProcessor * from = nodes[node]->processor; @@ -72,7 +103,7 @@ bool ExecutingGraph::addEdges(uint64_t node) for (auto it = std::next(inputs.begin(), from_input); it != inputs.end(); ++it, ++from_input) { const IProcessor * to = &it->getOutputPort().getProcessor(); - auto output_port_number = to->getOutputPortNumber(&it->getOutputPort()); + auto output_port_number = getPortNumber(it->getOutputPort(), from); Edge edge(0, true, from_input, output_port_number, &nodes[node]->post_updated_input_ports); auto & added_edge = addEdge(nodes[node]->back_edges, std::move(edge), from, to); it->setUpdateInfo(&added_edge.update_info); @@ -90,7 +121,7 @@ bool ExecutingGraph::addEdges(uint64_t node) for (auto it = std::next(outputs.begin(), from_output); it != outputs.end(); ++it, ++from_output) { const IProcessor * to = &it->getInputPort().getProcessor(); - auto input_port_number = to->getInputPortNumber(&it->getInputPort()); + auto input_port_number = getPortNumber(it->getInputPort(), from); Edge edge(0, false, input_port_number, from_output, &nodes[node]->post_updated_output_ports); auto & added_edge = addEdge(nodes[node]->direct_edges, std::move(edge), from, to); it->setUpdateInfo(&added_edge.update_info); @@ -154,6 +185,9 @@ ExecutingGraph::UpdateNodeStatus ExecutingGraph::expandPipeline(boost::container std::vector updated_nodes; + for (uint64_t node = 0; node < num_processors; ++node) + addPortNumbers(node); + for (uint64_t node = 0; node < num_processors; ++node) { if (addEdges(node)) diff --git a/src/Processors/Executors/ExecutingGraph.h b/src/Processors/Executors/ExecutingGraph.h index 8ef0e9527a5d..fc213abf9c65 100644 --- a/src/Processors/Executors/ExecutingGraph.h +++ b/src/Processors/Executors/ExecutingGraph.h @@ -156,6 +156,13 @@ class ExecutingGraph /// Add single edge to edges list. Check processor is known. Edge & addEdge(Edges & edges, Edge edge, const IProcessor * from, const IProcessor * to); + /// Remember positions of node's ports which do not have edges yet. + /// Must be called for all nodes before addEdges, so that addEdges can find the port number of a peer port in O(1). + void addPortNumbers(uint64_t node); + + /// Position of the port in its processor's list of inputs or outputs. + uint64_t getPortNumber(const Port & port, const IProcessor * from) const; + /// Append new edges for node. It is called for new node or when new port were added after ExpandPipeline. /// Returns true if new edge was added. bool addEdges(uint64_t node); @@ -166,6 +173,10 @@ class ExecutingGraph std::shared_ptr processors; std::vector source_processors; + + /// Port -> its position in processor's list of inputs or outputs. Filled by addPortNumbers. + std::unordered_map port_numbers; + std::mutex processors_mutex; SharedMutex nodes_mutex; diff --git a/src/Processors/tests/gtest_executing_graph_many_ports.cpp b/src/Processors/tests/gtest_executing_graph_many_ports.cpp new file mode 100644 index 000000000000..51bb9c2bc585 --- /dev/null +++ b/src/Processors/tests/gtest_executing_graph_many_ports.cpp @@ -0,0 +1,39 @@ +#include + +#include +#include +#include +#include +#include + +using namespace DB; + +/// Many sources connected to a single processor with as many inputs, like the merge of many +/// temporary files after external aggregation. Building the graph must not be quadratic in the number of ports. +TEST(Processors, ExecutingGraphManyInputPorts) +{ + constexpr size_t num_sources = 200000; + + auto header = std::make_shared(); + auto processors = std::make_shared(); + + auto resize = std::make_shared(header, num_sources, 1); + auto input = resize->getInputs().begin(); + for (size_t i = 0; i < num_sources; ++i, ++input) + { + auto source = std::make_shared(header); + connect(source->getPort(), *input); + processors->emplace_back(std::move(source)); + } + + auto sink = std::make_shared(header); + connect(resize->getOutputs().front(), sink->getPort()); + processors->emplace_back(std::move(resize)); + processors->emplace_back(std::move(sink)); + + Stopwatch watch; + ExecutingGraph graph(processors, false); + double elapsed = watch.elapsedSeconds(); + + ASSERT_LT(elapsed, 5) << "Building ExecutingGraph with " << num_sources << " input ports took " << elapsed << " sec"; +} diff --git a/tests/queries/0_stateless/03221_merge_profile_events.reference b/tests/queries/0_stateless/03221_merge_profile_events.reference index 6558ad3e5811..c0fdf2217462 100644 --- a/tests/queries/0_stateless/03221_merge_profile_events.reference +++ b/tests/queries/0_stateless/03221_merge_profile_events.reference @@ -1,3 +1,3 @@ -Horizontal 1 20000 3 0 480000 1 1 1 1 1 1 -Vertical 1 20000 1 2 480000 1 1 1 1 1 1 1 1 -Vertical 2 400000 2 3 8000000 1 1 1 1 1 1 1 1 1 1 1 1 +Horizontal 1 20000 3 0 480000 1 1 1 1 1 +Vertical 1 20000 1 2 480000 1 1 1 1 1 1 1 +Vertical 2 400000 2 3 8000000 1 1 1 1 1 1 1 1 1 1 1 diff --git a/tests/queries/0_stateless/03221_merge_profile_events.sql b/tests/queries/0_stateless/03221_merge_profile_events.sql index 8ebaf7121904..ad9702844def 100644 --- a/tests/queries/0_stateless/03221_merge_profile_events.sql +++ b/tests/queries/0_stateless/03221_merge_profile_events.sql @@ -23,7 +23,6 @@ SELECT ProfileEvents['MergeExecuteMilliseconds'] > 0, ProfileEvents['MergeHorizontalStageTotalMilliseconds'] > 0, ProfileEvents['MergeHorizontalStageExecuteMilliseconds'] > 0, - ProfileEvents['UserTimeMicroseconds'] > 0, ProfileEvents['OSCPUVirtualTimeMicroseconds'] > 0, FROM system.part_log WHERE database = currentDatabase() AND table = 't_merge_profile_events_1' AND event_type = 'MergeParts' AND part_name = 'all_1_2_1'; @@ -54,7 +53,6 @@ SELECT ProfileEvents['MergeHorizontalStageExecuteMilliseconds'] > 0, ProfileEvents['MergeVerticalStageTotalMilliseconds'] > 0, ProfileEvents['MergeVerticalStageExecuteMilliseconds'] > 0, - ProfileEvents['UserTimeMicroseconds'] > 0, ProfileEvents['OSCPUVirtualTimeMicroseconds'] > 0, FROM system.part_log WHERE database = currentDatabase() AND table = 't_merge_profile_events_2' AND event_type = 'MergeParts' AND part_name = 'all_1_2_1'; @@ -89,7 +87,6 @@ SELECT ProfileEvents['MergeProjectionStageExecuteMilliseconds'] > 0, ProfileEvents['MergeExecuteMilliseconds'] <= duration_ms, ProfileEvents['MergeTotalMilliseconds'] <= duration_ms, - ProfileEvents['UserTimeMicroseconds'] > 0, ProfileEvents['OSCPUVirtualTimeMicroseconds'] > 0, FROM system.part_log WHERE database = currentDatabase() AND table = 't_merge_profile_events_3' AND event_type = 'MergeParts' AND part_name = 'all_1_2_1';