-
Notifications
You must be signed in to change notification settings - Fork 19
Bulk upsert integration test #280
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
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
06b2c92
bulk upsert test
stanislav-shchetinin 68f0d4e
spaceship & added directory
stanislav-shchetinin 96a2811
fix after review
stanislav-shchetinin 16b6d5d
remove set
stanislav-shchetinin 9f3244b
fix after review
stanislav-shchetinin b0aecbd
valid query
stanislav-shchetinin ffcb4ce
remove operator<
stanislav-shchetinin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
add_subdirectory(basic_example_it) | ||
add_subdirectory(bulk_upsert_simple_it) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
add_ydb_test(NAME bulk_upsert_simple_it | ||
SOURCES | ||
main.cpp | ||
bulk_upsert.cpp | ||
bulk_upsert.h | ||
LINK_LIBRARIES | ||
yutil | ||
YDB-CPP-SDK::Table | ||
library-getopt | ||
GTest::gtest_main | ||
LABELS | ||
integration | ||
) |
163 changes: 163 additions & 0 deletions
163
tests/integration/bulk_upsert_simple_it/bulk_upsert.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
#include "bulk_upsert.h" | ||
|
||
#include <src/library/getopt/last_getopt.h> | ||
|
||
#include <filesystem> | ||
|
||
static constexpr size_t BATCH_SIZE = 1000; | ||
|
||
static void ThrowOnError(const TStatus& status) { | ||
if (!status.IsSuccess()) { | ||
throw TYdbErrorException(status) << status; | ||
} | ||
} | ||
|
||
static std::string JoinPath(const std::string& basePath, const std::string& path) { | ||
if (basePath.empty()) { | ||
return path; | ||
} | ||
|
||
std::filesystem::path prefixPathSplit(basePath); | ||
prefixPathSplit /= path; | ||
|
||
return prefixPathSplit; | ||
} | ||
|
||
TRunArgs GetRunArgs() { | ||
|
||
std::string database = std::getenv("YDB_DATABASE"); | ||
std::string endpoint = std::getenv("YDB_ENDPOINT"); | ||
|
||
auto driverConfig = TDriverConfig() | ||
.SetEndpoint(endpoint) | ||
.SetDatabase(database) | ||
.SetAuthToken(std::getenv("YDB_TOKEN") ? std::getenv("YDB_TOKEN") : ""); | ||
|
||
TDriver driver(driverConfig); | ||
return {driver, JoinPath(database, "bulk")}; | ||
} | ||
|
||
TStatus CreateTable(TTableClient& client, const std::string& table) { | ||
std::cerr << "Create table " << table << "\n"; | ||
|
||
TRetryOperationSettings settings; | ||
auto status = client.RetryOperationSync([&table](TSession session) { | ||
auto tableDesc = TTableBuilder() | ||
.AddNonNullableColumn("pk", EPrimitiveType::Uint64) | ||
.AddNullableColumn("App", EPrimitiveType::Utf8) | ||
.AddNullableColumn("Timestamp", EPrimitiveType::Timestamp) | ||
.AddNullableColumn("Host", EPrimitiveType::Utf8) | ||
.AddNullableColumn("HttpCode", EPrimitiveType::Uint32) | ||
.AddNullableColumn("Message", EPrimitiveType::Utf8) | ||
.SetPrimaryKeyColumns({"pk"}) | ||
.Build(); | ||
|
||
return session.CreateTable(table, std::move(tableDesc)).GetValueSync(); | ||
}, settings); | ||
|
||
return status; | ||
} | ||
|
||
TStatistic GetLogBatch(uint64_t logOffset, std::vector<TLogMessage>& logBatch, uint32_t lastNumber) { | ||
logBatch.clear(); | ||
uint32_t correctSumApp = 0; | ||
uint32_t correctSumHost = 0; | ||
uint32_t correctRowCount = 0; | ||
|
||
for (size_t i = 0; i < BATCH_SIZE; ++i) { | ||
TLogMessage message; | ||
message.pk = correctRowCount + lastNumber; | ||
Gazizonoki marked this conversation as resolved.
Show resolved
Hide resolved
|
||
message.App = "App_" + std::to_string(logOffset % 10); | ||
message.Host = "192.168.0." + std::to_string(logOffset % 11); | ||
message.Timestamp = TInstant::Now() + TDuration::MilliSeconds(i % 1000); | ||
message.HttpCode = 200; | ||
message.Message = i % 2 ? "GET / HTTP/1.1" : "GET /images/logo.png HTTP/1.1"; | ||
logBatch.emplace_back(message); | ||
|
||
correctSumApp += logOffset % 10; | ||
correctSumHost += logOffset % 11; | ||
++correctRowCount; | ||
|
||
} | ||
return {correctSumApp, correctSumHost, correctRowCount}; | ||
} | ||
|
||
TStatus WriteLogBatch(TTableClient& tableClient, const std::string& table, const std::vector<TLogMessage>& logBatch, | ||
const TRetryOperationSettings& retrySettings) { | ||
TValueBuilder rows; | ||
rows.BeginList(); | ||
for (const auto& message : logBatch) { | ||
rows.AddListItem() | ||
.BeginStruct() | ||
.AddMember("pk").Uint64(message.pk) | ||
.AddMember("App").Utf8(message.App) | ||
.AddMember("Host").Utf8(message.Host) | ||
.AddMember("Timestamp").Timestamp(message.Timestamp) | ||
.AddMember("HttpCode").Uint32(message.HttpCode) | ||
.AddMember("Message").Utf8(message.Message) | ||
.EndStruct(); | ||
} | ||
rows.EndList(); | ||
auto bulkUpsertOperation = [table, rowsValue = rows.Build()](TTableClient& tableClient) { | ||
TValue r = rowsValue; | ||
auto status = tableClient.BulkUpsert(table, std::move(r)); | ||
return status.GetValueSync(); | ||
}; | ||
|
||
auto status = tableClient.RetryOperationSync(bulkUpsertOperation, retrySettings); | ||
return status; | ||
} | ||
|
||
static TStatus SelectTransaction(TSession session, const std::string& path, | ||
std::optional<TResultSet>& resultSet) { | ||
std::filesystem::path filesystemPath(path); | ||
auto query = std::format(R"( | ||
PRAGMA TablePathPrefix("{}"); | ||
|
||
SELECT | ||
SUM(CAST(SUBSTRING(CAST(App as string), 4) as Int32)), | ||
SUM(CAST(SUBSTRING(CAST(Host as string), 10) as Int32)), | ||
COUNT(*) | ||
FROM {} | ||
)", filesystemPath.parent_path().string(), filesystemPath.filename().string()); | ||
|
||
auto txControl = | ||
TTxControl::BeginTx(TTxSettings::SerializableRW()) | ||
.CommitTx(); | ||
|
||
auto result = session.ExecuteDataQuery(query, txControl).GetValueSync(); | ||
|
||
if (result.IsSuccess()) { | ||
resultSet = result.GetResultSet(0); | ||
} | ||
|
||
return result; | ||
} | ||
|
||
TStatistic Select(TTableClient& client, const std::string& path) { | ||
std::optional<TResultSet> resultSet; | ||
ThrowOnError(client.RetryOperationSync([path, &resultSet](TSession session) { | ||
return SelectTransaction(session, path, resultSet); | ||
})); | ||
|
||
TResultSetParser parser(*resultSet); | ||
Gazizonoki marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
uint64_t sumApp = 0; | ||
uint64_t sumHost = 0; | ||
uint64_t rowCount = 0; | ||
|
||
if (parser.TryNextRow()) { | ||
|
||
sumApp = *parser.ColumnParser("column0").GetOptionalInt64(); | ||
sumHost = *parser.ColumnParser("column1").GetOptionalInt64(); | ||
rowCount = parser.ColumnParser("column2").GetUint64(); | ||
} | ||
|
||
return {sumApp, sumHost, rowCount}; | ||
} | ||
|
||
void DropTable(TTableClient& client, const std::string& path) { | ||
ThrowOnError(client.RetryOperationSync([path](TSession session) { | ||
return session.DropTable(path).ExtractValueSync(); | ||
})); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
#pragma once | ||
|
||
#include <ydb-cpp-sdk/client/driver/driver.h> | ||
#include <ydb-cpp-sdk/client/table/table.h> | ||
|
||
using namespace NYdb; | ||
using namespace NYdb::NTable; | ||
|
||
struct TRunArgs { | ||
TDriver driver; | ||
stanislav-shchetinin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
std::string path; | ||
}; | ||
|
||
struct TLogMessage { | ||
uint64_t pk; | ||
stanislav-shchetinin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
std::string App; | ||
std::string Host; | ||
TInstant Timestamp; | ||
uint32_t HttpCode; | ||
std::string Message; | ||
}; | ||
|
||
class TYdbErrorException : public yexception { | ||
public: | ||
TYdbErrorException(const NYdb::TStatus& status) | ||
: Status(status) {} | ||
|
||
NYdb::TStatus Status; | ||
}; | ||
|
||
struct TStatistic { | ||
uint64_t sumApp; | ||
stanislav-shchetinin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
uint64_t sumHost; | ||
uint64_t rowCount; | ||
}; | ||
|
||
TRunArgs GetRunArgs(); | ||
TStatus CreateTable(TTableClient& client, const std::string& table); | ||
TStatistic GetLogBatch(uint64_t logOffset, std::vector<TLogMessage>& logBatch, uint32_t lastNumber); | ||
TStatus WriteLogBatch(TTableClient& tableClient, const std::string& table, const std::vector<TLogMessage>& logBatch, | ||
const TRetryOperationSettings& retrySettings); | ||
TStatistic Select(TTableClient& client, const std::string& path); | ||
void DropTable(TTableClient& client, const std::string& path); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
#include "bulk_upsert.h" | ||
|
||
#include <gtest/gtest.h> | ||
|
||
TEST(Integration, BulkUpsert) { | ||
|
||
uint32_t correctSumApp = 0; | ||
uint32_t correctSumHost = 0; | ||
uint32_t correctRowCount = 0; | ||
|
||
auto [driver, path] = GetRunArgs(); | ||
|
||
TTableClient client(driver); | ||
uint32_t count = 1000; | ||
TStatus statusCreate = CreateTable(client, path); | ||
if (!statusCreate.IsSuccess()) { | ||
FAIL() << "Create table failed with status: " << statusCreate << std::endl; | ||
} | ||
|
||
TRetryOperationSettings writeRetrySettings; | ||
writeRetrySettings | ||
.Idempotent(true) | ||
.MaxRetries(20); | ||
|
||
std::vector<TLogMessage> logBatch; | ||
for (uint32_t offset = 0; offset < count; ++offset) { | ||
|
||
auto [batchSumApp, batchSumHost, batchRowCount] = GetLogBatch(offset, logBatch, correctRowCount); | ||
correctSumApp += batchSumApp; | ||
correctSumHost += batchSumHost; | ||
correctRowCount += batchRowCount; | ||
|
||
TStatus statusWrite = WriteLogBatch(client, path, logBatch, writeRetrySettings); | ||
if (!statusWrite.IsSuccess()) { | ||
FAIL() << "Write failed with status: " << statusWrite << std::endl; | ||
} | ||
} | ||
|
||
auto [sumApp, sumHost, rowCount] = Select(client, path); | ||
|
||
EXPECT_EQ(rowCount, correctRowCount); | ||
EXPECT_EQ(sumApp, correctSumApp); | ||
EXPECT_EQ(sumHost, correctSumHost); | ||
|
||
DropTable(client, path); | ||
driver.Stop(true); | ||
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.