Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(collector): fix the concurrency problem caused by multiple execut… #1859

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
56 changes: 35 additions & 21 deletions collector/metrics/metric_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"io/ioutil"
"math"
"net/http"
"sync"
"time"

"github.com/prometheus/client_golang/prometheus"
Expand All @@ -49,6 +50,10 @@ type Metric struct {

type Metrics []Metric

// Only initialize once.
var hasInit = false
var initLock sync.Mutex

var GaugeMetricsMap map[string]prometheus.GaugeVec
var CounterMetricsMap map[string]prometheus.CounterVec
var SummaryMetricsMap map[string]prometheus.Summary
Expand All @@ -63,14 +68,15 @@ func NewMetricCollector(
role string,
detectInterval time.Duration,
detectTimeout time.Duration) MetricCollector {
GaugeMetricsMap = make(map[string]prometheus.GaugeVec, 128)
CounterMetricsMap = make(map[string]prometheus.CounterVec, 128)
SummaryMetricsMap = make(map[string]prometheus.Summary, 128)
TableNameByID = make(map[string]string, 128)

var collector = Collector{detectInterval: detectInterval, detectTimeout: detectTimeout, role: role}
collector.initMetrics()
return &collector
if !hasInit {
initLock.Lock()
if !hasInit {
hasInit = true
initMetrics()
}
initLock.Unlock()
}
return &Collector{detectInterval: detectInterval, detectTimeout: detectTimeout, role: role}
}

type Collector struct {
Expand Down Expand Up @@ -117,19 +123,8 @@ func getReplicaAddrs() ([]string, error) {
return rserverAddrs, nil
}

// Register all metrics.
func (collector *Collector) initMetrics() {
var addrs []string
var err error
if collector.role == MetaServer {
addrs = viper.GetStringSlice("meta_servers")
} else {
addrs, err = getReplicaAddrs()
if err != nil {
log.Errorf("Get replica server address failed, err: %s", err)
return
}
}
// Get all metrics of meta-server and replica-server by their addrs
func getAllMetricsByAddrs(addrs []string) {
for _, addr := range addrs {
data, err := getOneServerMetrics(addr)
if err != nil {
Expand Down Expand Up @@ -181,6 +176,25 @@ func (collector *Collector) initMetrics() {
}
}

// Register all metrics.
func initMetrics() {
GaugeMetricsMap = make(map[string]prometheus.GaugeVec, 256)
CounterMetricsMap = make(map[string]prometheus.CounterVec, 256)
SummaryMetricsMap = make(map[string]prometheus.Summary, 256)
TableNameByID = make(map[string]string, 256)
Comment on lines +181 to +184
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we define these as the members of struct rather than the global variables ?


var addrs []string
addrs = viper.GetStringSlice("meta_servers")
replicAddrs, err := getReplicaAddrs()
if err != nil {
log.Errorf("Get raw metrics from %s failed, err: %s", replicAddrs, err)
return
}
addrs = append(addrs, replicAddrs...)

getAllMetricsByAddrs(addrs)
}

// Parse metric data and update metrics.
func (collector *Collector) processAllServerMetrics() {
var addrs []string
Expand Down