Coverage Report

Created: 2026-04-14 10:07

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/service/http/action/config_action.cpp
Line
Count
Source
1
// Licensed to the Apache Software Foundation (ASF) under one
2
// or more contributor license agreements.  See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership.  The ASF licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License.  You may obtain a copy of the License at
8
//
9
//   http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied.  See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
18
#include "service/http/action/config_action.h"
19
20
#include <rapidjson/document.h>
21
#include <rapidjson/encodings.h>
22
#include <rapidjson/prettywriter.h>
23
#include <rapidjson/stringbuffer.h>
24
#include <rapidjson/writer.h>
25
26
#include <cstdint>
27
#include <map>
28
#include <ostream>
29
#include <string>
30
#include <utility>
31
#include <vector>
32
33
#include "absl/strings/substitute.h"
34
#include "common/cast_set.h"
35
#include "common/config.h"
36
#include "common/logging.h"
37
#include "common/status.h"
38
#include "service/http/http_channel.h"
39
#include "service/http/http_headers.h"
40
#include "service/http/http_request.h"
41
#include "service/http/http_status.h"
42
43
namespace doris {
44
45
const static std::string HEADER_JSON = "application/json";
46
const static std::string PERSIST_PARAM = "persist";
47
const std::string CONF_ITEM = "conf_item";
48
49
116
void ConfigAction::handle(HttpRequest* req) {
50
116
    if (_config_type == ConfigActionType::UPDATE_CONFIG) {
51
39
        handle_update_config(req);
52
77
    } else if (_config_type == ConfigActionType::SHOW_CONFIG) {
53
77
        handle_show_config(req);
54
77
    }
55
116
}
56
57
77
void ConfigAction::handle_show_config(HttpRequest* req) {
58
77
    std::vector<std::vector<std::string>> config_info = config::get_config_info();
59
60
77
    rapidjson::StringBuffer str_buf;
61
77
    rapidjson::Writer<rapidjson::StringBuffer> writer(str_buf);
62
63
77
    const std::string& conf_item = req->param(CONF_ITEM);
64
65
77
    writer.StartArray();
66
54.1k
    for (const auto& _config : config_info) {
67
54.1k
        if (!conf_item.empty()) {
68
4.11k
            if (_config[0] == conf_item) {
69
14
                writer.StartArray();
70
56
                for (const std::string& config_filed : _config) {
71
56
                    writer.String(config_filed.c_str());
72
56
                }
73
14
                writer.EndArray();
74
14
                break;
75
14
            }
76
50.0k
        } else {
77
50.0k
            writer.StartArray();
78
200k
            for (const std::string& config_filed : _config) {
79
200k
                writer.String(config_filed.c_str());
80
200k
            }
81
50.0k
            writer.EndArray();
82
50.0k
        }
83
54.1k
    }
84
85
77
    writer.EndArray();
86
77
    HttpChannel::send_reply(req, str_buf.GetString());
87
77
}
88
89
39
void ConfigAction::handle_update_config(HttpRequest* req) {
90
39
    LOG(INFO) << req->debug_string();
91
92
39
    Status s;
93
39
    std::string msg;
94
39
    rapidjson::Document root;
95
39
    root.SetObject();
96
39
    rapidjson::Document results;
97
39
    results.SetArray();
98
39
    if (req->params()->size() < 1) {
99
0
        s = Status::InvalidArgument("");
100
0
        msg = "Now only support to set a single config once, via 'config_name=new_value', and with "
101
0
              "an optional parameter 'persist'.";
102
39
    } else {
103
39
        bool need_persist = false;
104
39
        if (req->params()->find(PERSIST_PARAM)->second.compare("true") == 0) {
105
0
            need_persist = true;
106
0
        }
107
40
        for (const auto& [key, value] : *req->params()) {
108
40
            if (key == PERSIST_PARAM) {
109
0
                continue;
110
0
            }
111
40
            s = config::set_config(key, value, need_persist);
112
40
            if (s.ok()) {
113
39
                LOG(INFO) << "set_config " << key << "=" << value
114
39
                          << " success. persist: " << need_persist;
115
39
            } else {
116
1
                LOG(WARNING) << "set_config " << key << "=" << value << " failed";
117
1
                msg = absl::Substitute("set $0=$1 failed, reason: $2.", key, value, s.to_string());
118
1
            }
119
40
            std::string status(s.ok() ? "OK" : "BAD");
120
40
            rapidjson::Value result;
121
40
            result.SetObject();
122
40
            result.AddMember("config_name",
123
40
                             rapidjson::Value(key.c_str(), cast_set<uint32_t>(key.size()),
124
40
                                              results.GetAllocator()),
125
40
                             results.GetAllocator());
126
40
            result.AddMember("status",
127
40
                             rapidjson::Value(status.c_str(), cast_set<uint32_t>(status.size()),
128
40
                                              results.GetAllocator()),
129
40
                             results.GetAllocator());
130
40
            result.AddMember("msg",
131
40
                             rapidjson::Value(msg.c_str(), cast_set<uint32_t>(msg.size()),
132
40
                                              results.GetAllocator()),
133
40
                             results.GetAllocator());
134
40
            results.PushBack(result, results.GetAllocator());
135
40
        }
136
39
    }
137
138
39
    rapidjson::StringBuffer strbuf;
139
39
    rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(strbuf);
140
39
    results.Accept(writer);
141
142
39
    req->add_output_header(HttpHeaders::CONTENT_TYPE, HEADER_JSON.c_str());
143
39
    HttpChannel::send_reply(req, HttpStatus::OK, strbuf.GetString());
144
39
}
145
146
} // namespace doris