-
Notifications
You must be signed in to change notification settings - Fork 581
/
Copy pathstate_replication.go
343 lines (289 loc) · 11.8 KB
/
state_replication.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
// SPDX-License-Identifier: AGPL-3.0-only
// Provenance-includes-location: https://github.com/cortexproject/cortex/blob/master/pkg/alertmanager/state_replication.go
// Provenance-includes-license: Apache-2.0
// Provenance-includes-copyright: The Cortex Authors.
package alertmanager
import (
"context"
"fmt"
"sync"
"time"
"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/grafana/dskit/services"
"github.com/pkg/errors"
"github.com/prometheus/alertmanager/cluster"
"github.com/prometheus/alertmanager/cluster/clusterpb"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/grafana/mimir/pkg/alertmanager/alertspb"
"github.com/grafana/mimir/pkg/alertmanager/alertstore"
)
const (
defaultSettleReadTimeout = 15 * time.Second
defaultStoreReadTimeout = 15 * time.Second
// Initial sync outcome label values.
syncFromReplica = "from-replica"
syncFromStorage = "from-storage"
syncUserNotFound = "user-not-found"
syncFailed = "failed"
)
// state represents the Alertmanager silences and notification log internal state.
type state struct {
services.Service
userID string
logger log.Logger
reg prometheus.Registerer
settleReadTimeout time.Duration
storeReadTimeout time.Duration
mtx sync.Mutex
states map[string]cluster.State
replicationFactor int
replicator Replicator
store alertstore.AlertStore
partialStateMergesTotal *prometheus.CounterVec
partialStateMergesFailed *prometheus.CounterVec
stateReplicationTotal *prometheus.CounterVec
stateReplicationFailed *prometheus.CounterVec
fetchReplicaStateTotal prometheus.Counter
fetchReplicaStateFailed prometheus.Counter
initialSyncTotal prometheus.Counter
initialSyncCompleted *prometheus.CounterVec
initialSyncDuration prometheus.Histogram
msgc chan *clusterpb.Part
}
// newReplicatedStates creates a new state struct, which manages state to be replicated between alertmanagers.
func newReplicatedStates(userID string, rf int, re Replicator, st alertstore.AlertStore, l log.Logger, r prometheus.Registerer) *state {
s := &state{
logger: log.With(l, "user", userID),
userID: userID,
replicationFactor: rf,
replicator: re,
store: st,
states: make(map[string]cluster.State, 2), // we use two, one for the notifications and one for silences.
msgc: make(chan *clusterpb.Part),
reg: r,
settleReadTimeout: defaultSettleReadTimeout,
storeReadTimeout: defaultStoreReadTimeout,
partialStateMergesTotal: promauto.With(r).NewCounterVec(prometheus.CounterOpts{
Name: "alertmanager_partial_state_merges_total",
Help: "Number of times we have received a partial state to merge for a key.",
}, []string{"key"}),
partialStateMergesFailed: promauto.With(r).NewCounterVec(prometheus.CounterOpts{
Name: "alertmanager_partial_state_merges_failed_total",
Help: "Number of times we have failed to merge a partial state received for a key.",
}, []string{"key"}),
stateReplicationTotal: promauto.With(r).NewCounterVec(prometheus.CounterOpts{
Name: "alertmanager_state_replication_total",
Help: "Number of times we have tried to replicate a state to other alertmanagers.",
}, []string{"key"}),
stateReplicationFailed: promauto.With(r).NewCounterVec(prometheus.CounterOpts{
Name: "alertmanager_state_replication_failed_total",
Help: "Number of times we have failed to replicate a state to other alertmanagers.",
}, []string{"key"}),
fetchReplicaStateTotal: promauto.With(r).NewCounter(prometheus.CounterOpts{
Name: "alertmanager_state_fetch_replica_state_total",
Help: "Number of times we have tried to read and merge the full state from another replica.",
}),
fetchReplicaStateFailed: promauto.With(r).NewCounter(prometheus.CounterOpts{
Name: "alertmanager_state_fetch_replica_state_failed_total",
Help: "Number of times we have failed to read and merge the full state from another replica.",
}),
initialSyncTotal: promauto.With(r).NewCounter(prometheus.CounterOpts{
Name: "alertmanager_state_initial_sync_total",
Help: "Number of times we have tried to sync initial state from peers or remote storage.",
}),
initialSyncCompleted: promauto.With(r).NewCounterVec(prometheus.CounterOpts{
Name: "alertmanager_state_initial_sync_completed_total",
Help: "Number of times we have completed syncing initial state for each possible outcome.",
}, []string{"outcome"}),
initialSyncDuration: promauto.With(r).NewHistogram(prometheus.HistogramOpts{
Name: "alertmanager_state_initial_sync_duration_seconds",
Help: "Time spent syncing initial state from peers or remote storage.",
Buckets: prometheus.ExponentialBuckets(0.008, 4, 7),
}),
}
s.initialSyncCompleted.WithLabelValues(syncFromReplica)
s.initialSyncCompleted.WithLabelValues(syncFromStorage)
s.initialSyncCompleted.WithLabelValues(syncUserNotFound)
s.initialSyncCompleted.WithLabelValues(syncFailed)
s.Service = services.NewBasicService(s.starting, s.running, nil)
return s
}
// AddState adds a new state that will be replicated using the ReplicationFunc. It returns a channel to which the client can broadcast messages of the state to be sent.
func (s *state) AddState(key string, cs cluster.State, _ prometheus.Registerer) cluster.ClusterChannel {
s.mtx.Lock()
defer s.mtx.Unlock()
s.states[key] = cs
s.partialStateMergesTotal.WithLabelValues(key)
s.partialStateMergesFailed.WithLabelValues(key)
s.stateReplicationTotal.WithLabelValues(key)
s.stateReplicationFailed.WithLabelValues(key)
return &stateChannel{
s: s,
key: key,
}
}
// MergePartialState merges a received partial message with an internal state.
func (s *state) MergePartialState(p *clusterpb.Part) error {
s.partialStateMergesTotal.WithLabelValues(p.Key).Inc()
s.mtx.Lock()
defer s.mtx.Unlock()
st, ok := s.states[p.Key]
if !ok {
s.partialStateMergesFailed.WithLabelValues(p.Key).Inc()
return fmt.Errorf("key not found while merging")
}
if err := st.Merge(p.Data); err != nil {
s.partialStateMergesFailed.WithLabelValues(p.Key).Inc()
return err
}
return nil
}
// Position helps in determining how long should we wait before sending a notification based on the number of replicas.
func (s *state) Position() int {
return s.replicator.GetPositionForUser(s.userID)
}
// GetFullState returns the full internal state.
func (s *state) GetFullState() (*clusterpb.FullState, error) {
s.mtx.Lock()
defer s.mtx.Unlock()
all := &clusterpb.FullState{
Parts: make([]clusterpb.Part, 0, len(s.states)),
}
for key, s := range s.states {
b, err := s.MarshalBinary()
if err != nil {
return nil, errors.Wrapf(err, "failed to encode state for key: %v", key)
}
all.Parts = append(all.Parts, clusterpb.Part{Key: key, Data: b})
}
return all, nil
}
// starting waits until the alertmanagers are ready (and sets the appropriate internal state when it is).
// The idea is that we don't want to start working" before we get a chance to know most of the notifications and/or silences.
func (s *state) starting(ctx context.Context) error {
s.initialSyncTotal.Inc()
timer := prometheus.NewTimer(s.initialSyncDuration)
defer timer.ObserveDuration()
// If replication factor is > 1 attempt to read state from other replicas, falling back to reading from
// storage if they are unavailable.
if s.replicationFactor > 1 {
level.Info(s.logger).Log("msg", "Waiting for notification and silences to settle...")
// We can check other alertmanager(s) and explicitly ask them to propagate their state to us if available.
readCtx, cancel := context.WithTimeout(ctx, s.settleReadTimeout)
defer cancel()
s.fetchReplicaStateTotal.Inc()
fullStates, err := s.replicator.ReadFullStateForUser(readCtx, s.userID)
if err == nil {
if err = s.MergeFullStates(fullStates); err == nil {
level.Info(s.logger).Log("msg", "state settled; proceeding")
s.initialSyncCompleted.WithLabelValues(syncFromReplica).Inc()
return nil
}
}
// The user not being found in all of the replicas is not recorded as a failure, as this is
// expected when this is the first replica to come up for a user. Note that it is important
// to continue and try to read from the state from remote storage, as the replicas may have
// lost state due to an all-replica restart.
if !errors.Is(err, errAllReplicasUserNotFound) {
s.fetchReplicaStateFailed.Inc()
}
level.Info(s.logger).Log("msg", "unable to read state from other Alertmanager replicas; trying to read from storage", "err", err)
}
level.Info(s.logger).Log("msg", "reading state from storage")
// Attempt to read the state from persistent storage instead.
storeReadCtx, cancel := context.WithTimeout(ctx, s.storeReadTimeout)
defer cancel()
fullState, err := s.store.GetFullState(storeReadCtx, s.userID)
if errors.Is(err, alertspb.ErrNotFound) {
level.Info(s.logger).Log("msg", "no state for user in storage; proceeding")
s.initialSyncCompleted.WithLabelValues(syncUserNotFound).Inc()
return nil
}
if err == nil {
if err = s.MergeFullStates([]*clusterpb.FullState{fullState.State}); err == nil {
level.Info(s.logger).Log("msg", "state read from storage; proceeding")
s.initialSyncCompleted.WithLabelValues(syncFromStorage).Inc()
return nil
}
}
level.Warn(s.logger).Log("msg", "failed to read state from storage; continuing anyway", "err", err)
s.initialSyncCompleted.WithLabelValues(syncFailed).Inc()
return nil
}
// WaitReady is needed for the pipeline builder to know whenever we've settled and the state is up to date.
func (s *state) WaitReady(ctx context.Context) error {
return s.AwaitRunning(ctx)
}
func (s *state) Ready() bool {
return s.State() == services.Running
}
func (s *state) MergeGrafanaState(fs []*clusterpb.FullState) error {
if err := s.MergeFullStates(fs); err != nil {
return err
}
for _, fs := range fs {
for _, p := range fs.Parts {
if cluster.OversizedMessage(p.Data) {
// When merging state, upstream Alertmanager code drops oversized messages.
// Manually broadcast oversized Grafana states to avoid missing silences/nflog entries.
s.broadcast(p.Key, p.Data)
}
}
}
return nil
}
// MergeFullStates attempts to merge all full states received from peers during settling.
func (s *state) MergeFullStates(fs []*clusterpb.FullState) error {
s.mtx.Lock()
defer s.mtx.Unlock()
for _, f := range fs {
for _, p := range f.Parts {
level.Debug(s.logger).Log("msg", "merging full state", "key", p.Key, "bytes", len(p.Data))
st, ok := s.states[p.Key]
if !ok {
level.Error(s.logger).Log("msg", "key not found while merging full state", "key", p.Key)
continue
}
if err := st.Merge(p.Data); err != nil {
return errors.Wrapf(err, "failed to merge part of full state for key: %v", p.Key)
}
}
}
return nil
}
func (s *state) running(ctx context.Context) error {
for {
select {
case p := <-s.msgc:
// If the replication factor is <= 1, we don't need to replicate any state anywhere else.
if s.replicationFactor <= 1 {
return nil
}
s.stateReplicationTotal.WithLabelValues(p.Key).Inc()
if err := s.replicator.ReplicateStateForUser(ctx, s.userID, p); err != nil {
s.stateReplicationFailed.WithLabelValues(p.Key).Inc()
level.Error(s.logger).Log("msg", "failed to replicate state to other alertmanagers", "key", p.Key, "err", err)
}
case <-ctx.Done():
return nil
}
}
}
func (s *state) broadcast(key string, b []byte) {
// We should ignore the Merges into the initial state during settling.
if s.Ready() {
s.msgc <- &clusterpb.Part{Key: key, Data: b}
}
}
// stateChannel allows a state publisher to send messages that will be broadcasted to all other alertmanagers that a tenant
// belongs to.
type stateChannel struct {
s *state
key string
}
// Broadcast receives a message to be replicated by the state.
func (c *stateChannel) Broadcast(b []byte) {
c.s.broadcast(c.key, b)
}