Skip to content

Commit

Permalink
Capture MySQL thread identifiers for worker queries
Browse files Browse the repository at this point in the history
The identifiers are captured for the worker task monitoring
purposes. They are used by the Web Dashboard for associating task
activities with the corresponding MySQL queries issued by the tasks.
The task monitoring page of the Dashboard has been extended to display
a value of the identifier (if it was set for the task).
  • Loading branch information
iagaponenko committed Aug 17, 2023
1 parent 3f550dc commit a502f15
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/mysql/MySqlConnection.cc
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ MYSQL* MySqlConnection::_connectHelper() {
mysql_close(m);
return c;
}
_threadId = mysql_thread_id(m);
return m;
}

Expand Down
3 changes: 3 additions & 0 deletions src/mysql/MySqlConnection.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,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 Down Expand Up @@ -104,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
1 change: 1 addition & 0 deletions src/wbase/Task.cc
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,7 @@ nlohmann::json Task::getJson() const {
js["queryTime_msec"] = util::TimeUtils::tp2ms(_queryTime);
js["finishTime_msec"] = util::TimeUtils::tp2ms(_finishTime);
js["sizeSoFar"] = _totalSize;
js["mysqlThreadId"] = _mysqlThreadId.load();
return js;
}

Expand Down
7 changes: 7 additions & 0 deletions src/wbase/Task.h
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,11 @@ class Task : public util::CommandForThreadPool {
/// Return a reference to the list of subchunk ids.
const IntVector& getSubchunksVect() const { return _dbTblsAndSubchunks->subchunksVect; }

/// Set MySQL thread associated with a MySQL connection open before executing
/// task's queries. The identifier is sampled by the worker tasks monitoring
/// system in order to see what MySQL queries are being executed by tasks.
void setMySqlThreadId(unsigned long id) { _mysqlThreadId.store(id); }

private:
std::shared_ptr<UserQueryInfo> _userQueryInfo; ///< Details common to Tasks in this UserQuery.
std::shared_ptr<ChannelShared> _sendChannel; ///< Send channel.
Expand Down Expand Up @@ -339,6 +344,8 @@ class Task : public util::CommandForThreadPool {

/// Stores information on the query's resource usage.
std::weak_ptr<wpublish::QueryStatistics> _queryStats;

std::atomic<unsigned long> _mysqlThreadId{0}; ///< 0 if not connected to MySQL
};

} // namespace lsst::qserv::wbase
Expand Down
1 change: 1 addition & 0 deletions src/wdb/QueryRunner.cc
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ bool QueryRunner::_initConnection() {
_multiError.push_back(error);
return false;
}
_task->setMySqlThreadId(_mysqlConn->threadId());
return true;
}

Expand Down
2 changes: 2 additions & 0 deletions src/www/qserv/js/QservWorkerTasks.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ function(CSSLoader,
<th class="sticky" style="text-align:right;">queried</th>
<th class="sticky right-aligned"><elem style="color:red;">&rarr;</elem></th>
<th class="sticky" style="text-align:right;">finished</th>
<th class="sticky" style="text-align:right;">MySQL thrd</th>
</tr>
</thead>
<caption class="updating">Loading...</caption>
Expand Down Expand Up @@ -331,6 +332,7 @@ function(CSSLoader,
<td style="text-align:right;"><pre>${task.queryTime_msec ? QservWorkerTasks._timestamp2hhmmss(task.queryTime_msec) : ""}</pre></td>
<th style="text-align:right;"><pre>${QservWorkerTasks._timestamps_diff2str(task.queryTime_msec, task.finishTime_msec, snapshotTime_msec)}</pre></th>
<td style="text-align:right;"><pre>${task.finishTime_msec ? QservWorkerTasks._timestamp2hhmmss(task.finishTime_msec) : ""}</pre></td>
<td style="text-align:right;"><pre>${task.mysqlThreadId || "&nbsp;"}</pre></td>
</tr>`;
rowspan++;
numTasksDisplayed++;
Expand Down

0 comments on commit a502f15

Please sign in to comment.