Redshift: Support caching async aws queries (#71682)
Co-authored-by: Sarah Zinger <sarah.zinger@grafana.com>
This commit is contained in:
co-authored by
Sarah Zinger
parent
3457ccbf12
commit
56913fbd95
@@ -19,6 +19,14 @@ var QueryCachingRequestHistogram = prometheus.NewHistogramVec(prometheus.Histogr
|
||||
Buckets: []float64{.005, .01, .025, .05, .1, .25, .5, 1, 2.5, 5, 10, 25, 50, 100},
|
||||
}, []string{"datasource_type", "cache", "query_type"})
|
||||
|
||||
var ShouldCacheQueryHistogram = prometheus.NewHistogramVec(prometheus.HistogramOpts{
|
||||
Namespace: metrics.ExporterName,
|
||||
Subsystem: "caching",
|
||||
Name: "should_cache_query_request_duration_seconds",
|
||||
Help: "histogram of grafana query endpoint requests in seconds",
|
||||
Buckets: []float64{.005, .01, .025, .05, .1, .25, .5, 1, 2.5, 5, 10, 25, 50, 100},
|
||||
}, []string{"datasource_type", "cache", "shouldCache", "query_type"})
|
||||
|
||||
var ResourceCachingRequestHistogram = prometheus.NewHistogramVec(prometheus.HistogramOpts{
|
||||
Namespace: metrics.ExporterName,
|
||||
Subsystem: "caching",
|
||||
|
||||
@@ -2,19 +2,31 @@ package clientmiddleware
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/grafana/grafana-aws-sdk/pkg/awsds"
|
||||
"github.com/grafana/grafana-plugin-sdk-go/backend"
|
||||
"github.com/grafana/grafana/pkg/infra/log"
|
||||
"github.com/grafana/grafana/pkg/plugins"
|
||||
"github.com/grafana/grafana/pkg/services/caching"
|
||||
"github.com/grafana/grafana/pkg/services/contexthandler"
|
||||
"github.com/grafana/grafana/pkg/services/featuremgmt"
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
)
|
||||
|
||||
// needed to mock the function for testing
|
||||
var shouldCacheQuery = awsds.ShouldCacheQuery
|
||||
|
||||
// NewCachingMiddleware creates a new plugins.ClientMiddleware that will
|
||||
// attempt to read and write query results to the cache
|
||||
func NewCachingMiddleware(cachingService caching.CachingService) plugins.ClientMiddleware {
|
||||
return NewCachingMiddlewareWithFeatureManager(cachingService, nil)
|
||||
}
|
||||
|
||||
// NewCachingMiddlewareWithFeatureManager creates a new plugins.ClientMiddleware that will
|
||||
// attempt to read and write query results to the cache with a feature manager
|
||||
func NewCachingMiddlewareWithFeatureManager(cachingService caching.CachingService, features *featuremgmt.FeatureManager) plugins.ClientMiddleware {
|
||||
log := log.New("caching_middleware")
|
||||
if err := prometheus.Register(QueryCachingRequestHistogram); err != nil {
|
||||
log.Error("error registering prometheus collector 'QueryRequestHistogram'", "error", err)
|
||||
@@ -24,17 +36,19 @@ func NewCachingMiddleware(cachingService caching.CachingService) plugins.ClientM
|
||||
}
|
||||
return plugins.ClientMiddlewareFunc(func(next plugins.Client) plugins.Client {
|
||||
return &CachingMiddleware{
|
||||
next: next,
|
||||
caching: cachingService,
|
||||
log: log,
|
||||
next: next,
|
||||
caching: cachingService,
|
||||
log: log,
|
||||
features: features,
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
type CachingMiddleware struct {
|
||||
next plugins.Client
|
||||
caching caching.CachingService
|
||||
log log.Logger
|
||||
next plugins.Client
|
||||
caching caching.CachingService
|
||||
log log.Logger
|
||||
features *featuremgmt.FeatureManager
|
||||
}
|
||||
|
||||
// QueryData receives a data request and attempts to access results already stored in the cache for that request.
|
||||
@@ -57,7 +71,8 @@ func (m *CachingMiddleware) QueryData(ctx context.Context, req *backend.QueryDat
|
||||
hit, cr := m.caching.HandleQueryRequest(ctx, req)
|
||||
|
||||
// record request duration if caching was used
|
||||
if ch := reqCtx.Resp.Header().Get(caching.XCacheHeader); ch != "" {
|
||||
ch := reqCtx.Resp.Header().Get(caching.XCacheHeader)
|
||||
if ch != "" {
|
||||
defer func() {
|
||||
QueryCachingRequestHistogram.With(prometheus.Labels{
|
||||
"datasource_type": req.PluginContext.DataSourceInstanceSettings.Type,
|
||||
@@ -77,7 +92,25 @@ func (m *CachingMiddleware) QueryData(ctx context.Context, req *backend.QueryDat
|
||||
|
||||
// Update the query cache with the result for this metrics request
|
||||
if err == nil && cr.UpdateCacheFn != nil {
|
||||
cr.UpdateCacheFn(ctx, resp)
|
||||
// If AWS async caching is not enabled, use the old code path
|
||||
if m.features == nil || !m.features.IsEnabled(featuremgmt.FlagAwsAsyncQueryCaching) {
|
||||
cr.UpdateCacheFn(ctx, resp)
|
||||
} else {
|
||||
// time how long shouldCacheQuery takes
|
||||
startShouldCacheQuery := time.Now()
|
||||
shouldCache := shouldCacheQuery(resp)
|
||||
ShouldCacheQueryHistogram.With(prometheus.Labels{
|
||||
"datasource_type": req.PluginContext.DataSourceInstanceSettings.Type,
|
||||
"cache": ch,
|
||||
"shouldCache": strconv.FormatBool(shouldCache),
|
||||
"query_type": getQueryType(reqCtx),
|
||||
}).Observe(time.Since(startShouldCacheQuery).Seconds())
|
||||
|
||||
// If AWS async caching is enabled and resp is for a running async query, don't cache it
|
||||
if shouldCache {
|
||||
cr.UpdateCacheFn(ctx, resp)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return resp, err
|
||||
|
||||
@@ -10,6 +10,7 @@ import (
|
||||
"github.com/grafana/grafana/pkg/plugins/manager/client/clienttest"
|
||||
"github.com/grafana/grafana/pkg/services/caching"
|
||||
"github.com/grafana/grafana/pkg/services/contexthandler"
|
||||
"github.com/grafana/grafana/pkg/services/featuremgmt"
|
||||
"github.com/grafana/grafana/pkg/services/user"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
@@ -74,8 +75,17 @@ func TestCachingMiddleware(t *testing.T) {
|
||||
})
|
||||
|
||||
t.Run("If cache returns a miss, queries are issued and the update cache function is called", func(t *testing.T) {
|
||||
origShouldCacheQuery := shouldCacheQuery
|
||||
var shouldCacheQueryCalled bool
|
||||
shouldCacheQuery = func(resp *backend.QueryDataResponse) bool {
|
||||
shouldCacheQueryCalled = true
|
||||
return true
|
||||
}
|
||||
|
||||
t.Cleanup(func() {
|
||||
updateCacheCalled = false
|
||||
shouldCacheQueryCalled = false
|
||||
shouldCacheQuery = origShouldCacheQuery
|
||||
cs.Reset()
|
||||
})
|
||||
|
||||
@@ -90,6 +100,75 @@ func TestCachingMiddleware(t *testing.T) {
|
||||
assert.Nil(t, resp)
|
||||
// Since it was a miss, the middleware called the update func
|
||||
assert.True(t, updateCacheCalled)
|
||||
// Since the feature flag was not set, the middleware did not call shouldCacheQuery
|
||||
assert.False(t, shouldCacheQueryCalled)
|
||||
})
|
||||
|
||||
t.Run("with async queries", func(t *testing.T) {
|
||||
asyncCdt := clienttest.NewClientDecoratorTest(t,
|
||||
clienttest.WithReqContext(req, &user.SignedInUser{}),
|
||||
clienttest.WithMiddlewares(
|
||||
NewCachingMiddlewareWithFeatureManager(cs, featuremgmt.WithFeatures(featuremgmt.FlagAwsAsyncQueryCaching))),
|
||||
)
|
||||
t.Run("If shoudCacheQuery returns true update cache function is called", func(t *testing.T) {
|
||||
origShouldCacheQuery := shouldCacheQuery
|
||||
var shouldCacheQueryCalled bool
|
||||
shouldCacheQuery = func(resp *backend.QueryDataResponse) bool {
|
||||
shouldCacheQueryCalled = true
|
||||
return true
|
||||
}
|
||||
|
||||
t.Cleanup(func() {
|
||||
updateCacheCalled = false
|
||||
shouldCacheQueryCalled = false
|
||||
shouldCacheQuery = origShouldCacheQuery
|
||||
cs.Reset()
|
||||
})
|
||||
|
||||
cs.ReturnHit = false
|
||||
cs.ReturnQueryResponse = dataResponse
|
||||
|
||||
resp, err := asyncCdt.Decorator.QueryData(req.Context(), qdr)
|
||||
assert.NoError(t, err)
|
||||
// Cache service is called once
|
||||
cs.AssertCalls(t, "HandleQueryRequest", 1)
|
||||
// Equals nil (returned by the decorator test)
|
||||
assert.Nil(t, resp)
|
||||
// Since it was a miss, the middleware called the update func
|
||||
assert.True(t, updateCacheCalled)
|
||||
// Since the feature flag set, the middleware called shouldCacheQuery
|
||||
assert.True(t, shouldCacheQueryCalled)
|
||||
})
|
||||
|
||||
t.Run("If shoudCacheQuery returns false update cache function is not called", func(t *testing.T) {
|
||||
origShouldCacheQuery := shouldCacheQuery
|
||||
var shouldCacheQueryCalled bool
|
||||
shouldCacheQuery = func(resp *backend.QueryDataResponse) bool {
|
||||
shouldCacheQueryCalled = true
|
||||
return false
|
||||
}
|
||||
|
||||
t.Cleanup(func() {
|
||||
updateCacheCalled = false
|
||||
shouldCacheQueryCalled = false
|
||||
shouldCacheQuery = origShouldCacheQuery
|
||||
cs.Reset()
|
||||
})
|
||||
|
||||
cs.ReturnHit = false
|
||||
cs.ReturnQueryResponse = dataResponse
|
||||
|
||||
resp, err := asyncCdt.Decorator.QueryData(req.Context(), qdr)
|
||||
assert.NoError(t, err)
|
||||
// Cache service is called once
|
||||
cs.AssertCalls(t, "HandleQueryRequest", 1)
|
||||
// Equals nil (returned by the decorator test)
|
||||
assert.Nil(t, resp)
|
||||
// Since it was a miss, the middleware called the update func
|
||||
assert.False(t, updateCacheCalled)
|
||||
// Since the feature flag set, the middleware called shouldCacheQuery
|
||||
assert.True(t, shouldCacheQueryCalled)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
|
||||
Reference in New Issue
Block a user