From 69a9b0493ed1a79ea11b648286635edfd63e7a2f Mon Sep 17 00:00:00 2001 From: Ilnaz Nizametdinov Date: Wed, 6 Mar 2024 16:27:22 +0300 Subject: [PATCH] Fix dst creator, add tests (#2497) --- .../tx/replication/controller/dst_creator.cpp | 25 ++++++- .../replication/controller/dst_creator_ut.cpp | 70 +++++++++++++++++++ .../controller/ut_dst_creator/ya.make | 20 ++++++ ydb/core/tx/replication/controller/ya.make | 4 ++ ydb/core/tx/replication/ut_helpers/test_env.h | 4 ++ 5 files changed, 121 insertions(+), 2 deletions(-) create mode 100644 ydb/core/tx/replication/controller/dst_creator_ut.cpp create mode 100644 ydb/core/tx/replication/controller/ut_dst_creator/ya.make diff --git a/ydb/core/tx/replication/controller/dst_creator.cpp b/ydb/core/tx/replication/controller/dst_creator.cpp index 5c5157b1eb66..d514cada876b 100644 --- a/ydb/core/tx/replication/controller/dst_creator.cpp +++ b/ydb/core/tx/replication/controller/dst_creator.cpp @@ -36,6 +36,25 @@ class TDstCreator: public TActorBootstrapped { } } + NKikimrScheme::EStatus ConvertStatus(NYdb::EStatus status) { + switch (status) { + case NYdb::EStatus::SUCCESS: + return NKikimrScheme::StatusSuccess; + case NYdb::EStatus::BAD_REQUEST: + return NKikimrScheme::StatusInvalidParameter; + case NYdb::EStatus::UNAUTHORIZED: + return NKikimrScheme::StatusAccessDenied; + case NYdb::EStatus::SCHEME_ERROR: + return NKikimrScheme::StatusSchemeError; + case NYdb::EStatus::PRECONDITION_FAILED: + return NKikimrScheme::StatusPreconditionFailed; + case NYdb::EStatus::ALREADY_EXISTS: + return NKikimrScheme::StatusAlreadyExists; + default: + return NKikimrScheme::StatusNotAvailable; + } + } + void Handle(TEvYdbProxy::TEvDescribeTableResponse::TPtr& ev) { LOG_T("Handle " << ev->Get()->ToString()); @@ -47,7 +66,7 @@ class TDstCreator: public TActorBootstrapped { return Retry(); } - return Error(NKikimrScheme::StatusNotAvailable, TStringBuilder() << "Cannot describe table" + return Error(ConvertStatus(result.GetStatus()), TStringBuilder() << "Cannot describe table" << ": status: " << result.GetStatus() << ", issue: " << result.GetIssues().ToOneLineString()); } @@ -62,6 +81,8 @@ class TDstCreator: public TActorBootstrapped { return Error(NKikimrScheme::StatusSchemeError, error); } + // TODO: support indexed tables + TxBody.SetOperationType(NKikimrSchemeOp::ESchemeOpCreateTable); TxBody.MutableCreateTable()->SetName(ToString(ExtractBase(DstPath))); AllocateTxId(); } @@ -320,7 +341,7 @@ class TDstCreator: public TActorBootstrapped { void Handle(TEvPipeCache::TEvDeliveryProblem::TPtr& ev) { LOG_T("Handle " << ev->Get()->ToString()); - if (SchemeShardId == ev->Get()->TabletId) { + if (SchemeShardId != ev->Get()->TabletId) { return; } diff --git a/ydb/core/tx/replication/controller/dst_creator_ut.cpp b/ydb/core/tx/replication/controller/dst_creator_ut.cpp new file mode 100644 index 000000000000..75719174ddb8 --- /dev/null +++ b/ydb/core/tx/replication/controller/dst_creator_ut.cpp @@ -0,0 +1,70 @@ +#include "dst_creator.h" +#include "private_events.h" + +#include +#include + +#include + +#include + +namespace NKikimr::NReplication::NController { + +Y_UNIT_TEST_SUITE(DstCreator) { + using namespace NTestHelpers; + + Y_UNIT_TEST(Basic) { + TEnv env; + env.GetRuntime().SetLogPriority(NKikimrServices::REPLICATION_CONTROLLER, NLog::PRI_TRACE); + + const auto tableDesc = TTestTableDescription{ + .Name = "Table", + .KeyColumns = {"key"}, + .Columns = { + {.Name = "key", .Type = "Uint32"}, + {.Name = "value", .Type = "Utf8"}, + }, + }; + + env.CreateTable("/Root", *MakeTableDescription(tableDesc)); + env.GetRuntime().Register(CreateDstCreator( + env.GetSender(), env.GetSchemeshardId("/Root/Table"), env.GetYdbProxy(), 1 /* rid */, 1 /* tid */, + TReplication::ETargetKind::Table, "/Root/Table", "/Root/Replicated" + )); + + auto ev = env.GetRuntime().GrabEdgeEvent(env.GetSender()); + UNIT_ASSERT_VALUES_EQUAL(ev->Get()->Status, NKikimrScheme::StatusSuccess); + + auto desc = env.GetDescription("/Root/Replicated"); + const auto& replicatedDesc = desc.GetPathDescription().GetTable(); + + UNIT_ASSERT_VALUES_EQUAL(replicatedDesc.KeyColumnNamesSize(), tableDesc.KeyColumns.size()); + for (ui32 i = 0; i < replicatedDesc.KeyColumnNamesSize(); ++i) { + UNIT_ASSERT_VALUES_EQUAL(replicatedDesc.GetKeyColumnNames(i), tableDesc.KeyColumns[i]); + } + + UNIT_ASSERT_VALUES_EQUAL(replicatedDesc.ColumnsSize(), tableDesc.Columns.size()); + for (ui32 i = 0; i < replicatedDesc.ColumnsSize(); ++i) { + auto pred = [name = replicatedDesc.GetColumns(i).GetName()](const auto& column) { + return name == column.Name; + }; + + UNIT_ASSERT(FindIfPtr(tableDesc.Columns, pred)); + } + } + + Y_UNIT_TEST(NonExistentSrc) { + TEnv env; + env.GetRuntime().SetLogPriority(NKikimrServices::REPLICATION_CONTROLLER, NLog::PRI_TRACE); + + env.GetRuntime().Register(CreateDstCreator( + env.GetSender(), env.GetSchemeshardId("/Root"), env.GetYdbProxy(), 1 /* rid */, 1 /* tid */, + TReplication::ETargetKind::Table, "/Root/Table", "/Root/Replicated" + )); + + auto ev = env.GetRuntime().GrabEdgeEvent(env.GetSender()); + UNIT_ASSERT_VALUES_EQUAL(ev->Get()->Status, NKikimrScheme::StatusSchemeError); + } +} + +} diff --git a/ydb/core/tx/replication/controller/ut_dst_creator/ya.make b/ydb/core/tx/replication/controller/ut_dst_creator/ya.make new file mode 100644 index 000000000000..c36093a06bfe --- /dev/null +++ b/ydb/core/tx/replication/controller/ut_dst_creator/ya.make @@ -0,0 +1,20 @@ +UNITTEST_FOR(ydb/core/tx/replication/controller) + +FORK_SUBTESTS() + +SIZE(MEDIUM) + +TIMEOUT(600) + +PEERDIR( + ydb/core/tx/replication/ut_helpers + library/cpp/testing/unittest +) + +SRCS( + dst_creator_ut.cpp +) + +YQL_LAST_ABI_VERSION() + +END() diff --git a/ydb/core/tx/replication/controller/ya.make b/ydb/core/tx/replication/controller/ya.make index f19f6ecbcc22..9ef7419621f5 100644 --- a/ydb/core/tx/replication/controller/ya.make +++ b/ydb/core/tx/replication/controller/ya.make @@ -44,3 +44,7 @@ GENERATE_ENUM_SERIALIZATION(replication.h) YQL_LAST_ABI_VERSION() END() + +RECURSE_FOR_TESTS( + ut_dst_creator +) diff --git a/ydb/core/tx/replication/ut_helpers/test_env.h b/ydb/core/tx/replication/ut_helpers/test_env.h index d5e8310185d8..e077759aedf8 100644 --- a/ydb/core/tx/replication/ut_helpers/test_env.h +++ b/ydb/core/tx/replication/ut_helpers/test_env.h @@ -129,6 +129,10 @@ class TEnv { return TPathId(self.GetSchemeshardId(), self.GetPathId()); } + ui64 GetSchemeshardId(const TString& path) { + return GetPathId(path).OwnerId; + } + template auto CreateTable(Args&&... args) { return Client.CreateTable(std::forward(args)...);