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

Commit 6e3d03b

Browse files
authored
Merge pull request #897 from bloomberg/feature_multiDef
Fix multi-interval series issue
2 parents e797a9f + 81a95cf commit 6e3d03b

File tree

4 files changed

+61
-27
lines changed

4 files changed

+61
-27
lines changed

api/dataprocessor.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ func (s *Server) getTargets(ctx context.Context, reqs []models.Req) ([]models.Se
170170
if len(remoteReqs) > 0 {
171171
wg.Add(1)
172172
go func() {
173-
// all errors returned returned are *response.Error.
173+
// all errors returned are *response.Error.
174174
series, err := s.getTargetsRemote(getCtx, remoteReqs)
175175
if err != nil {
176176
cancel()

api/graphite.go

+7-12
Original file line numberDiff line numberDiff line change
@@ -924,8 +924,6 @@ func (s *Server) graphiteTagFindSeries(ctx *middleware.Context, request models.G
924924
}
925925

926926
func (s *Server) clusterFindByTag(ctx context.Context, orgId uint32, expressions []string, from int64) ([]Series, error) {
927-
seriesSet := make(map[string]Series)
928-
929927
result, err := s.MetricIndex.FindByTag(orgId, expressions, from)
930928
if err != nil {
931929
return nil, err
@@ -938,12 +936,14 @@ func (s *Server) clusterFindByTag(ctx context.Context, orgId uint32, expressions
938936
default:
939937
}
940938

939+
var allSeries []Series
940+
941941
for _, series := range result {
942-
seriesSet[series.Path] = Series{
942+
allSeries = append(allSeries, Series{
943943
Pattern: series.Path,
944944
Node: cluster.Manager.ThisNode(),
945945
Series: []idx.Node{series},
946-
}
946+
})
947947
}
948948

949949
data := models.IndexFindByTag{OrgId: orgId, Expr: expressions, From: from}
@@ -966,20 +966,15 @@ func (s *Server) clusterFindByTag(ctx context.Context, orgId uint32, expressions
966966
return nil, err
967967
}
968968
for _, series := range resp.Metrics {
969-
seriesSet[series.Path] = Series{
969+
allSeries = append(allSeries, Series{
970970
Pattern: series.Path,
971971
Node: r.peer,
972972
Series: []idx.Node{series},
973-
}
973+
})
974974
}
975975
}
976976

977-
series := make([]Series, 0, len(seriesSet))
978-
for _, s := range seriesSet {
979-
series = append(series, s)
980-
}
981-
982-
return series, nil
977+
return allSeries, nil
983978
}
984979

985980
func (s *Server) graphiteTags(ctx *middleware.Context, request models.GraphiteTags) {

idx/memory/memory.go

+33-13
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,10 @@ func (m *MemoryIdx) Update(point schema.MetricPoint, partition int32) (idx.Archi
222222
if LogLevel < 2 {
223223
log.Debug("metricDef with id %v already in index", point.MKey)
224224
}
225-
existing.LastUpdate = int64(point.Time)
225+
226+
if existing.LastUpdate < int64(point.Time) {
227+
existing.LastUpdate = int64(point.Time)
228+
}
226229
existing.Partition = partition
227230
statUpdate.Inc()
228231
statUpdateDuration.Value(time.Since(pre))
@@ -244,7 +247,9 @@ func (m *MemoryIdx) AddOrUpdate(mkey schema.MKey, data *schema.MetricData, parti
244247
if ok {
245248
oldPart := existing.Partition
246249
log.Debug("metricDef with id %s already in index.", mkey)
247-
existing.LastUpdate = data.Time
250+
if existing.LastUpdate < int64(data.Time) {
251+
existing.LastUpdate = int64(data.Time)
252+
}
248253
existing.Partition = partition
249254
statUpdate.Inc()
250255
statUpdateDuration.Value(time.Since(pre))
@@ -831,8 +836,9 @@ func (m *MemoryIdx) FindByTag(orgId uint32, expressions []string, from int64) ([
831836
m.RLock()
832837
defer m.RUnlock()
833838

839+
// construct the output slice of idx.Node's such that there is only 1 idx.Node for each path
834840
ids := m.idsByTagQuery(orgId, query)
835-
res := make([]idx.Node, 0, len(ids))
841+
byPath := make(map[string]*idx.Node)
836842
for id := range ids {
837843
def, ok := m.defById[id]
838844
if !ok {
@@ -841,14 +847,25 @@ func (m *MemoryIdx) FindByTag(orgId uint32, expressions []string, from int64) ([
841847
continue
842848
}
843849

844-
res = append(res, idx.Node{
845-
Path: def.NameWithTags(),
846-
Leaf: true,
847-
HasChildren: false,
848-
Defs: []idx.Archive{*def},
849-
})
850+
if existing, ok := byPath[def.NameWithTags()]; !ok {
851+
byPath[def.NameWithTags()] = &idx.Node{
852+
Path: def.NameWithTags(),
853+
Leaf: true,
854+
HasChildren: false,
855+
Defs: []idx.Archive{*def},
856+
}
857+
} else {
858+
existing.Defs = append(existing.Defs, *def)
859+
}
860+
}
861+
862+
results := make([]idx.Node, 0, len(byPath))
863+
864+
for _, v := range byPath {
865+
results = append(results, *v)
850866
}
851-
return res, nil
867+
868+
return results, nil
852869
}
853870

854871
func (m *MemoryIdx) idsByTagQuery(orgId uint32, query TagQuery) IdSet {
@@ -877,11 +894,13 @@ func (m *MemoryIdx) Find(orgId uint32, pattern string, from int64) ([]idx.Node,
877894
}
878895
log.Debug("memory-idx: %d nodes matching pattern %s found", len(matchedNodes), pattern)
879896
results := make([]idx.Node, 0)
880-
seen := make(map[string]struct{})
897+
byPath := make(map[string]struct{})
898+
// construct the output slice of idx.Node's such that there is only 1 idx.Node
899+
// for each path, and it holds all defs that the Node refers too.
881900
// if there are public (orgId OrgIdPublic) and private leaf nodes with the same series
882901
// path, then the public metricDefs will be excluded.
883902
for _, n := range matchedNodes {
884-
if _, ok := seen[n.Path]; !ok {
903+
if _, ok := byPath[n.Path]; !ok {
885904
idxNode := idx.Node{
886905
Path: n.Path,
887906
Leaf: n.Leaf(),
@@ -904,7 +923,7 @@ func (m *MemoryIdx) Find(orgId uint32, pattern string, from int64) ([]idx.Node,
904923
}
905924
}
906925
results = append(results, idxNode)
907-
seen[n.Path] = struct{}{}
926+
byPath[n.Path] = struct{}{}
908927
} else {
909928
log.Debug("memory-idx: path %s already seen", n.Path)
910929
}
@@ -914,6 +933,7 @@ func (m *MemoryIdx) Find(orgId uint32, pattern string, from int64) ([]idx.Node,
914933
return results, nil
915934
}
916935

936+
// find returns all Nodes matching the pattern for the given orgId
917937
func (m *MemoryIdx) find(orgId uint32, pattern string) ([]*Node, error) {
918938
tree, ok := m.tree[orgId]
919939
if !ok {

idx/memory/memory_test.go

+20-1
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,20 @@ func testGetAddKey(t *testing.T) {
126126
So(defs, ShouldHaveLength, 15)
127127
})
128128
})
129+
130+
if TagSupport {
131+
Convey("When adding metricDefs with the same series name as existing metricDefs (tagged)", t, func() {
132+
Convey("then findByTag", func() {
133+
nodes, err := ix.FindByTag(1, []string{"name!="}, 0)
134+
So(err, ShouldBeNil)
135+
defs := make([]idx.Archive, 0, len(nodes))
136+
for i := range nodes {
137+
defs = append(defs, nodes[i].Defs...)
138+
}
139+
So(defs, ShouldHaveLength, 2*len(org1Series))
140+
})
141+
})
142+
}
129143
}
130144

131145
func TestFind(t *testing.T) {
@@ -673,7 +687,12 @@ func TestPruneTaggedSeriesWithCollidingTagSets(t *testing.T) {
673687
Convey("After purge", t, func() {
674688
nodes, err := ix.FindByTag(1, findExpressions, 0)
675689
So(err, ShouldBeNil)
676-
So(nodes, ShouldHaveLength, 2)
690+
So(nodes, ShouldHaveLength, 1)
691+
defs := make([]idx.Archive, 0, len(nodes))
692+
for i := range nodes {
693+
defs = append(defs, nodes[i].Defs...)
694+
}
695+
So(defs, ShouldHaveLength, 2)
677696
})
678697

679698
Convey("When purging newer series", t, func() {

0 commit comments

Comments
 (0)