mirror of
https://github.com/grafana/grafana.git
synced 2024-11-25 02:10:45 -06:00
Chore: Remove SQL engine cache (#39254)
Fix bug with engine cache not updating after data source updated, introduced in #36635. Removes the SQL engine cache because since #36635 we already have instance caching in place using the SDK instance manager. This also adds support for disposing cached SQL/xorm engines before a query happens to a data source that has been updated. Done by letting the SQL engine implementing the InstanceDisposer interface. Fixes #37299
This commit is contained in:
parent
64ff15c157
commit
e31b8f4d33
@ -41,17 +41,6 @@ type SqlQueryResultTransformer interface {
|
||||
GetConverterList() []sqlutil.StringConverter
|
||||
}
|
||||
|
||||
type engineCacheType struct {
|
||||
cache map[int64]*xorm.Engine
|
||||
updates map[int64]time.Time
|
||||
sync.Mutex
|
||||
}
|
||||
|
||||
var engineCache = engineCacheType{
|
||||
cache: make(map[int64]*xorm.Engine),
|
||||
updates: make(map[int64]time.Time),
|
||||
}
|
||||
|
||||
var sqlIntervalCalculator = intervalv2.NewCalculator()
|
||||
|
||||
// NewXormEngine is an xorm.Engine factory, that can be stubbed by tests.
|
||||
@ -129,6 +118,11 @@ func (e *DataSourceHandler) transformQueryError(err error) error {
|
||||
|
||||
func NewQueryDataHandler(config DataPluginConfiguration, queryResultTransformer SqlQueryResultTransformer,
|
||||
macroEngine SQLMacroEngine, log log.Logger) (*DataSourceHandler, error) {
|
||||
log.Debug("Creating engine...")
|
||||
defer func() {
|
||||
log.Debug("Engine created")
|
||||
}()
|
||||
|
||||
queryDataHandler := DataSourceHandler{
|
||||
queryResultTransformer: queryResultTransformer,
|
||||
macroEngine: macroEngine,
|
||||
@ -146,16 +140,6 @@ func NewQueryDataHandler(config DataPluginConfiguration, queryResultTransformer
|
||||
queryDataHandler.metricColumnTypes = config.MetricColumnTypes
|
||||
}
|
||||
|
||||
engineCache.Lock()
|
||||
defer engineCache.Unlock()
|
||||
|
||||
if engine, present := engineCache.cache[config.DSInfo.ID]; present {
|
||||
if updateTime := engineCache.updates[config.DSInfo.ID]; updateTime.Before(config.DSInfo.Updated) {
|
||||
queryDataHandler.engine = engine
|
||||
return &queryDataHandler, nil
|
||||
}
|
||||
}
|
||||
|
||||
engine, err := NewXormEngine(config.DriverName, config.ConnectionString)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@ -165,8 +149,6 @@ func NewQueryDataHandler(config DataPluginConfiguration, queryResultTransformer
|
||||
engine.SetMaxIdleConns(config.DSInfo.JsonData.MaxIdleConns)
|
||||
engine.SetConnMaxLifetime(time.Duration(config.DSInfo.JsonData.ConnMaxLifetime) * time.Second)
|
||||
|
||||
engineCache.updates[config.DSInfo.ID] = config.DSInfo.Updated
|
||||
engineCache.cache[config.DSInfo.ID] = engine
|
||||
queryDataHandler.engine = engine
|
||||
return &queryDataHandler, nil
|
||||
}
|
||||
@ -176,6 +158,16 @@ type DBDataResponse struct {
|
||||
refID string
|
||||
}
|
||||
|
||||
func (e *DataSourceHandler) Dispose() {
|
||||
e.log.Debug("Disposing engine...")
|
||||
if e.engine != nil {
|
||||
if err := e.engine.Close(); err != nil {
|
||||
e.log.Error("Failed to dispose engine", "error", err)
|
||||
}
|
||||
}
|
||||
e.log.Debug("Engine disposed")
|
||||
}
|
||||
|
||||
func (e *DataSourceHandler) QueryData(ctx context.Context, req *backend.QueryDataRequest) (*backend.QueryDataResponse, error) {
|
||||
result := backend.NewQueryDataResponse()
|
||||
ch := make(chan DBDataResponse, len(req.Queries))
|
||||
|
Loading…
Reference in New Issue
Block a user