|
| 1 | +#include <library/cpp/colorizer/colors.h> |
| 2 | +#include <library/cpp/getopt/last_getopt.h> |
| 3 | + |
| 4 | +#include <util/datetime/base.h> |
| 5 | + |
| 6 | +#include <ydb/tests/tools/fqrun/src/fq_runner.h> |
| 7 | +#include <ydb/tests/tools/kqprun/runlib/application.h> |
| 8 | +#include <ydb/tests/tools/kqprun/runlib/utils.h> |
| 9 | + |
| 10 | +using namespace NKikimrRun; |
| 11 | + |
| 12 | +namespace NFqRun { |
| 13 | + |
| 14 | +namespace { |
| 15 | + |
| 16 | +struct TExecutionOptions { |
| 17 | + TString Query; |
| 18 | + |
| 19 | + bool HasResults() const { |
| 20 | + return !Query.empty(); |
| 21 | + } |
| 22 | + |
| 23 | + TRequestOptions GetQueryOptions() const { |
| 24 | + return { |
| 25 | + .Query = Query |
| 26 | + }; |
| 27 | + } |
| 28 | + |
| 29 | + void Validate(const TRunnerOptions& runnerOptions) const { |
| 30 | + if (!Query && !runnerOptions.FqSettings.MonitoringEnabled && !runnerOptions.FqSettings.GrpcEnabled) { |
| 31 | + ythrow yexception() << "Nothing to execute and is not running as daemon"; |
| 32 | + } |
| 33 | + } |
| 34 | +}; |
| 35 | + |
| 36 | +void RunArgumentQueries(const TExecutionOptions& executionOptions, TFqRunner& runner) { |
| 37 | + NColorizer::TColors colors = NColorizer::AutoColors(Cout); |
| 38 | + |
| 39 | + if (executionOptions.Query) { |
| 40 | + Cout << colors.Yellow() << TInstant::Now().ToIsoStringLocal() << " Executing query..." << colors.Default() << Endl; |
| 41 | + if (!runner.ExecuteStreamQuery(executionOptions.GetQueryOptions())) { |
| 42 | + ythrow yexception() << TInstant::Now().ToIsoStringLocal() << " Query execution failed"; |
| 43 | + } |
| 44 | + Cout << colors.Yellow() << TInstant::Now().ToIsoStringLocal() << " Fetching query results..." << colors.Default() << Endl; |
| 45 | + if (!runner.FetchQueryResults()) { |
| 46 | + ythrow yexception() << TInstant::Now().ToIsoStringLocal() << " Fetch query results failed"; |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + if (executionOptions.HasResults()) { |
| 51 | + try { |
| 52 | + runner.PrintQueryResults(); |
| 53 | + } catch (...) { |
| 54 | + ythrow yexception() << "Failed to print script results, reason:\n" << CurrentExceptionMessage(); |
| 55 | + } |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +void RunAsDaemon() { |
| 60 | + NColorizer::TColors colors = NColorizer::AutoColors(Cout); |
| 61 | + |
| 62 | + Cout << colors.Yellow() << TInstant::Now().ToIsoStringLocal() << " Initialization finished" << colors.Default() << Endl; |
| 63 | + while (true) { |
| 64 | + Sleep(TDuration::Seconds(1)); |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +void RunScript(const TExecutionOptions& executionOptions, const TRunnerOptions& runnerOptions) { |
| 69 | + NColorizer::TColors colors = NColorizer::AutoColors(Cout); |
| 70 | + |
| 71 | + Cout << colors.Yellow() << TInstant::Now().ToIsoStringLocal() << " Initialization of fq runner..." << colors.Default() << Endl; |
| 72 | + TFqRunner runner(runnerOptions); |
| 73 | + |
| 74 | + try { |
| 75 | + RunArgumentQueries(executionOptions, runner); |
| 76 | + } catch (const yexception& exception) { |
| 77 | + if (runnerOptions.FqSettings.MonitoringEnabled) { |
| 78 | + Cerr << colors.Red() << CurrentExceptionMessage() << colors.Default() << Endl; |
| 79 | + } else { |
| 80 | + throw exception; |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + if (runnerOptions.FqSettings.MonitoringEnabled || runnerOptions.FqSettings.GrpcEnabled) { |
| 85 | + RunAsDaemon(); |
| 86 | + } |
| 87 | + |
| 88 | + Cout << colors.Yellow() << TInstant::Now().ToIsoStringLocal() << " Finalization of fq runner..." << colors.Default() << Endl; |
| 89 | +} |
| 90 | + |
| 91 | +class TMain : public TMainBase { |
| 92 | +protected: |
| 93 | + void RegisterOptions(NLastGetopt::TOpts& options) override { |
| 94 | + options.SetTitle("FqRun -- tool to execute stream queries through FQ proxy"); |
| 95 | + options.AddHelpOption('h'); |
| 96 | + options.SetFreeArgsNum(0); |
| 97 | + |
| 98 | + // Inputs |
| 99 | + |
| 100 | + options.AddLongOption('p', "query", "Query to execute") |
| 101 | + .RequiredArgument("file") |
| 102 | + .StoreMappedResult(&ExecutionOptions.Query, &LoadFile); |
| 103 | + |
| 104 | + options.AddLongOption("fq-cfg", "File with FQ config (NFq::NConfig::TConfig for FQ proxy)") |
| 105 | + .RequiredArgument("file") |
| 106 | + .DefaultValue("./configuration/fq_config.conf") |
| 107 | + .Handler1([this](const NLastGetopt::TOptsParser* option) { |
| 108 | + if (!google::protobuf::TextFormat::ParseFromString(LoadFile(TString(option->CurValOrDef())), &RunnerOptions.FqSettings.FqConfig)) { |
| 109 | + ythrow yexception() << "Bad format of FQ configuration"; |
| 110 | + } |
| 111 | + }); |
| 112 | + |
| 113 | + // Outputs |
| 114 | + |
| 115 | + options.AddLongOption("result-file", "File with query results (use '-' to write in stdout)") |
| 116 | + .RequiredArgument("file") |
| 117 | + .DefaultValue("-") |
| 118 | + .StoreMappedResultT<TString>(&RunnerOptions.ResultOutput, &GetDefaultOutput); |
| 119 | + |
| 120 | + TChoices<EResultOutputFormat> resultFormat({ |
| 121 | + {"rows", EResultOutputFormat::RowsJson}, |
| 122 | + {"full-json", EResultOutputFormat::FullJson}, |
| 123 | + {"full-proto", EResultOutputFormat::FullProto} |
| 124 | + }); |
| 125 | + options.AddLongOption('R', "result-format", "Query result format") |
| 126 | + .RequiredArgument("result-format") |
| 127 | + .DefaultValue("rows") |
| 128 | + .Choices(resultFormat.GetChoices()) |
| 129 | + .StoreMappedResultT<TString>(&RunnerOptions.ResultOutputFormat, resultFormat); |
| 130 | + |
| 131 | + RegisterKikimrOptions(options, RunnerOptions.FqSettings); |
| 132 | + } |
| 133 | + |
| 134 | + int DoRun(NLastGetopt::TOptsParseResult&&) override { |
| 135 | + ExecutionOptions.Validate(RunnerOptions); |
| 136 | + |
| 137 | + auto& logConfig = RunnerOptions.FqSettings.LogConfig; |
| 138 | + logConfig.SetDefaultLevel(NActors::NLog::EPriority::PRI_CRIT); |
| 139 | + FillLogConfig(logConfig); |
| 140 | + |
| 141 | + RunScript(ExecutionOptions, RunnerOptions); |
| 142 | + |
| 143 | + return 0; |
| 144 | + } |
| 145 | + |
| 146 | +private: |
| 147 | + TExecutionOptions ExecutionOptions; |
| 148 | + TRunnerOptions RunnerOptions; |
| 149 | +}; |
| 150 | + |
| 151 | +} // anonymous namespace |
| 152 | + |
| 153 | +} // namespace NFqRun |
| 154 | + |
| 155 | +int main(int argc, const char* argv[]) { |
| 156 | + SetupSignalActions(); |
| 157 | + |
| 158 | + try { |
| 159 | + NFqRun::TMain().Run(argc, argv); |
| 160 | + } catch (...) { |
| 161 | + NColorizer::TColors colors = NColorizer::AutoColors(Cerr); |
| 162 | + |
| 163 | + Cerr << colors.Red() << CurrentExceptionMessage() << colors.Default() << Endl; |
| 164 | + return 1; |
| 165 | + } |
| 166 | +} |
0 commit comments