Skip to content
Open

Cpk #11716

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions server/controller/prometheus/encoder/label.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func newLabel(org *common.ORG, ln *labelName, lv *labelValue) *label {

func (l *label) store(item *metadbmodel.PrometheusLabel) {
nameID, ok1 := l.labelName.getID(item.Name)
valueID, ok2 := l.labelValue.getID(item.Value)
valueID, ok2 := l.labelValue.getIDSafe(item.Value)
if !ok1 || !ok2 {
return
}
Expand Down Expand Up @@ -107,7 +107,7 @@ func (l *label) refresh(args ...interface{}) error {
return scanErr
}
nameID, ok1 := l.labelName.getID(name)
valueID, ok2 := l.labelValue.getID(value)
valueID, ok2 := l.labelValue.getIDSafe(value)
if ok1 && ok2 {
newMap[cache.IDLabelKey{NameID: nameID, ValueID: valueID}] = id
}
Expand All @@ -129,15 +129,13 @@ func (l *label) refresh(args ...interface{}) error {

func (l *label) encode(toAdd []*controller.PrometheusLabelRequest) ([]*controller.PrometheusLabel, error) {
l.lock.Lock()
defer l.lock.Unlock()

resp := make([]*controller.PrometheusLabel, 0)
var dbToAdd []*metadbmodel.PrometheusLabel
for _, item := range toAdd {
n := item.GetName()
v := item.GetValue()
nameID, ok1 := l.labelName.getID(n)
valueID, ok2 := l.labelValue.getID(v)
valueID, ok2 := l.labelValue.getIDSafe(v)
if ok1 && ok2 {
if id, ok := l.getID(cache.IDLabelKey{NameID: nameID, ValueID: valueID}); ok {
resp = append(resp, &controller.PrometheusLabel{
Expand All @@ -153,20 +151,27 @@ func (l *label) encode(toAdd []*controller.PrometheusLabelRequest) ([]*controlle
Value: v,
})
}
l.lock.Unlock()

if len(dbToAdd) == 0 {
return resp, nil
}

err := addBatch(l.org.DB, dbToAdd, l.resourceType)
if err != nil {
log.Errorf("add %s error: %s", l.resourceType, err.Error(), l.org.LogPrefix)
return nil, err
}

l.lock.Lock()
for _, item := range dbToAdd {
l.store(item)
resp = append(resp, &controller.PrometheusLabel{
Name: &item.Name,
Value: &item.Value,
Id: proto.Uint32(uint32(item.ID)),
})

}
l.lock.Unlock()
return resp, nil
}
14 changes: 12 additions & 2 deletions server/controller/prometheus/encoder/label_value.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,6 @@ func (lv *labelValue) refresh(args ...interface{}) error {

func (lv *labelValue) encode(strs []string) ([]*controller.PrometheusLabelValue, error) {
lv.lock.Lock()
defer lv.lock.Unlock()

resp := make([]*controller.PrometheusLabelValue, 0)
dbToAdd := make([]*metadbmodel.PrometheusLabelValue, 0)
for i := range strs {
Expand All @@ -114,6 +112,8 @@ func (lv *labelValue) encode(strs []string) ([]*controller.PrometheusLabelValue,
}
dbToAdd = append(dbToAdd, &metadbmodel.PrometheusLabelValue{Value: str})
}
lv.lock.Unlock()

if len(dbToAdd) == 0 {
return resp, nil
}
Expand All @@ -123,10 +123,13 @@ func (lv *labelValue) encode(strs []string) ([]*controller.PrometheusLabelValue,
log.Errorf("add %s error: %s", lv.resourceType, err.Error(), lv.org.LogPrefix)
return nil, err
}

lv.lock.Lock()
for i := range dbToAdd {
lv.store(dbToAdd[i])
resp = append(resp, &controller.PrometheusLabelValue{Value: &dbToAdd[i].Value, Id: proto.Uint32(uint32(dbToAdd[i].ID))})
}
lv.lock.Unlock()
return resp, nil
}

Expand All @@ -135,6 +138,13 @@ func (lv *labelValue) getID(str string) (int, bool) {
return id, ok
}

// getIDSafe is a thread-safe variant for callers that do not already hold lv.lock.
func (lv *labelValue) getIDSafe(str string) (int, bool) {
lv.lock.Lock()
defer lv.lock.Unlock()
return lv.getID(str)
}

func (lv *labelValue) store(item *metadbmodel.PrometheusLabelValue) {
lv.strToID[item.Value] = item.ID

Expand Down
4 changes: 2 additions & 2 deletions server/controller/prometheus/label.go
Original file line number Diff line number Diff line change
Expand Up @@ -329,10 +329,10 @@ func (s *LabelSynchronizer) assembleMetricLabel(mls []*trident.MetricLabelReques
log.Errorf("metric name id not found, name to request count: %+v", nonMetricNameToCount, s.org.LogPrefix)
}
if nonLabelNames.Cardinality() > 0 {
log.Errorf("label name id not found, names: %v", nonLabelNames.ToSlice(), s.org.LogPrefix)
log.Errorf("label name id not found, %s", logNotFoundDetail(nonLabelNames.ToSlice()), s.org.LogPrefix)
}
if nonLabelValues.Cardinality() > 0 {
log.Errorf("label value id not found, values: %v", nonLabelValues.ToSlice(), s.org.LogPrefix)
log.Errorf("label value id not found, %s", logNotFoundDetail(nonLabelValues.ToSlice()), s.org.LogPrefix)
}
return respMLs, nil
}
Expand Down
Loading