mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
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>
This commit is contained in:
parent
a73e6f728d
commit
45adfe7732
@ -4,10 +4,10 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/grafana/grafana/pkg/bus"
|
|
||||||
"github.com/grafana/grafana/pkg/infra/localcache"
|
"github.com/grafana/grafana/pkg/infra/localcache"
|
||||||
"github.com/grafana/grafana/pkg/models"
|
"github.com/grafana/grafana/pkg/models"
|
||||||
"github.com/grafana/grafana/pkg/registry"
|
"github.com/grafana/grafana/pkg/registry"
|
||||||
|
"github.com/grafana/grafana/pkg/services/sqlstore"
|
||||||
)
|
)
|
||||||
|
|
||||||
type CacheService interface {
|
type CacheService interface {
|
||||||
@ -15,8 +15,8 @@ type CacheService interface {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type CacheServiceImpl struct {
|
type CacheServiceImpl struct {
|
||||||
Bus bus.Bus `inject:""`
|
|
||||||
CacheService *localcache.CacheService `inject:""`
|
CacheService *localcache.CacheService `inject:""`
|
||||||
|
SQLStore *sqlstore.SqlStore `inject:""`
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
@ -31,7 +31,11 @@ func (dc *CacheServiceImpl) Init() error {
|
|||||||
return nil
|
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)
|
cacheKey := fmt.Sprintf("ds-%d", datasourceID)
|
||||||
|
|
||||||
if !skipCache {
|
if !skipCache {
|
||||||
@ -43,11 +47,12 @@ func (dc *CacheServiceImpl) GetDatasource(datasourceID int64, user *models.Signe
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
query := models.GetDataSourceByIdQuery{Id: datasourceID, OrgId: user.OrgId}
|
plog.Debug("Querying for data source via SQL store", "id", datasourceID, "orgId", user.OrgId)
|
||||||
if err := dc.Bus.Dispatch(&query); err != nil {
|
ds, err := dc.SQLStore.GetDataSourceByID(datasourceID, user.OrgId)
|
||||||
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
dc.CacheService.Set(cacheKey, query.Result, time.Second*5)
|
dc.CacheService.Set(cacheKey, ds, time.Second*5)
|
||||||
return query.Result, nil
|
return ds, nil
|
||||||
}
|
}
|
||||||
|
9
pkg/services/datasources/log.go
Normal file
9
pkg/services/datasources/log.go
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
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)
|
bus.AddHandler("sql", GetDataSourceByName)
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetDataSourceById(query *models.GetDataSourceByIdQuery) error {
|
func getDataSourceByID(id, orgID int64, engine *xorm.Engine) (*models.DataSource, error) {
|
||||||
metrics.MDBDataSourceQueryByID.Inc()
|
metrics.MDBDataSourceQueryByID.Inc()
|
||||||
|
|
||||||
datasource := models.DataSource{OrgId: query.OrgId, Id: query.Id}
|
datasource := models.DataSource{OrgId: orgID, Id: id}
|
||||||
has, err := x.Get(&datasource)
|
has, err := engine.Get(&datasource)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
sqlog.Error("Failed getting data source", "err", err, "id", id, "orgId", orgID)
|
||||||
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if !has {
|
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
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -70,7 +70,7 @@ func (ss *SqlStore) Init() error {
|
|||||||
|
|
||||||
engine, err := ss.getEngine()
|
engine, err := ss.getEngine()
|
||||||
if err != nil {
|
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
|
ss.engine = engine
|
||||||
@ -91,7 +91,7 @@ func (ss *SqlStore) Init() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if err := migrator.Start(); err != nil {
|
if err := migrator.Start(); err != nil {
|
||||||
return fmt.Errorf("Migration failed err: %v", err)
|
return errutil.Wrap("migration failed", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Init repo instances
|
// Init repo instances
|
||||||
|
Loading…
Reference in New Issue
Block a user