-
Notifications
You must be signed in to change notification settings - Fork 730
DLQ policy for MLP consumer #28573
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
DLQ policy for MLP consumer #28573
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
75fce81
WIP
nshestakov b640782
more tests
nshestakov 92d7596
WIP
nshestakov 8ddb765
more tests
nshestakov c30c22e
WIP
nshestakov a6115c3
WIP
nshestakov 6bc6a00
Merge branch 'main' into MLP-dlq
nshestakov eec1ad2
compiled
nshestakov 7780210
test passed
nshestakov 000d6f1
WIP
nshestakov 5a9aa13
fix
nshestakov 22e7963
fix
nshestakov 4ffa24f
fix
nshestakov e0228e6
fix
nshestakov c2bda7a
WIP
nshestakov 21e860b
Merge branch 'main' into MLP-dlq
nshestakov 6e33dca
more tests
nshestakov 43ea96d
fix
nshestakov cc49397
typing
nshestakov 5dc9765
fix
nshestakov 50d8af9
fix
nshestakov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,4 @@ | ||
| #include "mlp_consumer.h" | ||
| #include "mlp_message_enricher.h" | ||
| #include "mlp_storage.h" | ||
|
|
||
| #include <ydb/core/persqueue/common/key.h> | ||
|
|
@@ -82,10 +81,11 @@ void AddReadWAL(std::unique_ptr<TEvKeyValue::TEvRequest>& request, ui32 partitio | |
| readWAL->SetIncludeData(true); | ||
| } | ||
|
|
||
| TConsumerActor::TConsumerActor(ui64 tabletId, const TActorId& tabletActorId, ui32 partitionId, | ||
| TConsumerActor::TConsumerActor(const TString& database,ui64 tabletId, const TActorId& tabletActorId, ui32 partitionId, | ||
nshestakov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| const TActorId& partitionActorId, const NKikimrPQ::TPQTabletConfig_TConsumer& config, | ||
| std::optional<TDuration> retentionPeriod) | ||
| : TBaseTabletActor(tabletId, tabletActorId, NKikimrServices::EServiceKikimr::PQ_MLP_CONSUMER) | ||
| , Database(database) | ||
| , PartitionId(partitionId) | ||
| , PartitionActorId(partitionActorId) | ||
| , Config(config) | ||
|
|
@@ -122,6 +122,10 @@ void TConsumerActor::PassAway() { | |
| ReplyErrorAll(SelfId(), UnlockRequestsQueue); | ||
| ReplyErrorAll(SelfId(), ChangeMessageDeadlineRequestsQueue); | ||
|
|
||
| if (DLQMoverActorId) { | ||
| Send(DLQMoverActorId, new TEvents::TEvPoison()); | ||
| } | ||
|
|
||
| TBase::PassAway(); | ||
| } | ||
|
|
||
|
|
@@ -202,6 +206,7 @@ void TConsumerActor::HandleOnInit(TEvKeyValue::TEvResponse::TPtr& ev) { | |
| LOG_D("Read snapshot"); | ||
| HasSnapshot = true; | ||
| LastWALIndex = snapshot.GetWALIndex(); | ||
| DLQMovedMessageCount = snapshot.GetMeta().GetDLQMovedMessages(); | ||
| Storage->Initialize(snapshot); | ||
| } else { | ||
| LOG_W("Received snapshot from old consumer generation: " << Config.GetGeneration() << " vs " << snapshot.GetConfiguration().GetGeneration()); | ||
|
|
@@ -238,6 +243,7 @@ void TConsumerActor::HandleOnInit(TEvKeyValue::TEvResponse::TPtr& ev) { | |
| if (Config.GetGeneration() == wal.GetGeneration()) { | ||
| LOG_D("Read WAL " << w.key()); | ||
| LastWALIndex = wal.GetWALIndex(); | ||
| DLQMovedMessageCount = wal.GetDLQMovedMessages(); | ||
| Storage->ApplyWAL(wal); | ||
| } else { | ||
| LOG_W("Received snapshot from old consumer generation: " << Config.GetGeneration() << " vs " << wal.GetGeneration()); | ||
|
|
@@ -311,7 +317,7 @@ void TConsumerActor::Handle(TEvKeyValue::TEvResponse::TPtr& ev) { | |
|
|
||
| if (!PendingReadQueue.empty()) { | ||
| auto msgs = std::exchange(PendingReadQueue, {}); | ||
| RegisterWithSameMailbox(new TMessageEnricherActor(TabletActorId, PartitionId, Config.GetName(), std::move(msgs))); | ||
| RegisterWithSameMailbox(CreateMessageEnricher(TabletId, PartitionId, Config.GetName(), std::move(msgs))); | ||
| } | ||
| ReplyOk<TEvPQ::TEvMLPCommitResponse>(SelfId(), PendingCommitQueue); | ||
| ReplyOk<TEvPQ::TEvMLPUnlockResponse>(SelfId(), PendingUnlockQueue); | ||
|
|
@@ -337,6 +343,11 @@ void TConsumerActor::UpdateStorageConfig() { | |
| Storage->SetKeepMessageOrder(Config.GetKeepMessageOrder()); | ||
| Storage->SetMaxMessageProcessingCount(Config.GetMaxProcessingAttempts()); | ||
| Storage->SetRetentionPeriod(RetentionPeriod); | ||
| if (Config.GetDeadLetterPolicyEnabled() && Config.GetDeadLetterPolicy() != NKikimrPQ::TPQTabletConfig::DEAD_LETTER_POLICY_UNSPECIFIED) { | ||
| Storage->SetDeadLetterPolicy(Config.GetDeadLetterPolicy()); | ||
| } else { | ||
| Storage->SetDeadLetterPolicy(std::nullopt); | ||
| } | ||
| } | ||
|
|
||
| void TConsumerActor::Handle(TEvPQ::TEvMLPConsumerUpdateConfig::TPtr& ev) { | ||
|
|
@@ -366,6 +377,10 @@ void TConsumerActor::Handle(TEvPQ::TEvGetMLPConsumerStateRequest::TPtr& ev) { | |
| Send(ev->Sender, std::move(response), 0, ev->Cookie); | ||
| } | ||
|
|
||
| void TConsumerActor::Handle(TEvPipeCache::TEvDeliveryProblem::TPtr&) { | ||
| FirstPipeCacheRequest = true; | ||
| } | ||
|
|
||
| STFUNC(TConsumerActor::StateInit) { | ||
| switch (ev->GetTypeRewrite()) { | ||
| hFunc(TEvPQ::TEvMLPReadRequest, Queue); | ||
|
|
@@ -395,7 +410,10 @@ STFUNC(TConsumerActor::StateWork) { | |
| hFunc(TEvPQ::TEvGetMLPConsumerStateRequest, Handle); | ||
| hFunc(TEvKeyValue::TEvResponse, Handle); | ||
| hFunc(TEvPQ::TEvProxyResponse, Handle); | ||
| hFunc(TEvPersQueue::TEvHasDataInfoResponse, Handle); | ||
| hFunc(TEvPQ::TEvError, Handle); | ||
| hFunc(TEvPipeCache::TEvDeliveryProblem, Handle); | ||
| hFunc(TEvPQ::TEvMLPDLQMoverResponse, Handle); | ||
| hFunc(TEvents::TEvWakeup, HandleOnWork); | ||
| sFunc(TEvents::TEvPoison, PassAway); | ||
| default: | ||
|
|
@@ -414,7 +432,10 @@ STFUNC(TConsumerActor::StateWrite) { | |
| hFunc(TEvPQ::TEvGetMLPConsumerStateRequest, Handle); | ||
| hFunc(TEvKeyValue::TEvResponse, Handle); | ||
| hFunc(TEvPQ::TEvProxyResponse, Handle); | ||
| hFunc(TEvPersQueue::TEvHasDataInfoResponse, Handle); | ||
| hFunc(TEvPQ::TEvError, Handle); | ||
| hFunc(TEvPipeCache::TEvDeliveryProblem, Handle); | ||
| hFunc(TEvPQ::TEvMLPDLQMoverResponse, Handle); | ||
| hFunc(TEvents::TEvWakeup, Handle); | ||
| sFunc(TEvents::TEvPoison, PassAway); | ||
| default: | ||
|
|
@@ -533,6 +554,7 @@ void TConsumerActor::Persist() { | |
|
|
||
| NKikimrPQ::TMLPStorageWAL wal; | ||
| wal.SetWALIndex(LastWALIndex); | ||
| wal.SetDLQMovedMessages(DLQMovedMessageCount); | ||
| batch.SerializeTo(wal); | ||
|
|
||
| auto data = wal.SerializeAsString(); | ||
|
|
@@ -559,6 +581,7 @@ void TConsumerActor::Persist() { | |
| Storage->SerializeTo(snapshot); | ||
|
|
||
| snapshot.SetWALIndex(LastWALIndex); | ||
| snapshot.MutableMeta()->SetDLQMovedMessages(DLQMovedMessageCount); | ||
|
|
||
| auto request = std::make_unique<TEvKeyValue::TEvRequest>(); | ||
|
|
||
|
|
@@ -583,6 +606,17 @@ void TConsumerActor::Persist() { | |
| } | ||
| } | ||
|
|
||
| size_t TConsumerActor::RequiredToFetchMessageCount() const { | ||
| auto& metrics = Storage->GetMetrics(); | ||
|
|
||
| auto maxMessages = Storage->MinMessages; | ||
| if (metrics.LockedMessageCount * 2 > metrics.UnprocessedMessageCount) { | ||
| maxMessages = std::max<size_t>(maxMessages, metrics.LockedMessageCount * 2 - metrics.UnprocessedMessageCount); | ||
| } | ||
|
|
||
| return std::min(maxMessages, Storage->MaxMessages - metrics.InflyMessageCount); | ||
| } | ||
|
|
||
| bool TConsumerActor::FetchMessagesIfNeeded() { | ||
| if (FetchInProgress) { | ||
| return false; | ||
|
|
@@ -602,12 +636,7 @@ bool TConsumerActor::FetchMessagesIfNeeded() { | |
|
|
||
| FetchInProgress = true; | ||
|
|
||
| auto maxMessages = Storage->MinMessages; | ||
| if (metrics.LockedMessageCount * 2 > metrics.UnprocessedMessageCount) { | ||
| maxMessages = std::max<size_t>(maxMessages, metrics.LockedMessageCount * 2 - metrics.UnprocessedMessageCount); | ||
| } | ||
| maxMessages = std::min(maxMessages, Storage->MaxMessages - metrics.InflyMessageCount); | ||
|
|
||
| auto maxMessages = RequiredToFetchMessageCount(); | ||
| LOG_D("Fetching " << maxMessages << " messages from offset " << Storage->GetLastOffset() << " from " << PartitionActorId); | ||
| Send(PartitionActorId, MakeEvRead(SelfId(), Config.GetName(), Storage->GetLastOffset(), maxMessages, ++FetchCookie)); | ||
|
|
||
|
|
@@ -665,32 +694,88 @@ void TConsumerActor::Handle(TEvPQ::TEvProxyResponse::TPtr& ev) { | |
| if (CurrentStateFunc() == &TConsumerActor::StateWork) { | ||
| ProcessEventQueue(); | ||
| } | ||
|
|
||
| if (!HasDataInProgress && RequiredToFetchMessageCount()) { | ||
| HasDataInProgress = true; | ||
| auto request = MakeEvHasData(SelfId(), PartitionId, Storage->GetLastOffset(), Config); | ||
| LOG_D("Subscribing to data: " << request->Record.ShortDebugString()); | ||
| SendToPQTablet(std::move(request)); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| void TConsumerActor::Handle(TEvPersQueue::TEvHasDataInfoResponse::TPtr&) { | ||
| LOG_D("Handle TEvPersQueue::TEvHasDataInfo"); | ||
| FetchMessagesIfNeeded(); | ||
| } | ||
|
|
||
| void TConsumerActor::Handle(TEvPQ::TEvError::TPtr& ev) { | ||
| Restart(TStringBuilder() << "Received error: " << ev->Get()->Error); | ||
| } | ||
|
|
||
| void TConsumerActor::HandleOnWork(TEvents::TEvWakeup::TPtr&) { | ||
| FetchMessagesIfNeeded(); | ||
| ProcessEventQueue(); | ||
| MoveToDLQIfPossible(); | ||
| Schedule(WakeupInterval, new TEvents::TEvWakeup()); | ||
| } | ||
|
|
||
| void TConsumerActor::MoveToDLQIfPossible() { | ||
| if (!DLQMoverActorId && !Storage->GetDLQMessages().empty()) { | ||
| std::deque<ui64> messages(Storage->GetDLQMessages()); | ||
| DLQMoverActorId = RegisterWithSameMailbox(CreateDLQMover({ | ||
| .ParentActorId = SelfId(), | ||
| .Database = Database, | ||
| .TabletId = TabletId, | ||
| .PartitionId = PartitionId, | ||
| .ConsumerName = Config.GetName(), | ||
| .ConsumerGeneration = Config.GetGeneration(), | ||
| .DestinationTopic = Config.GetDeadLetterQueue(), | ||
| .FirstMessageSeqNo = DLQMovedMessageCount + 1, | ||
| .Messages = std::move(messages) | ||
| })); | ||
| } | ||
| } | ||
|
|
||
| void TConsumerActor::Handle(TEvPQ::TEvMLPDLQMoverResponse::TPtr& ev) { | ||
| LOG_D("Handle TEvPQ::TEvMLPDLQMoverResponse"); | ||
|
|
||
| if (ev->Get()->Status != Ydb::StatusIds::SUCCESS) { | ||
| LOG_W("Error moving messages to the DLQ: " << ev->Get()->ErrorDescription); | ||
| } | ||
|
|
||
| auto& moved = ev->Get()->MovedMessages; | ||
| LOG_D("Moved to the DLQ: " << JoinRange(", ", moved.begin(), moved.end())); | ||
|
|
||
| DLQMoverActorId = {}; | ||
| for (auto offset : moved) { | ||
| AFL_ENSURE(Storage->MarkDLQMoved(offset))("o", offset); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Вот так писать нельзя никогда |
||
| } | ||
|
|
||
| DLQMovedMessageCount += moved.size(); | ||
| } | ||
|
|
||
| void TConsumerActor::Handle(TEvents::TEvWakeup::TPtr&) { | ||
| LOG_D("Handle TEvents::TEvWakeup"); | ||
| MoveToDLQIfPossible(); | ||
| Schedule(WakeupInterval, new TEvents::TEvWakeup()); | ||
| } | ||
|
|
||
| void TConsumerActor::SendToPQTablet(std::unique_ptr<IEventBase> ev) { | ||
| auto forward = std::make_unique<TEvPipeCache::TEvForward>(ev.release(), TabletId, FirstPipeCacheRequest, 1); | ||
| Send(MakePipePerNodeCacheID(false), forward.release(), IEventHandle::FlagTrackDelivery); | ||
| FirstPipeCacheRequest = false; | ||
| } | ||
|
|
||
| NActors::IActor* CreateConsumerActor( | ||
| const TString& database, | ||
| ui64 tabletId, | ||
| const NActors::TActorId& tabletActorId, | ||
| ui32 partitionId, | ||
| const NActors::TActorId& partitionActorId, | ||
| const NKikimrPQ::TPQTabletConfig_TConsumer& config, | ||
| const std::optional<TDuration> reteintion) { | ||
| return new TConsumerActor(tabletId, tabletActorId, partitionId, partitionActorId, config, reteintion); | ||
| return new TConsumerActor(database, tabletId, tabletActorId, partitionId, partitionActorId, config, reteintion); | ||
| } | ||
|
|
||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Зачем такая подпись? Проще же не подписываться, а со стороны партиции EndOffset на каждый чих пушить в MLP консумер-актор.