|
| 1 | +#include "key_value.h" |
| 2 | + |
| 3 | +#include <ydb-cpp-sdk/client/table/table.h> |
| 4 | +#include <util/string/printf.h> |
| 5 | + |
| 6 | +using namespace NYdb; |
| 7 | +using namespace NYdb::NTable; |
| 8 | + |
| 9 | +TWriteJob::TWriteJob(const TCommonOptions& opts, std::uint32_t maxId) |
| 10 | + : TThreadJob(opts) |
| 11 | + , Executor(opts, Stats, TExecutor::ModeNonBlocking) |
| 12 | + , Generator(opts, maxId) |
| 13 | +{} |
| 14 | + |
| 15 | +void TWriteJob::ShowProgress(TStringBuilder& report) { |
| 16 | + report << Endl << "=====- WriteJob report (Thread B) -=====" << Endl; |
| 17 | + Executor.Report(report); |
| 18 | + report << "Generated " << ValuesGenerated.load() << " new elements." << Endl |
| 19 | + << "Generator compute time: " << Generator.GetComputeTime() << Endl; |
| 20 | + Stats.PrintStatistics(report); |
| 21 | + report << "==================================================" << Endl; |
| 22 | +} |
| 23 | + |
| 24 | +void TWriteJob::DoJob() { |
| 25 | + while (!ShouldStop.load() && TInstant::Now() < Deadline) { |
| 26 | + TKeyValueRecordData recordData = Generator.Get(); |
| 27 | + auto value = BuildValueFromRecord(recordData); |
| 28 | + |
| 29 | + ValuesGenerated.fetch_add(1); |
| 30 | + |
| 31 | + auto upload = [value{ std::move(value) }, this](TSession session)->TAsyncStatus { |
| 32 | + static const TString query = Sprintf(R"( |
| 33 | +--!syntax_v1 |
| 34 | +PRAGMA TablePathPrefix("%s"); |
| 35 | +
|
| 36 | +DECLARE $items AS |
| 37 | + List<Struct< |
| 38 | + `object_id_key`: Uint32, |
| 39 | + `object_id`: Uint32, |
| 40 | + `timestamp`: Uint64, |
| 41 | + `payload`: Utf8>>; |
| 42 | +
|
| 43 | +UPSERT INTO `%s` SELECT * FROM AS_TABLE($items); |
| 44 | +
|
| 45 | +)", Prefix.c_str(), TableName.c_str()); |
| 46 | + |
| 47 | + auto promise = NThreading::NewPromise<TStatus>(); |
| 48 | + auto params = PackValuesToParamsAsList({value}); |
| 49 | + |
| 50 | + auto resultFuture = session.ExecuteDataQuery( |
| 51 | + query, |
| 52 | + TTxControl::BeginTx(TTxSettings::SerializableRW()).CommitTx(), |
| 53 | + std::move(params), |
| 54 | + TExecDataQuerySettings() |
| 55 | + .KeepInQueryCache(true) |
| 56 | + .OperationTimeout(MaxDelay + ReactionTimeDelay) |
| 57 | + .ClientTimeout(MaxDelay + ReactionTimeDelay) |
| 58 | + ); |
| 59 | + |
| 60 | + resultFuture.Subscribe([promise](TAsyncDataQueryResult queryFuture) mutable { |
| 61 | + Y_ABORT_UNLESS(queryFuture.HasValue()); |
| 62 | + TDataQueryResult queryResult = queryFuture.GetValue(); |
| 63 | + promise.SetValue(std::move(queryResult)); |
| 64 | + }); |
| 65 | + |
| 66 | + return promise.GetFuture(); |
| 67 | + }; |
| 68 | + |
| 69 | + RpsProvider.Use(); |
| 70 | + |
| 71 | + if (!Executor.Execute(upload)) { |
| 72 | + break; |
| 73 | + } |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +void TWriteJob::OnFinish() { |
| 78 | + Executor.Finish(); |
| 79 | + Executor.Wait(); |
| 80 | + Stats.Flush(); |
| 81 | +} |
| 82 | + |
| 83 | +// Implementation of TReadJob |
| 84 | +TReadJob::TReadJob(const TCommonOptions& opts, std::uint32_t maxId) |
| 85 | + : TThreadJob(opts) |
| 86 | + , Executor(opts.RetryMode ? new TExecutorWithRetry(opts, Stats) : new TExecutor(opts, Stats)) |
| 87 | + , ObjectIdRange(static_cast<std::uint32_t>(maxId * 1.25)) // 20% of requests with no result |
| 88 | + , SaveResult(opts.SaveResult) |
| 89 | +{} |
| 90 | + |
| 91 | +void TReadJob::ShowProgress(TStringBuilder& report) { |
| 92 | + report << Endl << "======- ReadJob report (Thread A) -======" << Endl; |
| 93 | + Executor->Report(report); |
| 94 | + Stats.PrintStatistics(report); |
| 95 | + report << "========================================" << Endl; |
| 96 | +} |
| 97 | + |
| 98 | +void TReadJob::DoJob() { |
| 99 | + while (!ShouldStop.load() && TInstant::Now() < Deadline) { |
| 100 | + std::uint32_t idToSelect = RandomNumber<std::uint32_t>() % ObjectIdRange; |
| 101 | + |
| 102 | + auto read = [idToSelect, this](TSession session) -> TAsyncStatus { |
| 103 | + static const TString query = Sprintf(R"( |
| 104 | +--!syntax_v1 |
| 105 | +PRAGMA TablePathPrefix("%s"); |
| 106 | +
|
| 107 | +DECLARE $object_id_key AS Uint32; |
| 108 | +DECLARE $object_id AS Uint32; |
| 109 | +
|
| 110 | +SELECT * FROM `%s` WHERE `object_id_key` = $object_id_key AND `object_id` = $object_id; |
| 111 | +
|
| 112 | +)", Prefix.c_str(), TableName.c_str()); |
| 113 | + |
| 114 | + auto promise = NThreading::NewPromise<TStatus>(); |
| 115 | + auto params = TParamsBuilder() |
| 116 | + .AddParam("$object_id_key") |
| 117 | + .Uint32(GetHash(idToSelect)) |
| 118 | + .Build() |
| 119 | + .AddParam("$object_id") |
| 120 | + .Uint32(idToSelect) |
| 121 | + .Build() |
| 122 | + .Build(); |
| 123 | + |
| 124 | + auto resultFuture = session.ExecuteDataQuery( |
| 125 | + query, |
| 126 | + TTxControl::BeginTx(TTxSettings::SerializableRW()).CommitTx(), |
| 127 | + std::move(params), |
| 128 | + TExecDataQuerySettings() |
| 129 | + .KeepInQueryCache(true) |
| 130 | + .OperationTimeout(MaxDelay + ReactionTimeDelay) |
| 131 | + .ClientTimeout(MaxDelay + ReactionTimeDelay) |
| 132 | + ); |
| 133 | + |
| 134 | + resultFuture.Subscribe([promise](TAsyncDataQueryResult queryFuture) mutable { |
| 135 | + Y_ABORT_UNLESS(queryFuture.HasValue()); |
| 136 | + TDataQueryResult queryResult = queryFuture.GetValue(); |
| 137 | + promise.SetValue(std::move(queryResult)); |
| 138 | + }); |
| 139 | + |
| 140 | + return promise.GetFuture(); |
| 141 | + }; |
| 142 | + |
| 143 | + RpsProvider.Use(); |
| 144 | + |
| 145 | + if (!Executor->Execute(read)) { |
| 146 | + break; |
| 147 | + } |
| 148 | + } |
| 149 | +} |
| 150 | + |
| 151 | +void TReadJob::OnFinish() { |
| 152 | + Executor->Finish(); |
| 153 | + std::uint32_t infly = Executor->Wait(WaitTimeout); |
| 154 | + if (infly) { |
| 155 | + Cerr << "Warning: thread A finished while having " << infly << " infly requests." << Endl; |
| 156 | + } |
| 157 | + Stats.Flush(); |
| 158 | + if (SaveResult) { |
| 159 | + Stats.SaveResult(); |
| 160 | + } |
| 161 | +} |
0 commit comments