|
| 1 | +#include "logging.h" |
| 2 | +#include "private_events.h" |
| 3 | +#include "stream_consumer_remover.h" |
| 4 | +#include "util.h" |
| 5 | + |
| 6 | +#include <ydb/core/tx/replication/ydb_proxy/ydb_proxy.h> |
| 7 | +#include <ydb/library/actors/core/actor_bootstrapped.h> |
| 8 | +#include <ydb/library/actors/core/hfunc.h> |
| 9 | +#include <ydb-cpp-sdk/client/types/status/status.h> |
| 10 | + |
| 11 | +namespace NKikimr::NReplication::NController { |
| 12 | + |
| 13 | +class TStreamConsumerRemover: public TActorBootstrapped<TStreamConsumerRemover> { |
| 14 | + void RequestPermission() { |
| 15 | + Send(Parent, new TEvPrivate::TEvRequestDropStream()); |
| 16 | + Become(&TThis::StateRequestPermission); |
| 17 | + } |
| 18 | + |
| 19 | + STATEFN(StateRequestPermission) { |
| 20 | + switch (ev->GetTypeRewrite()) { |
| 21 | + hFunc(TEvPrivate::TEvAllowDropStream, Handle); |
| 22 | + default: |
| 23 | + return StateBase(ev); |
| 24 | + } |
| 25 | + } |
| 26 | + |
| 27 | + void Handle(TEvPrivate::TEvAllowDropStream::TPtr& ev) { |
| 28 | + LOG_T("Handle " << ev->Get()->ToString()); |
| 29 | + DropStreamConsumer(); |
| 30 | + } |
| 31 | + |
| 32 | + void DropStreamConsumer() { |
| 33 | + Send(YdbProxy, new TEvYdbProxy::TEvAlterTopicRequest(SrcPath, NYdb::NTopic::TAlterTopicSettings() |
| 34 | + .AppendDropConsumers(ConsumerName))); |
| 35 | + |
| 36 | + Become(&TThis::StateWork); |
| 37 | + } |
| 38 | + |
| 39 | + STATEFN(StateWork) { |
| 40 | + switch (ev->GetTypeRewrite()) { |
| 41 | + hFunc(TEvYdbProxy::TEvAlterTopicResponse, Handle); |
| 42 | + sFunc(TEvents::TEvWakeup, DropStreamConsumer); |
| 43 | + default: |
| 44 | + return StateBase(ev); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + void Handle(TEvYdbProxy::TEvAlterTopicResponse::TPtr& ev) { |
| 49 | + LOG_T("Handle " << ev->Get()->ToString()); |
| 50 | + auto& result = ev->Get()->Result; |
| 51 | + |
| 52 | + if (!result.IsSuccess()) { |
| 53 | + if (IsRetryableError(result)) { |
| 54 | + LOG_D("Retry"); |
| 55 | + return Schedule(TDuration::Seconds(10), new TEvents::TEvWakeup); |
| 56 | + } |
| 57 | + |
| 58 | + LOG_E("Error" |
| 59 | + << ": status# " << result.GetStatus() |
| 60 | + << ", issues# " << result.GetIssues().ToOneLineString()); |
| 61 | + } else { |
| 62 | + LOG_I("Success" |
| 63 | + << ": issues# " << result.GetIssues().ToOneLineString()); |
| 64 | + } |
| 65 | + |
| 66 | + Send(Parent, new TEvPrivate::TEvDropStreamResult(ReplicationId, TargetId, std::move(result))); |
| 67 | + PassAway(); |
| 68 | + } |
| 69 | + |
| 70 | +public: |
| 71 | + static constexpr NKikimrServices::TActivity::EType ActorActivityType() { |
| 72 | + return NKikimrServices::TActivity::REPLICATION_CONTROLLER_STREAM_REMOVER; |
| 73 | + } |
| 74 | + |
| 75 | + explicit TStreamConsumerRemover( |
| 76 | + const TActorId& parent, |
| 77 | + const TActorId& proxy, |
| 78 | + ui64 rid, |
| 79 | + ui64 tid, |
| 80 | + TReplication::ETargetKind kind, |
| 81 | + const TString& srcPath, |
| 82 | + const TString& consumerName) |
| 83 | + : Parent(parent) |
| 84 | + , YdbProxy(proxy) |
| 85 | + , ReplicationId(rid) |
| 86 | + , TargetId(tid) |
| 87 | + , Kind(kind) |
| 88 | + , SrcPath(srcPath) |
| 89 | + , ConsumerName(consumerName) |
| 90 | + , LogPrefix("StreamConsumerRemover", ReplicationId, TargetId) |
| 91 | + { |
| 92 | + } |
| 93 | + |
| 94 | + void Bootstrap() { |
| 95 | + switch (Kind) { |
| 96 | + case TReplication::ETargetKind::Table: |
| 97 | + case TReplication::ETargetKind::IndexTable: |
| 98 | + Y_ABORT("Unreachable"); |
| 99 | + case TReplication::ETargetKind::Transfer: |
| 100 | + return RequestPermission(); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + STATEFN(StateBase) { |
| 105 | + switch (ev->GetTypeRewrite()) { |
| 106 | + sFunc(TEvents::TEvPoison, PassAway); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | +private: |
| 111 | + const TActorId Parent; |
| 112 | + const TActorId YdbProxy; |
| 113 | + const ui64 ReplicationId; |
| 114 | + const ui64 TargetId; |
| 115 | + const TReplication::ETargetKind Kind; |
| 116 | + const TString SrcPath; |
| 117 | + const TString ConsumerName; |
| 118 | + const TActorLogPrefix LogPrefix; |
| 119 | + |
| 120 | +}; // TStreamRemover |
| 121 | + |
| 122 | +IActor* CreateStreamConsumerRemover(TReplication* replication, ui64 targetId, const TActorContext& ctx) { |
| 123 | + const auto* target = replication->FindTarget(targetId); |
| 124 | + Y_ABORT_UNLESS(target); |
| 125 | + return CreateStreamConsumerRemover(ctx.SelfID, replication->GetYdbProxy(), |
| 126 | + replication->GetId(), target->GetId(), target->GetKind(), target->GetSrcPath(), target->GetStreamConsumerName()); |
| 127 | +} |
| 128 | + |
| 129 | +IActor* CreateStreamConsumerRemover(const TActorId& parent, const TActorId& proxy, ui64 rid, ui64 tid, |
| 130 | + TReplication::ETargetKind kind, const TString& srcPath, const TString& consumerName) |
| 131 | +{ |
| 132 | + return new TStreamConsumerRemover(parent, proxy, rid, tid, kind, srcPath, consumerName); |
| 133 | +} |
| 134 | + |
| 135 | +} |
0 commit comments