Merge pull request #120174 from pohly/apiserver-etcd-metrics-race
apiserver: fix data race in etcd metrics
This commit is contained in:
		@@ -228,7 +228,7 @@ func UpdateEtcdDbSize(ep string, size int64) {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
// SetStorageMonitorGetter sets monitor getter to allow monitoring etcd stats.
 | 
					// SetStorageMonitorGetter sets monitor getter to allow monitoring etcd stats.
 | 
				
			||||||
func SetStorageMonitorGetter(getter func() ([]Monitor, error)) {
 | 
					func SetStorageMonitorGetter(getter func() ([]Monitor, error)) {
 | 
				
			||||||
	storageMonitor.monitorGetter = getter
 | 
						storageMonitor.setGetter(getter)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// UpdateLeaseObjectCount sets the etcd_lease_object_counts metric.
 | 
					// UpdateLeaseObjectCount sets the etcd_lease_object_counts metric.
 | 
				
			||||||
@@ -258,9 +258,22 @@ type StorageMetrics struct {
 | 
				
			|||||||
type monitorCollector struct {
 | 
					type monitorCollector struct {
 | 
				
			||||||
	compbasemetrics.BaseStableCollector
 | 
						compbasemetrics.BaseStableCollector
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						mutex         sync.Mutex
 | 
				
			||||||
	monitorGetter func() ([]Monitor, error)
 | 
						monitorGetter func() ([]Monitor, error)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (m *monitorCollector) setGetter(monitorGetter func() ([]Monitor, error)) {
 | 
				
			||||||
 | 
						m.mutex.Lock()
 | 
				
			||||||
 | 
						defer m.mutex.Unlock()
 | 
				
			||||||
 | 
						m.monitorGetter = monitorGetter
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (m *monitorCollector) getGetter() func() ([]Monitor, error) {
 | 
				
			||||||
 | 
						m.mutex.Lock()
 | 
				
			||||||
 | 
						defer m.mutex.Unlock()
 | 
				
			||||||
 | 
						return m.monitorGetter
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// DescribeWithStability implements compbasemetrics.StableColletor
 | 
					// DescribeWithStability implements compbasemetrics.StableColletor
 | 
				
			||||||
func (c *monitorCollector) DescribeWithStability(ch chan<- *compbasemetrics.Desc) {
 | 
					func (c *monitorCollector) DescribeWithStability(ch chan<- *compbasemetrics.Desc) {
 | 
				
			||||||
	ch <- storageSizeDescription
 | 
						ch <- storageSizeDescription
 | 
				
			||||||
@@ -268,7 +281,7 @@ func (c *monitorCollector) DescribeWithStability(ch chan<- *compbasemetrics.Desc
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
// CollectWithStability implements compbasemetrics.StableColletor
 | 
					// CollectWithStability implements compbasemetrics.StableColletor
 | 
				
			||||||
func (c *monitorCollector) CollectWithStability(ch chan<- compbasemetrics.Metric) {
 | 
					func (c *monitorCollector) CollectWithStability(ch chan<- compbasemetrics.Metric) {
 | 
				
			||||||
	monitors, err := c.monitorGetter()
 | 
						monitors, err := c.getGetter()()
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		return
 | 
							return
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user