Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 36 additions & 2 deletions src/Processors/Executors/ExecutingGraph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ ExecutingGraph::ExecutingGraph(std::shared_ptr<Processors> 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);
}
Expand All @@ -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;
Expand All @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -154,6 +185,9 @@ ExecutingGraph::UpdateNodeStatus ExecutingGraph::expandPipeline(boost::container

std::vector<uint64_t> 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))
Expand Down
11 changes: 11 additions & 0 deletions src/Processors/Executors/ExecutingGraph.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -166,6 +173,10 @@ class ExecutingGraph

std::shared_ptr<Processors> processors;
std::vector<bool> source_processors;

/// Port -> its position in processor's list of inputs or outputs. Filled by addPortNumbers.
std::unordered_map<const Port *, uint64_t> port_numbers;

std::mutex processors_mutex;

SharedMutex nodes_mutex;
Expand Down
39 changes: 39 additions & 0 deletions src/Processors/tests/gtest_executing_graph_many_ports.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include <gtest/gtest.h>

#include <Processors/Executors/ExecutingGraph.h>
#include <Processors/ResizeProcessor.h>
#include <Processors/Sinks/NullSink.h>
#include <Processors/Sources/NullSource.h>
#include <Common/Stopwatch.h>

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<const Block>();
auto processors = std::make_shared<Processors>();

auto resize = std::make_shared<ResizeProcessor>(header, num_sources, 1);
auto input = resize->getInputs().begin();
for (size_t i = 0; i < num_sources; ++i, ++input)
{
auto source = std::make_shared<NullSource>(header);
connect(source->getPort(), *input);
processors->emplace_back(std::move(source));
}

auto sink = std::make_shared<NullSink>(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";
}
Original file line number Diff line number Diff line change
@@ -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
3 changes: 0 additions & 3 deletions tests/queries/0_stateless/03221_merge_profile_events.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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';

Expand Down Expand Up @@ -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';

Expand Down
Loading