Commit 45adfe77 by Arve Knudsen Committed by GitHub

Data sources: Use SQL store directly, not via bus (#27000)

* Server: Make it possible to avoid getting SQL store via bus

Co-authored-by: Emil Tullstedt <emil.tullstedt@grafana.com>
parent a73e6f72
......@@ -4,10 +4,10 @@ import (
"fmt"
"time"
"github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/infra/localcache"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/registry"
"github.com/grafana/grafana/pkg/services/sqlstore"
)
type CacheService interface {
......@@ -15,8 +15,8 @@ type CacheService interface {
}
type CacheServiceImpl struct {
Bus bus.Bus `inject:""`
CacheService *localcache.CacheService `inject:""`
SQLStore *sqlstore.SqlStore `inject:""`
}
func init() {
......@@ -31,7 +31,11 @@ func (dc *CacheServiceImpl) Init() error {
return nil
}
func (dc *CacheServiceImpl) GetDatasource(datasourceID int64, user *models.SignedInUser, skipCache bool) (*models.DataSource, error) {
func (dc *CacheServiceImpl) GetDatasource(
datasourceID int64,
user *models.SignedInUser,
skipCache bool,
) (*models.DataSource, error) {
cacheKey := fmt.Sprintf("ds-%d", datasourceID)
if !skipCache {
......@@ -43,11 +47,12 @@ func (dc *CacheServiceImpl) GetDatasource(datasourceID int64, user *models.Signe
}
}
query := models.GetDataSourceByIdQuery{Id: datasourceID, OrgId: user.OrgId}
if err := dc.Bus.Dispatch(&query); err != nil {
plog.Debug("Querying for data source via SQL store", "id", datasourceID, "orgId", user.OrgId)
ds, err := dc.SQLStore.GetDataSourceByID(datasourceID, user.OrgId)
if err != nil {
return nil, err
}
dc.CacheService.Set(cacheKey, query.Result, time.Second*5)
return query.Result, nil
dc.CacheService.Set(cacheKey, ds, time.Second*5)
return ds, nil
}
package datasources
import (
"github.com/grafana/grafana/pkg/infra/log"
)
var (
plog = log.New("datasources")
)
......@@ -27,20 +27,31 @@ func init() {
bus.AddHandler("sql", GetDataSourceByName)
}
func GetDataSourceById(query *models.GetDataSourceByIdQuery) error {
func getDataSourceByID(id, orgID int64, engine *xorm.Engine) (*models.DataSource, error) {
metrics.MDBDataSourceQueryByID.Inc()
datasource := models.DataSource{OrgId: query.OrgId, Id: query.Id}
has, err := x.Get(&datasource)
datasource := models.DataSource{OrgId: orgID, Id: id}
has, err := engine.Get(&datasource)
if err != nil {
return err
sqlog.Error("Failed getting data source", "err", err, "id", id, "orgId", orgID)
return nil, err
}
if !has {
return models.ErrDataSourceNotFound
sqlog.Debug("Failed to find data source", "id", id, "orgId", orgID)
return nil, models.ErrDataSourceNotFound
}
query.Result = &datasource
return &datasource, nil
}
func (ss *SqlStore) GetDataSourceByID(id, orgID int64) (*models.DataSource, error) {
return getDataSourceByID(id, orgID, ss.engine)
}
func GetDataSourceById(query *models.GetDataSourceByIdQuery) error {
ds, err := getDataSourceByID(query.Id, query.OrgId, x)
query.Result = ds
return err
}
......
......@@ -70,7 +70,7 @@ func (ss *SqlStore) Init() error {
engine, err := ss.getEngine()
if err != nil {
return fmt.Errorf("Fail to connect to database: %v", err)
return errutil.Wrap("failed to connect to database", err)
}
ss.engine = engine
......@@ -91,7 +91,7 @@ func (ss *SqlStore) Init() error {
}
if err := migrator.Start(); err != nil {
return fmt.Errorf("Migration failed err: %v", err)
return errutil.Wrap("migration failed", err)
}
// Init repo instances
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment