forked from ClickHouse/ClickHouse
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExceptionKeepingTransform.cpp
More file actions
164 lines (133 loc) · 4.13 KB
/
Copy pathExceptionKeepingTransform.cpp
File metadata and controls
164 lines (133 loc) · 4.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include <Processors/Transforms/ExceptionKeepingTransform.h>
#include <Common/ThreadStatus.h>
#include <Common/Stopwatch.h>
#include <base/scope_guard.h>
#include <iostream>
namespace DB
{
namespace ErrorCodes
{
extern const int LOGICAL_ERROR;
}
ExceptionKeepingTransform::ExceptionKeepingTransform(const Block & in_header, const Block & out_header, bool ignore_on_start_and_finish_)
: IProcessor({in_header}, {out_header})
, input(inputs.front()), output(outputs.front())
, ignore_on_start_and_finish(ignore_on_start_and_finish_)
{
}
IProcessor::Status ExceptionKeepingTransform::prepare()
{
if (!ignore_on_start_and_finish && !was_on_start_called)
return Status::Ready;
/// Check can output.
if (output.isFinished())
throw Exception(ErrorCodes::LOGICAL_ERROR, "Output port is finished for {}", getName());
if (!output.canPush())
{
input.setNotNeeded();
return Status::PortFull;
}
/// Output if has data.
if (ready_output)
{
output.pushData(std::move(data));
ready_output = false;
return Status::PortFull;
}
if (!ready_input)
{
if (input.isFinished())
{
if (!ignore_on_start_and_finish && !was_on_finish_called && !has_exception)
return Status::Ready;
output.finish();
return Status::Finished;
}
input.setNeeded();
if (!input.hasData())
return Status::NeedData;
data = input.pullData(true);
if (data.exception)
{
has_exception = true;
output.pushData(std::move(data));
return Status::PortFull;
}
if (has_exception)
/// In case of exception, just drop all other data.
/// If transform is stateful, it's state may be broken after exception from transform()
data.chunk.clear();
else
ready_input = true;
}
return Status::Ready;
}
static std::exception_ptr runStep(std::function<void()> step, ThreadStatus * thread_status, std::atomic_uint64_t * elapsed_ms)
{
std::exception_ptr res;
std::optional<Stopwatch> watch;
auto * original_thread = current_thread;
SCOPE_EXIT({ current_thread = original_thread; });
if (thread_status)
{
/// Change thread context to store individual metrics. Once the work in done, go back to the original thread
thread_status->resetPerformanceCountersLastUsage();
current_thread = thread_status;
}
if (elapsed_ms)
watch.emplace();
try
{
step();
}
catch (...)
{
res = std::current_exception();
}
if (thread_status)
thread_status->updatePerformanceCounters();
if (elapsed_ms && watch)
*elapsed_ms += watch->elapsedMilliseconds();
return res;
}
void ExceptionKeepingTransform::work()
{
if (!ignore_on_start_and_finish && !was_on_start_called)
{
was_on_start_called = true;
if (auto exception = runStep([this] { onStart(); }, thread_status, elapsed_counter_ms))
{
has_exception = true;
ready_output = true;
data.exception = std::move(exception);
}
}
else if (ready_input)
{
ready_input = false;
if (auto exception = runStep([this] { transform(data.chunk); }, thread_status, elapsed_counter_ms))
{
has_exception = true;
data.chunk.clear();
data.exception = std::move(exception);
}
if (data.chunk || data.exception)
ready_output = true;
}
else if (!ignore_on_start_and_finish && !was_on_finish_called)
{
was_on_finish_called = true;
if (auto exception = runStep([this] { onFinish(); }, thread_status, elapsed_counter_ms))
{
has_exception = true;
ready_output = true;
data.exception = std::move(exception);
}
}
}
void ExceptionKeepingTransform::setRuntimeData(ThreadStatus * thread_status_, std::atomic_uint64_t * elapsed_counter_ms_)
{
thread_status = thread_status_;
elapsed_counter_ms = elapsed_counter_ms_;
}
}