lib/promscrape/discovery/kubernetes: wait for 10 seconds before checking whether the urlWatcher must be stopped

This should prevent from excess urlWatcher churn on config reload, since it leads to removal of all the apiWatchers
before creating new apiWatchers. So, every config reload would lead to stopping of all the previous urlWatchers
and starting new urlWatchers.

The new logic gives 10 seconds for config reload before stopping unused urlWatchers.

Updates https://github.com/VictoriaMetrics/VictoriaMetrics/issues/4850
Updates https://github.com/VictoriaMetrics/VictoriaMetrics/pull/4861
This commit is contained in:
Aliaksandr Valialkin 2023-09-18 17:45:10 +02:00
parent 76af32d869
commit 03fece44e0
No known key found for this signature in database
GPG key ID: A72BEC6CD3D0DED1

View file

@ -446,11 +446,10 @@ func (gw *groupWatcher) registerPendingAPIWatchers() {
func (gw *groupWatcher) unsubscribeAPIWatcher(aw *apiWatcher) {
gw.mu.Lock()
defer gw.mu.Unlock()
for key, uw := range gw.m {
for _, uw := range gw.m {
uw.unsubscribeAPIWatcherLocked(aw)
if (len(uw.aws) + len(uw.awsPending)) == 0 {
uw.cancel()
delete(gw.m, key)
if len(uw.aws)+len(uw.awsPending) == 0 {
time.AfterFunc(10*time.Second, uw.stopIfNoUsers)
}
}
}
@ -524,6 +523,16 @@ func newURLWatcher(role, apiURL string, gw *groupWatcher) *urlWatcher {
return uw
}
func (uw *urlWatcher) stopIfNoUsers() {
gw := uw.gw
gw.mu.Lock()
if len(uw.aws)+len(uw.awsPending) == 0 {
uw.cancel()
delete(gw.m, uw.apiURL)
}
gw.mu.Unlock()
}
func (uw *urlWatcher) subscribeAPIWatcherLocked(aw *apiWatcher) {
if _, ok := uw.aws[aw]; !ok {
if _, ok := uw.awsPending[aw]; !ok {