|
1 | 1 | #include "kafka_offset_commit_actor.h"
|
2 | 2 |
|
3 |
| -#include <ydb/core/kafka_proxy/kafka_events.h> |
4 |
| - |
5 | 3 | namespace NKafka {
|
6 | 4 |
|
7 | 5 |
|
8 | 6 | NActors::IActor* CreateKafkaOffsetCommitActor(const TContext::TPtr context, const ui64 correlationId, const TMessagePtr<TOffsetCommitRequestData>& message) {
|
9 | 7 | return new TKafkaOffsetCommitActor(context, correlationId, message);
|
10 | 8 | }
|
11 | 9 |
|
12 |
| -TOffsetCommitResponseData::TPtr TKafkaOffsetCommitActor::GetOffsetCommitResponse() { |
13 |
| - TOffsetCommitResponseData::TPtr response = std::make_shared<TOffsetCommitResponseData>(); |
| 10 | +TString TKafkaOffsetCommitActor::LogPrefix() { |
| 11 | + return "TKafkaOffsetCommitActor"; |
| 12 | +} |
| 13 | + |
| 14 | +void TKafkaOffsetCommitActor::Die(const TActorContext& ctx) { |
| 15 | + KAFKA_LOG_D("PassAway"); |
| 16 | + ctx.Send(AuthInitActor, new TEvents::TEvPoisonPill()); |
| 17 | + for (const auto& tabletToPipePair: TabletIdToPipe) { |
| 18 | + NTabletPipe::CloseClient(ctx, tabletToPipePair.second); |
| 19 | + } |
| 20 | + TBase::Die(ctx); |
| 21 | +} |
| 22 | + |
| 23 | +void TKafkaOffsetCommitActor::Handle(NKikimr::NGRpcProxy::V1::TEvPQProxy::TEvCloseSession::TPtr& ev, const TActorContext& ctx) { |
| 24 | + KAFKA_LOG_CRIT("Auth failed. reason# " << ev->Get()->Reason); |
| 25 | + Error = ConvertErrorCode(ev->Get()->ErrorCode); |
| 26 | + SendFailedForAllPartitions(Error, ctx); |
| 27 | +} |
14 | 28 |
|
| 29 | +void TKafkaOffsetCommitActor::SendFailedForAllPartitions(EKafkaErrors error, const TActorContext& ctx) { |
15 | 30 | for (auto topicReq: Message->Topics) {
|
16 | 31 | TOffsetCommitResponseData::TOffsetCommitResponseTopic topic;
|
17 | 32 | topic.Name = topicReq.Name;
|
18 | 33 | for (auto partitionRequest: topicReq.Partitions) {
|
19 | 34 | TOffsetCommitResponseData::TOffsetCommitResponseTopic::TOffsetCommitResponsePartition partition;
|
20 | 35 | partition.PartitionIndex = partitionRequest.PartitionIndex;
|
21 |
| - partition.ErrorCode = NONE_ERROR; |
| 36 | + partition.ErrorCode = error; |
22 | 37 | topic.Partitions.push_back(partition);
|
23 | 38 | }
|
24 |
| - response->Topics.push_back(topic); |
| 39 | + Response->Topics.push_back(topic); |
| 40 | + } |
| 41 | + Send(Context->ConnectionId, new TEvKafka::TEvResponse(CorrelationId, Response, Error)); |
| 42 | + Die(ctx); |
| 43 | +} |
| 44 | + |
| 45 | +void TKafkaOffsetCommitActor::Handle(TEvTabletPipe::TEvClientConnected::TPtr& ev, const TActorContext& ctx) { |
| 46 | + TEvTabletPipe::TEvClientConnected *msg = ev->Get(); |
| 47 | + |
| 48 | + if (msg->Status != NKikimrProto::OK) { |
| 49 | + KAFKA_LOG_CRIT("Pipe to tablet is dead. status# " << ev->Get()->Status); |
| 50 | + ProcessPipeProblem(msg->TabletId, ctx); |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +void TKafkaOffsetCommitActor::Handle(TEvTabletPipe::TEvClientDestroyed::TPtr& ev, const TActorContext& ctx) { |
| 55 | + KAFKA_LOG_CRIT("Pipe to tablet is destroyed"); |
| 56 | + ProcessPipeProblem(ev->Get()->TabletId, ctx); |
| 57 | +} |
| 58 | + |
| 59 | +void TKafkaOffsetCommitActor::ProcessPipeProblem(ui64 tabletId, const TActorContext& ctx) { |
| 60 | + auto cookiesIt = TabletIdToCookies.find(tabletId); |
| 61 | + Y_ABORT_UNLESS(cookiesIt != TabletIdToCookies.end()); |
| 62 | + |
| 63 | + for (auto cookie: cookiesIt->second) { |
| 64 | + auto requestInfoIt = CookieToRequestInfo.find(cookie); |
| 65 | + Y_ABORT_UNLESS(requestInfoIt != CookieToRequestInfo.end()); |
| 66 | + |
| 67 | + if (!requestInfoIt->second.Done) { |
| 68 | + requestInfoIt->second.Done = true; |
| 69 | + AddPartitionResponse(EKafkaErrors::UNKNOWN_SERVER_ERROR, requestInfoIt->second.TopicName, requestInfoIt->second.PartitionId, ctx); |
| 70 | + } |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +void TKafkaOffsetCommitActor::Handle(NGRpcProxy::V1::TEvPQProxy::TEvAuthResultOk::TPtr& ev, const TActorContext& ctx) { |
| 75 | + KAFKA_LOG_D("Auth success. Topics count: " << ev->Get()->TopicAndTablets.size()); |
| 76 | + TopicAndTablets = std::move(ev->Get()->TopicAndTablets); |
| 77 | + |
| 78 | + for (auto topicReq: Message->Topics) { |
| 79 | + auto topicIt = TopicAndTablets.find(NormalizePath(Context->DatabasePath, topicReq.Name.value())); |
| 80 | + for (auto partitionRequest: topicReq.Partitions) { |
| 81 | + if (topicIt == TopicAndTablets.end()) { |
| 82 | + AddPartitionResponse(UNKNOWN_TOPIC_OR_PARTITION, topicReq.Name.value(), partitionRequest.PartitionIndex, ctx); |
| 83 | + continue; |
| 84 | + } |
| 85 | + |
| 86 | + auto tabletIdIt = topicIt->second.PartitionIdToTabletId.find(partitionRequest.PartitionIndex); |
| 87 | + if (tabletIdIt == topicIt->second.PartitionIdToTabletId.end()) { |
| 88 | + AddPartitionResponse(UNKNOWN_TOPIC_OR_PARTITION, topicReq.Name.value(), partitionRequest.PartitionIndex, ctx); |
| 89 | + continue; |
| 90 | + } |
| 91 | + |
| 92 | + ui64 tabletId = tabletIdIt->second; |
| 93 | + |
| 94 | + if (!TabletIdToPipe.contains(tabletId)) { |
| 95 | + NTabletPipe::TClientConfig clientConfig; |
| 96 | + clientConfig.RetryPolicy = RetryPolicyForPipes; |
| 97 | + TabletIdToPipe[tabletId] = ctx.Register(NTabletPipe::CreateClient(ctx.SelfID, tabletId, clientConfig)); |
| 98 | + } |
| 99 | + |
| 100 | + NKikimrClient::TPersQueueRequest request; |
| 101 | + request.MutablePartitionRequest()->SetTopic(topicIt->second.TopicNameConverter->GetPrimaryPath()); |
| 102 | + request.MutablePartitionRequest()->SetPartition(partitionRequest.PartitionIndex); |
| 103 | + request.MutablePartitionRequest()->SetCookie(NextCookie); |
| 104 | + |
| 105 | + TRequestInfo info(topicReq.Name.value(), partitionRequest.PartitionIndex); |
| 106 | + |
| 107 | + CookieToRequestInfo.emplace(std::make_pair(NextCookie, info)); |
| 108 | + TabletIdToCookies[tabletId].push_back(NextCookie); |
| 109 | + NextCookie++; |
| 110 | + |
| 111 | + auto commit = request.MutablePartitionRequest()->MutableCmdSetClientOffset(); |
| 112 | + commit->SetClientId(Message->GroupId.value()); |
| 113 | + commit->SetOffset(partitionRequest.CommittedOffset); |
| 114 | + commit->SetStrict(true); |
| 115 | + |
| 116 | + PendingResponses++; |
| 117 | + KAFKA_LOG_D("Send commit request for group# " << Message->GroupId.value() << |
| 118 | + ", topic# " << topicIt->second.TopicNameConverter->GetPrimaryPath() << |
| 119 | + ", partition# " << partitionRequest.PartitionIndex << |
| 120 | + ", offset# " << partitionRequest.CommittedOffset); |
| 121 | + |
| 122 | + TAutoPtr<TEvPersQueue::TEvRequest> req(new TEvPersQueue::TEvRequest); |
| 123 | + req->Record.Swap(&request); |
| 124 | + |
| 125 | + NTabletPipe::SendData(ctx, TabletIdToPipe[tabletId], req.Release()); |
| 126 | + } |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +void TKafkaOffsetCommitActor::Handle(TEvPersQueue::TEvResponse::TPtr& ev, const TActorContext& ctx) { |
| 131 | + const auto& partitionResult = ev->Get()->Record.GetPartitionResponse(); |
| 132 | + auto requestInfo = CookieToRequestInfo.find(partitionResult.GetCookie()); |
| 133 | + requestInfo->second.Done = true; |
| 134 | + |
| 135 | + Y_ABORT_UNLESS(requestInfo != CookieToRequestInfo.end()); |
| 136 | + if (ev->Get()->Record.GetErrorCode() != NPersQueue::NErrorCode::OK) { |
| 137 | + KAFKA_LOG_CRIT("Commit offset error. status# " << EErrorCode_Name(ev->Get()->Record.GetErrorCode()) << ", reason# " << ev->Get()->Record.GetErrorReason()); |
25 | 138 | }
|
26 | 139 |
|
27 |
| - return response; |
| 140 | + AddPartitionResponse(ConvertErrorCode(NGRpcProxy::V1::ConvertOldCode(ev->Get()->Record.GetErrorCode())), requestInfo->second.TopicName, requestInfo->second.PartitionId, ctx); |
| 141 | +} |
| 142 | + |
| 143 | +void TKafkaOffsetCommitActor::AddPartitionResponse(EKafkaErrors error, const TString& topicName, ui64 partitionId, const TActorContext& ctx) { |
| 144 | + if (error != NONE_ERROR) { |
| 145 | + Error = error; |
| 146 | + } |
| 147 | + |
| 148 | + PendingResponses--; |
| 149 | + TOffsetCommitResponseData::TOffsetCommitResponseTopic::TOffsetCommitResponsePartition partitionResponse; |
| 150 | + partitionResponse.PartitionIndex = partitionId; |
| 151 | + partitionResponse.ErrorCode = error; |
| 152 | + |
| 153 | + auto topicIdIt = ResponseTopicIds.find(topicName); |
| 154 | + |
| 155 | + if (topicIdIt != ResponseTopicIds.end()) { |
| 156 | + Response->Topics[topicIdIt->second].Partitions.push_back(partitionResponse); |
| 157 | + } else { |
| 158 | + ResponseTopicIds[topicName] = Response->Topics.size(); |
| 159 | + |
| 160 | + TOffsetCommitResponseData::TOffsetCommitResponseTopic topicResponse; |
| 161 | + topicResponse.Name = topicName; |
| 162 | + topicResponse.Partitions.push_back(partitionResponse); |
| 163 | + |
| 164 | + Response->Topics.push_back(topicResponse); |
| 165 | + } |
| 166 | + |
| 167 | + if (PendingResponses == 0) { |
| 168 | + Send(Context->ConnectionId, new TEvKafka::TEvResponse(CorrelationId, Response, Error)); |
| 169 | + Die(ctx); |
| 170 | + } |
28 | 171 | }
|
29 | 172 |
|
30 | 173 | void TKafkaOffsetCommitActor::Bootstrap(const NActors::TActorContext& ctx) {
|
31 |
| - Y_UNUSED(Message); |
32 |
| - auto response = GetOffsetCommitResponse(); |
| 174 | + THashSet<TString> topicsToResolve; |
| 175 | + for (auto topicReq: Message->Topics) { |
| 176 | + topicsToResolve.insert(NormalizePath(Context->DatabasePath, topicReq.Name.value())); |
| 177 | + } |
33 | 178 |
|
34 |
| - Send(Context->ConnectionId, new TEvKafka::TEvResponse(CorrelationId, response, EKafkaErrors::NONE_ERROR)); |
35 |
| - Die(ctx); |
| 179 | + auto topicConverterFactory = std::make_shared<NPersQueue::TTopicNamesConverterFactory>( |
| 180 | + NKikimr::AppData(ctx)->PQConfig, "" |
| 181 | + ); |
| 182 | + |
| 183 | + auto topicHandler = std::make_unique<NPersQueue::TTopicsListController>( |
| 184 | + topicConverterFactory |
| 185 | + ); |
| 186 | + |
| 187 | + auto topicsToConverter = topicHandler->GetReadTopicsList(topicsToResolve, false, Context->DatabasePath); |
| 188 | + if (!topicsToConverter.IsValid) { |
| 189 | + KAFKA_LOG_CRIT("Commit offsets failed. reason# topicsToConverter is not valid"); |
| 190 | + Error = INVALID_REQUEST; |
| 191 | + SendFailedForAllPartitions(Error, ctx); |
| 192 | + return; |
| 193 | + } |
| 194 | + |
| 195 | + AuthInitActor = ctx.Register(new NKikimr::NGRpcProxy::V1::TReadInitAndAuthActor( |
| 196 | + ctx, ctx.SelfID, Message->GroupId.value(), 0, "", |
| 197 | + NKikimr::NMsgBusProxy::CreatePersQueueMetaCacheV2Id(), NKikimr::MakeSchemeCacheID(), nullptr, Context->UserToken, topicsToConverter, |
| 198 | + topicHandler->GetLocalCluster(), false) |
| 199 | + ); |
| 200 | + |
| 201 | + Become(&TKafkaOffsetCommitActor::StateWork); |
36 | 202 | }
|
37 | 203 |
|
38 | 204 | } // NKafka
|
0 commit comments