Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DM-39516: Extend Qserv Web Dashboard to allow monitoring active queries at Czar and worker database servers #803

Merged
merged 12 commits into from
Aug 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/admin/python/lsst/qserv/admin/replicationInterface.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ def __init__(
self.repl_ctrl = urlparse(repl_ctrl_uri)
self.auth_key = auth_key
self.admin_auth_key = admin_auth_key
self.repl_api_version = 23
self.repl_api_version = 24
_log.debug(f"ReplicationInterface %s", self.repl_ctrl)

def version(self) -> str:
Expand Down
1 change: 1 addition & 0 deletions src/mysql/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ target_sources(mysql PRIVATE
LocalInfile.cc
MySqlConfig.cc
MySqlConnection.cc
MySqlUtils.cc
RowBuffer.cc
SchemaFactory.cc
)
Expand Down
14 changes: 14 additions & 0 deletions src/mysql/MySqlConnection.cc
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,19 @@ bool MySqlConnection::selectDb(std::string const& dbName) {
return true;
}

std::vector<std::string> MySqlConnection::getColumnNames() const {
assert(_mysql);
assert(_mysql_res);
std::vector<std::string> names;
if (0 != mysql_field_count(_mysql)) {
auto fields = mysql_fetch_fields(_mysql_res);
for (unsigned int i = 0; i < mysql_num_fields(_mysql_res); i++) {
names.push_back(std::string(fields[i].name));
}
}
return names;
}

////////////////////////////////////////////////////////////////////////
// MySqlConnection
// private:
Expand Down Expand Up @@ -223,6 +236,7 @@ MYSQL* MySqlConnection::_connectHelper() {
mysql_close(m);
return c;
}
_threadId = mysql_thread_id(m);
return m;
}

Expand Down
5 changes: 5 additions & 0 deletions src/mysql/MySqlConnection.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include <memory>
#include <mutex>
#include <string>
#include <vector>

// Third-party headers
#include "boost/utility.hpp"
Expand Down Expand Up @@ -64,6 +65,8 @@ class MySqlConnection : boost::noncopyable {
static bool checkConnection(mysql::MySqlConfig const& mysqlconfig);

bool connected() const { return _isConnected; }
unsigned long threadId() const { return _threadId; }

// instance destruction invalidates this return value
MYSQL* getMySql() { return _mysql; }
MySqlConfig const& getMySqlConfig() const { return *_sqlConfig; }
Expand All @@ -80,6 +83,7 @@ class MySqlConnection : boost::noncopyable {
assert(_mysql);
return mysql_field_count(_mysql);
}
std::vector<std::string> getColumnNames() const;
unsigned int getErrno() const {
assert(_mysql);
return mysql_errno(_mysql);
Expand All @@ -102,6 +106,7 @@ class MySqlConnection : boost::noncopyable {
MYSQL* _mysql;
MYSQL_RES* _mysql_res;
bool _isConnected;
unsigned long _threadId = 0; ///< 0 if not connected
std::shared_ptr<MySqlConfig> _sqlConfig;
bool _isExecuting; ///< true during mysql_real_query and mysql_use_result
bool _interrupted; ///< true if cancellation requested
Expand Down
95 changes: 95 additions & 0 deletions src/mysql/MySqlUtils.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
// -*- LSST-C++ -*-
/*
* 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 "mysql/MySqlUtils.h"

// System headers
#include <string>

// Third party headers
#include <mysql/mysql.h>
#include <mysql/mysqld_error.h>
#include <mysql/errmsg.h>

// Qserv headers
#include "mysql/MySqlConfig.h"
#include "mysql/MySqlConnection.h"

using namespace std;
using json = nlohmann::json;

namespace {

string errInfo(lsst::qserv::mysql::MySqlConnection const& conn) {
return "errno: " + to_string(conn.getErrno()) + ", error: " + conn.getError();
}

} // anonymous namespace

namespace lsst::qserv::mysql {

json MySqlUtils::processList(MySqlConfig const& config, bool full) {
string const context = "MySqlUtils::" + string(__func__);
string const query = "SHOW" + string(full ? " FULL" : "") + " PROCESSLIST";

MySqlConnection conn(config);
if (!conn.connect()) {
string const err = context + " failed to connect to the worker database, " + ::errInfo(conn);
throw MySqlQueryError(err);
}
if (!conn.queryUnbuffered(query)) {
string const err = "failed to execute the query: '" + query + "', " + ::errInfo(conn);
throw MySqlQueryError(err);
}
json result;
result["queries"] = json::object({{"columns", json::array()}, {"rows", json::array()}});
int const numFields = conn.getResultFieldCount();
if (numFields > 0) {
result["queries"]["columns"] = conn.getColumnNames();
auto& rows = result["queries"]["rows"];
MYSQL_RES* mysqlResult = conn.getResult();
while (true) {
MYSQL_ROW mysqlRow = mysql_fetch_row(mysqlResult);
if (!mysqlRow) {
if (0 == conn.getErrno()) {
// End of iteration if no specific error was reported.
break;
}
string const err =
context + " failed to fetch next row for query: '" + query + "', " + ::errInfo(conn);
throw MySqlQueryError(err);
}
size_t const* lengths = mysql_fetch_lengths(mysqlResult);
json row = json::array();
for (int i = 0; i < numFields; i++) {
// Report the empty string for SQL NULL.
auto const length = lengths[i];
row.push_back(length == 0 ? string() : string(mysqlRow[i], length));
}
rows.push_back(row);
}
}
return result;
}

} // namespace lsst::qserv::mysql
71 changes: 71 additions & 0 deletions src/mysql/MySqlUtils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// -*- LSST-C++ -*-
/*
* 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_MYSQL_MYSQLUTILS_H
#define LSST_QSERV_MYSQL_MYSQLUTILS_H

// System headers
#include <stdexcept>

// Third-party headers
#include "nlohmann/json.hpp"

/// Forward declarations.
namespace lsst::qserv::mysql {
class MySqlConfig;
} // namespace lsst::qserv::mysql

/// This header declarations.
namespace lsst::qserv::mysql {

/**
* Class MySqlQueryError represents exceptions to be throw on specific errors
* detected when attempting to execute the queries.
*/
class MySqlQueryError : public std::runtime_error {
using std::runtime_error::runtime_error;
};

/**
* Class MySqlUtils is the utility class providing a collection of useful queries reporting
* small result sets.
* @note Each tool of the collection does its own connection handling (opening/etc.).
*/
class MySqlUtils {
public:
/**
* Report info on the on-going queries using 'SHOW [FULL] PROCESSLIST'.
* @param A scope of the operaton depends on the user credentials privided
* in the configuration object. Normally, a subset of queries which belong
* to the specified user will be reported.
* @param config Configuration parameters of the MySQL connector.
* @param full The optional modifier which (if set) allows seeing the full text
* of the queries.
* @return A collection of queries encoded as the JSON object. Please, see the code
* for further details on the schema of the object.
* @throws MySqlQueryError on errors detected during query execution/processing.
*/
static nlohmann::json processList(MySqlConfig const& config, bool full = false);
};

} // namespace lsst::qserv::mysql

#endif // LSST_QSERV_MYSQL_MYSQLUTILS_H
Loading
Loading