Skip to content

Commit

Permalink
[CONTROLLER/PROMETHEUS] fixes creating target bug
Browse files Browse the repository at this point in the history
  • Loading branch information
zhengya committed Sep 8, 2023
1 parent 5ef4c6d commit 6dcc48d
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 30 deletions.
6 changes: 3 additions & 3 deletions server/controller/prometheus/encoder/id_allocator.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@ func newIDAllocator(resourceType string, min, max int) idAllocator {

func (ia *idAllocator) allocate(count int) (ids []int, err error) {
if len(ia.usableIDs) == 0 {
return nil, errors.New(fmt.Sprintf("%s has no more usable ids", ia.resourceType))
return nil, errors.New(fmt.Sprintf("%s has no more usable ids, usable ids count: 0", ia.resourceType))
}

if len(ia.usableIDs) < count {
return nil, errors.New(fmt.Sprintf("%s has no more usable ids", ia.resourceType))
return nil, errors.New(fmt.Sprintf("%s has no more usable ids, usable ids count: %d, except ids count: %d", ia.resourceType, len(ia.usableIDs), count))
}

ids = make([]int, count)
Expand All @@ -70,7 +70,7 @@ func (ia *idAllocator) allocate(count int) (ids []int, err error) {
return
}
if len(inUseIDs) != 0 {
return nil, errors.New(fmt.Sprintf("%s some ids are in use", ia.resourceType))
return nil, errors.New(fmt.Sprintf("%s ids: %v are in use", ia.resourceType, inUseIDs))
}

log.Infof("allocate %s ids: %v (expected count: %d, true count: %d)", ia.resourceType, ids, count, len(ids))
Expand Down
29 changes: 15 additions & 14 deletions server/controller/prometheus/encoder/label_layout.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package encoder

import (
"fmt"
"sync"

mapset "github.com/deckarep/golang-set/v2"
Expand All @@ -36,14 +37,14 @@ type indexAllocator struct {
}

func newIndexAllocator(metricName string, max int) *indexAllocator {
ln := &indexAllocator{
resourceType: "app_label_index",
ia := &indexAllocator{
resourceType: fmt.Sprintf("%s app_label_index", metricName),
metricName: metricName,
strToIdx: make(map[string]int),
}
ln.ascIDAllocator = newAscIDAllocator(ln.resourceType, 1, max)
ln.rawDataProvider = ln
return ln
ia.ascIDAllocator = newAscIDAllocator(ia.resourceType, 1, max)
ia.rawDataProvider = ia
return ia
}

func (ia *indexAllocator) refresh(labelNameToIdx map[string]int) error {
Expand Down Expand Up @@ -101,7 +102,7 @@ func (ia *indexAllocator) encode(strs []string) ([]*controller.PrometheusMetricA

func (ia *indexAllocator) check(ids []int) (inUseIDs []int, err error) {
var dbItems []*mysql.PrometheusMetricAPPLabelLayout
err = mysql.Db.Where("metric_name = ? AND id IN (?)", ia.metricName, ids).Find(&dbItems).Error
err = mysql.Db.Where("metric_name = ? AND app_label_column_index IN (?)", ia.metricName, ids).Find(&dbItems).Error
if err != nil {
log.Errorf("db query %s failed: %v", ia.resourceType, err)
return
Expand Down Expand Up @@ -156,12 +157,12 @@ func (ll *labelLayout) refresh(args ...interface{}) error {
}

for mn, lnToIdx := range mnToLnIdx {
ll.metricNameToIdxAllocator[mn], _ = ll.createIndexAllocatorIfNotExists(mn)
ll.metricNameToIdxAllocator[mn].refresh(lnToIdx)
ia, _ := ll.createIndexAllocatorIfNotExists(mn)
ia.refresh(lnToIdx)
}
for mn := range ll.metricNameToIdxAllocator {
for mn, ia := range ll.metricNameToIdxAllocator {
if _, ok := mnToLnIdx[mn]; !ok {
ll.metricNameToIdxAllocator[mn].refresh(make(map[string]int))
ia.refresh(make(map[string]int))
}
}
return nil
Expand Down Expand Up @@ -194,8 +195,8 @@ func (ll *labelLayout) createIndexAllocatorIfNotExists(metricName string) (*inde
if allocator, ok := ll.metricNameToIdxAllocator[metricName]; ok {
return allocator, nil
}
allocator := newIndexAllocator(metricName, ll.appLabelIndexMax)
return allocator, nil
ll.metricNameToIdxAllocator[metricName] = newIndexAllocator(metricName, ll.appLabelIndexMax)
return ll.metricNameToIdxAllocator[metricName], nil
}

func (ll *labelLayout) getIndexAllocator(metricName string) (*indexAllocator, bool) {
Expand All @@ -207,8 +208,8 @@ func (ll *labelLayout) getIndexAllocator(metricName string) (*indexAllocator, bo

func (ll *labelLayout) SingleEncode(metricName string, labelNames []string) ([]*controller.PrometheusMetricAPPLabelLayout, error) {
log.Infof("encode metric: %s app label names: %v", metricName, labelNames)
ll.metricNameToIdxAllocator[metricName], _ = ll.createIndexAllocatorIfNotExists(metricName)
return ll.metricNameToIdxAllocator[metricName].encode(labelNames)
ia, _ := ll.createIndexAllocatorIfNotExists(metricName)
return ia.encode(labelNames)
}

func (ll *labelLayout) SingleRelease(metricName string, indexes []int) error {
Expand Down
2 changes: 1 addition & 1 deletion server/controller/prometheus/encoder/target.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func (ln *target) encode(ts []*controller.PrometheusTargetRequest) ([]*controlle

func (ln *target) load() (ids mapset.Set[int], err error) {
var items []*mysql.PrometheusTarget
err = mysql.Db.Where(&mysql.PrometheusTarget{CreateMethod: common.PROMETHEUS_TARGET_CREATE_METHOD_PROMETHEUS}).Find(&items).Error
err = mysql.Db.Unscoped().Where(&mysql.PrometheusTarget{CreateMethod: common.PROMETHEUS_TARGET_CREATE_METHOD_PROMETHEUS}).Find(&items).Error
if err != nil {
log.Errorf("db query %s failed: %v", ln.resourceType, err)
return nil, err
Expand Down
18 changes: 8 additions & 10 deletions server/controller/prometheus/label.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,22 +110,16 @@ func (s *LabelSynchronizer) generateDataToEncode(req *trident.PrometheusLabelReq
toEncode := newDataToEncode()
for _, m := range req.GetRequestLabels() {
mn := m.GetMetricName()
if mn == "" {
continue
}
targetKey, targetID, ok := s.getTargetInfoFromLabels(m.GetLabels())
toEncode.tryAppendMetricName(mn)
if ok {
targetKey, targetID, targetExists := s.getTargetInfoFromLabels(m.GetLabels())
if targetExists {
toEncode.tryAppendMetricTarget(mn, targetID)
} else {
toEncode.appendMetricTarget(mn, targetKey)
}

for _, l := range m.GetLabels() {
ln := l.GetName()
if ln == "" {
continue
}
lv := l.GetValue()
toEncode.tryAppendLabelName(ln)
toEncode.tryAppendLabelValue(lv)
Expand All @@ -134,7 +128,7 @@ func (s *LabelSynchronizer) generateDataToEncode(req *trident.PrometheusLabelReq
if ln == TargetLabelInstance || ln == TargetLabelJob {
continue
}
if ok {
if targetExists {
toEncode.tryAppendMetricAPPLabelLayout(mn, ln)
} else {
toEncode.appendMetricAPPLabelLayout(mn, ln)
Expand Down Expand Up @@ -241,15 +235,19 @@ func (s *LabelSynchronizer) generateSyncRequest(toEncode *dataToEncode) *control

func (s *LabelSynchronizer) getTargetInfoFromLabels(labels []*trident.LabelRequest) (cache.TargetKey, int, bool) {
var instanceValue string
var insGetted bool
var jobValue string
var jobGetted bool
for _, l := range labels {
ln := l.GetName()
if ln == TargetLabelInstance {
instanceValue = l.GetValue()
insGetted = true
} else if ln == TargetLabelJob {
jobValue = l.GetValue()
jobGetted = true
}
if instanceValue != "" && jobValue != "" {
if insGetted && jobGetted {
break
}
}
Expand Down
4 changes: 2 additions & 2 deletions server/controller/prometheus/updater.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func (au *APPLabelLayoutUpdater) refresh() error {
if appLabelNamesToEncode := newAPPLabelNames.Difference(oldAPPLabelNames).ToSlice(); len(appLabelNamesToEncode) > 0 {
_, err = au.encoder.LabelLayout.SingleEncode(mn, appLabelNamesToEncode)
if err != nil {
log.Debugf("encode metric: %s labels: %#v failed: %s", mn, appLabelNamesToEncode, err.Error())
log.Debugf("encode metric: %s labels: %v failed: %s", mn, appLabelNamesToEncode, err.Error())
}
}

Expand All @@ -117,7 +117,7 @@ func (au *APPLabelLayoutUpdater) refresh() error {
if len(indexesToRecycle) > 0 {
err = au.encoder.LabelLayout.SingleRelease(mn, indexesToRecycle)
if err != nil {
log.Debugf("recycle metric: %s label indexes: %#v failed: %s", mn, indexesToRecycle, err.Error())
log.Debugf("recycle metric: %s label indexes: %v failed: %s", mn, indexesToRecycle, err.Error())
}
}
}
Expand Down

0 comments on commit 6dcc48d

Please sign in to comment.