From 5c0f4c4b8b4b6f215db450507f3f9997bea8074b Mon Sep 17 00:00:00 2001 From: Michael Mandrus Date: Tue, 24 Jan 2023 01:45:48 -0500 Subject: [PATCH] *Create Caching Config interface and OSS impl *Create front-end facing DS Cache config *Populate Caching Config on Datasource DTO *Update OSS wire deps --- pkg/api/frontendsettings.go | 17 +++++++++++++++++ pkg/api/http_server.go | 5 ++++- pkg/plugins/models.go | 3 +++ pkg/server/wireexts_oss.go | 3 +++ pkg/services/datasources/querycaching/models.go | 16 ++++++++++++++++ .../datasources/querycaching/service.go | 14 ++++++++++++++ 6 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 pkg/services/datasources/querycaching/models.go create mode 100644 pkg/services/datasources/querycaching/service.go diff --git a/pkg/api/frontendsettings.go b/pkg/api/frontendsettings.go index 725b488acc9..a67ab5b46ef 100644 --- a/pkg/api/frontendsettings.go +++ b/pkg/api/frontendsettings.go @@ -9,6 +9,7 @@ import ( "github.com/grafana/grafana/pkg/plugins" "github.com/grafana/grafana/pkg/services/accesscontrol" "github.com/grafana/grafana/pkg/services/datasources" + "github.com/grafana/grafana/pkg/services/datasources/querycaching" "github.com/grafana/grafana/pkg/services/featuremgmt" "github.com/grafana/grafana/pkg/services/licensing" "github.com/grafana/grafana/pkg/services/pluginsettings" @@ -249,6 +250,13 @@ func (hs *HTTPServer) getFSDataSources(c *models.ReqContext, enabledPlugins Enab } } + // load enterprise caching config for all datasources + cacheCfgs, err := hs.DataSourceCacheCfgService.GetAllDatasourceConfig(c.Req.Context()) + if err != nil { + // non-fatal - log and continue + c.Logger.Error("failed to get datasource caching config", "error", err.Error()) + cacheCfgs = querycaching.CacheConfigMap{} + } dataSources := make(map[string]plugins.DataSourceDTO) for _, ds := range orgDataSources { @@ -337,6 +345,10 @@ func (hs *HTTPServer) getFSDataSources(c *models.ReqContext, enabledPlugins Enab ds.JsonData.Set("directUrl", ds.Url) } + if cfg, ok := cacheCfgs[dsDTO.UID]; ok { + dsDTO.EnterpriseCachingConfig = cfg + } + dataSources[ds.Name] = dsDTO } @@ -359,6 +371,11 @@ func (hs *HTTPServer) getFSDataSources(c *models.ReqContext, enabledPlugins Enab dto.ID = grafanads.DatasourceID dto.UID = grafanads.DatasourceUID } + + if cfg, ok := cacheCfgs[dto.UID]; ok { + dto.EnterpriseCachingConfig = cfg + } + dataSources[ds.Name] = dto } } diff --git a/pkg/api/http_server.go b/pkg/api/http_server.go index 15a75846c23..18e78a88d74 100644 --- a/pkg/api/http_server.go +++ b/pkg/api/http_server.go @@ -59,6 +59,7 @@ import ( "github.com/grafana/grafana/pkg/services/datasourceproxy" "github.com/grafana/grafana/pkg/services/datasources" "github.com/grafana/grafana/pkg/services/datasources/permissions" + "github.com/grafana/grafana/pkg/services/datasources/querycaching" "github.com/grafana/grafana/pkg/services/encryption" "github.com/grafana/grafana/pkg/services/export" "github.com/grafana/grafana/pkg/services/featuremgmt" @@ -167,6 +168,7 @@ type HTTPServer struct { secretsMigrator secrets.Migrator secretsPluginMigrator spm.SecretMigrationProvider DataSourcesService datasources.DataSourceService + DataSourceCacheCfgService querycaching.DatasourceCacheConfig cleanUpService *cleanup.CleanUpService tracer tracing.Tracer grafanaUpdateChecker *updatechecker.GrafanaService @@ -255,7 +257,7 @@ func ProvideHTTPServer(opts ServerOptions, cfg *setting.Cfg, routeRegister routi accesscontrolService accesscontrol.Service, dashboardThumbsService thumbs.DashboardThumbService, navTreeService navtree.Service, annotationRepo annotations.Repository, tagService tag.Service, searchv2HTTPService searchV2.SearchHTTPService, queryLibraryHTTPService querylibrary.HTTPService, queryLibraryService querylibrary.Service, oauthTokenService oauthtoken.OAuthTokenService, - statsService stats.Service, authnService authn.Service, + statsService stats.Service, authnService authn.Service, dataSourceCacheCfgService querycaching.DatasourceCacheConfig, k8saccess k8saccess.K8SAccess, // required so that the router is registered ) (*HTTPServer, error) { web.Env = cfg.Env @@ -319,6 +321,7 @@ func ProvideHTTPServer(opts ServerOptions, cfg *setting.Cfg, routeRegister routi secretsStore: secretsStore, httpEntityStore: httpEntityStore, DataSourcesService: dataSourcesService, + DataSourceCacheCfgService: dataSourceCacheCfgService, searchUsersService: searchUsersService, ldapGroups: ldapGroups, teamGuardian: teamGuardian, diff --git a/pkg/plugins/models.go b/pkg/plugins/models.go index d60c7656314..12fa003a522 100644 --- a/pkg/plugins/models.go +++ b/pkg/plugins/models.go @@ -4,6 +4,7 @@ import ( "errors" "fmt" + "github.com/grafana/grafana/pkg/services/datasources/querycaching" "github.com/grafana/grafana/pkg/services/org" ) @@ -226,6 +227,8 @@ type DataSourceDTO struct { BasicAuth string `json:"basicAuth,omitempty"` WithCredentials bool `json:"withCredentials,omitempty"` + EnterpriseCachingConfig querycaching.CacheConfig `json:"cachingConfig,omitempty"` + // InfluxDB Username string `json:"username,omitempty"` Password string `json:"password,omitempty"` diff --git a/pkg/server/wireexts_oss.go b/pkg/server/wireexts_oss.go index b69c63daa7e..e8e6cf0b75e 100644 --- a/pkg/server/wireexts_oss.go +++ b/pkg/server/wireexts_oss.go @@ -18,6 +18,7 @@ import ( "github.com/grafana/grafana/pkg/services/auth/authimpl" "github.com/grafana/grafana/pkg/services/datasources" "github.com/grafana/grafana/pkg/services/datasources/permissions" + querycaching "github.com/grafana/grafana/pkg/services/datasources/querycaching" datasourceservice "github.com/grafana/grafana/pkg/services/datasources/service" "github.com/grafana/grafana/pkg/services/encryption" encryptionprovider "github.com/grafana/grafana/pkg/services/encryption/provider" @@ -81,6 +82,8 @@ var wireExtsBasicSet = wire.NewSet( ossaccesscontrol.ProvideDatasourcePermissionsService, wire.Bind(new(accesscontrol.DatasourcePermissionsService), new(*ossaccesscontrol.DatasourcePermissionsService)), pluginsintegration.WireExtensionSet, + querycaching.ProvideService, + wire.Bind(new(querycaching.DatasourceCacheConfig), new(*querycaching.OSSDatasourceCacheConfigImpl)), ) var wireExtsSet = wire.NewSet( diff --git a/pkg/services/datasources/querycaching/models.go b/pkg/services/datasources/querycaching/models.go new file mode 100644 index 00000000000..437443a77dd --- /dev/null +++ b/pkg/services/datasources/querycaching/models.go @@ -0,0 +1,16 @@ +package querycaching + +import "context" + +type DatasourceCacheConfig interface { + GetAllDatasourceConfig(ctx context.Context) (CacheConfigMap, error) +} + +// Map of datasource UIDs to cache configs +type CacheConfigMap map[string]CacheConfig + +type CacheConfig struct { + Enabled bool `json:"enabled"` + TTLMS int64 `json:"ttl_ms"` + UseDefaultTTL bool `json:"default_ttl"` +} diff --git a/pkg/services/datasources/querycaching/service.go b/pkg/services/datasources/querycaching/service.go new file mode 100644 index 00000000000..bc58557a6c1 --- /dev/null +++ b/pkg/services/datasources/querycaching/service.go @@ -0,0 +1,14 @@ +package querycaching + +import "context" + +func ProvideService() *OSSDatasourceCacheConfigImpl { + return &OSSDatasourceCacheConfigImpl{} +} + +type OSSDatasourceCacheConfigImpl struct { +} + +func (c *OSSDatasourceCacheConfigImpl) GetAllDatasourceConfig(ctx context.Context) (CacheConfigMap, error) { + return CacheConfigMap{}, nil +}