userver: userver/ydb/topic.hpp Source File
Loading...
Searching...
No Matches
topic.hpp
Go to the documentation of this file.
1#pragma once
2
5
6#include <atomic>
7#include <chrono>
8#include <cstdint>
9#include <memory>
10#include <optional>
11#include <string>
12#include <string_view>
13
14#include <ydb-cpp-sdk/client/topic/client.h>
15#include <ydb-cpp-sdk/client/topic/producer.h>
16#include <ydb-cpp-sdk/client/types/executor/executor.h>
17
18#include <userver/compiler/impl/lifetime.hpp>
20#include <userver/utils/impl/internal_tag.hpp>
22
23USERVER_NAMESPACE_BEGIN
24
25namespace ydb {
26
27namespace impl {
28class Driver;
29struct TopicProducerStatistics;
30struct TopicSettings;
31} // namespace impl
32
43class TopicReadSession final {
44public:
46 // For internal use only.
47 explicit TopicReadSession(std::shared_ptr<NYdb::NTopic::IReadSession> read_session);
49
56 std::vector<NYdb::NTopic::TReadSessionEvent::TEvent> GetEvents(
57 std::optional<std::size_t> max_events_count = {},
58 size_t max_size_bytes = std::numeric_limits<size_t>::max()
59 );
60
65 std::vector<NYdb::NTopic::TReadSessionEvent::TEvent> GetEvents(
66 const NYdb::NTopic::TReadSessionGetEventSettings& settings
67 );
68
73 bool Close(std::chrono::milliseconds timeout);
74
79 NYdb::NTopic::IReadSession& GetNativeTopicReadSession() USERVER_IMPL_LIFETIME_BOUND;
80
81private:
82 std::shared_ptr<NYdb::NTopic::IReadSession> read_session_;
83};
84
88class TopicWriteSession final {
89public:
92 explicit TopicWriteSession(std::shared_ptr<NYdb::NTopic::IWriteSession> write_session);
94
98 NYdb::NTopic::TWriteSessionEvent::TEvent GetEvent();
99
106 std::optional<NYdb::NTopic::TWriteSessionEvent::TEvent> TryGetEvent();
107
111 void Write(NYdb::NTopic::TContinuationToken&& token, NYdb::NTopic::TWriteMessage&& message);
112
117 bool Close(std::chrono::milliseconds timeout);
118
123 NYdb::NTopic::IWriteSession& GetNativeTopicWriteSession() USERVER_IMPL_LIFETIME_BOUND;
124
125private:
126 std::shared_ptr<NYdb::NTopic::IWriteSession> write_session_;
127};
128
139class TopicSimpleWriteSession final {
140public:
143 explicit TopicSimpleWriteSession(std::shared_ptr<NYdb::NTopic::IWriteSession> write_session);
145
146 TopicSimpleWriteSession(const TopicSimpleWriteSession&) = delete;
147 TopicSimpleWriteSession& operator=(const TopicSimpleWriteSession&) = delete;
148 TopicSimpleWriteSession(TopicSimpleWriteSession&&) noexcept;
149 TopicSimpleWriteSession& operator=(TopicSimpleWriteSession&&) noexcept;
150
156 bool Write(
157 NYdb::NTopic::TWriteMessage&& message,
158 NYdb::TTransactionBase* tx = nullptr,
159 engine::Deadline deadline = {}
160 );
161
163 bool Write(
164 std::string_view data,
165 std::optional<std::uint64_t> seq_no = std::nullopt,
166 std::optional<std::chrono::system_clock::time_point> create_timestamp = std::nullopt,
167 engine::Deadline deadline = {}
168 );
169
171 std::uint64_t GetInitSeqNo(engine::Deadline deadline = {});
172
181 bool Close(std::chrono::milliseconds timeout);
182
184 bool IsAlive() const noexcept;
185
190 NYdb::NTopic::IWriteSession& GetNativeTopicWriteSession() USERVER_IMPL_LIFETIME_BOUND;
191
192private:
193 std::optional<NYdb::NTopic::TContinuationToken> WaitForToken(engine::Deadline deadline);
194
195 std::shared_ptr<NYdb::NTopic::IWriteSession> write_session_;
196 std::atomic_bool closed_{false};
197};
198
200class TopicProducerSettings final : public NYdb::NTopic::TProducerSettings {
201public:
207 TopicProducerSettings& CloseTimeout(std::chrono::milliseconds timeout) noexcept;
208
210 std::chrono::milliseconds GetCloseTimeout() const noexcept;
211
212private:
213 using NYdb::NTopic::TProducerSettings::MaxBlockTimeout;
214 using NYdb::NTopic::TProducerSettings::MaxBlockTimeout_;
215 using NYdb::NTopic::TProducerSettings::MaxMemoryUsage;
216 using NYdb::NTopic::TProducerSettings::MaxMemoryUsage_;
217
218 std::chrono::milliseconds close_timeout_{std::chrono::milliseconds::zero()};
219};
220
239class TopicProducer final {
240public:
243 explicit TopicProducer(
244 utils::impl::InternalTag,
245 std::shared_ptr<NYdb::NTopic::IProducer> producer,
246 std::chrono::milliseconds close_timeout
247 );
249
250 TopicProducer(const TopicProducer&) = delete;
251 TopicProducer(TopicProducer&&) noexcept;
252 TopicProducer& operator=(const TopicProducer&) = delete;
253 TopicProducer& operator=(TopicProducer&&) noexcept;
254 ~TopicProducer();
255
261 NYdb::NTopic::TWriteResult Write(NYdb::NTopic::TWriteMessage&& message);
262
267 NYdb::NTopic::TFlushResult Flush(engine::Deadline deadline = {});
268
274 NYdb::NTopic::TCloseResult Close(std::chrono::milliseconds timeout);
275
280 NYdb::NTopic::IProducer& GetNativeTopicProducer() USERVER_IMPL_LIFETIME_BOUND;
281
282private:
283 class Impl;
284 std::unique_ptr<Impl> impl_;
285 std::shared_ptr<impl::TopicProducerStatistics> statistics_;
286
287 friend void DumpMetric(utils::statistics::Writer& writer, const TopicProducer& producer);
288};
289
292void DumpMetric(utils::statistics::Writer& writer, const TopicProducer& producer);
293
299class TopicClient final {
300public:
301 static constexpr std::uint64_t kDefaultProducerMaxMemoryUsageBytes = 2ULL * 1024 * 1024 * 1024;
302
304 // For internal use only.
305 TopicClient(std::shared_ptr<impl::Driver> driver, impl::TopicSettings settings);
307
308 ~TopicClient();
309
311 void AlterTopic(const std::string& path, const NYdb::NTopic::TAlterTopicSettings& settings);
312
314 NYdb::NTopic::TDescribeTopicResult DescribeTopic(const std::string& path);
315
317 TopicReadSession CreateReadSession(const NYdb::NTopic::TReadSessionSettings& settings);
318
320 TopicWriteSession CreateWriteSession(const NYdb::NTopic::TWriteSessionSettings& settings);
321
326 TopicSimpleWriteSession CreateSimpleWriteSession(const NYdb::NTopic::TWriteSessionSettings& settings);
327
339 const TopicProducerSettings& settings,
340 std::uint64_t max_memory_usage_bytes = kDefaultProducerMaxMemoryUsageBytes
341 );
342
347 NYdb::NTopic::TTopicClient& GetNativeTopicClient() USERVER_IMPL_LIFETIME_BOUND;
348
349private:
350 std::shared_ptr<impl::Driver> driver_;
351 // Owned executors: Stop() only after `topic_client_` is destroyed (see
352 // ~TopicClient). Joining these threads after the native client is gone
353 // avoids atexit use-after-destroy (e.g. SEGV in TCodecMap). Stopping them
354 // while TTopicClient is still alive would deadlock or stall writes.
355 NYdb::IExecutor::TPtr compression_executor_;
356 NYdb::IExecutor::TPtr handlers_executor_;
357 // `reset()` in ~TopicClient runs before Stop() on the executors above.
358 std::optional<NYdb::NTopic::TTopicClient> topic_client_;
359};
360
361} // namespace ydb
362
363USERVER_NAMESPACE_END