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 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
21USERVER_NAMESPACE_BEGIN
22
23namespace ydb {
24
25namespace impl {
26class Driver;
27struct TopicSettings;
28} // namespace impl
29
30/// @brief Read session used to connect to one or more topics for reading
31///
32/// @see https://ydb.tech/docs/en/reference/ydb-sdk/topic#reading
33///
34/// ## Example usage:
35///
36/// @ref samples/ydb_service/components/federated_topic_reader.hpp
37/// @ref samples/ydb_service/components/federated_topic_reader.cpp
38///
39/// @example samples/ydb_service/components/federated_topic_reader.hpp
40class FederatedTopicReadSession final {
41public:
42 /// @cond
43 // For internal use only.
44 explicit FederatedTopicReadSession(std::shared_ptr<NYdb::NFederatedTopic::IFederatedReadSession> read_session);
45 /// @endcond
46
47 /// @brief Get read session events
48 ///
49 /// Waits until event occurs
50 /// @param max_events_count maximum events count in batch
51 /// @param max_size_bytes total size limit for data messages in batch
52 /// if not specified, read session chooses event batch size automatically
56 );
57
58 /// @brief Close read session
59 ///
60 /// Waits for all commit acknowledgments to arrive.
61 /// Force close after timeout
62 bool Close(std::chrono::milliseconds timeout);
63
64 /// @brief Get native read session
65 ///
66 /// @warning Use with care! Facilities from @ref userver/drivers/subscribable_futures.hpp can help
67 /// with non-blocking wait operations.
68 NYdb::NFederatedTopic::IFederatedReadSession& GetNativeTopicReadSession() USERVER_IMPL_LIFETIME_BOUND;
69
70private:
71 std::shared_ptr<NYdb::NFederatedTopic::IFederatedReadSession> read_session_;
72};
73
74/// @ingroup userver_clients
75///
76/// @brief YDB Federated Topic Client
77///
78/// @see https://ydb.tech/docs/en/concepts/topic
79class FederatedTopicClient final {
80public:
81 /// @cond
82 // For internal use only.
83 FederatedTopicClient(std::shared_ptr<impl::Driver> driver, impl::TopicSettings settings);
84 /// @endcond
85
86 ~FederatedTopicClient();
87
88 /// Create read session
89 FederatedTopicReadSession CreateReadSession(const NYdb::NFederatedTopic::TFederatedReadSessionSettings& settings);
90
91 /// Get native topic client
92 /// @warning Use with care! Facilities from
93 /// `<core/include/userver/drivers/subscribable_futures.hpp>` can help with
94 /// non-blocking wait operations.
95 NYdb::NFederatedTopic::TFederatedTopicClient& GetNativeTopicClient() USERVER_IMPL_LIFETIME_BOUND;
96
97private:
98 std::shared_ptr<impl::Driver> driver_;
99 // Owned executors: Stop() only after `topic_client_` is destroyed (see
100 // ~FederatedTopicClient). Joining these threads after the native client is
101 // gone avoids atexit use-after-destroy (e.g. SEGV in TCodecMap). Stopping
102 // them while TFederatedTopicClient is still alive would deadlock or stall
103 // writes.
104 NYdb::IExecutor::TPtr compression_executor_;
105 NYdb::IExecutor::TPtr handlers_executor_;
106 // `reset()` in ~FederatedTopicClient runs before Stop() on the executors
107 // above.
108 std::optional<NYdb::NFederatedTopic::TFederatedTopicClient> topic_client_;
109};
110
111} // namespace ydb
112
113USERVER_NAMESPACE_END