Skip to content
This repository was archived by the owner on Aug 23, 2023. It is now read-only.

Commit 52d5e51

Browse files
authored
Merge pull request #885 from grafana/orgid-uint
represent orgid in a uint32 to save memory
2 parents b70a9cb + 076fa4c commit 52d5e51

File tree

15 files changed

+94
-91
lines changed

15 files changed

+94
-91
lines changed

api/graphite.go

+13-13
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ type Series struct {
5454
Node cluster.Node
5555
}
5656

57-
func (s *Server) findSeries(ctx context.Context, orgId int, patterns []string, seenAfter int64) ([]Series, error) {
57+
func (s *Server) findSeries(ctx context.Context, orgId uint32, patterns []string, seenAfter int64) ([]Series, error) {
5858
peers, err := cluster.MembersForQuery()
5959
if err != nil {
6060
log.Error(3, "HTTP findSeries unable to get peers, %s", err)
@@ -118,7 +118,7 @@ func (s *Server) findSeries(ctx context.Context, orgId int, patterns []string, s
118118
return series, nil
119119
}
120120

121-
func (s *Server) findSeriesLocal(ctx context.Context, orgId int, patterns []string, seenAfter int64) ([]Series, error) {
121+
func (s *Server) findSeriesLocal(ctx context.Context, orgId uint32, patterns []string, seenAfter int64) ([]Series, error) {
122122
result := make([]Series, 0)
123123
for _, pattern := range patterns {
124124
select {
@@ -149,7 +149,7 @@ func (s *Server) findSeriesLocal(ctx context.Context, orgId int, patterns []stri
149149
return result, nil
150150
}
151151

152-
func (s *Server) findSeriesRemote(ctx context.Context, orgId int, patterns []string, seenAfter int64, peer cluster.Node) ([]Series, error) {
152+
func (s *Server) findSeriesRemote(ctx context.Context, orgId uint32, patterns []string, seenAfter int64, peer cluster.Node) ([]Series, error) {
153153
log.Debug("HTTP Render querying %s/index/find for %d:%q", peer.GetName(), orgId, patterns)
154154
data := models.IndexFind{
155155
Patterns: patterns,
@@ -370,11 +370,11 @@ func (s *Server) metricsFind(ctx *middleware.Context, request models.GraphiteFin
370370
}
371371
}
372372

373-
func (s *Server) listLocal(orgId int) []idx.Archive {
373+
func (s *Server) listLocal(orgId uint32) []idx.Archive {
374374
return s.MetricIndex.List(orgId)
375375
}
376376

377-
func (s *Server) listRemote(ctx context.Context, orgId int, peer cluster.Node) ([]idx.Archive, error) {
377+
func (s *Server) listRemote(ctx context.Context, orgId uint32, peer cluster.Node) ([]idx.Archive, error) {
378378
log.Debug("HTTP IndexJson() querying %s/index/list for %d", peer.GetName(), orgId)
379379
buf, err := peer.Post(ctx, "listRemote", "/index/list", models.IndexList{OrgId: orgId})
380380
if err != nil {
@@ -635,12 +635,12 @@ func (s *Server) metricsDelete(ctx *middleware.Context, req models.MetricsDelete
635635
response.Write(ctx, response.NewJson(200, resp, ""))
636636
}
637637

638-
func (s *Server) metricsDeleteLocal(orgId int, query string) (int, error) {
638+
func (s *Server) metricsDeleteLocal(orgId uint32, query string) (int, error) {
639639
defs, err := s.MetricIndex.Delete(orgId, query)
640640
return len(defs), err
641641
}
642642

643-
func (s *Server) metricsDeleteRemote(ctx context.Context, orgId int, query string, peer cluster.Node) (int, error) {
643+
func (s *Server) metricsDeleteRemote(ctx context.Context, orgId uint32, query string, peer cluster.Node) (int, error) {
644644
log.Debug("HTTP metricDelete calling %s/index/delete for %d:%q", peer.GetName(), orgId, query)
645645

646646
body := models.IndexDelete{
@@ -673,7 +673,7 @@ func (s *Server) metricsDeleteRemote(ctx context.Context, orgId int, query strin
673673
// executePlan looks up the needed data, retrieves it, and then invokes the processing
674674
// note if you do something like sum(foo.*) and all of those metrics happen to be on another node,
675675
// we will collect all the indidividual series from the peer, and then sum here. that could be optimized
676-
func (s *Server) executePlan(ctx context.Context, orgId int, plan expr.Plan) ([]models.Series, error) {
676+
func (s *Server) executePlan(ctx context.Context, orgId uint32, plan expr.Plan) ([]models.Series, error) {
677677

678678
minFrom := uint32(math.MaxUint32)
679679
var maxTo uint32
@@ -861,7 +861,7 @@ func (s *Server) graphiteTagDetails(ctx *middleware.Context, request models.Grap
861861
response.Write(ctx, response.NewJson(200, resp, ""))
862862
}
863863

864-
func (s *Server) clusterTagDetails(ctx context.Context, orgId int, tag, filter string, from int64) (map[string]uint64, error) {
864+
func (s *Server) clusterTagDetails(ctx context.Context, orgId uint32, tag, filter string, from int64) (map[string]uint64, error) {
865865
result, err := s.MetricIndex.TagDetails(orgId, tag, filter, from)
866866
if err != nil {
867867
return nil, err
@@ -923,7 +923,7 @@ func (s *Server) graphiteTagFindSeries(ctx *middleware.Context, request models.G
923923
response.Write(ctx, response.NewJson(200, seriesNames, ""))
924924
}
925925

926-
func (s *Server) clusterFindByTag(ctx context.Context, orgId int, expressions []string, from int64) ([]Series, error) {
926+
func (s *Server) clusterFindByTag(ctx context.Context, orgId uint32, expressions []string, from int64) ([]Series, error) {
927927
seriesSet := make(map[string]Series)
928928

929929
result, err := s.MetricIndex.FindByTag(orgId, expressions, from)
@@ -1005,7 +1005,7 @@ func (s *Server) graphiteTags(ctx *middleware.Context, request models.GraphiteTa
10051005
response.Write(ctx, response.NewJson(200, resp, ""))
10061006
}
10071007

1008-
func (s *Server) clusterTags(ctx context.Context, orgId int, filter string, from int64) ([]string, error) {
1008+
func (s *Server) clusterTags(ctx context.Context, orgId uint32, filter string, from int64) ([]string, error) {
10091009
result, err := s.MetricIndex.Tags(orgId, filter, from)
10101010
if err != nil {
10111011
return nil, err
@@ -1068,7 +1068,7 @@ func (s *Server) graphiteAutoCompleteTags(ctx *middleware.Context, request model
10681068
response.Write(ctx, response.NewJson(200, tags, ""))
10691069
}
10701070

1071-
func (s *Server) clusterAutoCompleteTags(ctx context.Context, orgId int, prefix string, expressions []string, from int64, limit uint) ([]string, error) {
1071+
func (s *Server) clusterAutoCompleteTags(ctx context.Context, orgId uint32, prefix string, expressions []string, from int64, limit uint) ([]string, error) {
10721072
result, err := s.MetricIndex.FindTags(orgId, prefix, expressions, from, limit)
10731073
if err != nil {
10741074
return nil, err
@@ -1123,7 +1123,7 @@ func (s *Server) graphiteAutoCompleteTagValues(ctx *middleware.Context, request
11231123
response.Write(ctx, response.NewJson(200, resp, ""))
11241124
}
11251125

1126-
func (s *Server) clusterAutoCompleteTagValues(ctx context.Context, orgId int, tag, prefix string, expressions []string, from int64, limit uint) ([]string, error) {
1126+
func (s *Server) clusterAutoCompleteTagValues(ctx context.Context, orgId uint32, tag, prefix string, expressions []string, from int64, limit uint) ([]string, error) {
11271127
result, err := s.MetricIndex.FindTagValues(orgId, tag, prefix, expressions, from, limit)
11281128
if err != nil {
11291129
return nil, err

api/middleware/middleware.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212

1313
type Context struct {
1414
*macaron.Context
15-
OrgId int
15+
OrgId uint32
1616
Body io.ReadCloser
1717
}
1818

@@ -31,7 +31,7 @@ func OrgMiddleware(multiTenant bool) macaron.Handler {
3131
}
3232
}
3333

34-
func getOrg(req *http.Request, multiTenant bool) (int, error) {
34+
func getOrg(req *http.Request, multiTenant bool) (uint32, error) {
3535
if !multiTenant {
3636
return 1, nil
3737
}
@@ -40,10 +40,10 @@ func getOrg(req *http.Request, multiTenant bool) (int, error) {
4040
return 0, nil
4141
}
4242
org, err := strconv.Atoi(orgStr)
43-
if err != nil {
43+
if err != nil || org < 1 {
4444
return 0, errors.New("bad org-id")
4545
}
46-
return org, nil
46+
return uint32(org), nil
4747
}
4848

4949
func RequireOrg() macaron.Handler {

api/models/ccache.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ type CCacheDelete struct {
99
Patterns []string `json:"patterns" form:"patterns" `
1010
// tag expressions to select series
1111
Expr []string `json:"expr" form:"expr"`
12-
OrgId int `json:"orgId" form:"orgId" binding:"Required"`
12+
OrgId uint32 `json:"orgId" form:"orgId" binding:"Required"`
1313
Propagate bool `json:"propagate" form:"propagate"`
1414
}
1515

api/models/node.go

+9-9
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ type ClusterMembersResp struct {
2626
}
2727

2828
type IndexList struct {
29-
OrgId int `json:"orgId" form:"orgId" binding:"Required"`
29+
OrgId uint32 `json:"orgId" form:"orgId" binding:"Required"`
3030
}
3131

3232
func (i IndexList) Trace(span opentracing.Span) {
@@ -37,7 +37,7 @@ func (i IndexList) TraceDebug(span opentracing.Span) {
3737
}
3838

3939
type IndexFindByTag struct {
40-
OrgId int `json:"orgId" binding:"Required"`
40+
OrgId uint32 `json:"orgId" binding:"Required"`
4141
Expr []string `json:"expressions"`
4242
From int64 `json:"from"`
4343
}
@@ -52,7 +52,7 @@ func (i IndexFindByTag) TraceDebug(span opentracing.Span) {
5252
}
5353

5454
type IndexTagDetails struct {
55-
OrgId int `json:"orgId" binding:"Required"`
55+
OrgId uint32 `json:"orgId" binding:"Required"`
5656
Filter string `json:"filter"`
5757
Tag string `json:"tag" binding:"Required"`
5858
From int64 `json:"from"`
@@ -69,7 +69,7 @@ func (i IndexTagDetails) TraceDebug(span opentracing.Span) {
6969
}
7070

7171
type IndexTags struct {
72-
OrgId int `json:"orgId" binding:"Required"`
72+
OrgId uint32 `json:"orgId" binding:"Required"`
7373
Filter string `json:"filter"`
7474
From int64 `json:"from"`
7575
}
@@ -84,7 +84,7 @@ func (i IndexTags) TraceDebug(span opentracing.Span) {
8484
}
8585

8686
type IndexAutoCompleteTags struct {
87-
OrgId int `json:"orgId" binding:"Required"`
87+
OrgId uint32 `json:"orgId" binding:"Required"`
8888
Prefix string `json:"Prefix"`
8989
Expr []string `json:"expressions"`
9090
From int64 `json:"from"`
@@ -103,7 +103,7 @@ func (i IndexAutoCompleteTags) TraceDebug(span opentracing.Span) {
103103
}
104104

105105
type IndexAutoCompleteTagValues struct {
106-
OrgId int `json:"orgId" binding:"Required"`
106+
OrgId uint32 `json:"orgId" binding:"Required"`
107107
Tag string `json:"tag"`
108108
Prefix string `json:"prefix"`
109109
Expr []string `json:"expressions"`
@@ -124,7 +124,7 @@ func (i IndexAutoCompleteTagValues) TraceDebug(span opentracing.Span) {
124124
}
125125

126126
type IndexTagDelSeries struct {
127-
OrgId int `json:"orgId" binding:"Required"`
127+
OrgId uint32 `json:"orgId" binding:"Required"`
128128
Paths []string `json:"path" form:"path"`
129129
}
130130

@@ -142,7 +142,7 @@ type IndexGet struct {
142142

143143
type IndexFind struct {
144144
Patterns []string `json:"patterns" form:"patterns" binding:"Required"`
145-
OrgId int `json:"orgId" form:"orgId" binding:"Required"`
145+
OrgId uint32 `json:"orgId" form:"orgId" binding:"Required"`
146146
From int64 `json:"from" form:"from"`
147147
}
148148

@@ -177,7 +177,7 @@ func (g GetData) TraceDebug(span opentracing.Span) {
177177

178178
type IndexDelete struct {
179179
Query string `json:"query" form:"query" binding:"Required"`
180-
OrgId int `json:"orgId" form:"orgId" binding:"Required"`
180+
OrgId uint32 `json:"orgId" form:"orgId" binding:"Required"`
181181
}
182182

183183
func (i IndexDelete) Trace(span opentracing.Span) {

api/prometheus_querier.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,20 @@ import (
2121
func (s *Server) Querier(ctx context.Context, min, max int64) (storage.Querier, error) {
2222
from := uint32(min / 1000)
2323
to := uint32(max / 1000)
24-
return NewQuerier(ctx, s, from, to, ctx.Value(orgID("org-id")).(int), false), nil
24+
return NewQuerier(ctx, s, from, to, ctx.Value(orgID("org-id")).(uint32), false), nil
2525
}
2626

2727
// querier implements Prometheus' Querier interface
2828
type querier struct {
2929
*Server
3030
from uint32
3131
to uint32
32-
OrgID int
32+
OrgID uint32
3333
metadataOnly bool
3434
ctx context.Context
3535
}
3636

37-
func NewQuerier(ctx context.Context, s *Server, from, to uint32, orgId int, metadataOnly bool) storage.Querier {
37+
func NewQuerier(ctx context.Context, s *Server, from, to uint32, orgId uint32, metadataOnly bool) storage.Querier {
3838
return &querier{
3939
s,
4040
from,

cmd/mt-index-migrate/main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ func getDefs(session *gocql.Session, defsChan chan *schema.MetricDefinition) {
156156
}
157157
mdef := schema.MetricDefinition{
158158
Id: mkey,
159-
OrgId: orgId,
159+
OrgId: uint32(orgId),
160160
Partition: partition,
161161
Name: name,
162162
Interval: interval,

idx/cassandra/cassandra.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -399,10 +399,13 @@ func (c *CasIdx) load(defs []schema.MetricDefinition, iter cqlIterator, cutoff u
399399
log.Error(3, "cassandra-idx: load() could not parse ID %q: %s -> skipping", id, err)
400400
continue
401401
}
402+
if orgId < 0 {
403+
orgId = int(idx.OrgIdPublic)
404+
}
402405

403406
mdef := &schema.MetricDefinition{
404407
Id: mkey,
405-
OrgId: orgId,
408+
OrgId: uint32(orgId),
406409
Partition: partition,
407410
Name: name,
408411
Interval: interval,
@@ -487,7 +490,7 @@ func (c *CasIdx) processWriteQueue() {
487490
c.wg.Done()
488491
}
489492

490-
func (c *CasIdx) Delete(orgId int, pattern string) ([]idx.Archive, error) {
493+
func (c *CasIdx) Delete(orgId uint32, pattern string) ([]idx.Archive, error) {
491494
pre := time.Now()
492495
defs, err := c.MemoryIdx.Delete(orgId, pattern)
493496
if err != nil {

idx/cassandra/cassandra_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -104,14 +104,14 @@ func getRandomString(n int, alphabets ...byte) string {
104104
return string(bytes)
105105
}
106106

107-
func getMetricData(orgId, depth, count, interval int, prefix string) []*schema.MetricData {
107+
func getMetricData(orgId uint32, depth, count, interval int, prefix string) []*schema.MetricData {
108108
data := make([]*schema.MetricData, count)
109109
series := getSeriesNames(depth, count, prefix)
110110
for i, s := range series {
111111

112112
data[i] = &schema.MetricData{
113113
Name: s,
114-
OrgId: orgId,
114+
OrgId: int(orgId),
115115
Interval: interval,
116116
}
117117
data[i].SetId()
@@ -131,7 +131,7 @@ func TestGetAddKey(t *testing.T) {
131131
org2Series := getMetricData(2, 2, 5, 10, "metric.org2")
132132

133133
for _, series := range [][]*schema.MetricData{publicSeries, org1Series, org2Series} {
134-
orgId := series[0].OrgId
134+
orgId := uint32(series[0].OrgId)
135135
Convey(fmt.Sprintf("When indexing metrics for orgId %d", orgId), t, func() {
136136
for _, s := range series {
137137
mkey, err := schema.MKeyFromString(s.Id)

idx/idx.go

+11-11
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
schema "gopkg.in/raintank/schema.v1"
1010
)
1111

12-
var OrgIdPublic = 0
12+
var OrgIdPublic = uint32(0)
1313

1414
var (
1515
BothBranchAndLeaf = errors.New("node can't be both branch and leaf")
@@ -69,23 +69,23 @@ type MetricIndex interface {
6969
Get(key schema.MKey) (Archive, bool)
7070

7171
// GetPath returns the archives under the given path.
72-
GetPath(orgId int, path string) []Archive
72+
GetPath(orgId uint32, path string) []Archive
7373

7474
// Delete deletes items from the index
7575
// If the pattern matches a branch node, then
7676
// all leaf nodes on that branch are deleted. So if the pattern is
7777
// "*", all items in the index are deleted.
7878
// It returns a copy of all of the Archives deleted.
79-
Delete(orgId int, pattern string) ([]Archive, error)
79+
Delete(orgId uint32, pattern string) ([]Archive, error)
8080

8181
// Find searches the index for matching nodes.
8282
// * orgId describes the org to search in (public data in orgIdPublic is automatically included)
8383
// * pattern is handled like graphite does. see https://graphite.readthedocs.io/en/latest/render_api.html#paths-and-wildcards
8484
// * from is a unix timestamp. series not updated since then are excluded.
85-
Find(orgId int, pattern string, from int64) ([]Node, error)
85+
Find(orgId uint32, pattern string, from int64) ([]Node, error)
8686

8787
// List returns all Archives for the passed OrgId and the public orgId
88-
List(orgId int) []Archive
88+
List(orgId uint32) []Archive
8989

9090
// Prune deletes all metrics that haven't been seen since the given timestamp.
9191
// It returns all Archives deleted and any error encountered.
@@ -99,24 +99,24 @@ type MetricIndex interface {
9999
// where the LastUpdate time is >= from will be returned as results.
100100
// The returned results are not deduplicated and in certain cases it is possible
101101
// that duplicate entries will be returned.
102-
FindByTag(orgId int, expressions []string, from int64) ([]Node, error)
102+
FindByTag(orgId uint32, expressions []string, from int64) ([]Node, error)
103103

104104
// Tags returns a list of all tag keys associated with the metrics of a given
105105
// organization. The return values are filtered by the regex in the second parameter.
106106
// If the third parameter is >0 then only metrics will be accounted of which the
107107
// LastUpdate time is >= the given value.
108-
Tags(orgId int, filter string, from int64) ([]string, error)
108+
Tags(orgId uint32, filter string, from int64) ([]string, error)
109109

110110
// FindTags generates a list of possible tags that could complete a
111111
// given prefix. It also accepts additional tag conditions to further narrow
112112
// down the result set in the format of graphite's tag queries
113-
FindTags(orgId int, prefix string, expressions []string, from int64, limit uint) ([]string, error)
113+
FindTags(orgId uint32, prefix string, expressions []string, from int64, limit uint) ([]string, error)
114114

115115
// FindTagValues generates a list of possible values that could
116116
// complete a given value prefix. It requires a tag to be specified and only values
117117
// of the given tag will be returned. It also accepts additional conditions to
118118
// further narrow down the result set in the format of graphite's tag queries
119-
FindTagValues(orgId int, tag string, prefix string, expressions []string, from int64, limit uint) ([]string, error)
119+
FindTagValues(orgId uint32, tag string, prefix string, expressions []string, from int64, limit uint) ([]string, error)
120120

121121
// TagDetails returns a list of all values associated with a given tag key in the
122122
// given org. The occurrences of each value is counted and the count is referred to by
@@ -125,9 +125,9 @@ type MetricIndex interface {
125125
// the values before accounting for them.
126126
// If the fourth parameter is > 0 then only those metrics of which the LastUpdate
127127
// time is >= the from timestamp will be included.
128-
TagDetails(orgId int, key string, filter string, from int64) (map[string]uint64, error)
128+
TagDetails(orgId uint32, key string, filter string, from int64) (map[string]uint64, error)
129129

130130
// DeleteTagged deletes the specified series from the tag index and also the
131131
// DefById index.
132-
DeleteTagged(orgId int, paths []string) ([]Archive, error)
132+
DeleteTagged(orgId uint32, paths []string) ([]Archive, error)
133133
}

0 commit comments

Comments
 (0)