Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

receive: fix limits reloading race #6826

Merged
merged 1 commit into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/thanos/receive.go
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ func runReceive(
ctx, cancel := context.WithCancel(context.Background())
g.Add(func() error {
return runutil.Repeat(15*time.Second, ctx.Done(), func() error {
if err := limiter.HeadSeriesLimiter.QueryMetaMonitoring(ctx); err != nil {
if err := limiter.HeadSeriesLimiter().QueryMetaMonitoring(ctx); err != nil {
level.Error(logger).Log("msg", "failed to query meta-monitoring", "err", err.Error())
}
return nil
Expand Down
2 changes: 1 addition & 1 deletion pkg/receive/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ func (h *Handler) receiveHTTP(w http.ResponseWriter, r *http.Request) {
return
}

under, err := h.Limiter.HeadSeriesLimiter.isUnderLimit(tenant)
under, err := h.Limiter.HeadSeriesLimiter().isUnderLimit(tenant)
if err != nil {
level.Error(tLogger).Log("msg", "error while limiting", "err", err.Error())
}
Expand Down
16 changes: 13 additions & 3 deletions pkg/receive/limiter.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ import (
type Limiter struct {
sync.RWMutex
requestLimiter requestLimiter
HeadSeriesLimiter headSeriesLimiter
headSeriesLimiterMtx sync.Mutex
headSeriesLimiter headSeriesLimiter
writeGate gate.Gate
registerer prometheus.Registerer
configPathOrContent fileContent
Expand Down Expand Up @@ -54,13 +55,20 @@ type fileContent interface {
Path() string
}

func (l *Limiter) HeadSeriesLimiter() headSeriesLimiter {
l.headSeriesLimiterMtx.Lock()
defer l.headSeriesLimiterMtx.Unlock()

return l.headSeriesLimiter
}

// NewLimiter creates a new *Limiter given a configuration and prometheus
// registerer.
func NewLimiter(configFile fileContent, reg prometheus.Registerer, r ReceiverMode, logger log.Logger, configReloadTimer time.Duration) (*Limiter, error) {
limiter := &Limiter{
writeGate: gate.NewNoop(),
requestLimiter: &noopRequestLimiter{},
HeadSeriesLimiter: NewNopSeriesLimit(),
headSeriesLimiter: NewNopSeriesLimit(),
logger: logger,
receiverMode: r,
configReloadTimer: configReloadTimer,
Expand Down Expand Up @@ -165,7 +173,9 @@ func (l *Limiter) loadConfig() error {
return false
}
if (l.receiverMode == RouterOnly || l.receiverMode == RouterIngestor) && seriesLimitIsActivated() {
l.HeadSeriesLimiter = NewHeadSeriesLimit(config.WriteLimits, l.registerer, l.logger)
l.headSeriesLimiterMtx.Lock()
l.headSeriesLimiter = NewHeadSeriesLimit(config.WriteLimits, l.registerer, l.logger)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this simple synchronization seems like an ideal place to use an atomic value instead of a mutex

l.headSeriesLimiterMtx.Unlock()
}
return nil
}
Expand Down
Loading