From d8aa433c4d195116ccc81510cbe9662352e3d856 Mon Sep 17 00:00:00 2001
From: Aliaksandr Valialkin <valyala@gmail.com>
Date: Fri, 7 Aug 2020 13:02:25 +0300
Subject: [PATCH] vendor: update github.com/VictoriaMetrics/metrics from
 v1.12.2 to v1.12.3

---
 go.mod                                        |  2 +-
 go.sum                                        |  2 +
 .../github.com/VictoriaMetrics/metrics/set.go | 64 +++++++++++--------
 .../VictoriaMetrics/metrics/summary.go        |  2 +-
 vendor/modules.txt                            |  2 +-
 5 files changed, 44 insertions(+), 28 deletions(-)

diff --git a/go.mod b/go.mod
index c6cf084c1b..ac8458b157 100644
--- a/go.mod
+++ b/go.mod
@@ -7,7 +7,7 @@ require (
 	// Do not use the original github.com/valyala/fasthttp because of issues
 	// like https://github.com/valyala/fasthttp/commit/996610f021ff45fdc98c2ce7884d5fa4e7f9199b
 	github.com/VictoriaMetrics/fasthttp v1.0.4
-	github.com/VictoriaMetrics/metrics v1.12.2
+	github.com/VictoriaMetrics/metrics v1.12.3
 	github.com/VictoriaMetrics/metricsql v0.4.1
 	github.com/aws/aws-sdk-go v1.33.19
 	github.com/cespare/xxhash/v2 v2.1.1
diff --git a/go.sum b/go.sum
index 07a077c17b..68cffbaf20 100644
--- a/go.sum
+++ b/go.sum
@@ -51,6 +51,8 @@ github.com/VictoriaMetrics/fasthttp v1.0.4 h1:Aw6UqmPc0v5PunYOpiiyf4hk5B9PTMswdT
 github.com/VictoriaMetrics/fasthttp v1.0.4/go.mod h1:m5wCmg1dJN6s1B/lp8/dcKrnnM2l3DWB2eAGZGCTK3g=
 github.com/VictoriaMetrics/metrics v1.12.2 h1:SG8iAmqavDNuh7GIdHPoGHUhDL23KeKfvSZSozucNeA=
 github.com/VictoriaMetrics/metrics v1.12.2/go.mod h1:Z1tSfPfngDn12bTfZSCqArT3OPY3u88J12hSoOhuiRE=
+github.com/VictoriaMetrics/metrics v1.12.3 h1:Fe6JHC6MSEKa+BtLhPN8WIvS+HKPzMc2evEpNeCGy7I=
+github.com/VictoriaMetrics/metrics v1.12.3/go.mod h1:Z1tSfPfngDn12bTfZSCqArT3OPY3u88J12hSoOhuiRE=
 github.com/VictoriaMetrics/metricsql v0.4.1 h1:WbVIfRNCK7HjrzayrpAl07mkh4kiDFZuECsh57rly2Q=
 github.com/VictoriaMetrics/metricsql v0.4.1/go.mod h1:ylO7YITho/Iw6P71oEaGyHbO94bGoGtzWfLGqFhMIg8=
 github.com/allegro/bigcache v1.2.1-0.20190218064605-e24eb225f156 h1:eMwmnE/GDgah4HI848JfFxHt+iPb26b4zyfspmqY0/8=
diff --git a/vendor/github.com/VictoriaMetrics/metrics/set.go b/vendor/github.com/VictoriaMetrics/metrics/set.go
index 6d36eede9b..69b4de866f 100644
--- a/vendor/github.com/VictoriaMetrics/metrics/set.go
+++ b/vendor/github.com/VictoriaMetrics/metrics/set.go
@@ -324,13 +324,20 @@ func (s *Set) NewSummary(name string) *Summary {
 //
 // The returned summary is safe to use from concurrent goroutines.
 func (s *Set) NewSummaryExt(name string, window time.Duration, quantiles []float64) *Summary {
+	if err := validateMetric(name); err != nil {
+		panic(fmt.Errorf("BUG: invalid metric name %q: %s", name, err))
+	}
 	sm := newSummary(window, quantiles)
-	s.registerMetric(name, sm)
-	registerSummary(sm)
-	s.registerSummaryQuantiles(name, sm)
+
 	s.mu.Lock()
+	// defer will unlock in case of panic
+	// checks in tests
+	defer s.mu.Unlock()
+
+	s.mustRegisterLocked(name, sm)
+	registerSummaryLocked(sm)
+	s.registerSummaryQuantilesLocked(name, sm)
 	s.summaries = append(s.summaries, sm)
-	s.mu.Unlock()
 	return sm
 }
 
@@ -379,21 +386,17 @@ func (s *Set) GetOrCreateSummaryExt(name string, window time.Duration, quantiles
 			name:   name,
 			metric: sm,
 		}
-		mustRegisterQuantiles := false
 		s.mu.Lock()
 		nm = s.m[name]
 		if nm == nil {
 			nm = nmNew
 			s.m[name] = nm
 			s.a = append(s.a, nm)
-			registerSummary(sm)
-			mustRegisterQuantiles = true
+			registerSummaryLocked(sm)
+			s.registerSummaryQuantilesLocked(name, sm)
 		}
 		s.summaries = append(s.summaries, sm)
 		s.mu.Unlock()
-		if mustRegisterQuantiles {
-			s.registerSummaryQuantiles(name, sm)
-		}
 	}
 	sm, ok := nm.metric.(*Summary)
 	if !ok {
@@ -408,14 +411,14 @@ func (s *Set) GetOrCreateSummaryExt(name string, window time.Duration, quantiles
 	return sm
 }
 
-func (s *Set) registerSummaryQuantiles(name string, sm *Summary) {
+func (s *Set) registerSummaryQuantilesLocked(name string, sm *Summary) {
 	for i, q := range sm.quantiles {
 		quantileValueName := addTag(name, fmt.Sprintf(`quantile="%g"`, q))
 		qv := &quantileValue{
 			sm:  sm,
 			idx: i,
 		}
-		s.registerMetric(quantileValueName, qv)
+		s.mustRegisterLocked(quantileValueName, qv)
 	}
 }
 
@@ -424,6 +427,16 @@ func (s *Set) registerMetric(name string, m metric) {
 		panic(fmt.Errorf("BUG: invalid metric name %q: %s", name, err))
 	}
 	s.mu.Lock()
+	// defer will unlock in case of panic
+	// checks in test
+	defer s.mu.Unlock()
+	s.mustRegisterLocked(name, m)
+}
+
+// mustRegisterLocked registers given metric with
+// the given name. Panics if the given name was
+// already registered before.
+func (s *Set) mustRegisterLocked(name string, m metric) {
 	nm, ok := s.m[name]
 	if !ok {
 		nm = &namedMetric{
@@ -433,7 +446,6 @@ func (s *Set) registerMetric(name string, m metric) {
 		s.m[name] = nm
 		s.a = append(s.a, nm)
 	}
-	s.mu.Unlock()
 	if ok {
 		panic(fmt.Errorf("BUG: metric %q is already registered", name))
 	}
@@ -455,32 +467,34 @@ func (s *Set) UnregisterMetric(name string) bool {
 
 	delete(s.m, name)
 
-	// remove metric from s.a
-	found := false
-	for i, nm := range s.a {
-		if nm.name == name {
-			s.a = append(s.a[:i], s.a[i+1:]...)
-			found = true
-			break
+	deleteFromList := func(metricName string) {
+		for i, nm := range s.a {
+			if nm.name == metricName {
+				s.a = append(s.a[:i], s.a[i+1:]...)
+				return
+			}
 		}
-	}
-	if !found {
 		panic(fmt.Errorf("BUG: cannot find metric %q in the list of registered metrics", name))
 	}
+
+	// remove metric from s.a
+	deleteFromList(name)
+
 	sm, ok := m.(*Summary)
 	if !ok {
-		// There is no need in cleaning up s.summaries.
+		// There is no need in cleaning up summary.
 		return true
 	}
 
-	// Remove summary metric name including quantile labels from set
+	// cleanup registry from per-quantile metrics
 	for _, q := range sm.quantiles {
 		quantileValueName := addTag(name, fmt.Sprintf(`quantile="%g"`, q))
 		delete(s.m, quantileValueName)
+		deleteFromList(quantileValueName)
 	}
 
 	// Remove sm from s.summaries
-	found = false
+	found := false
 	for i, xsm := range s.summaries {
 		if xsm == sm {
 			s.summaries = append(s.summaries[:i], s.summaries[i+1:]...)
diff --git a/vendor/github.com/VictoriaMetrics/metrics/summary.go b/vendor/github.com/VictoriaMetrics/metrics/summary.go
index 6cd278c044..0f01e9ae12 100644
--- a/vendor/github.com/VictoriaMetrics/metrics/summary.go
+++ b/vendor/github.com/VictoriaMetrics/metrics/summary.go
@@ -203,7 +203,7 @@ func addTag(name, tag string) string {
 	return fmt.Sprintf("%s,%s}", name[:len(name)-1], tag)
 }
 
-func registerSummary(sm *Summary) {
+func registerSummaryLocked(sm *Summary) {
 	window := sm.window
 	summariesLock.Lock()
 	summaries[window] = append(summaries[window], sm)
diff --git a/vendor/modules.txt b/vendor/modules.txt
index bca31537b5..99d624f0d0 100644
--- a/vendor/modules.txt
+++ b/vendor/modules.txt
@@ -14,7 +14,7 @@ github.com/VictoriaMetrics/fastcache
 github.com/VictoriaMetrics/fasthttp
 github.com/VictoriaMetrics/fasthttp/fasthttputil
 github.com/VictoriaMetrics/fasthttp/stackless
-# github.com/VictoriaMetrics/metrics v1.12.2
+# github.com/VictoriaMetrics/metrics v1.12.3
 github.com/VictoriaMetrics/metrics
 # github.com/VictoriaMetrics/metricsql v0.4.1
 github.com/VictoriaMetrics/metricsql