Skip to content

Fix for alertmanagers not configuring users before becoming ACTIVE. #4110

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

Merged
merged 3 commits into from
Apr 23, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
6 changes: 6 additions & 0 deletions pkg/alertmanager/alertmanager_ring.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ var RingOp = ring.NewOp([]ring.InstanceState{ring.ACTIVE}, func(s ring.InstanceS
return s != ring.ACTIVE
})

// UserOwnedRingOp is the operation used for checking if a user is owned by an alertmanager.
var UserOwnedRingOp = ring.NewOp([]ring.InstanceState{ring.ACTIVE, ring.JOINING}, func(s ring.InstanceState) bool {
// A user is owned by an alertmanager in both ACTIVE and JOINING states.
return (s != ring.ACTIVE && s != ring.JOINING)
})

// RingConfig masks the ring lifecycler config which contains
// many options not really required by the alertmanager ring. This config
// is used to strip down the config to the minimum, and avoid confusion
Expand Down
2 changes: 1 addition & 1 deletion pkg/alertmanager/multitenant.go
Original file line number Diff line number Diff line change
Expand Up @@ -710,7 +710,7 @@ func (am *MultitenantAlertmanager) isUserOwned(userID string) bool {
return true
}

alertmanagers, err := am.ring.Get(shardByUser(userID), RingOp, nil, nil, nil)
alertmanagers, err := am.ring.Get(shardByUser(userID), UserOwnedRingOp, nil, nil, nil)
if err != nil {
am.ringCheckErrors.Inc()
level.Error(am.logger).Log("msg", "failed to load alertmanager configuration", "user", userID, "err", err)
Expand Down
16 changes: 13 additions & 3 deletions pkg/alertmanager/multitenant_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"path/filepath"
"regexp"
"strings"
"sync"
"testing"
"time"

Expand Down Expand Up @@ -1146,7 +1147,7 @@ func TestAlertmanager_StateReplicationWithSharding(t *testing.T) {
defer services.StopAndAwaitTerminated(ctx, am) //nolint:errcheck

if tt.withSharding {
clientPool.servers[amConfig.ShardingRing.InstanceAddr+":0"] = am
clientPool.setServer(amConfig.ShardingRing.InstanceAddr+":0", am)
am.alertmanagerClientsPool = clientPool
}

Expand Down Expand Up @@ -1334,7 +1335,7 @@ func TestAlertmanager_StateReplicationWithSharding_InitialSyncFromPeers(t *testi
am, err := createMultitenantAlertmanager(amConfig, nil, nil, mockStore, ringStore, log.NewNopLogger(), reg)
require.NoError(t, err)

clientPool.servers[amConfig.ShardingRing.InstanceAddr+":0"] = am
clientPool.setServer(amConfig.ShardingRing.InstanceAddr+":0", am)
am.alertmanagerClientsPool = clientPool

require.NoError(t, services.StartAndAwaitRunning(ctx, am))
Expand Down Expand Up @@ -1517,7 +1518,8 @@ func (am *passthroughAlertmanagerClient) RemoteAddress() string {
// passthroughAlertmanagerClientPool allows testing the logic of gRPC calls between alertmanager instances
// by invoking client calls directly to a peer instance in the unit test, without the server running.
type passthroughAlertmanagerClientPool struct {
servers map[string]alertmanagerpb.AlertmanagerServer
serversMtx sync.Mutex
servers map[string]alertmanagerpb.AlertmanagerServer
}

func newPassthroughAlertmanagerClientPool() *passthroughAlertmanagerClientPool {
Expand All @@ -1526,7 +1528,15 @@ func newPassthroughAlertmanagerClientPool() *passthroughAlertmanagerClientPool {
}
}

func (f *passthroughAlertmanagerClientPool) setServer(addr string, server alertmanagerpb.AlertmanagerServer) {
f.serversMtx.Lock()
defer f.serversMtx.Unlock()
f.servers[addr] = server
}

func (f *passthroughAlertmanagerClientPool) GetClientFor(addr string) (Client, error) {
f.serversMtx.Lock()
defer f.serversMtx.Unlock()
s, ok := f.servers[addr]
if !ok {
return nil, fmt.Errorf("client not found for address: %v", addr)
Expand Down