Skip to content

Commit

Permalink
Alter replication config (ydb-platform#4333)
Browse files Browse the repository at this point in the history
  • Loading branch information
CyberROFL committed Jun 6, 2024
1 parent 464aafa commit f79e396
Show file tree
Hide file tree
Showing 9 changed files with 100 additions and 10 deletions.
13 changes: 10 additions & 3 deletions ydb/core/tx/replication/controller/dst_creator_ut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,16 @@ Y_UNIT_TEST_SUITE(DstCreator) {
return copy;
};

auto clearConfig = [](const TTestTableDescription& desc) {
auto copy = desc;
copy.ReplicationConfig.Clear();
return copy;
};

TEnv env;
env.GetRuntime().SetLogPriority(NKikimrServices::REPLICATION_CONTROLLER, NLog::PRI_TRACE);

env.CreateTable("/Root", *MakeTableDescription(changeName(desc, "Src")));
env.CreateTable("/Root", *MakeTableDescription(clearConfig(changeName(desc, "Src"))));
env.CreateTable("/Root", *MakeTableDescription(mod(changeName(desc, "Dst"))));

env.GetRuntime().Register(CreateDstCreator(
Expand Down Expand Up @@ -223,13 +229,14 @@ Y_UNIT_TEST_SUITE(DstCreator) {
}

Y_UNIT_TEST(UnsupportedReplicationMode) {
auto clearMode = [](const TTestTableDescription& desc) {
auto changeMode = [](const TTestTableDescription& desc) {
auto copy = desc;
copy.ReplicationConfig->Mode = TTestTableDescription::TReplicationConfig::MODE_NONE;
copy.ReplicationConfig->Consistency = TTestTableDescription::TReplicationConfig::CONSISTENCY_UNKNOWN;
return copy;
};

ExistingDst(NKikimrScheme::StatusSchemeError, "Unsupported replication mode", clearMode, TTestTableDescription{
ExistingDst(NKikimrScheme::StatusSchemeError, "Unsupported replication mode", changeMode, TTestTableDescription{
.Name = "Table",
.KeyColumns = {"key"},
.Columns = {
Expand Down
3 changes: 3 additions & 0 deletions ydb/core/tx/replication/ut_helpers/test_table.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ void TTestTableDescription::TReplicationConfig::SerializeTo(NKikimrSchemeOp::TTa
}

switch (Consistency) {
case CONSISTENCY_UNKNOWN:
proto.SetConsistency(NKikimrSchemeOp::TTableReplicationConfig::CONSISTENCY_UNKNOWN);
break;
case CONSISTENCY_STRONG:
proto.SetConsistency(NKikimrSchemeOp::TTableReplicationConfig::CONSISTENCY_STRONG);
break;
Expand Down
1 change: 1 addition & 0 deletions ydb/core/tx/replication/ut_helpers/test_table.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ struct TTestTableDescription {
};

enum EConsistency {
CONSISTENCY_UNKNOWN = 0,
CONSISTENCY_STRONG = 1,
CONSISTENCY_WEAK = 2,
};
Expand Down
11 changes: 9 additions & 2 deletions ydb/core/tx/schemeshard/schemeshard__operation_alter_table.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,11 @@ TTableInfo::TAlterDataPtr ParseParams(const TPath& path, TTableInfo::TPtr table,
return nullptr;
}

if (!hasSchemaChanges && !copyAlter.HasPartitionConfig() && !copyAlter.HasTTLSettings()) {
if (!hasSchemaChanges
&& !copyAlter.HasPartitionConfig()
&& !copyAlter.HasTTLSettings()
&& !copyAlter.HasReplicationConfig())
{
errStr = Sprintf("No changes specified");
status = NKikimrScheme::StatusInvalidParameter;
return nullptr;
Expand Down Expand Up @@ -497,9 +501,12 @@ class TAlterTable: public TSubOperation {
.IsResolved()
.NotDeleted()
.IsTable()
.NotAsyncReplicaTable()
.NotUnderOperation();

if (!Transaction.GetInternal()) {
checks.NotAsyncReplicaTable();
}

if (!context.IsAllowedPrivateTables) {
checks.IsCommonSensePath(); //forbid alter impl index tables outside consistent operation
}
Expand Down
14 changes: 9 additions & 5 deletions ydb/core/tx/schemeshard/schemeshard_info_types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -265,14 +265,18 @@ TTableInfo::TAlterDataPtr TTableInfo::CreateAlterData(
if (op.HasReplicationConfig()) {
const auto& cfg = op.GetReplicationConfig();

if (source) {
errStr = "Cannot alter replication config";
return nullptr;
}

switch (cfg.GetMode()) {
case NKikimrSchemeOp::TTableReplicationConfig::REPLICATION_MODE_NONE:
if (cfg.HasConsistency() && cfg.GetConsistency() != NKikimrSchemeOp::TTableReplicationConfig::CONSISTENCY_UNKNOWN) {
errStr = "Cannot set replication consistency";
return nullptr;
}
break;
case NKikimrSchemeOp::TTableReplicationConfig::REPLICATION_MODE_READ_ONLY:
if (source) {
errStr = "Cannot set replication mode";
return nullptr;
}
break;
default:
errStr = "Unknown replication mode";
Expand Down
8 changes: 8 additions & 0 deletions ydb/core/tx/schemeshard/ut_helpers/helpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2191,6 +2191,14 @@ namespace NSchemeShardUT_Private {
}
}

TEvTx* InternalTransaction(TEvTx* tx) {
for (auto& x : *tx->Record.MutableTransaction()) {
x.SetInternal(true);
}

return tx;
}

TTestActorRuntimeBase::TEventObserver SetSuppressObserver(TTestActorRuntime &runtime, TVector<THolder<IEventHandle> > &suppressed, ui32 type) {
return runtime.SetObserverFunc([&suppressed, type](TAutoPtr<IEventHandle>& ev) {
if (ev->GetTypeRewrite() == type) {
Expand Down
1 change: 1 addition & 0 deletions ydb/core/tx/schemeshard/ut_helpers/helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ namespace NSchemeShardUT_Private {
TEvTx* CombineSchemeTransactions(const TVector<TEvTx*>& transactions);
void AsyncSend(TTestActorRuntime &runtime, ui64 targetTabletId,
IEventBase *ev, ui32 nodeIndex = 0, TActorId sender = TActorId());
TEvTx* InternalTransaction(TEvTx* tx);

////////// generic

Expand Down
22 changes: 22 additions & 0 deletions ydb/core/tx/schemeshard/ut_replication/ut_replication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,28 @@ Y_UNIT_TEST_SUITE(TReplicationTests) {

TestBuildIndex(runtime, ++txId, TTestTxConfig::SchemeShard, "/MyRoot", "/MyRoot/Table", "by_value", {"value"},
Ydb::StatusIds::BAD_REQUEST);

AsyncSend(runtime, TTestTxConfig::SchemeShard, InternalTransaction(AlterTableRequest(++txId, "/MyRoot", R"(
Name: "Table"
ReplicationConfig {
Mode: REPLICATION_MODE_NONE
Consistency: CONSISTENCY_WEAK
}
)")));
TestModificationResults(runtime, txId, {NKikimrScheme::StatusInvalidParameter});

AsyncSend(runtime, TTestTxConfig::SchemeShard, InternalTransaction(AlterTableRequest(++txId, "/MyRoot", R"(
Name: "Table"
ReplicationConfig {
Mode: REPLICATION_MODE_NONE
}
)")));
TestModificationResults(runtime, txId, {NKikimrScheme::StatusAccepted});
env.TestWaitNotification(runtime, txId);

TestDescribeResult(DescribePath(runtime, "/MyRoot/Table"), {
NLs::ReplicationMode(NKikimrSchemeOp::TTableReplicationConfig::REPLICATION_MODE_NONE),
});
}

Y_UNIT_TEST(CopyReplicatedTable) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,4 +173,41 @@ Y_UNIT_TEST_SUITE(TReplicationWithRebootsTests) {
});
}

Y_UNIT_TEST(AlterReplicationConfig) {
TTestWithReboots t(false);
t.GetTestEnvOptions().InitYdbDriver(true);

t.Run([&](TTestActorRuntime& runtime, bool& activeZone) {
{
TInactiveZone inactive(activeZone);
SetupLogging(runtime);

TestCreateTable(runtime, ++t.TxId, "/MyRoot", R"(
Name: "Table"
Columns { Name: "key" Type: "Uint64" }
Columns { Name: "value" Type: "Uint64" }
KeyColumnNames: ["key"]
ReplicationConfig {
Mode: REPLICATION_MODE_READ_ONLY
}
)");
t.TestEnv->TestWaitNotification(runtime, t.TxId);
}

AsyncSend(runtime, TTestTxConfig::SchemeShard, InternalTransaction(AlterTableRequest(++t.TxId, "/MyRoot", R"(
Name: "Table"
ReplicationConfig {
Mode: REPLICATION_MODE_NONE
}
)")));
t.TestEnv->TestWaitNotification(runtime, t.TxId);

{
TInactiveZone inactive(activeZone);
TestLs(runtime, "/MyRoot/Table", false,
NLs::ReplicationMode(NKikimrSchemeOp::TTableReplicationConfig::REPLICATION_MODE_NONE));
}
});
}

} // TReplicationWithRebootsTests

0 comments on commit f79e396

Please sign in to comment.