Skip to content

Commit 07d78da

Browse files
committed
Merge branch 'ds_cache_refactor' of https://github.com/marefr/grafana into marefr-ds_cache_refactor
2 parents 749d7a2 + d0c0038 commit 07d78da

File tree

14 files changed

+165
-70
lines changed

14 files changed

+165
-70
lines changed

pkg/api/dataproxy.go

Lines changed: 5 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,22 @@
11
package api
22

33
import (
4-
"fmt"
5-
"github.com/pkg/errors"
6-
"time"
7-
84
"github.com/grafana/grafana/pkg/api/pluginproxy"
9-
"github.com/grafana/grafana/pkg/bus"
105
"github.com/grafana/grafana/pkg/metrics"
116
m "github.com/grafana/grafana/pkg/models"
127
"github.com/grafana/grafana/pkg/plugins"
138
)
149

15-
const HeaderNameNoBackendCache = "X-Grafana-NoCache"
16-
17-
func (hs *HTTPServer) getDatasourceFromCache(id int64, c *m.ReqContext) (*m.DataSource, error) {
18-
userPermissionsQuery := m.GetDataSourcePermissionsForUserQuery{
19-
User: c.SignedInUser,
20-
}
21-
if err := bus.Dispatch(&userPermissionsQuery); err != nil {
22-
if err != bus.ErrHandlerNotFound {
23-
return nil, err
24-
}
25-
} else {
26-
permissionType, exists := userPermissionsQuery.Result[id]
27-
if exists && permissionType != m.DsPermissionQuery {
28-
return nil, errors.New("User not allowed to access datasource")
29-
}
30-
}
31-
32-
nocache := c.Req.Header.Get(HeaderNameNoBackendCache) == "true"
33-
cacheKey := fmt.Sprintf("ds-%d", id)
34-
35-
if !nocache {
36-
if cached, found := hs.cache.Get(cacheKey); found {
37-
ds := cached.(*m.DataSource)
38-
if ds.OrgId == c.OrgId {
39-
return ds, nil
40-
}
41-
}
42-
}
43-
44-
query := m.GetDataSourceByIdQuery{Id: id, OrgId: c.OrgId}
45-
if err := bus.Dispatch(&query); err != nil {
46-
return nil, err
47-
}
48-
49-
hs.cache.Set(cacheKey, query.Result, time.Second*5)
50-
return query.Result, nil
51-
}
52-
5310
func (hs *HTTPServer) ProxyDataSourceRequest(c *m.ReqContext) {
5411
c.TimeRequest(metrics.M_DataSource_ProxyReq_Timer)
5512

5613
dsId := c.ParamsInt64(":id")
57-
ds, err := hs.getDatasourceFromCache(dsId, c)
58-
14+
ds, err := hs.DatasourceCache.GetDatasource(dsId, c.SignedInUser, c.SkipCache)
5915
if err != nil {
16+
if err == m.ErrDataSourceAccessDenied {
17+
c.JsonApiErr(403, "Access denied to datasource", err)
18+
return
19+
}
6020
c.JsonApiErr(500, "Unable to load datasource meta data", err)
6121
return
6222
}

pkg/api/http_server.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import (
1616

1717
"github.com/prometheus/client_golang/prometheus/promhttp"
1818

19-
gocache "github.com/patrickmn/go-cache"
2019
macaron "gopkg.in/macaron.v1"
2120

2221
"github.com/grafana/grafana/pkg/api/live"
@@ -28,6 +27,8 @@ import (
2827
"github.com/grafana/grafana/pkg/models"
2928
"github.com/grafana/grafana/pkg/plugins"
3029
"github.com/grafana/grafana/pkg/registry"
30+
"github.com/grafana/grafana/pkg/services/cache"
31+
"github.com/grafana/grafana/pkg/services/datasources"
3132
"github.com/grafana/grafana/pkg/services/hooks"
3233
"github.com/grafana/grafana/pkg/services/rendering"
3334
"github.com/grafana/grafana/pkg/setting"
@@ -46,19 +47,19 @@ type HTTPServer struct {
4647
macaron *macaron.Macaron
4748
context context.Context
4849
streamManager *live.StreamManager
49-
cache *gocache.Cache
5050
httpSrv *http.Server
5151

52-
RouteRegister routing.RouteRegister `inject:""`
53-
Bus bus.Bus `inject:""`
54-
RenderService rendering.Service `inject:""`
55-
Cfg *setting.Cfg `inject:""`
56-
HooksService *hooks.HooksService `inject:""`
52+
RouteRegister routing.RouteRegister `inject:""`
53+
Bus bus.Bus `inject:""`
54+
RenderService rendering.Service `inject:""`
55+
Cfg *setting.Cfg `inject:""`
56+
HooksService *hooks.HooksService `inject:""`
57+
CacheService *cache.CacheService `inject:""`
58+
DatasourceCache datasources.CacheService `inject:""`
5759
}
5860

5961
func (hs *HTTPServer) Init() error {
6062
hs.log = log.New("http.server")
61-
hs.cache = gocache.New(5*time.Minute, 10*time.Minute)
6263

6364
hs.streamManager = live.NewStreamManager()
6465
hs.macaron = hs.newMacaron()
@@ -231,6 +232,7 @@ func (hs *HTTPServer) addMiddlewaresAndStaticRoutes() {
231232
m.Use(middleware.ValidateHostHeader(setting.Domain))
232233
}
233234

235+
m.Use(middleware.HandleNoCacheHeader())
234236
m.Use(middleware.AddDefaultResponseHeaders())
235237
}
236238

pkg/api/metrics.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,11 @@ func (hs *HTTPServer) QueryMetrics(c *m.ReqContext, reqDto dtos.MetricRequest) R
2525
return Error(400, "Query missing datasourceId", nil)
2626
}
2727

28-
ds, err := hs.getDatasourceFromCache(datasourceId, c)
28+
ds, err := hs.DatasourceCache.GetDatasource(datasourceId, c.SignedInUser, c.SkipCache)
2929
if err != nil {
30+
if err == m.ErrDataSourceAccessDenied {
31+
return Error(403, "Access denied to datasource", err)
32+
}
3033
return Error(500, "Unable to load datasource meta data", err)
3134
}
3235

pkg/cmd/grafana-server/server.go

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,29 @@ import (
1515
"github.com/grafana/grafana/pkg/api"
1616
"github.com/grafana/grafana/pkg/api/routing"
1717
"github.com/grafana/grafana/pkg/bus"
18-
_ "github.com/grafana/grafana/pkg/extensions"
19-
"github.com/grafana/grafana/pkg/log"
2018
"github.com/grafana/grafana/pkg/login"
21-
_ "github.com/grafana/grafana/pkg/metrics"
2219
"github.com/grafana/grafana/pkg/middleware"
23-
_ "github.com/grafana/grafana/pkg/plugins"
2420
"github.com/grafana/grafana/pkg/registry"
21+
"github.com/grafana/grafana/pkg/social"
22+
23+
"golang.org/x/sync/errgroup"
24+
25+
"github.com/grafana/grafana/pkg/log"
26+
"github.com/grafana/grafana/pkg/services/cache"
27+
"github.com/grafana/grafana/pkg/setting"
28+
29+
// self registering services
30+
_ "github.com/grafana/grafana/pkg/extensions"
31+
_ "github.com/grafana/grafana/pkg/metrics"
32+
_ "github.com/grafana/grafana/pkg/plugins"
2533
_ "github.com/grafana/grafana/pkg/services/alerting"
2634
_ "github.com/grafana/grafana/pkg/services/cleanup"
2735
_ "github.com/grafana/grafana/pkg/services/notifications"
2836
_ "github.com/grafana/grafana/pkg/services/provisioning"
2937
_ "github.com/grafana/grafana/pkg/services/rendering"
3038
_ "github.com/grafana/grafana/pkg/services/search"
3139
_ "github.com/grafana/grafana/pkg/services/sqlstore"
32-
"github.com/grafana/grafana/pkg/setting"
33-
"github.com/grafana/grafana/pkg/social" // self registering services
3440
_ "github.com/grafana/grafana/pkg/tracing"
35-
"golang.org/x/sync/errgroup"
3641
)
3742

3843
func NewGrafanaServer() *GrafanaServerImpl {
@@ -72,6 +77,7 @@ func (g *GrafanaServerImpl) Run() error {
7277
serviceGraph.Provide(&inject.Object{Value: bus.GetBus()})
7378
serviceGraph.Provide(&inject.Object{Value: g.cfg})
7479
serviceGraph.Provide(&inject.Object{Value: routing.NewRouteRegister(middleware.RequestMetrics, middleware.RequestTracing)})
80+
serviceGraph.Provide(&inject.Object{Value: cache.New(5*time.Minute, 10*time.Minute)})
7581

7682
// self registered services
7783
services := registry.GetServices()
@@ -138,7 +144,6 @@ func (g *GrafanaServerImpl) Run() error {
138144
}
139145

140146
sendSystemdNotification("READY=1")
141-
142147
return g.childRoutines.Wait()
143148
}
144149

pkg/login/auth.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package login
22

33
import (
44
"errors"
5+
56
"github.com/grafana/grafana/pkg/bus"
67
m "github.com/grafana/grafana/pkg/models"
78
)

pkg/middleware/headers.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package middleware
2+
3+
import (
4+
m "github.com/grafana/grafana/pkg/models"
5+
macaron "gopkg.in/macaron.v1"
6+
)
7+
8+
const HeaderNameNoBackendCache = "X-Grafana-NoCache"
9+
10+
func HandleNoCacheHeader() macaron.Handler {
11+
return func(ctx *m.ReqContext) {
12+
ctx.SkipCache = ctx.Req.Header.Get(HeaderNameNoBackendCache) == "true"
13+
}
14+
}

pkg/middleware/middleware.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ func GetContextHandler() macaron.Handler {
2929
Session: session.GetSession(),
3030
IsSignedIn: false,
3131
AllowAnonymous: false,
32+
SkipCache: false,
3233
Logger: log.New("context"),
3334
}
3435

pkg/models/context.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ type ReqContext struct {
2020
IsSignedIn bool
2121
IsRenderCall bool
2222
AllowAnonymous bool
23+
SkipCache bool
2324
Logger log.Logger
2425
}
2526

pkg/models/datasource.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -207,11 +207,6 @@ func (p DsPermissionType) String() string {
207207
return names[int(p)]
208208
}
209209

210-
type GetDataSourcePermissionsForUserQuery struct {
211-
User *SignedInUser
212-
Result map[int64]DsPermissionType
213-
}
214-
215210
type DatasourcesPermissionFilterQuery struct {
216211
User *SignedInUser
217212
Datasources []*DataSource

pkg/models/user.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ type SignedInUser struct {
165165
IsAnonymous bool
166166
HelpFlags1 HelpFlags1
167167
LastSeenAt time.Time
168+
Teams []int64
168169
}
169170

170171
func (u *SignedInUser) ShouldUpdateLastSeenAt() bool {

0 commit comments

Comments
 (0)