-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathlive_threaded.cpp
More file actions
256 lines (218 loc) · 9.51 KB
/
live_threaded.cpp
File metadata and controls
256 lines (218 loc) · 9.51 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
#include "databento/live_threaded.hpp"
#include <atomic>
#include <chrono> // milliseconds
#include <condition_variable>
#include <exception>
#include <mutex>
#include <sstream>
#include <thread>
#include <utility> // forward, move, swap
#include "databento/detail/scoped_thread.hpp" // ScopedThread
#include "databento/live.hpp" // LiveBuilder
#include "databento/live_blocking.hpp" // LiveBlocking
#include "databento/log.hpp" // ILogReceiver
using databento::LiveThreaded;
struct LiveThreaded::Impl {
template <typename... A>
explicit Impl(ILogReceiver* log_recv, A&&... args)
: log_receiver{log_recv}, blocking{log_receiver, std::forward<A>(args)...} {}
void NotifyOfStop() {
const std::lock_guard<std::mutex> lock{last_cb_ret_mutex};
last_cb_ret = KeepGoing::Stop;
last_cb_ret_cv.notify_all();
}
ILogReceiver* log_receiver;
std::atomic<std::thread::id> thread_id_{};
// Set to false when destructor is called
std::atomic<bool> keep_going{true};
KeepGoing last_cb_ret{KeepGoing::Continue};
std::mutex last_cb_ret_mutex;
std::condition_variable last_cb_ret_cv;
LiveBlocking blocking;
};
databento::LiveBuilder LiveThreaded::Builder() { return databento::LiveBuilder{}; }
LiveThreaded::LiveThreaded(LiveThreaded&& other) noexcept
: impl_{std::move(other.impl_)}, thread_{std::move(other.thread_)} {}
LiveThreaded& LiveThreaded::operator=(LiveThreaded&& rhs) noexcept {
if (impl_) {
impl_->keep_going.store(false, std::memory_order_relaxed);
}
std::swap(impl_, rhs.impl_);
std::swap(thread_, rhs.thread_);
return *this;
}
LiveThreaded::~LiveThreaded() {
if (impl_) {
impl_->keep_going.store(false, std::memory_order_relaxed);
}
}
LiveThreaded::LiveThreaded(
ILogReceiver* log_receiver, std::string key, std::string dataset, bool send_ts_out,
VersionUpgradePolicy upgrade_policy,
std::optional<std::chrono::seconds> heartbeat_interval, std::size_t buffer_size,
std::string user_agent_ext, databento::Compression compression,
std::optional<databento::SlowReaderBehavior> slow_reader_behavior)
: impl_{std::make_unique<Impl>(log_receiver, std::move(key), std::move(dataset),
send_ts_out, upgrade_policy, heartbeat_interval,
buffer_size, std::move(user_agent_ext), compression,
slow_reader_behavior)} {}
LiveThreaded::LiveThreaded(
ILogReceiver* log_receiver, std::string key, std::string dataset,
std::string gateway, std::uint16_t port, bool send_ts_out,
VersionUpgradePolicy upgrade_policy,
std::optional<std::chrono::seconds> heartbeat_interval, std::size_t buffer_size,
std::string user_agent_ext, databento::Compression compression,
std::optional<databento::SlowReaderBehavior> slow_reader_behavior)
: impl_{std::make_unique<Impl>(
log_receiver, std::move(key), std::move(dataset), std::move(gateway), port,
send_ts_out, upgrade_policy, heartbeat_interval, buffer_size,
std::move(user_agent_ext), compression, slow_reader_behavior)} {}
const std::string& LiveThreaded::Key() const { return impl_->blocking.Key(); }
const std::string& LiveThreaded::Dataset() const { return impl_->blocking.Dataset(); }
const std::string& LiveThreaded::Gateway() const { return impl_->blocking.Gateway(); }
std::uint16_t LiveThreaded::Port() const { return impl_->blocking.Port(); }
bool LiveThreaded::SendTsOut() const { return impl_->blocking.SendTsOut(); }
databento::VersionUpgradePolicy LiveThreaded::UpgradePolicy() const {
return impl_->blocking.UpgradePolicy();
}
std::optional<std::chrono::seconds> LiveThreaded::HeartbeatInterval() const {
return impl_->blocking.HeartbeatInterval();
}
databento::Compression LiveThreaded::Compression() const {
return impl_->blocking.Compression();
}
std::optional<databento::SlowReaderBehavior> LiveThreaded::SlowReaderBehavior() const {
return impl_->blocking.SlowReaderBehavior();
}
const std::vector<databento::LiveSubscription>& LiveThreaded::Subscriptions() const {
return impl_->blocking.Subscriptions();
}
std::vector<databento::LiveSubscription>& LiveThreaded::Subscriptions() {
return impl_->blocking.Subscriptions();
}
void LiveThreaded::Subscribe(const std::vector<std::string>& symbols, Schema schema,
SType stype_in) {
impl_->blocking.Subscribe(symbols, schema, stype_in);
}
void LiveThreaded::Subscribe(const std::vector<std::string>& symbols, Schema schema,
SType stype_in, UnixNanos start) {
impl_->blocking.Subscribe(symbols, schema, stype_in, start);
}
void LiveThreaded::Subscribe(const std::vector<std::string>& symbols, Schema schema,
SType stype_in, const std::string& start) {
impl_->blocking.Subscribe(symbols, schema, stype_in, start);
}
void LiveThreaded::SubscribeWithSnapshot(const std::vector<std::string>& symbols,
Schema schema, SType stype_in) {
impl_->blocking.SubscribeWithSnapshot(symbols, schema, stype_in);
}
void LiveThreaded::Start(RecordCallback callback) {
Start({}, std::move(callback), {});
}
void LiveThreaded::Start(databento::MetadataCallback metadata_callback,
RecordCallback record_callback) {
Start(std::move(metadata_callback), std::move(record_callback), {});
}
void LiveThreaded::Start(MetadataCallback metadata_callback,
RecordCallback record_callback,
ExceptionCallback exception_callback) {
// Deadlock check
if (std::this_thread::get_id() == impl_->thread_id_) {
std::ostringstream log_ss;
log_ss << "[LiveThreaded::Start] Called Start from callback thread, which "
"would cause a deadlock. Ignoring.";
impl_->log_receiver->Receive(LogLevel::Warning, log_ss.str());
return;
}
// Safe to pass raw pointer because `thread_` cannot outlive `impl_`
thread_ = detail::ScopedThread{
&LiveThreaded::ProcessingThread, impl_.get(), std::move(metadata_callback),
std::move(record_callback), std::move(exception_callback)};
}
void LiveThreaded::Reconnect() { impl_->blocking.Reconnect(); }
void LiveThreaded::Resubscribe() { impl_->blocking.Resubscribe(); }
void LiveThreaded::BlockForStop() {
std::unique_lock<std::mutex> lock{impl_->last_cb_ret_mutex};
auto* impl = impl_.get();
// wait for stop
impl_->last_cb_ret_cv.wait(lock,
[impl] { return impl->last_cb_ret == KeepGoing::Stop; });
}
databento::KeepGoing LiveThreaded::BlockForStop(std::chrono::milliseconds timeout) {
std::unique_lock<std::mutex> lock{impl_->last_cb_ret_mutex};
auto* impl = impl_.get();
// wait for stop
if (impl_->last_cb_ret_cv.wait_for(
lock, timeout, [impl] { return impl->last_cb_ret == KeepGoing::Stop; })) {
return KeepGoing::Stop;
}
return KeepGoing::Continue;
}
void LiveThreaded::ProcessingThread(Impl* impl, MetadataCallback&& metadata_callback,
RecordCallback&& record_callback,
ExceptionCallback&& exception_callback) {
// Thread safety: non-const calls to `blocking` are only performed from this
// thread
static constexpr auto kMethodName = "LiveThreaded::ProcessingThread";
constexpr std::chrono::milliseconds kTimeout{50};
impl->thread_id_ = std::this_thread::get_id();
const auto metadata_cb{std::move(metadata_callback)};
const auto record_cb{std::move(record_callback)};
const auto exception_cb{std::move(exception_callback)};
// Start loop
while (impl->keep_going.load(std::memory_order_relaxed)) {
try {
auto metadata = impl->blocking.Start();
if (metadata_cb) {
metadata_cb(std::move(metadata));
}
} catch (const std::exception& exc) {
if (ExceptionHandler(impl, exception_cb, exc, kMethodName,
"Caught exception starting session: ") ==
ExceptionAction::Restart) {
continue; // restart Start loop
} else {
return;
}
}
// NextRecord loop
while (impl->keep_going.load(std::memory_order_relaxed)) {
try {
const Record* rec = impl->blocking.NextRecord(kTimeout);
if (rec) {
if (record_cb(*rec) == KeepGoing::Stop) {
impl->blocking.Stop();
impl->NotifyOfStop();
return;
}
} // else timeout
} catch (const std::exception& exc) {
if (ExceptionHandler(impl, exception_cb, exc, kMethodName,
"Caught exception reading next record: ") ==
ExceptionAction::Restart) {
break; // break out of NextRecord loop, to restart Start loop
} else {
impl->NotifyOfStop();
return;
}
}
}
}
}
LiveThreaded::ExceptionAction LiveThreaded::ExceptionHandler(
Impl* impl, const ExceptionCallback& exception_callback, const std::exception& exc,
std::string_view pretty_function_name, std::string_view message) {
if (exception_callback && exception_callback(exc) == ExceptionAction::Restart) {
std::ostringstream log_ss;
log_ss << pretty_function_name << ' ' << message << exc.what()
<< ". Attempting to restart session.";
impl->log_receiver->Receive(LogLevel::Warning, log_ss.str());
return ExceptionAction::Restart;
}
impl->blocking.Stop();
std::ostringstream log_ss;
log_ss << pretty_function_name << ' ' << message << exc.what()
<< ". Stopping thread.";
impl->log_receiver->Receive(LogLevel::Error, log_ss.str());
return ExceptionAction::Stop;
}