userver: userver/ydb/federated_topic.hpp Source File
Loading...
Searching...
No Matches
federated_topic.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/ydb/federated_topic.hpp
4/// @brief YDB Federated Topic client
5///
6/// Federated Topic SDK serves as a wrapper over Topic SDK, coordinates
7/// reading from and writing to federated YDB installations topics, has a subset of functions
8/// of usual Topic SDK
9///
10/// @ref userver/ydb/topic.hpp
11
12#include <chrono>
13#include <memory>
14#include <optional>
15
16#include <ydb-cpp-sdk/client/federated_topic/federated_topic.h>
17#include <ydb-cpp-sdk/client/types/executor/executor.h>
18
19#include <userver/compiler/impl/lifetime.hpp>
20#include <userver/ydb/topic.hpp>
21
22USERVER_NAMESPACE_BEGIN
23
24namespace ydb {
25
26namespace impl {
27class Driver;
28struct TopicSettings;
29} // namespace impl
30
31/// @brief Read session used to connect to one or more topics for reading
32///
33/// @see https://ydb.tech/docs/en/reference/ydb-sdk/topic#reading
34///
35/// ## Example usage:
36///
37/// @ref samples/ydb_service/components/federated_topic_reader.hpp
38/// @ref samples/ydb_service/components/federated_topic_reader.cpp
39///
40/// @example samples/ydb_service/components/federated_topic_reader.hpp
41class FederatedTopicReadSession final {
42public:
43 /// @cond
44 // For internal use only.
45 explicit FederatedTopicReadSession(std::shared_ptr<NYdb::NFederatedTopic::IFederatedReadSession> read_session);
46 /// @endcond
47
48 /// @brief Get read session events
49 ///
50 /// Waits until event occurs
51 /// @param max_events_count maximum events count in batch
52 /// @param max_size_bytes total size limit for data messages in batch
53 /// if not specified, read session chooses event batch size automatically
57 );
58
59 /// @brief Close read session
60 ///
61 /// Waits for all commit acknowledgments to arrive.
62 /// Force close after timeout
63 bool Close(std::chrono::milliseconds timeout);
64
65 /// @brief Get native read session
66 ///
67 /// @warning Use with care! Facilities from @ref userver/drivers/subscribable_futures.hpp can help
68 /// with non-blocking wait operations.
69 NYdb::NFederatedTopic::IFederatedReadSession& GetNativeTopicReadSession() USERVER_IMPL_LIFETIME_BOUND;
70
71private:
72 std::shared_ptr<NYdb::NFederatedTopic::IFederatedReadSession> read_session_;
73};
74
75/// @ingroup userver_clients
76///
77/// @brief YDB Federated Topic Client
78///
79/// @see https://ydb.tech/docs/en/concepts/topic
80class FederatedTopicClient final {
81public:
82 /// @cond
83 // For internal use only.
84 FederatedTopicClient(std::shared_ptr<impl::Driver> driver, impl::TopicSettings settings);
85 /// @endcond
86
87 ~FederatedTopicClient();
88
89 /// Create read session
90 FederatedTopicReadSession CreateReadSession(const NYdb::NFederatedTopic::TFederatedReadSessionSettings& settings);
91
92 /// @brief Create a write session with the provided Federated Topic SDK settings.
93 /// Use @ref ydb::TopicWriteSession to write messages, receive acknowledgements and close the session.
94 TopicWriteSession CreateWriteSession(const NYdb::NFederatedTopic::TFederatedWriteSessionSettings& settings);
95
96 /// Get native topic client
97 /// @warning Use with care! Facilities from
98 /// `<core/include/userver/drivers/subscribable_futures.hpp>` can help with
99 /// non-blocking wait operations.
100 NYdb::NFederatedTopic::TFederatedTopicClient& GetNativeTopicClient() USERVER_IMPL_LIFETIME_BOUND;
101
102private:
103 std::shared_ptr<impl::Driver> driver_;
104 // Owned executors: Stop() only after `topic_client_` is destroyed (see
105 // ~FederatedTopicClient). Joining these threads after the native client is
106 // gone avoids atexit use-after-destroy (e.g. SEGV in TCodecMap). Stopping
107 // them while TFederatedTopicClient is still alive would deadlock or stall
108 // writes.
109 NYdb::IExecutor::TPtr compression_executor_;
110 NYdb::IExecutor::TPtr handlers_executor_;
111 // `reset()` in ~FederatedTopicClient runs before Stop() on the executors
112 // above.
113 std::optional<NYdb::NFederatedTopic::TFederatedTopicClient> topic_client_;
114};
115
116} // namespace ydb
117
118USERVER_NAMESPACE_END