|
| 1 | +#include "rpc_scheme_base.h" |
| 2 | +#include "service_replication.h" |
| 3 | + |
| 4 | +#include <ydb/core/grpc_services/base/base.h> |
| 5 | +#include <ydb/core/tx/replication/controller/public_events.h> |
| 6 | +#include <ydb/core/tx/schemeshard/schemeshard.h> |
| 7 | +#include <ydb/core/ydb_convert/ydb_convert.h> |
| 8 | +#include <ydb/library/actors/core/actor.h> |
| 9 | +#include <ydb/library/actors/core/hfunc.h> |
| 10 | +#include <ydb/public/api/protos/draft/ydb_replication.pb.h> |
| 11 | + |
| 12 | +namespace NKikimr::NGRpcService { |
| 13 | + |
| 14 | +using namespace Ydb; |
| 15 | + |
| 16 | +using TEvDescribeReplication = TGrpcRequestOperationCall<Replication::DescribeReplicationRequest, Replication::DescribeReplicationResponse>; |
| 17 | + |
| 18 | +class TDescribeReplicationRPC: public TRpcSchemeRequestActor<TDescribeReplicationRPC, TEvDescribeReplication> { |
| 19 | + using TBase = TRpcSchemeRequestActor<TDescribeReplicationRPC, TEvDescribeReplication>; |
| 20 | + |
| 21 | +public: |
| 22 | + using TBase::TBase; |
| 23 | + |
| 24 | + void Bootstrap() { |
| 25 | + DescribeScheme(); |
| 26 | + } |
| 27 | + |
| 28 | + void PassAway() override { |
| 29 | + if (ControllerPipeClient) { |
| 30 | + NTabletPipe::CloseAndForgetClient(SelfId(), ControllerPipeClient); |
| 31 | + } |
| 32 | + |
| 33 | + TBase::PassAway(); |
| 34 | + } |
| 35 | + |
| 36 | +private: |
| 37 | + void DescribeScheme() { |
| 38 | + auto ev = std::make_unique<TEvTxUserProxy::TEvNavigate>(); |
| 39 | + SetAuthToken(ev, *Request_); |
| 40 | + SetDatabase(ev.get(), *Request_); |
| 41 | + ev->Record.MutableDescribePath()->SetPath(GetProtoRequest()->path()); |
| 42 | + |
| 43 | + Send(MakeTxProxyID(), ev.release()); |
| 44 | + Become(&TDescribeReplicationRPC::StateDescribeScheme); |
| 45 | + } |
| 46 | + |
| 47 | + STATEFN(StateDescribeScheme) { |
| 48 | + switch (ev->GetTypeRewrite()) { |
| 49 | + HFunc(NSchemeShard::TEvSchemeShard::TEvDescribeSchemeResult, Handle); |
| 50 | + default: |
| 51 | + return TBase::StateWork(ev); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + void Handle(NSchemeShard::TEvSchemeShard::TEvDescribeSchemeResult::TPtr& ev, const TActorContext& ctx) { |
| 56 | + const auto& record = ev->Get()->GetRecord(); |
| 57 | + const auto& desc = record.GetPathDescription(); |
| 58 | + |
| 59 | + if (record.HasReason()) { |
| 60 | + auto issue = NYql::TIssue(record.GetReason()); |
| 61 | + Request_->RaiseIssue(issue); |
| 62 | + } |
| 63 | + |
| 64 | + switch (record.GetStatus()) { |
| 65 | + case NKikimrScheme::StatusSuccess: |
| 66 | + if (desc.GetSelf().GetPathType() != NKikimrSchemeOp::EPathTypeReplication) { |
| 67 | + auto issue = NYql::TIssue("Is not a replication"); |
| 68 | + Request_->RaiseIssue(issue); |
| 69 | + return Reply(Ydb::StatusIds::SCHEME_ERROR, ctx); |
| 70 | + } |
| 71 | + |
| 72 | + ConvertDirectoryEntry(desc.GetSelf(), Result.mutable_self(), true); |
| 73 | + return DescribeReplication(desc.GetReplicationDescription().GetControllerId(), |
| 74 | + PathIdFromPathId(desc.GetReplicationDescription().GetPathId())); |
| 75 | + |
| 76 | + case NKikimrScheme::StatusPathDoesNotExist: |
| 77 | + case NKikimrScheme::StatusSchemeError: |
| 78 | + return Reply(Ydb::StatusIds::SCHEME_ERROR, ctx); |
| 79 | + |
| 80 | + case NKikimrScheme::StatusAccessDenied: |
| 81 | + return Reply(Ydb::StatusIds::UNAUTHORIZED, ctx); |
| 82 | + |
| 83 | + case NKikimrScheme::StatusNotAvailable: |
| 84 | + return Reply(Ydb::StatusIds::UNAVAILABLE, ctx); |
| 85 | + |
| 86 | + default: { |
| 87 | + return Reply(Ydb::StatusIds::GENERIC_ERROR, ctx); |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + void DescribeReplication(ui64 tabletId, const TPathId& pathId) { |
| 93 | + if (!ControllerPipeClient) { |
| 94 | + NTabletPipe::TClientConfig config; |
| 95 | + config.RetryPolicy = { |
| 96 | + .RetryLimitCount = 3, |
| 97 | + }; |
| 98 | + ControllerPipeClient = Register(NTabletPipe::CreateClient(SelfId(), tabletId, config)); |
| 99 | + } |
| 100 | + |
| 101 | + auto ev = std::make_unique<NReplication::TEvController::TEvDescribeReplication>(); |
| 102 | + PathIdFromPathId(pathId, ev->Record.MutablePathId()); |
| 103 | + |
| 104 | + NTabletPipe::SendData(SelfId(), ControllerPipeClient, ev.release()); |
| 105 | + Become(&TDescribeReplicationRPC::StateDescribeReplication); |
| 106 | + } |
| 107 | + |
| 108 | + STATEFN(StateDescribeReplication) { |
| 109 | + switch (ev->GetTypeRewrite()) { |
| 110 | + HFunc(NReplication::TEvController::TEvDescribeReplicationResult, Handle); |
| 111 | + default: |
| 112 | + return TBase::StateWork(ev); |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + void Handle(NReplication::TEvController::TEvDescribeReplicationResult::TPtr& ev, const TActorContext& ctx) { |
| 117 | + auto& record = ev->Get()->Record; |
| 118 | + |
| 119 | + switch (record.GetStatus()) { |
| 120 | + case NKikimrReplication::TEvDescribeReplicationResult::SUCCESS: |
| 121 | + break; |
| 122 | + case NKikimrReplication::TEvDescribeReplicationResult::NOT_FOUND: |
| 123 | + return Reply(Ydb::StatusIds::SCHEME_ERROR, ctx); |
| 124 | + default: |
| 125 | + return Reply(Ydb::StatusIds::INTERNAL_ERROR, ctx); |
| 126 | + } |
| 127 | + |
| 128 | + ConvertConnectionParams(record.GetConnectionParams(), *Result.mutable_connection_params()); |
| 129 | + ConvertState(*record.MutableState(), Result); |
| 130 | + |
| 131 | + for (const auto& target : record.GetTargets()) { |
| 132 | + ConvertItem(target, *Result.add_items()); |
| 133 | + } |
| 134 | + |
| 135 | + return ReplyWithResult(Ydb::StatusIds::SUCCESS, Result, ctx); |
| 136 | + } |
| 137 | + |
| 138 | + static void ConvertConnectionParams(const NKikimrReplication::TConnectionParams& from, Ydb::Replication::ConnectionParams& to) { |
| 139 | + to.set_endpoint(from.GetEndpoint()); |
| 140 | + to.set_database(from.GetDatabase()); |
| 141 | + |
| 142 | + switch (from.GetCredentialsCase()) { |
| 143 | + case NKikimrReplication::TConnectionParams::kStaticCredentials: |
| 144 | + return ConvertStaticCredentials(from.GetStaticCredentials(), *to.mutable_static_credentials()); |
| 145 | + case NKikimrReplication::TConnectionParams::kOAuthToken: |
| 146 | + return ConvertOAuth(from.GetOAuthToken(), *to.mutable_oauth()); |
| 147 | + default: |
| 148 | + break; |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + static void ConvertStaticCredentials(const NKikimrReplication::TStaticCredentials& from, Ydb::Replication::ConnectionParams::StaticCredentials& to) { |
| 153 | + to.set_user(from.GetUser()); |
| 154 | + to.set_password_secret_name(from.GetPasswordSecretName()); |
| 155 | + } |
| 156 | + |
| 157 | + static void ConvertOAuth(const NKikimrReplication::TOAuthToken& from, Ydb::Replication::ConnectionParams::OAuth& to) { |
| 158 | + to.set_token_secret_name(from.GetTokenSecretName()); |
| 159 | + } |
| 160 | + |
| 161 | + static void ConvertItem(const NKikimrReplication::TReplicationConfig::TTargetSpecific::TTarget& from, Ydb::Replication::DescribeReplicationResult::Item& to) { |
| 162 | + to.set_source_path(from.GetSrcPath()); |
| 163 | + to.set_destination_path(from.GetDstPath()); |
| 164 | + } |
| 165 | + |
| 166 | + static void ConvertState(NKikimrReplication::TReplicationState& from, Ydb::Replication::DescribeReplicationResult& to) { |
| 167 | + switch (from.GetStateCase()) { |
| 168 | + case NKikimrReplication::TReplicationState::kStandBy: |
| 169 | + to.mutable_running(); |
| 170 | + break; |
| 171 | + case NKikimrReplication::TReplicationState::kError: |
| 172 | + *to.mutable_error()->mutable_issues() = std::move(*from.MutableError()->MutableIssues()); |
| 173 | + break; |
| 174 | + case NKikimrReplication::TReplicationState::kDone: |
| 175 | + to.mutable_done(); |
| 176 | + break; |
| 177 | + default: |
| 178 | + break; |
| 179 | + } |
| 180 | + } |
| 181 | + |
| 182 | +private: |
| 183 | + Ydb::Replication::DescribeReplicationResult Result; |
| 184 | + TActorId ControllerPipeClient; |
| 185 | +}; |
| 186 | + |
| 187 | +void DoDescribeReplication(std::unique_ptr<IRequestOpCtx> p, const IFacilityProvider& f) { |
| 188 | + f.RegisterActor(new TDescribeReplicationRPC(p.release())); |
| 189 | +} |
| 190 | + |
| 191 | +} |
0 commit comments