Skip to content

Commit 3ee5135

Browse files
authored
YQ-3561 add features for FQ-run (#14826)
1 parent fe62452 commit 3ee5135

File tree

20 files changed

+351
-132
lines changed

20 files changed

+351
-132
lines changed

ydb/library/yql/providers/pq/gateway/dummy/yql_pq_dummy_gateway.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ struct TDummyTopic {
2626
TString TopicName;
2727
TMaybe<TString> Path;
2828
size_t PartitionsCount;
29+
bool CancelOnFileFinish = false;
2930
};
3031

3132
// Dummy Pq gateway for tests.

ydb/library/yql/providers/pq/gateway/dummy/yql_pq_file_topic_client.cpp

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,15 @@ class TFileTopicReadSession : public NYdb::NTopic::IReadSession {
1616
constexpr static auto FILE_POLL_PERIOD = TDuration::MilliSeconds(5);
1717

1818
public:
19-
TFileTopicReadSession(TFile file, NYdb::NTopic::TPartitionSession::TPtr session, const TString& producerId = "")
19+
TFileTopicReadSession(TFile file, NYdb::NTopic::TPartitionSession::TPtr session, const TString& producerId = "", bool cancelOnFileFinish = false)
2020
: File_(std::move(file))
2121
, Session_(std::move(session))
2222
, ProducerId_(producerId)
2323
, FilePoller_([this] () {
2424
PollFileForChanges();
2525
})
2626
, Counters_()
27+
, CancelOnFileFinish_(cancelOnFileFinish)
2728
{
2829
Pool_.Start(1);
2930
}
@@ -123,7 +124,7 @@ constexpr static auto FILE_POLL_PERIOD = TDuration::MilliSeconds(5);
123124
size_t size = 0;
124125
ui64 maxBatchRowSize = 100;
125126

126-
while (size_t read = fi.ReadLine(rawMsg)) {
127+
while (fi.ReadLine(rawMsg)) {
127128
msgs.emplace_back(MakeNextMessage(rawMsg));
128129
MsgOffset_++;
129130
if (!maxBatchRowSize--) {
@@ -133,6 +134,8 @@ constexpr static auto FILE_POLL_PERIOD = TDuration::MilliSeconds(5);
133134
}
134135
if (!msgs.empty()) {
135136
EventsQ_.Push(NYdb::NTopic::TReadSessionEvent::TDataReceivedEvent(msgs, {}, Session_), size);
137+
} else if (CancelOnFileFinish_) {
138+
EventsQ_.Push(NYdb::NTopic::TSessionClosedEvent(NYdb::EStatus::CANCELLED, {NYdb::NIssue::TIssue("PQ file topic was finished")}), size);
136139
}
137140

138141
Sleep(FILE_POLL_PERIOD);
@@ -145,6 +148,7 @@ constexpr static auto FILE_POLL_PERIOD = TDuration::MilliSeconds(5);
145148
TString ProducerId_;
146149
std::thread FilePoller_;
147150
NYdb::NTopic::TReaderCounters::TPtr Counters_;
151+
bool CancelOnFileFinish_ = false;
148152

149153
TThreadPool Pool_;
150154
size_t MsgOffset_ = 0;
@@ -336,6 +340,10 @@ struct TDummyPartitionSession: public NYdb::NTopic::TPartitionSession {
336340
}
337341
};
338342

343+
TFileTopicClient::TFileTopicClient(THashMap<TDummyPqGateway::TClusterNPath, TDummyTopic> topics)
344+
: Topics_(std::move(topics))
345+
{}
346+
339347
std::shared_ptr<NYdb::NTopic::IReadSession> TFileTopicClient::CreateReadSession(const NYdb::NTopic::TReadSessionSettings& settings) {
340348
Y_ENSURE(!settings.Topics_.empty());
341349
const auto& topic = settings.Topics_.front();
@@ -358,7 +366,8 @@ std::shared_ptr<NYdb::NTopic::IReadSession> TFileTopicClient::CreateReadSession(
358366
ui64 sessionId = 0;
359367
return std::make_shared<TFileTopicReadSession>(
360368
TFile(*filePath, EOpenMode::TEnum::RdOnly),
361-
MakeIntrusive<TDummyPartitionSession>(sessionId, TString{topicPath}, partitionId)
369+
MakeIntrusive<TDummyPartitionSession>(sessionId, TString{topicPath}, partitionId),
370+
"", topicsIt->second.CancelOnFileFinish
362371
);
363372
}
364373

ydb/library/yql/providers/pq/gateway/dummy/yql_pq_file_topic_client.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace NYql {
66
struct TFileTopicClient : public ITopicClient {
7-
TFileTopicClient(THashMap<TDummyPqGateway::TClusterNPath, TDummyTopic> topics): Topics_(topics) {}
7+
explicit TFileTopicClient(THashMap<TDummyPqGateway::TClusterNPath, TDummyTopic> topics);
88

99
NYdb::TAsyncStatus CreateTopic(const TString& path, const NYdb::NTopic::TCreateTopicSettings& settings = {}) override;
1010

@@ -32,5 +32,7 @@ struct TFileTopicClient : public ITopicClient {
3232

3333
private:
3434
THashMap<TDummyPqGateway::TClusterNPath, TDummyTopic> Topics_;
35+
bool CancelOnFileFinish_ = false;
3536
};
36-
}
37+
38+
}

ydb/tests/tools/fqrun/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,5 @@ sync_dir
55
*.conf
66
*.parquet
77
*.json
8+
*.svg
9+
*.txt

ydb/tests/tools/fqrun/README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,15 @@
22

33
Tool can be used to execute streaming queries by using FQ proxy infrastructure.
44

5+
For profiling memory allocations build fqrun with ya make flags `-D PROFILE_MEMORY_ALLOCATIONS -D CXXFLAGS=-DPROFILE_MEMORY_ALLOCATIONS`.
6+
7+
## Scripts
8+
9+
* `flame_graph.sh` - script for collecting flame graphs in svg format, usage:
10+
```(bash)
11+
./flame_graph.sh [graph collection time in seconds]
12+
```
13+
514
## Examples
615
716
### Queries
@@ -25,7 +34,7 @@ Tool can be used to execute streaming queries by using FQ proxy infrastructure.
2534
./fqrun -M 32000
2635
```
2736
28-
Monitoring endpoint: https://localhost:32000
37+
Monitoring endpoint: http://localhost:32000
2938
3039
* gRPC endpoint:
3140
```(bash)
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
Executor {
2+
Type: BASIC
3+
Threads: 1
4+
SpinThreshold: 10
5+
Name: "System"
6+
}
7+
Executor {
8+
Type: BASIC
9+
Threads: 6
10+
SpinThreshold: 1
11+
Name: "User"
12+
}
13+
Executor {
14+
Type: BASIC
15+
Threads: 1
16+
SpinThreshold: 1
17+
Name: "Batch"
18+
}
19+
Executor {
20+
Type: IO
21+
Threads: 1
22+
Name: "IO"
23+
}
24+
Executor {
25+
Type: BASIC
26+
Threads: 2
27+
SpinThreshold: 10
28+
Name: "IC"
29+
TimePerMailboxMicroSecs: 100
30+
}
31+
32+
Scheduler {
33+
Resolution: 64
34+
SpinThreshold: 0
35+
ProgressThreshold: 10000
36+
}
37+
38+
SysExecutor: 0
39+
UserExecutor: 1
40+
IoExecutor: 3
41+
BatchExecutor: 2
42+
43+
ServiceExecutor {
44+
ServiceName: "Interconnect"
45+
ExecutorId: 4
46+
}

ydb/tests/tools/fqrun/flame_graph.sh

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/usr/bin/env bash
2+
3+
set -eux
4+
5+
function cleanup {
6+
sudo rm ./profdata
7+
rm ./profdata.txt
8+
}
9+
trap cleanup EXIT
10+
11+
if [ $# -gt 1 ]; then
12+
echo "Too many arguments"
13+
exit -1
14+
fi
15+
16+
fqrun_pid=$(pgrep -u $USER fqrun)
17+
18+
sudo perf record -F 50 --call-graph dwarf -g --proc-map-timeout=10000 --pid $fqrun_pid -v -o profdata -- sleep ${1:-'30'}
19+
sudo perf script -i profdata > profdata.txt
20+
21+
SCRIPT_DIR=$(cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd)
22+
23+
flame_graph_tool="$SCRIPT_DIR/../../../../contrib/tools/flame-graph/"
24+
25+
${flame_graph_tool}/stackcollapse-perf.pl profdata.txt | ${flame_graph_tool}/flamegraph.pl > profdata.svg

ydb/tests/tools/fqrun/fqrun.cpp

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,16 @@
33

44
#include <util/datetime/base.h>
55

6+
#include <ydb/core/blob_depot/mon_main.h>
67
#include <ydb/library/yql/providers/pq/gateway/dummy/yql_pq_dummy_gateway.h>
78
#include <ydb/tests/tools/fqrun/src/fq_runner.h>
89
#include <ydb/tests/tools/kqprun/runlib/application.h>
910
#include <ydb/tests/tools/kqprun/runlib/utils.h>
1011

12+
#ifdef PROFILE_MEMORY_ALLOCATIONS
13+
#include <library/cpp/lfalloc/alloc_profiler/profiler.h>
14+
#endif
15+
1116
using namespace NKikimrRun;
1217

1318
namespace NFqRun {
@@ -156,6 +161,21 @@ class TMain : public TMainBase {
156161
}
157162
});
158163

164+
options.AddLongOption("as-cfg", "File with actor system config (TActorSystemConfig), use '-' for default")
165+
.RequiredArgument("file")
166+
.DefaultValue("./configuration/as_config.conf")
167+
.Handler1([this](const NLastGetopt::TOptsParser* option) {
168+
const TString file(option->CurValOrDef());
169+
if (file == "-") {
170+
return;
171+
}
172+
173+
RunnerOptions.FqSettings.ActorSystemConfig = NKikimrConfig::TActorSystemConfig();
174+
if (!google::protobuf::TextFormat::ParseFromString(LoadFile(file), &(*RunnerOptions.FqSettings.ActorSystemConfig))) {
175+
ythrow yexception() << "Bad format of actor system configuration";
176+
}
177+
});
178+
159179
options.AddLongOption("emulate-s3", "Enable readings by s3 provider from files, `bucket` value in connection - path to folder with files")
160180
.NoArgument()
161181
.SetFlag(&RunnerOptions.FqSettings.EmulateS3);
@@ -165,17 +185,27 @@ class TMain : public TMainBase {
165185
.Handler1([this](const NLastGetopt::TOptsParser* option) {
166186
TStringBuf topicName, others;
167187
TStringBuf(option->CurVal()).Split('@', topicName, others);
188+
168189
TStringBuf path, partitionCountStr;
169190
TStringBuf(others).Split(':', path, partitionCountStr);
170191
size_t partitionCount = !partitionCountStr.empty() ? FromString<size_t>(partitionCountStr) : 1;
192+
if (!partitionCount) {
193+
ythrow yexception() << "Topic partition count should be at least one";
194+
}
171195
if (topicName.empty() || path.empty()) {
172-
ythrow yexception() << "Incorrect PQ file mapping, expected form topic@path[:partitions_count]" << Endl;
196+
ythrow yexception() << "Incorrect PQ file mapping, expected form topic@path[:partitions_count]";
173197
}
174198
if (!PqFilesMapping.emplace(topicName, NYql::TDummyTopic("pq", TString(topicName), TString(path), partitionCount)).second) {
175199
ythrow yexception() << "Got duplicated topic name: " << topicName;
176200
}
177201
});
178202

203+
options.AddLongOption("cancel-on-file-finish", "Cancel emulate YDS topics when topic file finished")
204+
.RequiredArgument("topic")
205+
.Handler1([this](const NLastGetopt::TOptsParser* option) {
206+
TopicsSettings[option->CurVal()].CancelOnFileFinish = true;
207+
});
208+
179209
// Outputs
180210

181211
options.AddLongOption("result-file", "File with query results (use '-' to write in stdout)")
@@ -210,6 +240,7 @@ class TMain : public TMainBase {
210240
ExecutionOptions.Validate(RunnerOptions);
211241

212242
RunnerOptions.FqSettings.YqlToken = GetEnv(YQL_TOKEN_VARIABLE);
243+
RunnerOptions.FqSettings.FunctionRegistry = CreateFunctionRegistry().Get();
213244

214245
auto& gatewayConfig = *RunnerOptions.FqSettings.FqConfig.mutable_gateways();
215246
FillTokens(gatewayConfig.mutable_pq());
@@ -224,14 +255,39 @@ class TMain : public TMainBase {
224255

225256
if (!PqFilesMapping.empty()) {
226257
auto fileGateway = MakeIntrusive<NYql::TDummyPqGateway>();
227-
for (const auto& [_, topic] : PqFilesMapping) {
258+
for (auto [_, topic] : PqFilesMapping) {
259+
if (const auto it = TopicsSettings.find(topic.TopicName); it != TopicsSettings.end()) {
260+
topic.CancelOnFileFinish = it->second.CancelOnFileFinish;
261+
TopicsSettings.erase(it);
262+
}
228263
fileGateway->AddDummyTopic(topic);
229264
}
230265
RunnerOptions.FqSettings.PqGateway = std::move(fileGateway);
231266
}
267+
if (!TopicsSettings.empty()) {
268+
ythrow yexception() << "Found topic settings for not existing topic: '" << TopicsSettings.begin()->first << "'";
269+
}
270+
271+
#ifdef PROFILE_MEMORY_ALLOCATIONS
272+
if (RunnerOptions.FqSettings.VerboseLevel >= EVerbose::Info) {
273+
Cout << CoutColors.Cyan() << "Starting profile memory allocations" << CoutColors.Default() << Endl;
274+
}
275+
NAllocProfiler::StartAllocationSampling(true);
276+
#else
277+
if (ProfileAllocationsOutput) {
278+
ythrow yexception() << "Profile memory allocations disabled, please rebuild fqrun with flag `-D PROFILE_MEMORY_ALLOCATIONS`";
279+
}
280+
#endif
232281

233282
RunScript(ExecutionOptions, RunnerOptions);
234283

284+
#ifdef PROFILE_MEMORY_ALLOCATIONS
285+
if (RunnerOptions.FqSettings.VerboseLevel >= EVerbose::Info) {
286+
Cout << CoutColors.Cyan() << "Finishing profile memory allocations" << CoutColors.Default() << Endl;
287+
}
288+
FinishProfileMemoryAllocations();
289+
#endif
290+
235291
return 0;
236292
}
237293

@@ -249,6 +305,11 @@ class TMain : public TMainBase {
249305
TExecutionOptions ExecutionOptions;
250306
TRunnerOptions RunnerOptions;
251307
std::unordered_map<TString, NYql::TDummyTopic> PqFilesMapping;
308+
309+
struct TTopicSettings {
310+
bool CancelOnFileFinish = false;
311+
};
312+
std::unordered_map<TString, TTopicSettings> TopicsSettings;
252313
};
253314

254315
} // anonymous namespace

ydb/tests/tools/fqrun/src/common.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
#include <ydb/library/yql/providers/pq/provider/yql_pq_gateway.h>
88
#include <ydb/tests/tools/kqprun/runlib/settings.h>
99

10+
#include <yql/essentials/minikql/mkql_function_registry.h>
11+
1012
namespace NFqRun {
1113

1214
constexpr char YQL_TOKEN_VARIABLE[] = "YQL_TOKEN";
@@ -27,8 +29,10 @@ struct TFqSetupSettings : public NKikimrRun::TServerSettings {
2729

2830
TString YqlToken;
2931
NYql::IPqGateway::TPtr PqGateway;
32+
TIntrusivePtr<NKikimr::NMiniKQL::IMutableFunctionRegistry> FunctionRegistry;
3033
NFq::NConfig::TConfig FqConfig;
3134
NKikimrConfig::TLogConfig LogConfig;
35+
std::optional<NKikimrConfig::TActorSystemConfig> ActorSystemConfig;
3236
};
3337

3438
struct TRunnerOptions {

ydb/tests/tools/fqrun/src/fq_setup.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,14 @@ class TFqSetup::TImpl {
4444
serverSettings.SetLoggerInitializer(loggerInitializer);
4545
}
4646

47+
void SetFunctionRegistry(NKikimr::Tests::TServerSettings& serverSettings) const {
48+
if (Settings.FunctionRegistry) {
49+
serverSettings.SetFrFactory([this](const NKikimr::NScheme::TTypeRegistry&) {
50+
return Settings.FunctionRegistry.Get();
51+
});
52+
}
53+
}
54+
4755
NKikimr::Tests::TServerSettings GetServerSettings(ui32 grpcPort) {
4856
NKikimr::Tests::TServerSettings serverSettings(PortManager.GetPort());
4957

@@ -52,9 +60,13 @@ class TFqSetup::TImpl {
5260

5361
NKikimrConfig::TAppConfig config;
5462
*config.MutableLogConfig() = Settings.LogConfig;
63+
if (Settings.ActorSystemConfig) {
64+
*config.MutableActorSystemConfig() = *Settings.ActorSystemConfig;
65+
}
5566
serverSettings.SetAppConfig(config);
5667

5768
SetLoggerSettings(serverSettings);
69+
SetFunctionRegistry(serverSettings);
5870

5971
if (Settings.MonitoringEnabled) {
6072
serverSettings.InitKikimrRunConfig();

ydb/tests/tools/fqrun/src/ya.make

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ SRCS(
99
PEERDIR(
1010
library/cpp/colorizer
1111
library/cpp/testing/unittest
12-
util
1312
ydb/core/fq/libs/config/protos
1413
ydb/core/fq/libs/control_plane_proxy/events
1514
ydb/core/fq/libs/init
@@ -20,6 +19,7 @@ PEERDIR(
2019
ydb/library/security
2120
ydb/library/yql/providers/pq/provider
2221
ydb/tests/tools/kqprun/runlib
22+
yql/essentials/minikql
2323
)
2424

2525
YQL_LAST_ABI_VERSION()

0 commit comments

Comments
 (0)