Skip to content

Commit

Permalink
Ruler: protect overrides map a mutex when creating a new appender
Browse files Browse the repository at this point in the history
  • Loading branch information
verejoel committed Jul 26, 2024
1 parent 8a3ae22 commit 000d1c4
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
4 changes: 4 additions & 0 deletions pkg/ruler/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,12 @@ func (r *walRegistry) get(tenant string) storage.Storage {
}

func (r *walRegistry) Appender(ctx context.Context) storage.Appender {
// concurrency-safe retrieval of remote-write config for this tenant, using the global remote-write for defaults
r.overridesMu.Lock()
tenant, _ := user.ExtractOrgID(ctx)
rwCfg, err := r.getTenantRemoteWriteConfig(tenant, r.config.RemoteWrite)
r.overridesMu.Unlock()

if err != nil {
level.Error(r.logger).Log("msg", "error retrieving remote-write config; discarding samples", "user", tenant, "err", err)
return discardingAppender{}
Expand Down
24 changes: 24 additions & 0 deletions pkg/ruler/registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,30 @@ func TestTenantRemoteWriteConfigWithOverrideConcurrentAccess(t *testing.T) {
})
}

func TestAppenderConcurrentAccess(t *testing.T) {
require.NotPanics(t, func() {
reg := setupRegistry(t, cfg, newFakeLimits())
var wg sync.WaitGroup
for i := 0; i < 1000; i++ {
wg.Add(1)
go func(reg *walRegistry) {
defer wg.Done()

_ = reg.Appender(user.InjectOrgID(context.Background(), enabledRWTenant))
}(reg)

wg.Add(1)
go func(reg *walRegistry) {
defer wg.Done()

_ = reg.Appender(user.InjectOrgID(context.Background(), additionalHeadersRWTenant))
}(reg)
}

wg.Wait()
})
}

func TestTenantRemoteWriteConfigWithoutOverride(t *testing.T) {
reg := setupRegistry(t, backCompatCfg, newFakeLimitsBackwardCompat())

Expand Down

0 comments on commit 000d1c4

Please sign in to comment.