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

Commit 2446a0a

Browse files
committed
make "public org" explicit + optimize find in case orgIdPublic passed
1 parent a7ebc62 commit 2446a0a

File tree

5 files changed

+29
-25
lines changed

5 files changed

+29
-25
lines changed

idx/cassandra/cassandra.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,7 @@ func (c *CasIdx) prune() {
504504
for range ticker.C {
505505
log.Debug("cassandra-idx: pruning items from index that have not been seen for %s", maxStale.String())
506506
staleTs := time.Now().Add(maxStale * -1)
507-
_, err := c.Prune(-1, staleTs)
507+
_, err := c.Prune(idx.OrgIdPublic, staleTs)
508508
if err != nil {
509509
log.Error(3, "cassandra-idx: prune error. %s", err)
510510
}

idx/cassandra/cassandra_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ func TestGetAddKey(t *testing.T) {
125125
ix := New()
126126
initForTests(ix)
127127

128-
publicSeries := getMetricData(-1, 2, 5, 10, "metric.public")
128+
publicSeries := getMetricData(idx.OrgIdPublic, 2, 5, 10, "metric.public")
129129
org1Series := getMetricData(1, 2, 5, 10, "metric.org1")
130130
org2Series := getMetricData(2, 2, 5, 10, "metric.org2")
131131

@@ -138,7 +138,7 @@ func TestGetAddKey(t *testing.T) {
138138
Convey(fmt.Sprintf("Then listing metrics for OrgId %d", orgId), func() {
139139
defs := ix.List(orgId)
140140
numSeries := len(series)
141-
if orgId != -1 {
141+
if orgId != idx.OrgIdPublic {
142142
numSeries += 5
143143
}
144144
So(defs, ShouldHaveLength, numSeries)
@@ -258,7 +258,7 @@ func TestAddToWriteQueue(t *testing.T) {
258258
func TestFind(t *testing.T) {
259259
ix := New()
260260
initForTests(ix)
261-
for _, s := range getMetricData(-1, 2, 5, 10, "metric.demo") {
261+
for _, s := range getMetricData(idx.OrgIdPublic, 2, 5, 10, "metric.demo") {
262262
ix.AddOrUpdate(s, 1)
263263
}
264264
for _, s := range getMetricData(1, 2, 5, 10, "metric.demo") {

idx/idx.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import (
1313
schema "gopkg.in/raintank/schema.v1"
1414
)
1515

16+
const OrgIdPublic = -1
17+
1618
var (
1719
BothBranchAndLeaf = errors.New("node can't be both branch and leaf")
1820
BranchUnderLeaf = errors.New("can't add branch under leaf")
@@ -118,18 +120,18 @@ type MetricIndex interface {
118120

119121
// Find searches the index. The method is passed an OrgId, a query
120122
// pattern and a unix timestamp. Searches should return all nodes that match for
121-
// the given OrgId and OrgId -1. The pattern should be handled in the same way
123+
// the given OrgId and OrgIdPublic. The pattern should be handled in the same way
122124
// Graphite would. see https://graphite.readthedocs.io/en/latest/render_api.html#paths-and-wildcards
123125
// And the unix stimestamp is used to ignore series that have been stale since
124126
// the timestamp.
125127
Find(int, string, int64) ([]Node, error)
126128

127-
// List returns all Archives for the passed OrgId, or for all organisations if -1 is provided.
129+
// List returns all Archives for the passed OrgId, or for all organisations if OrgIdPublic
128130
List(int) []Archive
129131

130132
// Prune deletes all metrics from the index for the passed org where
131133
// the last time the metric was seen is older then the passed timestamp. If the org
132-
// passed is -1, then the all orgs should be examined for stale metrics to be deleted.
134+
// is OrgIdPublic, then the all orgs should be examined for stale metrics to be deleted.
133135
// It returns all Archives deleted and any error encountered.
134136
Prune(int, time.Time) ([]Archive, error)
135137

idx/memory/memory.go

+11-9
Original file line numberDiff line numberDiff line change
@@ -836,15 +836,17 @@ func (m *MemoryIdx) Find(orgId int, pattern string, from int64) ([]idx.Node, err
836836
if err != nil {
837837
return nil, err
838838
}
839-
publicNodes, err := m.find(-1, pattern)
840-
if err != nil {
841-
return nil, err
839+
if orgId != idx.OrgIdPublic {
840+
publicNodes, err := m.find(idx.OrgIdPublic, pattern)
841+
if err != nil {
842+
return nil, err
843+
}
844+
matchedNodes = append(matchedNodes, publicNodes...)
842845
}
843-
matchedNodes = append(matchedNodes, publicNodes...)
844846
log.Debug("memory-idx: %d nodes matching pattern %s found", len(matchedNodes), pattern)
845847
results := make([]idx.Node, 0)
846848
seen := make(map[string]struct{})
847-
// if there are public (orgId -1) and private leaf nodes with the same series
849+
// if there are public (orgId OrgIdPublic) and private leaf nodes with the same series
848850
// path, then the public metricDefs will be excluded.
849851
for _, n := range matchedNodes {
850852
if _, ok := seen[n.Path]; !ok {
@@ -978,7 +980,7 @@ func (m *MemoryIdx) List(orgId int) []idx.Archive {
978980
defer m.RUnlock()
979981

980982
orgs := make(map[int]struct{})
981-
if orgId == -1 {
983+
if orgId == idx.OrgIdPublic {
982984
log.Info("memory-idx: returning all metricDefs for all orgs")
983985
for org := range m.tree {
984986
orgs[org] = struct{}{}
@@ -987,7 +989,7 @@ func (m *MemoryIdx) List(orgId int) []idx.Archive {
987989
orgs[org] = struct{}{}
988990
}
989991
} else {
990-
orgs[-1] = struct{}{}
992+
orgs[idx.OrgIdPublic] = struct{}{}
991993
orgs[orgId] = struct{}{}
992994
}
993995

@@ -1190,7 +1192,7 @@ func (m *MemoryIdx) delete(orgId int, n *Node, deleteEmptyParents, deleteChildre
11901192
func (m *MemoryIdx) Prune(orgId int, oldest time.Time) ([]idx.Archive, error) {
11911193
oldestUnix := oldest.Unix()
11921194
orgs := make(map[int]struct{})
1193-
if orgId == -1 {
1195+
if orgId == idx.OrgIdPublic {
11941196
log.Info("memory-idx: pruning stale metricDefs across all orgs")
11951197
m.RLock()
11961198
for org := range m.tree {
@@ -1300,7 +1302,7 @@ DEFS:
13001302

13011303
statMetricsActive.Add(-1 * len(pruned))
13021304

1303-
if orgId == -1 {
1305+
if orgId == idx.OrgIdPublic {
13041306
log.Info("memory-idx: pruning stale metricDefs from memory for all orgs took %s", time.Since(pre).String())
13051307
}
13061308

idx/memory/memory_test.go

+9-9
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ func testGetAddKey(t *testing.T) {
8686
ix := New()
8787
ix.Init()
8888

89-
publicSeries := getMetricData(-1, 2, 5, 10, "metric.public", false)
89+
publicSeries := getMetricData(idx.OrgIdPublic, 2, 5, 10, "metric.public", false)
9090
org1Series := getMetricData(1, 2, 5, 10, "metric.org1", false)
9191
org2Series := getMetricData(2, 2, 5, 10, "metric.org2", false)
9292

@@ -99,7 +99,7 @@ func testGetAddKey(t *testing.T) {
9999
Convey(fmt.Sprintf("Then listing metrics for OrgId %d", orgId), func() {
100100
defs := ix.List(orgId)
101101
numSeries := len(series)
102-
if orgId != -1 {
102+
if orgId != idx.OrgIdPublic {
103103
numSeries += 5
104104
}
105105
So(defs, ShouldHaveLength, numSeries)
@@ -128,7 +128,7 @@ func TestFind(t *testing.T) {
128128
func testFind(t *testing.T) {
129129
ix := New()
130130
ix.Init()
131-
for _, s := range getMetricData(-1, 2, 5, 10, "metric.demo", false) {
131+
for _, s := range getMetricData(idx.OrgIdPublic, 2, 5, 10, "metric.demo", false) {
132132
s.Time = 10 * 86400
133133
ix.AddOrUpdate(s, 1)
134134
}
@@ -256,7 +256,7 @@ func testDelete(t *testing.T) {
256256
ix := New()
257257
ix.Init()
258258

259-
publicSeries := getMetricData(-1, 2, 5, 10, "metric.public", false)
259+
publicSeries := getMetricData(idx.OrgIdPublic, 2, 5, 10, "metric.public", false)
260260
org1Series := getMetricData(1, 2, 5, 10, "metric.org1", false)
261261

262262
for _, s := range publicSeries {
@@ -271,7 +271,7 @@ func TestDeleteTagged(t *testing.T) {
271271
ix := New()
272272
ix.Init()
273273

274-
publicSeries := getMetricData(-1, 2, 5, 10, "metric.public", true)
274+
publicSeries := getMetricData(idx.OrgIdPublic, 2, 5, 10, "metric.public", true)
275275
org1Series := getMetricData(1, 2, 5, 10, "metric.org1", true)
276276

277277
for _, s := range publicSeries {
@@ -506,7 +506,7 @@ func TestPruneTaggedSeries(t *testing.T) {
506506
}
507507

508508
Convey("after populating index", t, func() {
509-
defs := ix.List(-1)
509+
defs := ix.List(idx.OrgIdPublic)
510510
So(defs, ShouldHaveLength, 10)
511511
})
512512

@@ -523,7 +523,7 @@ func TestPruneTaggedSeries(t *testing.T) {
523523
})
524524

525525
Convey("after purge", t, func() {
526-
defs := ix.List(-1)
526+
defs := ix.List(idx.OrgIdPublic)
527527
So(defs, ShouldHaveLength, 5)
528528
data := &schema.MetricData{
529529
Name: defs[0].Name,
@@ -638,7 +638,7 @@ func testPrune(t *testing.T) {
638638
ix.AddOrUpdate(d, 1)
639639
}
640640
Convey("after populating index", t, func() {
641-
defs := ix.List(-1)
641+
defs := ix.List(idx.OrgIdPublic)
642642
So(defs, ShouldHaveLength, 10)
643643
})
644644
Convey("When purging old series", t, func() {
@@ -654,7 +654,7 @@ func testPrune(t *testing.T) {
654654

655655
})
656656
Convey("after purge", t, func() {
657-
defs := ix.List(-1)
657+
defs := ix.List(idx.OrgIdPublic)
658658
So(defs, ShouldHaveLength, 5)
659659
data := &schema.MetricData{
660660
Name: defs[0].Name,

0 commit comments

Comments
 (0)