From 303958bb85127ea23e7ae77d597838b5780d7a8f Mon Sep 17 00:00:00 2001 From: "Xin.Zh" Date: Sun, 24 Apr 2022 23:50:52 +0800 Subject: [PATCH 1/2] Revert "clean code (#1858)" (#1860) This reverts commit 8bea4b3e1607e13ed88b55300599e43274b6022f. --- metrics/prometheus/reporter.go | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/metrics/prometheus/reporter.go b/metrics/prometheus/reporter.go index 57df384553..845f6a8fc2 100644 --- a/metrics/prometheus/reporter.go +++ b/metrics/prometheus/reporter.go @@ -30,6 +30,7 @@ import ( ocprom "contrib.go.opencensus.io/exporter/prometheus" "github.com/prometheus/client_golang/prometheus" + prom "github.com/prometheus/client_golang/prometheus" ) import ( @@ -216,9 +217,9 @@ func newPrometheusReporter(reporterConfig *metrics.ReporterConfig) metrics.Repor providerRTGaugeVec: newGaugeVec(providerPrefix+serviceKey+rtSuffix, reporterConfig.Namespace, labelNames), } - prometheus.DefaultRegisterer.MustRegister(reporterInstance.consumerRTGaugeVec, reporterInstance.providerRTGaugeVec) + prom.DefaultRegisterer.MustRegister(reporterInstance.consumerRTGaugeVec, reporterInstance.providerRTGaugeVec) metricsExporter, err := ocprom.NewExporter(ocprom.Options{ - Registry: prometheus.DefaultRegisterer.(*prometheus.Registry), + Registry: prom.DefaultRegisterer.(*prom.Registry), }) if err != nil { logger.Errorf("new prometheus reporter with error = %s", err) @@ -249,7 +250,7 @@ func (reporter *PrometheusReporter) setGauge(gaugeName string, toSetValue float6 // gauge if val, exist := reporter.userGauge.Load(gaugeName); !exist { newGauge := newGauge(gaugeName, reporter.namespace) - _ = prometheus.DefaultRegisterer.Register(newGauge) + _ = prom.DefaultRegisterer.Register(newGauge) reporter.userGauge.Store(gaugeName, newGauge) newGauge.Set(toSetValue) @@ -266,7 +267,7 @@ func (reporter *PrometheusReporter) setGauge(gaugeName string, toSetValue float6 keyList = append(keyList, k) } newGaugeVec := newGaugeVec(gaugeName, reporter.namespace, keyList) - _ = prometheus.DefaultRegisterer.Register(newGaugeVec) + _ = prom.DefaultRegisterer.Register(newGaugeVec) reporter.userGaugeVec.Store(gaugeName, newGaugeVec) newGaugeVec.With(labelMap).Set(toSetValue) } else { @@ -281,7 +282,7 @@ func (reporter *PrometheusReporter) incCounter(counterName string, labelMap prom // counter if val, exist := reporter.userCounter.Load(counterName); !exist { newCounter := newCounter(counterName, reporter.namespace) - _ = prometheus.DefaultRegisterer.Register(newCounter) + _ = prom.DefaultRegisterer.Register(newCounter) reporter.userCounter.Store(counterName, newCounter) newCounter.Inc() } else { @@ -297,7 +298,7 @@ func (reporter *PrometheusReporter) incCounter(counterName string, labelMap prom keyList = append(keyList, k) } newCounterVec := newCounterVec(counterName, reporter.namespace, keyList) - _ = prometheus.DefaultRegisterer.Register(newCounterVec) + _ = prom.DefaultRegisterer.Register(newCounterVec) reporter.userCounterVec.Store(counterName, newCounterVec) newCounterVec.With(labelMap).Inc() } else { @@ -312,7 +313,7 @@ func (reporter *PrometheusReporter) incSummary(summaryName string, toSetValue fl // summary if val, exist := reporter.userSummary.Load(summaryName); !exist { newSummary := newSummary(summaryName, reporter.namespace) - _ = prometheus.DefaultRegisterer.Register(newSummary) + _ = prom.DefaultRegisterer.Register(newSummary) reporter.userSummary.Store(summaryName, newSummary) newSummary.Observe(toSetValue) } else { @@ -328,7 +329,7 @@ func (reporter *PrometheusReporter) incSummary(summaryName string, toSetValue fl keyList = append(keyList, k) } newSummaryVec := newSummaryVec(summaryName, reporter.namespace, keyList) - _ = prometheus.DefaultRegisterer.Register(newSummaryVec) + _ = prom.DefaultRegisterer.Register(newSummaryVec) reporter.userSummaryVec.Store(summaryName, newSummaryVec) newSummaryVec.With(labelMap).Observe(toSetValue) } else { From 6e2c29003d6f3de93acbb49ec43db3cdadd5d4f9 Mon Sep 17 00:00:00 2001 From: codingprh Date: Mon, 9 May 2022 11:39:29 +0800 Subject: [PATCH 2/2] feat: leastActiveLoadBalance code optimization, according to dubbo-java optimization records. For example: https://github.com/apache/dubbo/pull/2962 https://github.com/apache/dubbo/pull/5909 --- .../loadbalance/leastactive/loadbalance.go | 29 ++++++++++--------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/cluster/loadbalance/leastactive/loadbalance.go b/cluster/loadbalance/leastactive/loadbalance.go index 5fe23ea1a3..dc52011bee 100644 --- a/cluster/loadbalance/leastactive/loadbalance.go +++ b/cluster/loadbalance/leastactive/loadbalance.go @@ -57,12 +57,13 @@ func (lb *leastActiveLoadBalance) Select(invokers []protocol.Invoker, invocation } var ( - leastActive int32 = -1 // The least active value of all invokers - totalWeight int64 // The number of invokers having the same least active value (LEAST_ACTIVE) - firstWeight int64 // Initial value, used for comparison - leastCount int // The number of invokers having the same least active value (LEAST_ACTIVE) - leastIndexes = make([]int, count) // The index of invokers having the same least active value (LEAST_ACTIVE) - sameWeight = true // Every invoker has the same weight value? + leastActive int32 = -1 // The least active value of all invokers + totalWeight int64 // The number of invokers having the same least active value (LEAST_ACTIVE) + firstWeight int64 // Initial value, used for comparison + leastCount int // The number of invokers having the same least active value (LEAST_ACTIVE) + leastIndexes = make([]int, count) // The index of invokers having the same least active value (LEAST_ACTIVE) + sameWeight = true // Every invoker has the same weight value? + weights = make([]int64, count) // The weight of every invokers ) for i := 0; i < count; i++ { @@ -70,21 +71,23 @@ func (lb *leastActiveLoadBalance) Select(invokers []protocol.Invoker, invocation // Active number active := protocol.GetMethodStatus(invoker.GetURL(), invocation.MethodName()).GetActive() // current weight (maybe in warmUp) - weight := loadbalance.GetWeight(invoker, invocation) + afterWarmup := loadbalance.GetWeight(invoker, invocation) + // save for later use + weights[i] = afterWarmup // There are smaller active services if leastActive == -1 || active < leastActive { leastActive = active leastIndexes[0] = i leastCount = 1 // next available leastIndex offset - totalWeight = weight - firstWeight = weight + totalWeight = afterWarmup + firstWeight = afterWarmup sameWeight = true } else if active == leastActive { leastIndexes[leastCount] = i - totalWeight += weight + totalWeight += afterWarmup leastCount++ - if sameWeight && (i > 0) && weight != firstWeight { + if sameWeight && afterWarmup != firstWeight { sameWeight = false } } @@ -98,8 +101,8 @@ func (lb *leastActiveLoadBalance) Select(invokers []protocol.Invoker, invocation offsetWeight := rand.Int63n(totalWeight) + 1 for i := 0; i < leastCount; i++ { leastIndex := leastIndexes[i] - offsetWeight -= loadbalance.GetWeight(invokers[i], invocation) - if offsetWeight <= 0 { + offsetWeight -= weights[leastIndex] + if offsetWeight < 0 { return invokers[leastIndex] } }