Skip to content

Fix query counter gauge #21785

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

Open
wants to merge 8 commits into
base: devel
Choose a base branch
from
Open
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: 2 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
devel
-----

* Fix an integer underflow with gauge arangodb_aql_current_query.

* Upgraded bundled version of RocksDB to 9.5.

The default values for the following RocksDB-related startup options were
Expand Down
35 changes: 27 additions & 8 deletions arangod/Aql/Query.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
#include "Aql/QueryRegistry.h"
#include "Aql/SharedQueryState.h"
#include "Aql/Timing.h"
#include "Assertions/Assert.h"
#include "Auth/Common.h"
#include "Basics/Exceptions.h"
#include "Basics/ResourceUsage.h"
Expand Down Expand Up @@ -204,6 +205,7 @@ Query::Query(std::shared_ptr<transaction::Context> ctx, QueryString queryString,
scheduler)) {}

Query::~Query() {
TRI_ASSERT(!_isExecuting);
if (!_planSliceCopy.isNone()) {
_resourceMonitor->decreaseMemoryUsage(_planSliceCopy.byteSize());
}
Expand Down Expand Up @@ -747,6 +749,8 @@ ExecutionState Query::execute(QueryResult& queryResult) {
TRI_ASSERT(queryResult.data != nullptr);
TRI_ASSERT(queryResult.data->isOpenArray());
TRI_ASSERT(_trx != nullptr);
// We should do this only once
trackExecutionStart();

if (useQueryCache && (isModificationQuery() || !_warnings.empty() ||
!_ast->root()->isCacheable())) {
Expand Down Expand Up @@ -919,6 +923,8 @@ QueryResultV8 Query::executeV8(v8::Isolate* isolate) {
LOG_TOPIC("6cac7", DEBUG, Logger::QUERIES)
<< elapsedSince(_startTime) << " Query::executeV8"
<< " this: " << (uintptr_t)this;
trackExecutionStart();
ScopeGuard guard([this]() noexcept { trackExecutionEnd(); });

QueryResultV8 queryResult;

Expand Down Expand Up @@ -1712,6 +1718,25 @@ void Query::logAtEnd() const {
}
}

void Query::trackExecutionStart() noexcept {
// We should do this only once
if (!_isExecuting) {
auto& queryRegistryFeature =
vocbase().server().getFeature<QueryRegistryFeature>();
queryRegistryFeature.trackQueryStart();
_isExecuting = true;
}
}

void Query::trackExecutionEnd() noexcept {
if (_isExecuting) {
auto& queryRegistryFeature =
vocbase().server().getFeature<QueryRegistryFeature>();
queryRegistryFeature.trackQueryEnd(executionTime());
_isExecuting = false;
}
}

std::string Query::extractQueryString(size_t maxLength, bool show) const {
if (!show) {
return "<hidden>";
Expand Down Expand Up @@ -1853,6 +1878,8 @@ void Query::enterState(QueryExecutionState::ValueType state) {
ExecutionState Query::cleanupPlanAndEngine(bool sync) {
ensureExecutionTime();

trackExecutionEnd();

{
std::unique_lock<std::mutex> guard{_resultMutex};
if (!_postProcessingDone) {
Expand Down Expand Up @@ -1952,10 +1979,6 @@ void Query::handlePostProcessing(QueryList& querylist) {

void Query::handlePostProcessing() {
// elapsed time since query start
auto& queryRegistryFeature =
vocbase().server().getFeature<QueryRegistryFeature>();
queryRegistryFeature.trackQueryEnd(executionTime());

if (!queryOptions().skipAudit &&
ServerState::instance()->isSingleServerOrCoordinator()) {
try {
Expand Down Expand Up @@ -2521,10 +2544,6 @@ void Query::instantiatePlan(velocypack::Slice snippets) {
}

_queryProfile->registerInQueryList();

auto& feature = vocbase().server().getFeature<QueryRegistryFeature>();
feature.trackQueryStart();

enterState(QueryExecutionState::ValueType::EXECUTION);
}

Expand Down
8 changes: 8 additions & 0 deletions arangod/Aql/Query.h
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,12 @@ class Query : public QueryContext, public std::enable_shared_from_this<Query> {
// log the end of a query (warnings only)
void logAtEnd() const;

// set the isExecuting flag to true and change execution queries gauge
void trackExecutionStart() noexcept;

// set the isExecuting flag to false and change execution queries gauge
void trackExecutionEnd() noexcept;

struct CollectionSerializationFlags {
bool includeNumericIds = true;
bool includeViews = true;
Expand Down Expand Up @@ -506,6 +512,8 @@ class Query : public QueryContext, public std::enable_shared_from_this<Query> {
// If the query object was constructed from cache
// the consequence is that _ast is nullptr
bool _isCached{false};

bool _isExecuting{false};
};

} // namespace aql
Expand Down
87 changes: 87 additions & 0 deletions tests/js/client/aql/aql-query-metrics.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*jshint globalstrict:false, strict:false, maxlen: 500 */
/*global assertEqual, assertTrue, assertFalse, assertNotEqual, assertUndefined, assertNotUndefined, assertMatch, assertNotMatch, fail */

// //////////////////////////////////////////////////////////////////////////////
// / DISCLAIMER
// /
// / Copyright 2014-2025 ArangoDB GmbH, Cologne, Germany
// / Copyright 2004-2014 triAGENS GmbH, Cologne, Germany
// /
// / Licensed under the Business Source License 1.1 (the "License");
// / you may not use this file except in compliance with the License.
// / You may obtain a copy of the License at
// /
// / https://github.com/arangodb/arangodb/blob/devel/LICENSE
// /
// / Unless required by applicable law or agreed to in writing, software
// / distributed under the License is distributed on an "AS IS" BASIS,
// / WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// / See the License for the specific language governing permissions and
// / limitations under the License.
// /
// / Copyright holder is ArangoDB GmbH, Cologne, Germany
// /
/// @author Jure Bajic
// //////////////////////////////////////////////////////////////////////////////

const jsunity = require("jsunity");
const db = require("@arangodb").db;
const internal = require("internal");
const getMetricSingle = require("@arangodb/test-helper").getMetricSingle;
const arangodb = require("@arangodb");
const aql = arangodb.aql;

function QueryMetricsTestSuite() {
const dbName = "metricsTestDB";
const collName = "col";
let collection;

return {
setUpAll: function () {
db._createDatabase(dbName);
db._useDatabase(dbName);

collection = db._create(collName, {
numberOfShards: 3,
});

let docs = [];
for (let i = 0; i < 1000; ++i) {
docs.push({ value: i });
}
collection.save(docs);
},

tearDownAll: function () {
db._useDatabase("_system");
db._dropDatabase(dbName);
},

testMetricAqlCurrentQueryNoRunningQueries: function () {
const aqlCurrentQueryMetric = getMetricSingle(
"arangodb_aql_current_query",
);
assertEqual(aqlCurrentQueryMetric, 0);
},

testMetricAqlCurrentQueryWithRunningQueries: function () {
let aqlCurrentQueryMetric = getMetricSingle("arangodb_aql_current_query");
assertEqual(aqlCurrentQueryMetric, 0);

for (let i = 0; i < 1000; ++i) {
const query = aql`
FOR d IN ${collection}
FILTER d.value > ${i}
LIMIT 3
RETURN d`;
db._query(query);
}

aqlCurrentQueryMetric = getMetricSingle("arangodb_aql_current_query");
assertEqual(aqlCurrentQueryMetric, 0);
},
};
}

jsunity.run(QueryMetricsTestSuite);
return jsunity.done();