Skip to content

Commit f8aa0ca

Browse files
ZhengYa-0110SongZhen0704
authored andcommitted
feat: fix promethous encode only appending no delete bug
1 parent f11c981 commit f8aa0ca

2 files changed

Lines changed: 58 additions & 20 deletions

File tree

server/controller/prometheus/encoder/label.go

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,36 +31,56 @@ type label struct {
3131
org *common.ORG
3232
lock sync.Mutex
3333
resourceType string
34-
labelKeyToID sync.Map
34+
labelKeyToID map[cache.LabelKey]int
3535
}
3636

3737
func newLabel(org *common.ORG) *label {
3838
return &label{
3939
org: org,
4040
resourceType: "label",
41+
labelKeyToID: make(map[cache.LabelKey]int),
4142
}
4243
}
4344

4445
func (l *label) store(item *metadbmodel.PrometheusLabel) {
45-
l.labelKeyToID.Store(cache.NewLabelKey(item.Name, item.Value), item.ID)
46+
l.labelKeyToID[cache.NewLabelKey(item.Name, item.Value)] = item.ID
4647
}
4748

4849
func (l *label) getID(key cache.LabelKey) (int, bool) {
49-
if item, ok := l.labelKeyToID.Load(key); ok {
50-
return item.(int), true
51-
}
52-
return 0, false
50+
id, ok := l.labelKeyToID[key]
51+
return id, ok
5352
}
5453

5554
func (l *label) refresh(args ...interface{}) error {
55+
// Snapshot existing keys before querying DB, to identify entries added
56+
// by encode() during the query window (those must be preserved even if
57+
// not yet visible in the DB snapshot).
58+
l.lock.Lock()
59+
preKeys := make(map[cache.LabelKey]struct{}, len(l.labelKeyToID))
60+
for k := range l.labelKeyToID {
61+
preKeys[k] = struct{}{}
62+
}
63+
l.lock.Unlock()
64+
5665
var items []*metadbmodel.PrometheusLabel
57-
err := l.org.DB.Find(&items).Error
58-
if err != nil {
66+
if err := l.org.DB.Select("id", "name", "value").Find(&items).Error; err != nil {
5967
return err
6068
}
69+
newMap := make(map[cache.LabelKey]int, len(items))
6170
for _, item := range items {
62-
l.store(item)
71+
newMap[cache.NewLabelKey(item.Name, item.Value)] = item.ID
72+
}
73+
74+
l.lock.Lock()
75+
for k, v := range l.labelKeyToID {
76+
if _, wasInSnapshot := preKeys[k]; !wasInSnapshot {
77+
// Written by encode() after the snapshot; may not be in the DB
78+
// snapshot yet, so preserve it to avoid a spurious cache miss.
79+
newMap[k] = v
80+
}
6381
}
82+
l.labelKeyToID = newMap
83+
l.lock.Unlock()
6484
return nil
6585
}
6686

server/controller/prometheus/encoder/label_value.go

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ package encoder
1919
import (
2020
"sync"
2121

22-
cmap "github.com/orcaman/concurrent-map/v2"
2322
"google.golang.org/protobuf/proto"
2423

2524
"github.com/deepflowio/deepflow/message/controller"
@@ -31,27 +30,48 @@ type labelValue struct {
3130
org *common.ORG
3231
lock sync.Mutex
3332
resourceType string
34-
strToID cmap.ConcurrentMap[string, int]
33+
strToID map[string]int
3534
}
3635

3736
func newLabelValue(org *common.ORG) *labelValue {
3837
return &labelValue{
3938
org: org,
4039
resourceType: "label_value",
41-
strToID: cmap.New[int](),
40+
strToID: make(map[string]int),
4241
}
4342
}
4443

4544
func (lv *labelValue) refresh(args ...interface{}) error {
45+
// Snapshot existing keys before querying DB, to identify entries added
46+
// by encode() during the query window (those must be preserved even if
47+
// not yet visible in the DB snapshot).
48+
lv.lock.Lock()
49+
preKeys := make(map[string]struct{}, len(lv.strToID))
50+
for k := range lv.strToID {
51+
preKeys[k] = struct{}{}
52+
}
53+
lv.lock.Unlock()
54+
4655
var items []*metadbmodel.PrometheusLabelValue
47-
err := lv.org.DB.Unscoped().Find(&items).Error
48-
if err != nil {
56+
if err := lv.org.DB.Select("id", "value").Find(&items).Error; err != nil {
4957
log.Errorf("db query %s failed: %v", lv.resourceType, err, lv.org.LogPrefix)
5058
return err
5159
}
60+
newMap := make(map[string]int, len(items))
5261
for _, item := range items {
53-
lv.store(item)
62+
newMap[item.Value] = item.ID
5463
}
64+
65+
lv.lock.Lock()
66+
for k, v := range lv.strToID {
67+
if _, wasInSnapshot := preKeys[k]; !wasInSnapshot {
68+
// Written by encode() after the snapshot; may not be in the DB
69+
// snapshot yet, so preserve it to avoid a spurious cache miss.
70+
newMap[k] = v
71+
}
72+
}
73+
lv.strToID = newMap
74+
lv.lock.Unlock()
5575
return nil
5676
}
5777

@@ -86,12 +106,10 @@ func (lv *labelValue) encode(strs []string) ([]*controller.PrometheusLabelValue,
86106
}
87107

88108
func (lv *labelValue) getID(str string) (int, bool) {
89-
if item, ok := lv.strToID.Get(str); ok {
90-
return item, true
91-
}
92-
return 0, false
109+
id, ok := lv.strToID[str]
110+
return id, ok
93111
}
94112

95113
func (lv *labelValue) store(item *metadbmodel.PrometheusLabelValue) {
96-
lv.strToID.Set(item.Value, item.ID)
114+
lv.strToID[item.Value] = item.ID
97115
}

0 commit comments

Comments
 (0)