44#include < ydb/services/persqueue_v1/actors/schema_actors.h>
55#include < ydb/core/grpc_services/grpc_endpoint.h>
66#include < ydb/core/base/statestorage.h>
7+ #include < ydb/core/persqueue/list_all_topics_actor.h>
78
89namespace NKafka {
910using namespace NKikimr ;
@@ -32,12 +33,16 @@ void TKafkaMetadataActor::Bootstrap(const TActorContext& ctx) {
3233 SendDiscoveryRequest ();
3334
3435 if (Message->Topics .size () == 0 ) {
35- NeedCurrentNode = true ;
36+ ctx.Register (NKikimr::NPersQueue::MakeListAllTopicsActor (
37+ SelfId (), Context->DatabasePath , GetUserSerializedToken (Context), true , {}, {}));
38+
39+ PendingResponses++;
40+ NeedAllNodes = true ;
3641 }
3742 }
3843
3944 if (Message->Topics .size () != 0 ) {
40- ProcessTopics ();
45+ ProcessTopicsFromRequest ();
4146 }
4247
4348 Become (&TKafkaMetadataActor::StateWork);
@@ -111,39 +116,54 @@ void TKafkaMetadataActor::HandleNodesResponse(
111116 RespondIfRequired (ctx);
112117}
113118
114- void TKafkaMetadataActor::AddProxyNodeToBrokers () {
115- AddBroker (ProxyNodeId, Context->Config .GetProxy ().GetHostname (), Context->Config .GetProxy ().GetPort ());
116- }
117-
118- void TKafkaMetadataActor::ProcessTopics () {
119- THashMap<TString, TActorId> partitionActors;
119+ void TKafkaMetadataActor::ProcessTopicsFromRequest () {
120+ TVector<TString> topicsToRequest;
120121 for (size_t i = 0 ; i < Message->Topics .size (); ++i) {
121- Response->Topics [i] = TMetadataResponseData::TMetadataResponseTopic{};
122122 auto & reqTopic = Message->Topics [i];
123- Response->Topics [i].Name = reqTopic.Name .value_or (" " );
124-
125123 if (!reqTopic.Name .value_or (" " )) {
126124 AddTopicError (Response->Topics [i], EKafkaErrors::INVALID_TOPIC_EXCEPTION);
127125 continue ;
128126 }
129- const auto & topicName = reqTopic.Name .value ();
130- TActorId child;
131- auto namesIter = partitionActors.find (topicName);
132- if (namesIter.IsEnd ()) {
133- child = SendTopicRequest (reqTopic);
134- partitionActors[topicName] = child;
135- } else {
136- child = namesIter->second ;
137- }
138- TopicIndexes[child].push_back (i);
127+ AddTopic (reqTopic.Name .value_or (" " ), i);
139128 }
140129}
141130
142- TActorId TKafkaMetadataActor::SendTopicRequest (const TMetadataRequestData::TMetadataRequestTopic& topicRequest) {
143- KAFKA_LOG_D (" Describe partitions locations for topic '" << *topicRequest.Name << " ' for user '" << GetUsernameOrAnonymous (Context) << " '" );
131+ void TKafkaMetadataActor::HandleListTopics (NKikimr::TEvPQ::TEvListAllTopicsResponse::TPtr& ev) {
132+ Y_ABORT_UNLESS (PendingResponses > 0 );
133+ PendingResponses--;
134+ auto topics = std::move (ev->Get ()->Topics );
135+ Response->Topics .resize (topics.size ());
136+ for (size_t i = 0 ; i < topics.size (); ++i) {
137+ AddTopic (topics[i], i);
138+ }
139+ RespondIfRequired (ActorContext ());
140+ }
141+
142+ void TKafkaMetadataActor::AddProxyNodeToBrokers () {
143+ AddBroker (ProxyNodeId, Context->Config .GetProxy ().GetHostname (), Context->Config .GetProxy ().GetPort ());
144+ }
145+
146+
147+ void TKafkaMetadataActor::AddTopic (const TString& topic, ui64 index) {
148+ Response->Topics [index] = TMetadataResponseData::TMetadataResponseTopic{};
149+ Response->Topics [index].Name = topic;
150+
151+ TActorId child;
152+ auto namesIter = PartitionActors.find (topic);
153+ if (namesIter.IsEnd ()) {
154+ child = SendTopicRequest (topic);
155+ PartitionActors[topic] = child;
156+ } else {
157+ child = namesIter->second ;
158+ }
159+ TopicIndexes[child].push_back (index);
160+ }
161+
162+ TActorId TKafkaMetadataActor::SendTopicRequest (const TString& topic) {
163+ KAFKA_LOG_D (" Describe partitions locations for topic '" << topic << " ' for user '" << GetUsernameOrAnonymous (Context) << " '" );
144164
145165 TGetPartitionsLocationRequest locationRequest{};
146- locationRequest.Topic = NormalizePath (Context->DatabasePath , topicRequest. Name . value () );
166+ locationRequest.Topic = NormalizePath (Context->DatabasePath , topic );
147167 locationRequest.Token = GetUserSerializedToken (Context);
148168 locationRequest.Database = Context->DatabasePath ;
149169
@@ -193,7 +213,7 @@ void TKafkaMetadataActor::AddTopicResponse(
193213
194214 topic.Partitions .emplace_back (std::move (responsePartition));
195215
196- if (!WithProxy) {
216+ if (!WithProxy && !NeedAllNodes ) {
197217 auto ins = AllClusterNodes.insert (part.NodeId );
198218 if (ins.second ) {
199219 auto hostname = (*nodeIter)->Host ;
@@ -265,16 +285,6 @@ void TKafkaMetadataActor::RespondIfRequired(const TActorContext& ctx) {
265285 return ;
266286 }
267287
268- if (NeedCurrentNode) {
269- auto nodeIter = Nodes.find (SelfId ().NodeId ());
270- if (nodeIter.IsEnd ()) {
271- // Node info was not found, request from IC nodes cache instead
272- RequestICNodeCache ();
273- return ;
274- }
275- AddBroker (nodeIter->first , nodeIter->second .Host , nodeIter->second .Port );
276- NeedCurrentNode = false ;
277- }
278288 while (!PendingTopicResponses.empty ()) {
279289 auto & [index, ev] = *PendingTopicResponses.begin ();
280290 auto & topic = Response->Topics [index];
@@ -295,6 +305,11 @@ void TKafkaMetadataActor::RespondIfRequired(const TActorContext& ctx) {
295305 PendingTopicResponses.erase (PendingTopicResponses.begin ());
296306 }
297307
308+ if (NeedAllNodes) {
309+ for (const auto & [id, nodeInfo] : Nodes)
310+ AddBroker (id, nodeInfo.Host , nodeInfo.Port );
311+ }
312+
298313 Respond ();
299314}
300315
0 commit comments