Skip to content

Commit

Permalink
24-3: Adjust change queue reserved capacity at Enqueue() (#9509)
Browse files Browse the repository at this point in the history
  • Loading branch information
CyberROFL authored Sep 19, 2024
1 parent 644b5f8 commit 7eb941d
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 11 deletions.
3 changes: 2 additions & 1 deletion ydb/core/tx/datashard/cdc_stream_scan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,8 @@ class TDataShard::TTxCdcStreamScanProgress
const auto& valueTags = ev.ValueTags;

LOG_D("Progress"
<< ": streamPathId# " << streamPathId);
<< ": streamPathId# " << streamPathId
<< ", rows# " << ev.Rows.size());

if (!Self->GetUserTables().contains(tablePathId.LocalPathId)) {
LOG_W("Cannot progress on unknown table"
Expand Down
25 changes: 16 additions & 9 deletions ydb/core/tx/datashard/datashard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1092,19 +1092,33 @@ void TDataShard::RemoveChangeRecord(NIceDb::TNiceDb& db, ui64 order) {
if (!--rIt->second) {
ChangeQueueReservations.erase(rIt);
}

SetCounter(COUNTER_CHANGE_QUEUE_RESERVED_CAPACITY, ChangeQueueReservedCapacity);
}

UpdateChangeExchangeLag(AppData()->TimeProvider->Now());
ChangesQueue.erase(it);

IncCounter(COUNTER_CHANGE_RECORDS_REMOVED);
SetCounter(COUNTER_CHANGE_QUEUE_SIZE, ChangesQueue.size());
SetCounter(COUNTER_CHANGE_QUEUE_RESERVED_CAPACITY, ChangeQueueReservedCapacity);

CheckChangesQueueNoOverflow();
}

void TDataShard::EnqueueChangeRecords(TVector<IDataShardChangeCollector::TChange>&& records, ui64 cookie, bool afterMove) {
if (auto it = ChangeQueueReservations.find(cookie); it != ChangeQueueReservations.end()) {
Y_ABORT_UNLESS(!afterMove);

ChangeQueueReservedCapacity -= it->second;
it->second = records.size();
ChangeQueueReservedCapacity += it->second;
if (!it->second) {
ChangeQueueReservations.erase(it);
}

SetCounter(COUNTER_CHANGE_QUEUE_RESERVED_CAPACITY, ChangeQueueReservedCapacity);
}

if (!records) {
return;
}
Expand Down Expand Up @@ -1140,22 +1154,15 @@ void TDataShard::EnqueueChangeRecords(TVector<IDataShardChangeCollector::TChange
ChangesQueueBytes += record.BodySize;
}

if (auto it = ChangeQueueReservations.find(cookie); it != ChangeQueueReservations.end()) {
Y_ABORT_UNLESS(!afterMove);
ChangeQueueReservedCapacity -= it->second;
ChangeQueueReservedCapacity += records.size();
}

UpdateChangeExchangeLag(now);
IncCounter(COUNTER_CHANGE_RECORDS_ENQUEUED, forward.size());
SetCounter(COUNTER_CHANGE_QUEUE_SIZE, ChangesQueue.size());
SetCounter(COUNTER_CHANGE_QUEUE_RESERVED_CAPACITY, ChangeQueueReservedCapacity);

Y_ABORT_UNLESS(OutChangeSender);
Send(OutChangeSender, new NChangeExchange::TEvChangeExchange::TEvEnqueueRecords(std::move(forward)));
}

ui32 TDataShard::GetFreeChangeQueueCapacity(ui64 cookie) {
ui32 TDataShard::GetFreeChangeQueueCapacity(ui64 cookie) const {
const ui64 sizeLimit = AppData()->DataShardConfig.GetChangesQueueItemsLimit();
if (sizeLimit < ChangesQueue.size()) {
return 0;
Expand Down
2 changes: 1 addition & 1 deletion ydb/core/tx/datashard/datashard_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -1914,7 +1914,7 @@ class TDataShard
void RemoveChangeRecord(NIceDb::TNiceDb& db, ui64 order);
// TODO(ilnaz): remove 'afterMove' after #6541
void EnqueueChangeRecords(TVector<IDataShardChangeCollector::TChange>&& records, ui64 cookie = 0, bool afterMove = false);
ui32 GetFreeChangeQueueCapacity(ui64 cookie);
ui32 GetFreeChangeQueueCapacity(ui64 cookie) const;
ui64 ReserveChangeQueueCapacity(ui32 capacity);
void UpdateChangeExchangeLag(TInstant now);
void CreateChangeSender(const TActorContext& ctx);
Expand Down
57 changes: 57 additions & 0 deletions ydb/core/tx/datashard/datashard_ut_change_exchange.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3041,6 +3041,63 @@ Y_UNIT_TEST_SUITE(Cdc) {
});
}

Y_UNIT_TEST(InitialScanEnqueuesZeroRecords) {
TPortManager portManager;
TServer::TPtr server = new TServer(TServerSettings(portManager.GetPort(2134), {}, DefaultPQConfig())
.SetUseRealThreads(false)
.SetDomainName("Root")
.SetEnableChangefeedInitialScan(true)
.SetChangesQueueItemsLimit(2)
);

auto& runtime = *server->GetRuntime();
const auto edgeActor = runtime.AllocateEdgeActor();

SetupLogging(runtime);
InitRoot(server, edgeActor);
CreateShardedTable(server, edgeActor, "/Root", "Table", SimpleTable());

ExecSQL(server, edgeActor, R"(
UPSERT INTO `/Root/Table` (key, value) VALUES
(1, 10),
(2, 20),
(3, 30),
(4, 40);
)");

TBlockEvents<TEvDataShard::TEvCdcStreamScanRequest> blockScanRequest(runtime, [&](auto& ev) {
ev->Get()->Record.MutableLimits()->SetBatchMaxRows(1);
return true;
});

WaitTxNotification(server, edgeActor, AsyncAlterAddStream(server, "/Root", "Table",
WithInitialScan(Updates(NKikimrSchemeOp::ECdcStreamFormatJson))));

runtime.WaitFor("Scan request", [&]{ return blockScanRequest.size(); });
runtime.AddObserver<TEvDataShard::TEvCdcStreamScanRequest>([&](auto& ev) {
ev->Get()->Record.MutableLimits()->SetBatchMaxRows(1);
});

ExecSQL(server, edgeActor, R"(
UPSERT INTO `/Root/Table` (key, value) VALUES
(1, 100),
(2, 200),
(3, 300);
)");

blockScanRequest.Unblock().Stop();

WaitForContent(server, edgeActor, "/Root/Table/Stream", {
R"({"update":{"value":10},"key":[1]})",
R"({"update":{"value":100},"key":[1]})",
R"({"update":{"value":20},"key":[2]})",
R"({"update":{"value":200},"key":[2]})",
R"({"update":{"value":30},"key":[3]})",
R"({"update":{"value":300},"key":[3]})",
R"({"update":{"value":40},"key":[4]})",
});
}

Y_UNIT_TEST(InitialScanRacyProgressAndDrop) {
TPortManager portManager;
TServer::TPtr server = new TServer(TServerSettings(portManager.GetPort(2134), {}, DefaultPQConfig())
Expand Down

0 comments on commit 7eb941d

Please sign in to comment.