Skip to content

Tpcc import imrovements (#17333) #19791

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 34 additions & 12 deletions ydb/library/workload/tpcc/import.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -511,15 +511,28 @@ NTable::TBulkUpsertResult LoadOrderLines(
template<typename LoadFunc>
void ExecuteWithRetry(const TString& operationName, LoadFunc loadFunc, TLog* Log) {
for (int retryCount = 0; retryCount < MAX_RETRIES; ++retryCount) {
if (GetGlobalInterruptSource().stop_requested()) {
break;
Copy link
Preview

Copilot AI Jun 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Breaking out of the retry loop on interrupt will naturally exit the function, but using return; instead of break; makes the intent explicit and avoids confusion about control flow.

Suggested change
break;
return;

Copilot uses AI. Check for mistakes.

}

auto result = loadFunc();
if (result.IsSuccess()) {
return;
}

const auto status = result.GetStatus();
bool shouldFail = status == EStatus::NOT_FOUND || status == EStatus::UNDETERMINED
|| status == EStatus::UNAUTHORIZED || status == EStatus::SCHEME_ERROR;
if (shouldFail) {
LOG_E(operationName << " failed: " << result.GetIssues().ToOneLineString());
RequestStop();
return;
}

if (retryCount < MAX_RETRIES - 1) {
int waitMs = GetBackoffWaitMs(retryCount);
LOG_T("Retrying " << operationName << " after " << waitMs << " ms due to: "
<< result.GetIssues().ToOneLineString());
<< result.GetStatus() << ", " << result.GetIssues().ToOneLineString());
Sleep(TDuration::MilliSeconds(waitMs));
} else {
LOG_E(operationName << " failed after " << MAX_RETRIES << " retries: "
Expand Down Expand Up @@ -757,10 +770,8 @@ std::expected<double, std::string> GetIndexProgress(

//-----------------------------------------------------------------------------

std::stop_source StopByInterrupt;

void InterruptHandler(int) {
StopByInterrupt.request_stop();
GetGlobalInterruptSource().request_stop();
}

//-----------------------------------------------------------------------------
Expand All @@ -787,7 +798,7 @@ class TPCCLoader {
, Log(std::make_unique<TLog>(THolder(static_cast<TLogBackend*>(LogBackend))))
, PreviousDataSizeLoaded(0)
, StartTime(Clock::now())
, LoadState(StopByInterrupt.get_token())
, LoadState(GetGlobalInterruptSource().get_token())
{
}

Expand All @@ -812,18 +823,17 @@ class TPCCLoader {
Config.LoadThreadCount = DEFAULT_LOAD_THREAD_COUNT;
}

// in particular this log message
LOG_I("Starting TPC-C data import for " << Config.WarehouseCount << " warehouses using " <<
Config.LoadThreadCount << " threads. Approximate data size: "
<< GetFormattedSize(LoadState.ApproximateDataSize));

// TODO: detect number of threads
size_t threadCount = std::min(Config.WarehouseCount, Config.LoadThreadCount);
threadCount = std::max(threadCount, size_t(1));

// TODO: calculate optimal number of drivers (but per thread looks good)
size_t driverCount = threadCount;

LOG_I("Starting TPC-C data import for " << Config.WarehouseCount << " warehouses using " <<
threadCount << " threads and " << driverCount << " YDB drivers. Approximate data size: "
<< GetFormattedSize(LoadState.ApproximateDataSize));

std::vector<TDriver> drivers;
drivers.reserve(driverCount);
for (size_t i = 0; i < driverCount; ++i) {
Expand All @@ -849,6 +859,8 @@ class TPCCLoader {
auto& driver = drivers[threadId % driverCount];
if (threadId == 0) {
LoadSmallTables(driver, Config.Path, Config.WarehouseCount, Log.get());
} else {
std::this_thread::sleep_for(std::chrono::milliseconds(threadId));
}
LoadRange(driver, Config.Path, whStart, whEnd, LoadState, Log.get());
});
Expand All @@ -859,7 +871,7 @@ class TPCCLoader {
Clock::time_point lastIndexProgressCheck = Clock::time_point::min();

while (true) {
if (StopByInterrupt.stop_requested()) {
if (GetGlobalInterruptSource().stop_requested()) {
break;
}

Expand All @@ -876,6 +888,16 @@ class TPCCLoader {
size_t indexedRangesLoaded = LoadState.IndexedRangesLoaded.load(std::memory_order_relaxed);
if (indexedRangesLoaded >= threadCount) {
CreateIndices(drivers[0], Config.Path, LoadState, Log.get());
for (const auto& state: LoadState.IndexBuildStates) {
if (state.Id.GetKind() == TOperation::TOperationId::UNUSED) {
GetGlobalInterruptSource().request_stop();
break;
}
}
if (GetGlobalInterruptSource().stop_requested()) {
break;
}

LOG_I("Indexed tables loaded, indices are being built in background. Continuing with remaining tables");
LoadState.State = TLoadState::ELOAD_TABLES_BUILD_INDICES;
lastIndexProgressCheck = now;
Expand Down Expand Up @@ -932,7 +954,7 @@ class TPCCLoader {
ExitTuiMode();
}

if (StopByInterrupt.stop_requested()) {
if (GetGlobalInterruptSource().stop_requested()) {
LOG_I("Stop requested, waiting for threads to finish");
}

Expand Down
6 changes: 5 additions & 1 deletion ydb/library/workload/tpcc/ut/data_splitter_ut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ Y_UNIT_TEST_SUITE(TDataSplitterTest) {
int itemsPerShard = ITEM_COUNT / minShardCount;
itemsPerShard = std::max(minItemsPerShard, itemsPerShard);
int expectedItemSplits = (ITEM_COUNT - 1) / itemsPerShard;
UNIT_ASSERT(expectedItemSplits >= 0);
UNIT_ASSERT_VALUES_EQUAL(itemSplits.size(), expectedItemSplits);

// Heavy tables - check based on PER_WAREHOUSE_MB
Expand All @@ -73,6 +74,7 @@ Y_UNIT_TEST_SUITE(TDataSplitterTest) {
int stockWarehousesPerShard2 = (1000 + minShardCount - 1) / minShardCount;
stockWarehousesPerShard = std::min(stockWarehousesPerShard, stockWarehousesPerShard2);
int expectedStockSplits = (1000 - 1) / stockWarehousesPerShard;
UNIT_ASSERT(expectedStockSplits > 0);
Copy link
Preview

Copilot AI Jun 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The first assertion uses >= 0 but here you use > 0, which is inconsistent and may fail if zero splits are valid. Consider standardizing on one boundary check (e.g. >= 0) or adding a comment explaining why zero is invalid.

Suggested change
UNIT_ASSERT(expectedStockSplits > 0);
UNIT_ASSERT(expectedStockSplits >= 0); // Ensure consistency with earlier assertions

Copilot uses AI. Check for mistakes.

UNIT_ASSERT_VALUES_EQUAL(stockSplits.size(), expectedStockSplits);
if (!stockSplits.empty()) {
UNIT_ASSERT_VALUES_EQUAL(stockSplits[0], 1 + stockWarehousesPerShard);
Expand All @@ -85,12 +87,14 @@ Y_UNIT_TEST_SUITE(TDataSplitterTest) {
int customerWarehousesPerShard2 = (1000 + minShardCount - 1) / minShardCount;
customerWarehousesPerShard = std::min(customerWarehousesPerShard, customerWarehousesPerShard2);
int expectedCustomerSplits = (1000 - 1) / customerWarehousesPerShard;
UNIT_ASSERT(expectedCustomerSplits > 0);
UNIT_ASSERT_VALUES_EQUAL(customerSplits.size(), expectedCustomerSplits);

// Light tables
auto warehouseSplits = splitter.GetSplitKeys(TABLE_WAREHOUSE);
int lightWarehousesPerShard = (1000 + minShardCount - 1) / minShardCount;
int expectedLightSplits = (1000 - 1) / lightWarehousesPerShard;
UNIT_ASSERT(expectedLightSplits > 0);
UNIT_ASSERT_VALUES_EQUAL(warehouseSplits.size(), expectedLightSplits);
}

Expand Down Expand Up @@ -336,4 +340,4 @@ Y_UNIT_TEST_SUITE(TDataSplitterTest) {
}
}
}
}
}
31 changes: 23 additions & 8 deletions ydb/public/lib/ydb_cli/commands/ydb_workload_tpcc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class TCommandTPCCClean
TCommandTPCCClean(std::shared_ptr<NTPCC::TRunConfig> runConfig);
~TCommandTPCCClean() = default;

virtual int Run(TConfig& config) override;
int Run(TConfig& config) override;

private:
std::shared_ptr<NTPCC::TRunConfig> RunConfig;
Expand All @@ -50,7 +50,8 @@ class TCommandTPCCInit
TCommandTPCCInit(std::shared_ptr<NTPCC::TRunConfig> runConfig);
~TCommandTPCCInit() = default;

virtual int Run(TConfig& config) override;
void Config(TConfig& config) override;
int Run(TConfig& config) override;

private:
std::shared_ptr<NTPCC::TRunConfig> RunConfig;
Expand All @@ -62,6 +63,20 @@ TCommandTPCCInit::TCommandTPCCInit(std::shared_ptr<NTPCC::TRunConfig> runConfig)
{
}

void TCommandTPCCInit::Config(TConfig& config) {
TYdbCommand::Config(config);

config.Opts->AddLongOption(
'w', "warehouses", TStringBuilder() << "Number of warehouses")
.RequiredArgument("INT").Required().StoreResult(&RunConfig->WarehouseCount);

config.Opts->AddLongOption(
"log-level", TStringBuilder() << "Log level from 0 to 8, default is 6 (INFO)")
.Optional().StoreMappedResult(&RunConfig->LogPriority, [](const TString& v) {
return FromString<ELogPriority>(v);
}).DefaultValue(RunConfig->LogPriority).Hidden();
}

int TCommandTPCCInit::Run(TConfig& connectionConfig) {
RunConfig->SetFullPath(connectionConfig);
NTPCC::InitSync(connectionConfig, *RunConfig);
Expand All @@ -77,8 +92,8 @@ class TCommandTPCCImport
TCommandTPCCImport(std::shared_ptr<NTPCC::TRunConfig> runConfig);
~TCommandTPCCImport() = default;

virtual void Config(TConfig& config) override;
virtual int Run(TConfig& config) override;
void Config(TConfig& config) override;
int Run(TConfig& config) override;

private:
std::shared_ptr<NTPCC::TRunConfig> RunConfig;
Expand All @@ -95,7 +110,7 @@ void TCommandTPCCImport::Config(TConfig& config) {

config.Opts->AddLongOption(
'w', "warehouses", TStringBuilder() << "Number of warehouses")
.OptionalArgument("INT").StoreResult(&RunConfig->WarehouseCount).DefaultValue(RunConfig->WarehouseCount);
.RequiredArgument("INT").Required().StoreResult(&RunConfig->WarehouseCount);

// TODO: detect automatically
config.Opts->AddLongOption(
Expand Down Expand Up @@ -140,8 +155,8 @@ class TCommandTPCCRun
TCommandTPCCRun(std::shared_ptr<NTPCC::TRunConfig> runConfig);
~TCommandTPCCRun() = default;

virtual void Config(TConfig& config) override;
virtual int Run(TConfig& config) override;
void Config(TConfig& config) override;
int Run(TConfig& config) override;

private:
std::shared_ptr<NTPCC::TRunConfig> RunConfig;
Expand All @@ -158,7 +173,7 @@ void TCommandTPCCRun::Config(TConfig& config) {

config.Opts->AddLongOption(
'w', "warehouses", TStringBuilder() << "Number of warehouses")
.OptionalArgument("INT").StoreResult(&RunConfig->WarehouseCount).DefaultValue(RunConfig->WarehouseCount);
.RequiredArgument("INT").Required().StoreResult(&RunConfig->WarehouseCount);

// TODO: default value should be auto
config.Opts->AddLongOption(
Expand Down
Loading