-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added an http module for handling worker monitoring requests
- Loading branch information
1 parent
16d11b9
commit c7d2cea
Showing
6 changed files
with
298 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
/* | ||
* LSST Data Management System | ||
* | ||
* This product includes software developed by the | ||
* LSST Project (http://www.lsst.org/). | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the LSST License Statement and | ||
* the GNU General Public License along with this program. If not, | ||
* see <http://www.lsstcorp.org/LegalNotices/>. | ||
*/ | ||
|
||
// Class header | ||
#include "xrdsvc/HttpMonitorModule.h" | ||
|
||
// System headers | ||
#include <set> | ||
#include <stdexcept> | ||
|
||
// Qserv headers | ||
#include "http/Exceptions.h" | ||
#include "http/RequestQuery.h" | ||
#include "mysql/MySqlUtils.h" | ||
#include "qhttp/Request.h" | ||
#include "qhttp/Response.h" | ||
#include "wbase/FileChannelShared.h" | ||
#include "wbase/TaskState.h" | ||
#include "wconfig/WorkerConfig.h" | ||
#include "wcontrol/Foreman.h" | ||
#include "wcontrol/ResourceMonitor.h" | ||
#include "wpublish/QueriesAndChunks.h" | ||
|
||
using namespace std; | ||
using json = nlohmann::json; | ||
|
||
namespace { | ||
string const noAdminAuthKey; | ||
} // namespace | ||
|
||
namespace lsst::qserv::xrdsvc { | ||
|
||
void HttpMonitorModule::process(string const& context, shared_ptr<wcontrol::Foreman> const& foreman, | ||
shared_ptr<qhttp::Request> const& req, | ||
shared_ptr<qhttp::Response> const& resp, string const& subModuleName, | ||
http::AuthType const authType) { | ||
HttpMonitorModule module(context, foreman, req, resp); | ||
module.execute(subModuleName, authType); | ||
} | ||
|
||
HttpMonitorModule::HttpMonitorModule(string const& context, shared_ptr<wcontrol::Foreman> const& foreman, | ||
shared_ptr<qhttp::Request> const& req, | ||
shared_ptr<qhttp::Response> const& resp) | ||
: http::ModuleBase(wconfig::WorkerConfig::instance()->replicationAuthKey(), ::noAdminAuthKey, req, | ||
resp), | ||
_context(context), | ||
_foreman(foreman) {} | ||
|
||
json HttpMonitorModule::executeImpl(string const& subModuleName) { | ||
string const func = string(__func__) + "[sub-module='" + subModuleName + "']"; | ||
debug(func); | ||
enforceInstanceId(func, wconfig::WorkerConfig::instance()->replicationInstanceId()); | ||
if (subModuleName == "CONFIG") | ||
return _config(); | ||
else if (subModuleName == "MYSQL") | ||
return _mysql(); | ||
else if (subModuleName == "STATUS") | ||
return _status(); | ||
throw invalid_argument(context() + func + " unsupported sub-module"); | ||
} | ||
|
||
string HttpMonitorModule::context() const { return _context; } | ||
|
||
json HttpMonitorModule::_config() { | ||
debug(__func__); | ||
return wconfig::WorkerConfig::instance()->toJson(); | ||
} | ||
|
||
json HttpMonitorModule::_mysql() { | ||
debug(__func__); | ||
json result; | ||
try { | ||
bool const full = true; | ||
result = mysql::MySqlUtils::processList(wconfig::WorkerConfig::instance()->getMySqlConfig(), full); | ||
} catch (mysql::MySqlQueryError const& ex) { | ||
error(__func__, ex.what()); | ||
throw http::Error(__func__, ex.what()); | ||
} | ||
|
||
// Amend the result with a map linking MySQL thread identifiers to the corresponding | ||
// tasks that are being (or have been) processed by the worker. Note that only a subset | ||
// of tasks is selected for the known MySQL threads. This prevents the monitoring | ||
// system from pulling old tasks that may still keep records of the closed threads. | ||
set<unsigned long> activeMySqlThreadIds; | ||
for (auto const& row : result["queries"]["rows"]) { | ||
// The thread identifier is stored as a string at the very first element | ||
// of the array. See mysql::MySqlUtils::processList for details. | ||
activeMySqlThreadIds.insert(stoul(row[0].get<string>())); | ||
} | ||
result["mysql_thread_to_task"] = _foreman->queriesAndChunks()->mySqlThread2task(activeMySqlThreadIds); | ||
return result; | ||
} | ||
|
||
json HttpMonitorModule::_status() { | ||
debug(__func__); | ||
wbase::TaskSelector const taskSelector = _translateTaskSelector(__func__); | ||
json result; | ||
result["processor"] = _foreman->statusToJson(taskSelector); | ||
result["resources"] = _foreman->resourceMonitor()->statusToJson(); | ||
result["filesystem"] = wbase::FileChannelShared::statusToJson(); | ||
return result; | ||
} | ||
|
||
wbase::TaskSelector HttpMonitorModule::_translateTaskSelector(string const& func) const { | ||
wbase::TaskSelector selector; | ||
selector.includeTasks = query().optionalUInt("include_tasks", 0) != 0; | ||
selector.queryIds = query().optionalVectorUInt64("query_ids"); | ||
string const taskStatesParam = "task_states"; | ||
for (auto&& str : query().optionalVectorStr(taskStatesParam)) { | ||
try { | ||
auto const state = wbase::str2taskState(str); | ||
selector.taskStates.push_back(state); | ||
debug(func, "str='" + str + "', task state=" + wbase::taskState2str(state)); | ||
} catch (exception const& ex) { | ||
string const msg = | ||
"failed to parse query parameter '" + taskStatesParam + "', ex: " + string(ex.what()); | ||
error(func, msg); | ||
throw invalid_argument(msg); | ||
} | ||
} | ||
selector.maxTasks = query().optionalUInt("max_tasks", 0); | ||
debug(func, "include_tasks=" + string(selector.includeTasks ? "1" : "0")); | ||
debug(func, "queryIds.size()=" + to_string(selector.queryIds.size())); | ||
debug(func, "taskStates.size()=" + to_string(selector.taskStates.size())); | ||
debug(func, "max_tasks=" + to_string(selector.maxTasks)); | ||
return selector; | ||
} | ||
|
||
} // namespace lsst::qserv::xrdsvc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
/* | ||
* LSST Data Management System | ||
* | ||
* This product includes software developed by the | ||
* LSST Project (http://www.lsst.org/). | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the LSST License Statement and | ||
* the GNU General Public License along with this program. If not, | ||
* see <http://www.lsstcorp.org/LegalNotices/>. | ||
*/ | ||
#ifndef LSST_QSERV_XRDSVC_HTTPMONITORMODULE_H | ||
#define LSST_QSERV_XRDSVC_HTTPMONITORMODULE_H | ||
|
||
// System headers | ||
#include <memory> | ||
#include <string> | ||
|
||
// Third party headers | ||
#include "nlohmann/json.hpp" | ||
|
||
// Qserv headers | ||
#include "http/ModuleBase.h" | ||
|
||
namespace lsst::qserv::qhttp { | ||
class Request; | ||
class Response; | ||
} // namespace lsst::qserv::qhttp | ||
|
||
// Forward declarations | ||
namespace lsst::qserv::wbase { | ||
struct TaskSelector; | ||
} // namespace lsst::qserv::wbase | ||
|
||
namespace lsst::qserv::wcontrol { | ||
class Foreman; | ||
} // namespace lsst::qserv::wcontrol | ||
|
||
// This header declarations | ||
namespace lsst::qserv::xrdsvc { | ||
|
||
/** | ||
* Class HttpMonitorModule implements a handler for reporting various run-time monitoring | ||
* metrics and statistics collected from an instance of a Qserv worker. | ||
*/ | ||
class HttpMonitorModule : public http::ModuleBase { | ||
public: | ||
/** | ||
* @note supported values for parameter 'subModuleName' are: | ||
* 'CONFIG' - get configuration parameters | ||
* 'MYSQL' - get the status (running queries) of the worker's MySQL service | ||
* 'STATUS' - get the status info (tasks, schedulers, etc.) | ||
* | ||
* @throws std::invalid_argument for unknown values of parameter 'subModuleName' | ||
*/ | ||
static void process(std::string const& context, std::shared_ptr<wcontrol::Foreman> const& foreman, | ||
std::shared_ptr<qhttp::Request> const& req, | ||
std::shared_ptr<qhttp::Response> const& resp, std::string const& subModuleName, | ||
http::AuthType const authType = http::AuthType::NONE); | ||
|
||
HttpMonitorModule() = delete; | ||
HttpMonitorModule(HttpMonitorModule const&) = delete; | ||
HttpMonitorModule& operator=(HttpMonitorModule const&) = delete; | ||
|
||
~HttpMonitorModule() final = default; | ||
|
||
protected: | ||
virtual nlohmann::json executeImpl(std::string const& subModuleName) final; | ||
virtual std::string context() const final; | ||
|
||
private: | ||
HttpMonitorModule(std::string const& context, std::shared_ptr<wcontrol::Foreman> const& foreman, | ||
std::shared_ptr<qhttp::Request> const& req, | ||
std::shared_ptr<qhttp::Response> const& resp); | ||
|
||
/// @return Configuration parameters. | ||
nlohmann::json _config(); | ||
|
||
/// @return The status (running queries) of the worker's MySQL service. | ||
nlohmann::json _mysql(); | ||
|
||
/// @return The worker status info (tasks, schedulers, etc.). | ||
nlohmann::json _status(); | ||
|
||
/** | ||
* Extract and parse values of the worker task selector from the request's query. | ||
* @param func The calling context (for error reporting). | ||
* @return wbase::TaskSelector The translated selector. | ||
* @throws std::invalid_argument For not well formed request query or unsupported values in it. | ||
*/ | ||
wbase::TaskSelector _translateTaskSelector(std::string const& func) const; | ||
|
||
std::string const _context; | ||
std::shared_ptr<wcontrol::Foreman> const _foreman; | ||
}; | ||
|
||
} // namespace lsst::qserv::xrdsvc | ||
|
||
#endif // LSST_QSERV_XRDSVC_HTTPMONITORMODULE_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters