Skip to content

Commit

Permalink
queryserving, observability: instrument vttablet query cache plan hit…
Browse files Browse the repository at this point in the history
…s/misses (#14947)

Signed-off-by: Max Englander <max@planetscale.com>
Signed-off-by: Florent Poinsard <florent.poinsard@outlook.fr>
Co-authored-by: Florent Poinsard <florent.poinsard@outlook.fr>
  • Loading branch information
maxenglander and frouioui authored Feb 16, 2024
1 parent 2929dee commit 54c6dfc
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
12 changes: 10 additions & 2 deletions changelog/20.0/20.0.0/summary.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
- **[Flag changes](#flag-changes)**
- [`pprof-http` default change](#pprof-http-default)
- **[Minor Changes](#minor-changes)**

- **[New Stats](#new-stats)**
- [VTTablet Query Cache Hits and Misses](#vttablet-query-cache-hits-and-misses)

## <a id="major-changes"/>Major Changes


### <a id="query-compatibility"/>Query Compatibility

#### <a id="vindex-hints"/> Vindex Hints
Expand Down Expand Up @@ -64,3 +64,11 @@ To continue enabling these endpoints, explicitly set `--pprof-http` when startin

## <a id="minor-changes"/>Minor Changes

### <a id="new-stats"/>New Stats

#### <a id="vttablet-query-cache-hits-and-misses"/>VTTablet Query Cache Hits and Misses

VTTablet exposes two new counter stats:

* `QueryCacheHits`: Query engine query cache hits
* `QueryCacheMisses`: Query engine query cache misses
7 changes: 7 additions & 0 deletions go/vt/vttablet/tabletserver/query_engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ type QueryEngine struct {
// stats
// Note: queryErrorCountsWithCode is similar to queryErrorCounts except it contains error code as an additional dimension
queryCounts, queryCountsWithTabletType, queryTimes, queryErrorCounts, queryErrorCountsWithCode, queryRowsAffected, queryRowsReturned *stats.CountersWithMultiLabels
queryCacheHits, queryCacheMisses *stats.CounterFunc

// stats flags
enablePerWorkloadTableMetrics bool
Expand Down Expand Up @@ -280,6 +281,12 @@ func NewQueryEngine(env tabletenv.Env, se *schema.Engine) *QueryEngine {
env.Exporter().NewCounterFunc("QueryCacheEvictions", "Query engine query cache evictions", func() int64 {
return qe.plans.Metrics.Evicted()
})
qe.queryCacheHits = env.Exporter().NewCounterFunc("QueryCacheHits", "Query engine query cache hits", func() int64 {
return qe.plans.Metrics.Hits()
})
qe.queryCacheMisses = env.Exporter().NewCounterFunc("QueryCacheMisses", "Query engine query cache misses", func() int64 {
return qe.plans.Metrics.Misses()
})

labels := []string{"Table", "Plan"}
if config.EnablePerWorkloadTableMetrics {
Expand Down
16 changes: 16 additions & 0 deletions go/vt/vttablet/tabletserver/query_engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,11 +186,27 @@ func TestQueryPlanCache(t *testing.T) {
ctx := context.Background()
logStats := tabletenv.NewLogStats(ctx, "GetPlanStats")

initialHits := qe.queryCacheHits.Get()
initialMisses := qe.queryCacheMisses.Get()

firstPlan, err := qe.GetPlan(ctx, logStats, firstQuery, false)
require.NoError(t, err)
require.NotNil(t, firstPlan, "plan should not be nil")

assertPlanCacheSize(t, qe, 1)

require.Equal(t, int64(0), qe.queryCacheHits.Get()-initialHits)
require.Equal(t, int64(1), qe.queryCacheMisses.Get()-initialMisses)

secondPlan, err := qe.GetPlan(ctx, logStats, firstQuery, false)
require.NoError(t, err)
require.NotNil(t, secondPlan, "plan should not be nil")

assertPlanCacheSize(t, qe, 1)

require.Equal(t, int64(1), qe.queryCacheHits.Get()-initialHits)
require.Equal(t, int64(1), qe.queryCacheMisses.Get()-initialMisses)

qe.ClearQueryPlanCache()
}

Expand Down

0 comments on commit 54c6dfc

Please sign in to comment.