You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicecomb.apache.org by GitBox <gi...@apache.org> on 2018/11/28 09:31:53 UTC

[GitHub] asifdxtreme closed pull request #500: SCB-1050 Metrics cache is not clean

asifdxtreme closed pull request #500: SCB-1050 Metrics cache is not clean
URL: https://github.com/apache/servicecomb-service-center/pull/500
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/server/metric/calculator.go b/server/metric/calculator.go
index 7f0a8909..fea89ddb 100644
--- a/server/metric/calculator.go
+++ b/server/metric/calculator.go
@@ -55,14 +55,14 @@ func (c *CommonCalculator) Calc(mf *dto.MetricFamily) *Details {
 func metricGaugeOf(details *Details, m []*dto.Metric) {
 	for _, d := range m {
 		details.Summary += d.GetGauge().GetValue()
-		details.Put(d.GetLabel(), d.GetGauge().GetValue())
+		details.put(d.GetLabel(), d.GetGauge().GetValue())
 	}
 }
 
 func metricCounterOf(details *Details, m []*dto.Metric) {
 	for _, d := range m {
 		details.Summary += d.GetCounter().GetValue()
-		details.Put(d.GetLabel(), d.GetCounter().GetValue())
+		details.put(d.GetLabel(), d.GetCounter().GetValue())
 	}
 }
 
@@ -74,7 +74,7 @@ func metricSummaryOf(details *Details, m []*dto.Metric) {
 	for _, d := range m {
 		count += d.GetSummary().GetSampleCount()
 		sum += d.GetSummary().GetSampleSum()
-		details.Put(d.GetLabel(), d.GetSummary().GetSampleSum()/float64(d.GetSummary().GetSampleCount()))
+		details.put(d.GetLabel(), d.GetSummary().GetSampleSum()/float64(d.GetSummary().GetSampleCount()))
 	}
 
 	if count == 0 {
@@ -92,7 +92,7 @@ func metricHistogramOf(details *Details, m []*dto.Metric) {
 	for _, d := range m {
 		count += d.GetHistogram().GetSampleCount()
 		sum += d.GetHistogram().GetSampleSum()
-		details.Put(d.GetLabel(), d.GetHistogram().GetSampleSum()/float64(d.GetHistogram().GetSampleCount()))
+		details.put(d.GetLabel(), d.GetHistogram().GetSampleSum()/float64(d.GetHistogram().GetSampleCount()))
 	}
 
 	if count == 0 {
diff --git a/server/metric/gatherer.go b/server/metric/gatherer.go
index 52e2a598..48a1eed1 100644
--- a/server/metric/gatherer.go
+++ b/server/metric/gatherer.go
@@ -26,6 +26,7 @@ import (
 	"time"
 )
 
+// Gatherer is the reader of sc metrics
 var Gatherer *MetricsGatherer
 
 func init() {
@@ -83,13 +84,16 @@ func (mm *MetricsGatherer) Collect() error {
 		return err
 	}
 
+	records := NewMetrics()
 	for _, mf := range mfs {
 		name := mf.GetName()
 		if _, ok := SysMetrics.Get(name); strings.Index(name, familyNamePrefix) == 0 || ok {
 			if d := Calculate(mf); d != nil {
-				mm.Records.Put(strings.TrimPrefix(name, familyNamePrefix), d)
+				records.put(strings.TrimPrefix(name, familyNamePrefix), d)
 			}
 		}
 	}
+	// clean the old cache here
+	mm.Records = records
 	return nil
 }
diff --git a/server/metric/metrics.go b/server/metric/metrics.go
index 738b813c..4987d7cc 100644
--- a/server/metric/metrics.go
+++ b/server/metric/metrics.go
@@ -18,20 +18,19 @@ package metric
 
 import (
 	"github.com/apache/servicecomb-service-center/pkg/buffer"
-	"github.com/apache/servicecomb-service-center/pkg/util"
 	dto "github.com/prometheus/client_model/go"
 	"strings"
 )
 
 func NewMetrics() *Metrics {
 	return &Metrics{
-		mapper: util.NewConcurrentMap(0),
+		mapper: make(map[string]*Details),
 	}
 }
 
 func NewDetails() *Details {
 	return &Details{
-		mapper: util.NewConcurrentMap(0),
+		mapper: make(map[string]float64),
 		buffer: buffer.NewPool(bufferSize),
 	}
 }
@@ -41,7 +40,7 @@ type Details struct {
 	// Summary is the calculation results of the details
 	Summary float64
 
-	mapper *util.ConcurrentMap
+	mapper map[string]float64
 	buffer *buffer.Pool
 }
 
@@ -71,52 +70,52 @@ func (cm *Details) toLabels(key string) (p []*dto.LabelPair) {
 }
 
 func (cm *Details) Get(labels []*dto.LabelPair) (val float64) {
-	if v, ok := cm.mapper.Get(cm.toKey(labels)); ok {
-		val = v.(float64)
+	if v, ok := cm.mapper[cm.toKey(labels)]; ok {
+		val = v
 	}
 	return
 }
 
-func (cm *Details) Put(labels []*dto.LabelPair, val float64) {
-	cm.mapper.Put(cm.toKey(labels), val)
+func (cm *Details) put(labels []*dto.LabelPair, val float64) {
+	cm.mapper[cm.toKey(labels)] = val
 	return
 }
 
 func (cm *Details) ForEach(f func(labels []*dto.LabelPair, v float64) (next bool)) {
-	cm.mapper.ForEach(func(item util.MapItem) (next bool) {
-		k, _ := item.Key.(string)
-		v, _ := item.Value.(float64)
-		return f(cm.toLabels(k), v)
-	})
+	for k, v := range cm.mapper {
+		if !f(cm.toLabels(k), v) {
+			break
+		}
+	}
 }
 
 // Metrics is the struct to hold the Details objects store and index by metric name
 type Metrics struct {
-	mapper *util.ConcurrentMap
+	mapper map[string]*Details
 }
 
-func (cm *Metrics) Put(key string, val *Details) {
-	cm.mapper.Put(key, val)
+func (cm *Metrics) put(key string, val *Details) {
+	cm.mapper[key] = val
 }
 
 func (cm *Metrics) Get(key string) (val *Details) {
-	if v, ok := cm.mapper.Get(key); ok {
-		val = v.(*Details)
+	if v, ok := cm.mapper[key]; ok {
+		val = v
 	}
 	return
 }
 
 func (cm *Metrics) ForEach(f func(k string, v *Details) (next bool)) {
-	cm.mapper.ForEach(func(item util.MapItem) (next bool) {
-		k, _ := item.Key.(string)
-		v, _ := item.Value.(*Details)
-		return f(k, v)
-	})
+	for k, v := range cm.mapper {
+		if !f(k, v) {
+			break
+		}
+	}
 }
 
 func (cm *Metrics) Summary(key string) (sum float64) {
-	if v, ok := cm.mapper.Get(key); ok {
-		sum = v.(*Details).Summary
+	if v, ok := cm.mapper[key]; ok {
+		sum = v.Summary
 	}
 	return
 }
diff --git a/server/metric/metrics_test.go b/server/metric/metrics_test.go
index 2f3b4b19..27203697 100644
--- a/server/metric/metrics_test.go
+++ b/server/metric/metrics_test.go
@@ -28,16 +28,16 @@ func TestDetails_ForEach(t *testing.T) {
 		t.Fatalf("TestDetails_ForEach failed")
 		return true
 	})
-	d.Put([]*dto.LabelPair{{Name: &n, Value: &v}, {Name: &n, Value: &v}}, 1)
+	d.put([]*dto.LabelPair{{Name: &n, Value: &v}, {Name: &n, Value: &v}}, 1)
 	d.ForEach(func(labels []*dto.LabelPair, v float64) (next bool) {
 		if len(labels) != 2 || v != 1 {
 			t.Fatalf("TestDetails_ForEach failed")
 		}
 		return true
 	})
-	d.Put([]*dto.LabelPair{{}}, 3)
-	d.Put([]*dto.LabelPair{{}}, 4)
-	d.Put(nil, 2)
+	d.put([]*dto.LabelPair{{}}, 3)
+	d.put([]*dto.LabelPair{{}}, 4)
+	d.put(nil, 2)
 	l := 0
 	d.ForEach(func(labels []*dto.LabelPair, v float64) (next bool) {
 		switch {
@@ -78,7 +78,7 @@ func TestDetails_ForEach(t *testing.T) {
 	if find {
 		t.Fatalf("TestMetrics_ForEach failed")
 	}
-	ms.Put("a", d)
+	ms.put("a", d)
 	if ms.Summary("a") != 0 {
 		t.Fatalf("TestMetrics_Summary failed")
 	}


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services