Skip to content

Commit ba18bb4

Browse files
daverigbytrondn
authored andcommitted
MB-43680: Record JSON validation runtimes
Time the duration of JSON validation done as part of executing client requests, recoring the timings to: * A new trace span. * A new per-bucket histogram. The trace span will be reported as part of Slow operation logging - for example: WARNING 57: Slow operation. {...,"trace":"... json_validate=6446937532739980:18555 ... The histograms will be reported via mctimings using the stat key 'json_validate', this will be added to cbcollect_info via a future ns_server patch. Change-Id: I1c90ee8c815b7026dd42a9ec5b567f0519c3cec0 Reviewed-on: http://review.couchbase.org/c/kv_engine/+/143528 Tested-by: Build Bot <[email protected]> Reviewed-by: Trond Norbye <[email protected]>
1 parent 71804e2 commit ba18bb4

File tree

5 files changed

+33
-0
lines changed

5 files changed

+33
-0
lines changed

daemon/buckets.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,9 @@ class Bucket {
134134
*/
135135
Hdr1sfMicroSecHistogram subjson_operation_times;
136136

137+
/// JSON_checker validate() execution time histogram.
138+
Hdr1sfMicroSecHistogram jsonValidateTimes;
139+
137140
/// Snappy decompression time histogram.
138141
Hdr1sfMicroSecHistogram snappyDecompressionTimes;
139142

daemon/protocol/mcbp/stats_context.cc

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,18 @@ static ENGINE_ERROR_CODE stat_histogram_executor(
333333
}
334334
}
335335

336+
/**
337+
* Handler for the <code>stats json_validate</code> command used to retrieve
338+
* histograms of how long it took to validate JSON.
339+
*
340+
* @param arg - should be empty
341+
* @param cookie the command context
342+
*/
343+
static ENGINE_ERROR_CODE stat_json_validate_executor(const std::string& arg,
344+
Cookie& cookie) {
345+
return stat_histogram_executor(arg, cookie, &Bucket::jsonValidateTimes);
346+
}
347+
336348
/**
337349
* Handler for the <code>stats topkeys</code> command used to retrieve
338350
* the most popular keys in the attached bucket.
@@ -583,6 +595,8 @@ static std::unordered_map<std::string, struct command_stat_handler>
583595
{"aggregate", {false, true, true, stat_aggregate_executor}},
584596
{"connections",
585597
{false, false, true, stat_connections_executor}},
598+
{"json_validate",
599+
{false, true, true, stat_json_validate_executor}},
586600
{"topkeys", {false, true, true, stat_topkeys_executor}},
587601
{"topkeys_json",
588602
{false, true, true, stat_topkeys_json_executor}},

daemon/protocol/mcbp/steppable_command_context.cc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,16 @@
1717
#include "steppable_command_context.h"
1818
#include "executors.h"
1919

20+
#include <daemon/buckets.h>
2021
#include <daemon/connection.h>
2122
#include <daemon/cookie.h>
2223
#include <daemon/front_end_thread.h>
2324
#include <daemon/memcached.h>
2425
#include <daemon/stats.h>
2526
#include <logger/logger.h>
27+
#include <platform/histogram.h>
28+
#include <platform/scope_timer.h>
29+
#include <utilities/hdrhistogram.h>
2630

2731
SteppableCommandContext::SteppableCommandContext(Cookie& cookie_)
2832
: cookie(cookie_), connection(cookie.getConnection()) {
@@ -60,6 +64,14 @@ void SteppableCommandContext::drive() {
6064
void SteppableCommandContext::setDatatypeJSONFromValue(
6165
const cb::const_byte_buffer& value,
6266
protocol_binary_datatype_t& datatype) {
67+
// Record how long JSON checking takes to both Tracer and bucket-level
68+
// histogram.
69+
using namespace cb::tracing;
70+
ScopeTimer2<HdrMicroSecStopwatch, SpanStopwatch> timer(
71+
std::forward_as_tuple(
72+
cookie.getConnection().getBucket().jsonValidateTimes),
73+
std::forward_as_tuple(cookie, Code::JsonValidate));
74+
6375
// Determine if document is JSON or not. We do not trust what the client
6476
// sent - instead we check for ourselves.
6577
if (connection.getThread().validator.validate(value.data(), value.size())) {

include/memcached/tracer.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ enum class Code : uint8_t {
2929
Request,
3030
/// Time spent decompressing Snappy data.
3131
SnappyDecompress,
32+
/// Time spent validating if incoming value is JSON.
33+
JsonValidate,
3234
/// Time spent waiting for a background fetch operation to be scheduled.
3335
BackgroundWait,
3436
/// Time spent performing the actual background load from disk.

tracing/tracer.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,8 @@ MEMCACHED_PUBLIC_API std::string to_string(const cb::tracing::Code tracecode) {
132132
return "request";
133133
case Code::SnappyDecompress:
134134
return "snappy.decompress";
135+
case Code::JsonValidate:
136+
return "json_validate";
135137
case Code::BackgroundWait:
136138
return "bg.wait";
137139
case Code::BackgroundLoad:

0 commit comments

Comments
 (0)