Skip to content

Commit c563779

Browse files
openshift-merge-robotdtfranz
authored andcommitted
Merge pull request openshift#429 from dtfranz/metrics-map-mutex
OCPBUGS-5523: Catalog, fatal error: concurrent map read and map write Signed-off-by: dtfranz <[email protected]>
1 parent 1770db7 commit c563779

File tree

2 files changed

+72
-18
lines changed
  • staging/operator-lifecycle-manager/pkg/metrics
  • vendor/github.com/operator-framework/operator-lifecycle-manager/pkg/metrics

2 files changed

+72
-18
lines changed

staging/operator-lifecycle-manager/pkg/metrics/metrics.go

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package metrics
22

33
import (
4+
"sync"
45
"time"
56

67
"github.com/prometheus/client_golang/prometheus"
@@ -200,12 +201,37 @@ var (
200201
},
201202
)
202203

203-
// subscriptionSyncCounters keeps a record of the promethues counters emitted by
204-
// Subscription objects. The key of a record is the Subscription name, while the value
205-
// is struct containing label values used in the counter
206-
subscriptionSyncCounters = make(map[string]subscriptionSyncLabelValues)
204+
subscriptionSyncCounters = newSubscriptionSyncCounter()
207205
)
208206

207+
// subscriptionSyncCounter keeps a record of the Prometheus counters emitted by
208+
// Subscription objects. The key of a record is the Subscription name, while the value
209+
// is struct containing label values used in the counter. Read and Write access are
210+
// protected by mutex.
211+
type subscriptionSyncCounter struct {
212+
counters map[string]subscriptionSyncLabelValues
213+
countersLock sync.RWMutex
214+
}
215+
216+
func newSubscriptionSyncCounter() subscriptionSyncCounter {
217+
return subscriptionSyncCounter{
218+
counters: make(map[string]subscriptionSyncLabelValues),
219+
}
220+
}
221+
222+
func (s *subscriptionSyncCounter) setValues(key string, val subscriptionSyncLabelValues) {
223+
s.countersLock.Lock()
224+
defer s.countersLock.Unlock()
225+
s.counters[key] = val
226+
}
227+
228+
func (s *subscriptionSyncCounter) readValues(key string) (subscriptionSyncLabelValues, bool) {
229+
s.countersLock.RLock()
230+
defer s.countersLock.RUnlock()
231+
val, ok := s.counters[key]
232+
return val, ok
233+
}
234+
209235
type subscriptionSyncLabelValues struct {
210236
installedCSV string
211237
pkg string
@@ -281,14 +307,15 @@ func EmitSubMetric(sub *operatorsv1alpha1.Subscription) {
281307
if sub.Spec == nil {
282308
return
283309
}
310+
284311
SubscriptionSyncCount.WithLabelValues(sub.GetName(), sub.Status.InstalledCSV, sub.Spec.Channel, sub.Spec.Package, string(sub.Spec.InstallPlanApproval)).Inc()
285-
if _, present := subscriptionSyncCounters[sub.GetName()]; !present {
286-
subscriptionSyncCounters[sub.GetName()] = subscriptionSyncLabelValues{
312+
if _, present := subscriptionSyncCounters.readValues(sub.GetName()); !present {
313+
subscriptionSyncCounters.setValues(sub.GetName(), subscriptionSyncLabelValues{
287314
installedCSV: sub.Status.InstalledCSV,
288315
pkg: sub.Spec.Package,
289316
channel: sub.Spec.Channel,
290317
approvalStrategy: string(sub.Spec.InstallPlanApproval),
291-
}
318+
})
292319
}
293320
}
294321

@@ -303,7 +330,7 @@ func UpdateSubsSyncCounterStorage(sub *operatorsv1alpha1.Subscription) {
303330
if sub.Spec == nil {
304331
return
305332
}
306-
counterValues := subscriptionSyncCounters[sub.GetName()]
333+
counterValues, _ := subscriptionSyncCounters.readValues(sub.GetName())
307334
approvalStrategy := string(sub.Spec.InstallPlanApproval)
308335

309336
if sub.Spec.Channel != counterValues.channel ||
@@ -319,7 +346,7 @@ func UpdateSubsSyncCounterStorage(sub *operatorsv1alpha1.Subscription) {
319346
counterValues.channel = sub.Spec.Channel
320347
counterValues.approvalStrategy = approvalStrategy
321348

322-
subscriptionSyncCounters[sub.GetName()] = counterValues
349+
subscriptionSyncCounters.setValues(sub.GetName(), counterValues)
323350
}
324351
}
325352

vendor/github.com/operator-framework/operator-lifecycle-manager/pkg/metrics/metrics.go

Lines changed: 36 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)