Skip to content

Commit c2d135a

Browse files
committed
Add delete back to remove notifier as memory leak will still occur
Signed-off-by: Rohan Gupta <[email protected]>
1 parent e4ee809 commit c2d135a

File tree

2 files changed

+12
-10
lines changed

2 files changed

+12
-10
lines changed

pkg/ruler/manager.go

+11-9
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ func (r *DefaultMultiTenantManager) SyncRuleGroups(ctx context.Context, ruleGrou
107107
go mngr.Stop()
108108
delete(r.userManagers, userID)
109109

110-
r.stopNotifier(userID)
110+
r.removeNotifier(userID)
111111
r.mapper.cleanupUser(userID)
112112
r.lastReloadSuccessful.DeleteLabelValues(userID)
113113
r.lastReloadSuccessfulTimestamp.DeleteLabelValues(userID)
@@ -164,29 +164,31 @@ func (r *DefaultMultiTenantManager) syncRulesToManager(ctx context.Context, user
164164
// newManager creates a prometheus rule manager wrapped with a user id
165165
// configured storage, appendable, notifier, and instrumentation
166166
func (r *DefaultMultiTenantManager) newManager(ctx context.Context, userID string) (RulesManager, error) {
167-
notifier, err := r.getOrCreateNotifier(userID)
168-
if err != nil {
169-
return nil, err
170-
}
171-
172167
// Create a new Prometheus registry and register it within
173168
// our metrics struct for the provided user if it doesn't already exist.
174169
reg := prometheus.NewRegistry()
175170
r.userManagerMetrics.AddUserRegistry(userID, reg)
176171

172+
notifier, err := r.getOrCreateNotifier(userID, reg)
173+
if err != nil {
174+
return nil, err
175+
}
176+
177177
return r.managerFactory(ctx, userID, notifier, r.logger, reg), nil
178178
}
179179

180-
func (r *DefaultMultiTenantManager) stopNotifier(userID string) {
180+
func (r *DefaultMultiTenantManager) removeNotifier(userID string) {
181181
r.notifiersMtx.Lock()
182182
defer r.notifiersMtx.Unlock()
183183

184184
if n, ok := r.notifiers[userID]; ok {
185185
n.stop()
186186
}
187+
188+
delete(r.notifiers, userID)
187189
}
188190

189-
func (r *DefaultMultiTenantManager) getOrCreateNotifier(userID string) (*notifier.Manager, error) {
191+
func (r *DefaultMultiTenantManager) getOrCreateNotifier(userID string, userManagerRegistry prometheus.Registerer) (*notifier.Manager, error) {
190192
r.notifiersMtx.Lock()
191193
defer r.notifiersMtx.Unlock()
192194

@@ -197,7 +199,7 @@ func (r *DefaultMultiTenantManager) getOrCreateNotifier(userID string) (*notifie
197199
return n.notifier, nil
198200
}
199201

200-
reg := prometheus.WrapRegistererWith(prometheus.Labels{"user": userID}, r.registry)
202+
reg := prometheus.WrapRegistererWith(prometheus.Labels{"user": userID}, userManagerRegistry)
201203
reg = prometheus.WrapRegistererWithPrefix("cortex_", reg)
202204
n = newRulerNotifier(&notifier.Options{
203205
QueueCapacity: r.cfg.NotificationQueueCapacity,

pkg/ruler/ruler_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ func TestNotifierSendsUserIDHeader(t *testing.T) {
274274
defer rcleanup()
275275
defer manager.Stop()
276276

277-
n, err := manager.getOrCreateNotifier("1")
277+
n, err := manager.getOrCreateNotifier("1", manager.registry)
278278
require.NoError(t, err)
279279

280280
// Loop until notifier discovery syncs up

0 commit comments

Comments
 (0)