|
19 | 19 | #include <algorithm>
|
20 | 20 | #include <fstream>
|
21 | 21 | #include <future>
|
| 22 | +#include <iostream> |
22 | 23 | #include <memory>
|
23 | 24 | #include <string>
|
| 25 | +#include <sstream> |
24 | 26 | #include <unordered_map>
|
25 | 27 | #include <utility>
|
26 | 28 |
|
@@ -319,6 +321,7 @@ bool SQLClusterRouter::Init() {
|
319 | 321 | session_variables_.emplace("enable_trace", "false");
|
320 | 322 | session_variables_.emplace("sync_job", "false");
|
321 | 323 | session_variables_.emplace("job_timeout", "60000"); // rpc request timeout for taskmanager
|
| 324 | + session_variables_.emplace("spark_config", ""); |
322 | 325 | }
|
323 | 326 | return true;
|
324 | 327 | }
|
@@ -2980,7 +2983,7 @@ std::shared_ptr<hybridse::sdk::ResultSet> SQLClusterRouter::ExecuteOfflineQuery(
|
2980 | 2983 | bool is_sync_job, int job_timeout,
|
2981 | 2984 | ::hybridse::sdk::Status* status) {
|
2982 | 2985 | RET_IF_NULL_AND_WARN(status, "output status is nullptr");
|
2983 |
| - std::map<std::string, std::string> config; |
| 2986 | + std::map<std::string, std::string> config = ParseSparkConfigString(GetSparkConfig()); |
2984 | 2987 | ReadSparkConfFromFile(std::dynamic_pointer_cast<SQLRouterOptions>(options_)->spark_conf_path, &config);
|
2985 | 2988 |
|
2986 | 2989 | if (is_sync_job) {
|
@@ -3049,6 +3052,16 @@ int SQLClusterRouter::GetJobTimeout() {
|
3049 | 3052 | return 60000;
|
3050 | 3053 | }
|
3051 | 3054 |
|
| 3055 | +std::string SQLClusterRouter::GetSparkConfig() { |
| 3056 | + std::lock_guard<::openmldb::base::SpinMutex> lock(mu_); |
| 3057 | + auto it = session_variables_.find("spark_config"); |
| 3058 | + if (it != session_variables_.end()) { |
| 3059 | + return it->second; |
| 3060 | + } |
| 3061 | + |
| 3062 | + return ""; |
| 3063 | +} |
| 3064 | + |
3052 | 3065 | ::hybridse::sdk::Status SQLClusterRouter::SetVariable(hybridse::node::SetPlanNode* node) {
|
3053 | 3066 | std::string key = node->Key();
|
3054 | 3067 | std::transform(key.begin(), key.end(), key.begin(), ::tolower);
|
@@ -3083,13 +3096,34 @@ ::hybridse::sdk::Status SQLClusterRouter::SetVariable(hybridse::node::SetPlanNod
|
3083 | 3096 | if (!absl::SimpleAtoi(value, &new_timeout)) {
|
3084 | 3097 | return {StatusCode::kCmdError, "Fail to parse value, can't set the request timeout"};
|
3085 | 3098 | }
|
| 3099 | + } else if (key == "spark_config") { |
| 3100 | + if (!CheckSparkConfigString(value)) { |
| 3101 | + return { |
| 3102 | + StatusCode::kCmdError, |
| 3103 | + "Fail to parse spark config, set like 'spark.executor.memory=2g;spark.executor.cores=2'" |
| 3104 | + }; |
| 3105 | + } |
3086 | 3106 | } else {
|
3087 | 3107 | return {};
|
3088 | 3108 | }
|
3089 | 3109 | session_variables_[key] = value;
|
3090 | 3110 | return {};
|
3091 | 3111 | }
|
3092 | 3112 |
|
| 3113 | +bool SQLClusterRouter::CheckSparkConfigString(const std::string& input) { |
| 3114 | + std::istringstream iss(input); |
| 3115 | + std::string keyValue; |
| 3116 | + |
| 3117 | + while (std::getline(iss, keyValue, ';')) { |
| 3118 | + // Check if the substring starts with "spark." |
| 3119 | + if (keyValue.find("spark.") != 0) { |
| 3120 | + return false; |
| 3121 | + } |
| 3122 | + } |
| 3123 | + |
| 3124 | + return true; |
| 3125 | +} |
| 3126 | + |
3093 | 3127 | ::hybridse::sdk::Status SQLClusterRouter::ParseNamesFromArgs(const std::string& db,
|
3094 | 3128 | const std::vector<std::string>& args, std::string* db_name, std::string* name) {
|
3095 | 3129 | if (args.size() == 1) {
|
@@ -4523,6 +4557,34 @@ bool SQLClusterRouter::CheckTableStatus(const std::string& db, const std::string
|
4523 | 4557 | return check_succeed;
|
4524 | 4558 | }
|
4525 | 4559 |
|
| 4560 | +std::map<std::string, std::string> SQLClusterRouter::ParseSparkConfigString(const std::string& input) { |
| 4561 | + std::map<std::string, std::string> configMap; |
| 4562 | + |
| 4563 | + std::istringstream iss(input); |
| 4564 | + std::string keyValue; |
| 4565 | + |
| 4566 | + while (std::getline(iss, keyValue, ';')) { |
| 4567 | + // Split the key-value pair |
| 4568 | + size_t equalPos = keyValue.find('='); |
| 4569 | + if (equalPos != std::string::npos) { |
| 4570 | + std::string key = keyValue.substr(0, equalPos); |
| 4571 | + std::string value = keyValue.substr(equalPos + 1); |
| 4572 | + |
| 4573 | + // Check if the key starts with "spark." |
| 4574 | + if (key.find("spark.") == 0) { |
| 4575 | + // Add to the map |
| 4576 | + configMap[key] = value; |
| 4577 | + } else { |
| 4578 | + std::cerr << "Error: Key does not start with 'spark.' - " << key << std::endl; |
| 4579 | + } |
| 4580 | + } else { |
| 4581 | + std::cerr << "Error: Invalid key-value pair - " << keyValue << std::endl; |
| 4582 | + } |
| 4583 | + } |
| 4584 | + |
| 4585 | + return configMap; |
| 4586 | +} |
| 4587 | + |
4526 | 4588 | void SQLClusterRouter::ReadSparkConfFromFile(std::string conf_file_path, std::map<std::string, std::string>* config) {
|
4527 | 4589 | if (!conf_file_path.empty()) {
|
4528 | 4590 | boost::property_tree::ptree pt;
|
|
0 commit comments