2016-08-15 14:10:35 +00:00
|
|
|
// Copyright 2016 Prometheus Team
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2016-08-30 12:19:22 +00:00
|
|
|
// Package silence provides a storage for silences, which can share its
|
2016-08-15 14:10:35 +00:00
|
|
|
// state over a mesh network and snapshot it.
|
|
|
|
package silence
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"math/rand"
|
|
|
|
"os"
|
2017-05-16 14:48:25 +00:00
|
|
|
"reflect"
|
2016-08-15 14:10:35 +00:00
|
|
|
"regexp"
|
2019-02-27 11:33:46 +00:00
|
|
|
"sort"
|
2016-08-15 14:10:35 +00:00
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2022-03-28 04:46:58 +00:00
|
|
|
"github.com/benbjohnson/clock"
|
2021-07-30 08:11:43 +00:00
|
|
|
"github.com/go-kit/log"
|
|
|
|
"github.com/go-kit/log/level"
|
2021-03-15 14:09:15 +00:00
|
|
|
uuid "github.com/gofrs/uuid"
|
2016-08-15 14:10:35 +00:00
|
|
|
"github.com/matttproud/golang_protobuf_extensions/pbutil"
|
2017-05-16 14:48:25 +00:00
|
|
|
"github.com/pkg/errors"
|
2022-03-25 16:59:51 +00:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
|
"github.com/prometheus/common/model"
|
|
|
|
|
2018-06-15 11:40:21 +00:00
|
|
|
"github.com/prometheus/alertmanager/cluster"
|
2021-01-13 14:11:28 +00:00
|
|
|
"github.com/prometheus/alertmanager/pkg/labels"
|
2016-08-15 14:10:35 +00:00
|
|
|
pb "github.com/prometheus/alertmanager/silence/silencepb"
|
2016-09-08 14:02:27 +00:00
|
|
|
"github.com/prometheus/alertmanager/types"
|
2016-08-15 14:10:35 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// ErrNotFound is returned if a silence was not found.
|
api: Implement OpenAPI generated Alertmanager API V2
The current Alertmanager API v1 is undocumented and written by hand.
This patch introduces a new Alertmanager API - v2. The API is fully
generated via an OpenAPI 2.0 [1] specification (see
`api/v2/openapi.yaml`) with the exception of the http handlers itself.
Pros:
- Generated server code
- Ability to generate clients in all major languages
(Go, Java, JS, Python, Ruby, Haskell, *elm* [3] ...)
- Strict contract (OpenAPI spec) between server and clients.
- Instant feedback on frontend-breaking changes, due to strictly
typed frontend language elm.
- Generated documentation (See Alertmanager online Swagger UI [4])
Cons:
- Dependency on open api ecosystem including go-swagger [2]
In addition this patch includes the following changes.
- README.md: Add API section
- test: Duplicate acceptance test to API v1 & API v2 version
The Alertmanager acceptance test framework has a decent test coverage
on the Alertmanager API. Introducing the Alertmanager API v2 does not go
hand in hand with deprecating API v1. They should live alongside each
other for a couple of minor Alertmanager versions.
Instead of porting the acceptance test framework to use the new API v2,
this patch duplicates the acceptance tests, one using the API v1, the
other API v2.
Once API v1 is removed we can simply remove `test/with_api_v1` and bring
`test/with_api_v2` to `test/`.
[1]
https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md
[2] https://github.com/go-swagger/go-swagger/
[3] https://github.com/ahultgren/swagger-elm
[4]
http://petstore.swagger.io/?url=https://raw.githubusercontent.com/mxinden/alertmanager/apiv2/api/v2/openapi.yaml
Signed-off-by: Max Leonard Inden <IndenML@gmail.com>
2018-04-26 06:12:49 +00:00
|
|
|
var ErrNotFound = fmt.Errorf("silence not found")
|
2016-08-15 14:10:35 +00:00
|
|
|
|
2018-04-10 08:12:05 +00:00
|
|
|
// ErrInvalidState is returned if the state isn't valid.
|
|
|
|
var ErrInvalidState = fmt.Errorf("invalid state")
|
|
|
|
|
2021-01-13 14:11:28 +00:00
|
|
|
type matcherCache map[*pb.Silence]labels.Matchers
|
2016-09-08 14:02:27 +00:00
|
|
|
|
|
|
|
// Get retrieves the matchers for a given silence. If it is a missed cache
|
|
|
|
// access, it compiles and adds the matchers of the requested silence to the
|
|
|
|
// cache.
|
2021-01-13 14:11:28 +00:00
|
|
|
func (c matcherCache) Get(s *pb.Silence) (labels.Matchers, error) {
|
2016-09-08 14:02:27 +00:00
|
|
|
if m, ok := c[s]; ok {
|
|
|
|
return m, nil
|
|
|
|
}
|
|
|
|
return c.add(s)
|
|
|
|
}
|
|
|
|
|
|
|
|
// add compiles a silences' matchers and adds them to the cache.
|
|
|
|
// It returns the compiled matchers.
|
2021-01-13 14:11:28 +00:00
|
|
|
func (c matcherCache) add(s *pb.Silence) (labels.Matchers, error) {
|
|
|
|
ms := make(labels.Matchers, len(s.Matchers))
|
2016-09-08 14:02:27 +00:00
|
|
|
|
2021-01-13 14:11:28 +00:00
|
|
|
for i, m := range s.Matchers {
|
|
|
|
var mt labels.MatchType
|
2016-09-08 14:02:27 +00:00
|
|
|
switch m.Type {
|
|
|
|
case pb.Matcher_EQUAL:
|
2021-01-13 14:11:28 +00:00
|
|
|
mt = labels.MatchEqual
|
|
|
|
case pb.Matcher_NOT_EQUAL:
|
|
|
|
mt = labels.MatchNotEqual
|
2016-09-08 14:02:27 +00:00
|
|
|
case pb.Matcher_REGEXP:
|
2021-01-13 14:11:28 +00:00
|
|
|
mt = labels.MatchRegexp
|
|
|
|
case pb.Matcher_NOT_REGEXP:
|
|
|
|
mt = labels.MatchNotRegexp
|
2021-02-10 11:02:56 +00:00
|
|
|
default:
|
|
|
|
return nil, errors.Errorf("unknown matcher type %q", m.Type)
|
2016-09-08 14:02:27 +00:00
|
|
|
}
|
2021-01-13 14:11:28 +00:00
|
|
|
matcher, err := labels.NewMatcher(mt, m.Name, m.Pattern)
|
2016-09-08 14:02:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-01-13 14:11:28 +00:00
|
|
|
ms[i] = matcher
|
2016-09-08 14:02:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
c[s] = ms
|
|
|
|
return ms, nil
|
|
|
|
}
|
|
|
|
|
2019-02-26 10:59:14 +00:00
|
|
|
// Silencer binds together a Marker and a Silences to implement the Muter
|
|
|
|
// interface.
|
|
|
|
type Silencer struct {
|
|
|
|
silences *Silences
|
|
|
|
marker types.Marker
|
|
|
|
logger log.Logger
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewSilencer returns a new Silencer.
|
|
|
|
func NewSilencer(s *Silences, m types.Marker, l log.Logger) *Silencer {
|
|
|
|
return &Silencer{
|
|
|
|
silences: s,
|
|
|
|
marker: m,
|
|
|
|
logger: l,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mutes implements the Muter interface.
|
|
|
|
func (s *Silencer) Mutes(lset model.LabelSet) bool {
|
2019-02-27 11:33:46 +00:00
|
|
|
fp := lset.Fingerprint()
|
2021-05-20 22:13:16 +00:00
|
|
|
activeIDs, pendingIDs, markerVersion, _ := s.marker.Silenced(fp)
|
2019-02-27 11:33:46 +00:00
|
|
|
|
|
|
|
var (
|
|
|
|
err error
|
2021-05-20 22:13:16 +00:00
|
|
|
allSils []*pb.Silence
|
2019-02-27 11:33:46 +00:00
|
|
|
newVersion = markerVersion
|
2019-02-26 10:59:14 +00:00
|
|
|
)
|
2019-02-27 11:33:46 +00:00
|
|
|
if markerVersion == s.silences.Version() {
|
2021-05-20 22:13:16 +00:00
|
|
|
totalSilences := len(activeIDs) + len(pendingIDs)
|
2019-02-27 11:33:46 +00:00
|
|
|
// No new silences added, just need to check which of the old
|
2021-05-20 22:13:16 +00:00
|
|
|
// silences are still relevant and which of the pending ones
|
|
|
|
// have become active.
|
|
|
|
if totalSilences == 0 {
|
2019-02-27 11:33:46 +00:00
|
|
|
// Super fast path: No silences ever applied to this
|
|
|
|
// alert, none have been added. We are done.
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
// This is still a quite fast path: No silences have been added,
|
|
|
|
// we only need to check which of the applicable silences are
|
|
|
|
// currently active. Note that newVersion is left at
|
|
|
|
// markerVersion because the Query call might already return a
|
|
|
|
// newer version, which is not the version our old list of
|
|
|
|
// applicable silences is based on.
|
2021-05-20 22:13:16 +00:00
|
|
|
allIDs := append(append(make([]string, 0, totalSilences), activeIDs...), pendingIDs...)
|
|
|
|
allSils, _, err = s.silences.Query(
|
|
|
|
QIDs(allIDs...),
|
|
|
|
QState(types.SilenceStateActive, types.SilenceStatePending),
|
2019-02-27 11:33:46 +00:00
|
|
|
)
|
|
|
|
} else {
|
|
|
|
// New silences have been added, do a full query.
|
2021-05-20 22:13:16 +00:00
|
|
|
allSils, newVersion, err = s.silences.Query(
|
|
|
|
QState(types.SilenceStateActive, types.SilenceStatePending),
|
2019-02-27 11:33:46 +00:00
|
|
|
QMatches(lset),
|
|
|
|
)
|
|
|
|
}
|
2019-02-26 10:59:14 +00:00
|
|
|
if err != nil {
|
|
|
|
level.Error(s.logger).Log("msg", "Querying silences failed, alerts might not get silenced correctly", "err", err)
|
|
|
|
}
|
2021-05-20 22:13:16 +00:00
|
|
|
if len(allSils) == 0 {
|
|
|
|
// Easy case, neither active nor pending silences anymore.
|
2022-06-14 13:40:22 +00:00
|
|
|
s.marker.SetActiveOrSilenced(fp, newVersion, nil, nil)
|
2019-02-26 10:59:14 +00:00
|
|
|
return false
|
|
|
|
}
|
2021-05-20 22:13:16 +00:00
|
|
|
// It is still possible that nothing has changed, but finding out is not
|
|
|
|
// much less effort than just recreating the IDs from the query
|
|
|
|
// result. So let's do it in any case. Note that we cannot reuse the
|
|
|
|
// current ID slices for concurrency reasons.
|
|
|
|
activeIDs, pendingIDs = nil, nil
|
2022-03-28 04:46:58 +00:00
|
|
|
now := s.silences.nowUTC()
|
2021-05-20 22:13:16 +00:00
|
|
|
for _, sil := range allSils {
|
|
|
|
switch getState(sil, now) {
|
|
|
|
case types.SilenceStatePending:
|
|
|
|
pendingIDs = append(pendingIDs, sil.Id)
|
|
|
|
case types.SilenceStateActive:
|
|
|
|
activeIDs = append(activeIDs, sil.Id)
|
|
|
|
default:
|
|
|
|
// Do nothing, silence has expired in the meantime.
|
2019-02-27 11:33:46 +00:00
|
|
|
}
|
2019-02-26 10:59:14 +00:00
|
|
|
}
|
2021-05-20 22:13:16 +00:00
|
|
|
level.Debug(s.logger).Log(
|
|
|
|
"msg", "determined current silences state",
|
|
|
|
"now", now,
|
|
|
|
"total", len(allSils),
|
|
|
|
"active", len(activeIDs),
|
|
|
|
"pending", len(pendingIDs),
|
|
|
|
)
|
|
|
|
sort.Strings(activeIDs)
|
|
|
|
sort.Strings(pendingIDs)
|
|
|
|
|
2022-06-14 13:40:22 +00:00
|
|
|
s.marker.SetActiveOrSilenced(fp, newVersion, activeIDs, pendingIDs)
|
2021-05-20 22:13:16 +00:00
|
|
|
|
|
|
|
return len(activeIDs) > 0
|
2019-02-26 10:59:14 +00:00
|
|
|
}
|
|
|
|
|
2016-08-30 12:19:22 +00:00
|
|
|
// Silences holds a silence state that can be modified, queried, and snapshot.
|
2016-08-15 14:10:35 +00:00
|
|
|
type Silences struct {
|
2022-03-28 04:46:58 +00:00
|
|
|
clock clock.Clock
|
|
|
|
|
2016-08-15 14:10:35 +00:00
|
|
|
logger log.Logger
|
2016-09-19 19:56:12 +00:00
|
|
|
metrics *metrics
|
2016-08-15 14:10:35 +00:00
|
|
|
retention time.Duration
|
|
|
|
|
2018-02-07 15:36:47 +00:00
|
|
|
mtx sync.RWMutex
|
|
|
|
st state
|
2019-02-27 11:33:46 +00:00
|
|
|
version int // Increments whenever silences are added.
|
2018-02-07 15:36:47 +00:00
|
|
|
broadcast func([]byte)
|
|
|
|
mc matcherCache
|
2016-08-15 14:10:35 +00:00
|
|
|
}
|
|
|
|
|
2021-09-06 10:49:39 +00:00
|
|
|
// MaintenanceFunc represents the function to run as part of the periodic maintenance for silences.
|
|
|
|
// It returns the size of the snapshot taken or an error if it failed.
|
|
|
|
type MaintenanceFunc func() (int64, error)
|
|
|
|
|
2016-09-19 19:56:12 +00:00
|
|
|
type metrics struct {
|
2018-06-08 09:48:42 +00:00
|
|
|
gcDuration prometheus.Summary
|
|
|
|
snapshotDuration prometheus.Summary
|
|
|
|
snapshotSize prometheus.Gauge
|
|
|
|
queriesTotal prometheus.Counter
|
|
|
|
queryErrorsTotal prometheus.Counter
|
|
|
|
queryDuration prometheus.Histogram
|
|
|
|
silencesActive prometheus.GaugeFunc
|
|
|
|
silencesPending prometheus.GaugeFunc
|
|
|
|
silencesExpired prometheus.GaugeFunc
|
|
|
|
propagatedMessagesTotal prometheus.Counter
|
2016-09-19 19:56:12 +00:00
|
|
|
}
|
|
|
|
|
2017-11-07 10:36:30 +00:00
|
|
|
func newSilenceMetricByState(s *Silences, st types.SilenceState) prometheus.GaugeFunc {
|
2017-09-15 13:33:47 +00:00
|
|
|
return prometheus.NewGaugeFunc(
|
|
|
|
prometheus.GaugeOpts{
|
|
|
|
Name: "alertmanager_silences",
|
|
|
|
Help: "How many silences by state.",
|
|
|
|
ConstLabels: prometheus.Labels{"state": string(st)},
|
|
|
|
},
|
|
|
|
func() float64 {
|
|
|
|
count, err := s.CountState(st)
|
|
|
|
if err != nil {
|
2017-10-22 05:59:33 +00:00
|
|
|
level.Error(s.logger).Log("msg", "Counting silences failed", "err", err)
|
2017-09-15 13:33:47 +00:00
|
|
|
}
|
|
|
|
return float64(count)
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func newMetrics(r prometheus.Registerer, s *Silences) *metrics {
|
2016-09-19 19:56:12 +00:00
|
|
|
m := &metrics{}
|
|
|
|
|
|
|
|
m.gcDuration = prometheus.NewSummary(prometheus.SummaryOpts{
|
2019-06-11 23:41:15 +00:00
|
|
|
Name: "alertmanager_silences_gc_duration_seconds",
|
|
|
|
Help: "Duration of the last silence garbage collection cycle.",
|
|
|
|
Objectives: map[float64]float64{},
|
2016-09-19 19:56:12 +00:00
|
|
|
})
|
|
|
|
m.snapshotDuration = prometheus.NewSummary(prometheus.SummaryOpts{
|
2019-06-11 23:41:15 +00:00
|
|
|
Name: "alertmanager_silences_snapshot_duration_seconds",
|
|
|
|
Help: "Duration of the last silence snapshot.",
|
|
|
|
Objectives: map[float64]float64{},
|
2016-09-19 19:56:12 +00:00
|
|
|
})
|
2018-01-10 13:53:57 +00:00
|
|
|
m.snapshotSize = prometheus.NewGauge(prometheus.GaugeOpts{
|
|
|
|
Name: "alertmanager_silences_snapshot_size_bytes",
|
|
|
|
Help: "Size of the last silence snapshot in bytes.",
|
|
|
|
})
|
2016-09-19 19:56:12 +00:00
|
|
|
m.queriesTotal = prometheus.NewCounter(prometheus.CounterOpts{
|
|
|
|
Name: "alertmanager_silences_queries_total",
|
|
|
|
Help: "How many silence queries were received.",
|
|
|
|
})
|
|
|
|
m.queryErrorsTotal = prometheus.NewCounter(prometheus.CounterOpts{
|
|
|
|
Name: "alertmanager_silences_query_errors_total",
|
|
|
|
Help: "How many silence received queries did not succeed.",
|
|
|
|
})
|
|
|
|
m.queryDuration = prometheus.NewHistogram(prometheus.HistogramOpts{
|
|
|
|
Name: "alertmanager_silences_query_duration_seconds",
|
|
|
|
Help: "Duration of silence query evaluation.",
|
|
|
|
})
|
2018-06-08 09:48:42 +00:00
|
|
|
m.propagatedMessagesTotal = prometheus.NewCounter(prometheus.CounterOpts{
|
|
|
|
Name: "alertmanager_silences_gossip_messages_propagated_total",
|
|
|
|
Help: "Number of received gossip messages that have been further gossiped.",
|
|
|
|
})
|
2017-09-15 13:33:47 +00:00
|
|
|
if s != nil {
|
2017-11-07 10:36:30 +00:00
|
|
|
m.silencesActive = newSilenceMetricByState(s, types.SilenceStateActive)
|
|
|
|
m.silencesPending = newSilenceMetricByState(s, types.SilenceStatePending)
|
|
|
|
m.silencesExpired = newSilenceMetricByState(s, types.SilenceStateExpired)
|
2017-09-15 13:33:47 +00:00
|
|
|
}
|
2016-09-19 19:56:12 +00:00
|
|
|
|
|
|
|
if r != nil {
|
|
|
|
r.MustRegister(
|
|
|
|
m.gcDuration,
|
|
|
|
m.snapshotDuration,
|
2018-01-10 13:53:57 +00:00
|
|
|
m.snapshotSize,
|
2016-09-19 19:56:12 +00:00
|
|
|
m.queriesTotal,
|
|
|
|
m.queryErrorsTotal,
|
|
|
|
m.queryDuration,
|
2017-09-15 13:33:47 +00:00
|
|
|
m.silencesActive,
|
|
|
|
m.silencesPending,
|
|
|
|
m.silencesExpired,
|
2018-06-08 09:48:42 +00:00
|
|
|
m.propagatedMessagesTotal,
|
2016-09-19 19:56:12 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
2016-08-15 14:10:35 +00:00
|
|
|
// Options exposes configuration options for creating a new Silences object.
|
2016-08-30 12:19:22 +00:00
|
|
|
// Its zero value is a safe default.
|
2016-08-15 14:10:35 +00:00
|
|
|
type Options struct {
|
|
|
|
// A snapshot file or reader from which the initial state is loaded.
|
|
|
|
// None or only one of them must be set.
|
|
|
|
SnapshotFile string
|
|
|
|
SnapshotReader io.Reader
|
|
|
|
|
|
|
|
// Retention time for newly created Silences. Silences may be
|
|
|
|
// garbage collected after the given duration after they ended.
|
|
|
|
Retention time.Duration
|
|
|
|
|
|
|
|
// A logger used by background processing.
|
2016-09-19 19:56:12 +00:00
|
|
|
Logger log.Logger
|
|
|
|
Metrics prometheus.Registerer
|
2016-08-15 14:10:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (o *Options) validate() error {
|
|
|
|
if o.SnapshotFile != "" && o.SnapshotReader != nil {
|
|
|
|
return fmt.Errorf("only one of SnapshotFile and SnapshotReader must be set")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// New returns a new Silences object with the given configuration.
|
|
|
|
func New(o Options) (*Silences, error) {
|
|
|
|
if err := o.validate(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if o.SnapshotFile != "" {
|
2016-08-30 09:58:27 +00:00
|
|
|
if r, err := os.Open(o.SnapshotFile); err != nil {
|
|
|
|
if !os.IsNotExist(err) {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
o.SnapshotReader = r
|
2021-10-18 23:12:02 +00:00
|
|
|
defer r.Close()
|
2016-08-15 14:10:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
s := &Silences{
|
2022-03-28 04:46:58 +00:00
|
|
|
clock: clock.New(),
|
2016-09-08 14:02:27 +00:00
|
|
|
mc: matcherCache{},
|
2016-08-15 14:10:35 +00:00
|
|
|
logger: log.NewNopLogger(),
|
|
|
|
retention: o.Retention,
|
2018-02-07 15:36:47 +00:00
|
|
|
broadcast: func([]byte) {},
|
|
|
|
st: state{},
|
2016-08-15 14:10:35 +00:00
|
|
|
}
|
2017-09-15 13:33:47 +00:00
|
|
|
s.metrics = newMetrics(o.Metrics, s)
|
|
|
|
|
2016-08-15 14:10:35 +00:00
|
|
|
if o.Logger != nil {
|
|
|
|
s.logger = o.Logger
|
|
|
|
}
|
|
|
|
if o.SnapshotReader != nil {
|
|
|
|
if err := s.loadSnapshot(o.SnapshotReader); err != nil {
|
|
|
|
return s, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return s, nil
|
|
|
|
}
|
|
|
|
|
2022-03-28 04:46:58 +00:00
|
|
|
func (s *Silences) nowUTC() time.Time {
|
|
|
|
return s.clock.Now().UTC()
|
|
|
|
}
|
|
|
|
|
2016-08-15 14:10:35 +00:00
|
|
|
// Maintenance garbage collects the silence state at the given interval. If the snapshot
|
|
|
|
// file is set, a snapshot is written to it afterwards.
|
|
|
|
// Terminates on receiving from stopc.
|
2021-09-06 10:49:39 +00:00
|
|
|
// If not nil, the last argument is an override for what to do as part of the maintenance - for advanced usage.
|
|
|
|
func (s *Silences) Maintenance(interval time.Duration, snapf string, stopc <-chan struct{}, override MaintenanceFunc) {
|
2022-03-28 04:46:58 +00:00
|
|
|
t := s.clock.Ticker(interval)
|
2016-08-15 14:10:35 +00:00
|
|
|
defer t.Stop()
|
|
|
|
|
2021-09-06 10:49:39 +00:00
|
|
|
var doMaintenance MaintenanceFunc
|
|
|
|
doMaintenance = func() (int64, error) {
|
2018-02-07 15:36:47 +00:00
|
|
|
var size int64
|
|
|
|
|
2016-08-15 14:10:35 +00:00
|
|
|
if _, err := s.GC(); err != nil {
|
2021-09-06 10:49:39 +00:00
|
|
|
return size, err
|
2016-08-15 14:10:35 +00:00
|
|
|
}
|
|
|
|
if snapf == "" {
|
2021-09-06 10:49:39 +00:00
|
|
|
return size, nil
|
2016-08-15 14:10:35 +00:00
|
|
|
}
|
|
|
|
f, err := openReplace(snapf)
|
|
|
|
if err != nil {
|
2021-09-06 10:49:39 +00:00
|
|
|
return size, err
|
2016-08-15 14:10:35 +00:00
|
|
|
}
|
2018-01-10 13:53:57 +00:00
|
|
|
if size, err = s.Snapshot(f); err != nil {
|
2021-09-06 10:49:39 +00:00
|
|
|
return size, err
|
2016-08-15 14:10:35 +00:00
|
|
|
}
|
2021-09-06 10:49:39 +00:00
|
|
|
return size, f.Close()
|
|
|
|
}
|
|
|
|
|
2021-09-13 14:12:48 +00:00
|
|
|
if override != nil {
|
2021-09-06 10:49:39 +00:00
|
|
|
doMaintenance = override
|
|
|
|
}
|
|
|
|
|
|
|
|
runMaintenance := func(do MaintenanceFunc) error {
|
2022-03-28 04:46:58 +00:00
|
|
|
start := s.nowUTC()
|
2021-09-06 10:49:39 +00:00
|
|
|
level.Debug(s.logger).Log("msg", "Running maintenance")
|
|
|
|
size, err := do()
|
2022-05-05 03:28:24 +00:00
|
|
|
level.Debug(s.logger).Log("msg", "Maintenance done", "duration", s.clock.Since(start), "size", size)
|
2021-09-06 10:49:39 +00:00
|
|
|
s.metrics.snapshotSize.Set(float64(size))
|
|
|
|
return err
|
2016-08-15 14:10:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Loop:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-stopc:
|
|
|
|
break Loop
|
|
|
|
case <-t.C:
|
2021-09-06 10:49:39 +00:00
|
|
|
if err := runMaintenance(doMaintenance); err != nil {
|
2017-10-22 05:59:33 +00:00
|
|
|
level.Info(s.logger).Log("msg", "Running maintenance failed", "err", err)
|
2016-08-15 14:10:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// No need for final maintenance if we don't want to snapshot.
|
|
|
|
if snapf == "" {
|
|
|
|
return
|
|
|
|
}
|
2021-09-06 10:49:39 +00:00
|
|
|
if err := runMaintenance(doMaintenance); err != nil {
|
2017-10-22 05:59:33 +00:00
|
|
|
level.Info(s.logger).Log("msg", "Creating shutdown snapshot failed", "err", err)
|
2016-08-15 14:10:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GC runs a garbage collection that removes silences that have ended longer
|
|
|
|
// than the configured retention time ago.
|
|
|
|
func (s *Silences) GC() (int, error) {
|
2016-09-19 19:56:12 +00:00
|
|
|
start := time.Now()
|
|
|
|
defer func() { s.metrics.gcDuration.Observe(time.Since(start).Seconds()) }()
|
|
|
|
|
2022-03-28 04:46:58 +00:00
|
|
|
now := s.nowUTC()
|
2016-08-15 14:10:35 +00:00
|
|
|
var n int
|
|
|
|
|
|
|
|
s.mtx.Lock()
|
|
|
|
defer s.mtx.Unlock()
|
|
|
|
|
2018-02-07 15:36:47 +00:00
|
|
|
for id, sil := range s.st {
|
2017-04-18 08:46:40 +00:00
|
|
|
if sil.ExpiresAt.IsZero() {
|
|
|
|
return n, errors.New("unexpected zero expiration timestamp")
|
|
|
|
}
|
|
|
|
if !sil.ExpiresAt.After(now) {
|
2018-02-07 15:36:47 +00:00
|
|
|
delete(s.st, id)
|
2016-09-08 14:02:27 +00:00
|
|
|
delete(s.mc, sil.Silence)
|
2016-08-15 14:10:35 +00:00
|
|
|
n++
|
|
|
|
}
|
|
|
|
}
|
2017-09-28 08:25:35 +00:00
|
|
|
|
2016-08-15 14:10:35 +00:00
|
|
|
return n, nil
|
|
|
|
}
|
|
|
|
|
2021-10-21 07:29:55 +00:00
|
|
|
// ValidateMatcher runs validation on the matcher name, type, and pattern.
|
|
|
|
var ValidateMatcher = func(m *pb.Matcher) error {
|
2016-08-15 14:10:35 +00:00
|
|
|
if !model.LabelName(m.Name).IsValid() {
|
|
|
|
return fmt.Errorf("invalid label name %q", m.Name)
|
|
|
|
}
|
|
|
|
switch m.Type {
|
2021-01-18 20:58:20 +00:00
|
|
|
case pb.Matcher_EQUAL, pb.Matcher_NOT_EQUAL:
|
2016-08-15 14:10:35 +00:00
|
|
|
if !model.LabelValue(m.Pattern).IsValid() {
|
|
|
|
return fmt.Errorf("invalid label value %q", m.Pattern)
|
|
|
|
}
|
2021-01-18 20:58:20 +00:00
|
|
|
case pb.Matcher_REGEXP, pb.Matcher_NOT_REGEXP:
|
2016-08-15 14:10:35 +00:00
|
|
|
if _, err := regexp.Compile(m.Pattern); err != nil {
|
|
|
|
return fmt.Errorf("invalid regular expression %q: %s", m.Pattern, err)
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("unknown matcher type %q", m.Type)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-10-31 14:42:03 +00:00
|
|
|
func matchesEmpty(m *pb.Matcher) bool {
|
|
|
|
switch m.Type {
|
|
|
|
case pb.Matcher_EQUAL:
|
|
|
|
return m.Pattern == ""
|
|
|
|
case pb.Matcher_REGEXP:
|
|
|
|
matched, _ := regexp.MatchString(m.Pattern, "")
|
|
|
|
return matched
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-15 14:10:35 +00:00
|
|
|
func validateSilence(s *pb.Silence) error {
|
|
|
|
if s.Id == "" {
|
|
|
|
return errors.New("ID missing")
|
|
|
|
}
|
|
|
|
if len(s.Matchers) == 0 {
|
|
|
|
return errors.New("at least one matcher required")
|
|
|
|
}
|
2019-10-31 14:42:03 +00:00
|
|
|
allMatchEmpty := true
|
2016-08-15 14:10:35 +00:00
|
|
|
for i, m := range s.Matchers {
|
2021-10-21 07:29:55 +00:00
|
|
|
if err := ValidateMatcher(m); err != nil {
|
2016-08-15 14:10:35 +00:00
|
|
|
return fmt.Errorf("invalid label matcher %d: %s", i, err)
|
|
|
|
}
|
2019-10-31 14:42:03 +00:00
|
|
|
allMatchEmpty = allMatchEmpty && matchesEmpty(m)
|
|
|
|
}
|
|
|
|
if allMatchEmpty {
|
|
|
|
return errors.New("at least one matcher must not match the empty string")
|
2016-08-15 14:10:35 +00:00
|
|
|
}
|
2017-04-18 08:46:40 +00:00
|
|
|
if s.StartsAt.IsZero() {
|
|
|
|
return errors.New("invalid zero start timestamp")
|
2016-08-15 14:10:35 +00:00
|
|
|
}
|
2017-04-18 08:46:40 +00:00
|
|
|
if s.EndsAt.IsZero() {
|
|
|
|
return errors.New("invalid zero end timestamp")
|
2016-08-15 14:10:35 +00:00
|
|
|
}
|
2017-04-18 08:46:40 +00:00
|
|
|
if s.EndsAt.Before(s.StartsAt) {
|
2016-08-15 14:10:35 +00:00
|
|
|
return errors.New("end time must not be before start time")
|
|
|
|
}
|
2017-04-18 08:46:40 +00:00
|
|
|
if s.UpdatedAt.IsZero() {
|
|
|
|
return errors.New("invalid zero update timestamp")
|
2016-08-15 14:10:35 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// cloneSilence returns a shallow copy of a silence.
|
|
|
|
func cloneSilence(sil *pb.Silence) *pb.Silence {
|
|
|
|
s := *sil
|
|
|
|
return &s
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Silences) getSilence(id string) (*pb.Silence, bool) {
|
2018-02-07 15:36:47 +00:00
|
|
|
msil, ok := s.st[id]
|
2016-08-15 14:10:35 +00:00
|
|
|
if !ok {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
return msil.Silence, true
|
|
|
|
}
|
|
|
|
|
2019-02-27 17:00:27 +00:00
|
|
|
func (s *Silences) setSilence(sil *pb.Silence, now time.Time) error {
|
|
|
|
sil.UpdatedAt = now
|
2017-05-16 14:48:25 +00:00
|
|
|
|
|
|
|
if err := validateSilence(sil); err != nil {
|
|
|
|
return errors.Wrap(err, "silence invalid")
|
|
|
|
}
|
|
|
|
|
2016-08-15 14:10:35 +00:00
|
|
|
msil := &pb.MeshSilence{
|
|
|
|
Silence: sil,
|
2017-04-18 08:46:40 +00:00
|
|
|
ExpiresAt: sil.EndsAt.Add(s.retention),
|
2016-08-15 14:10:35 +00:00
|
|
|
}
|
2018-03-22 12:53:00 +00:00
|
|
|
b, err := marshalMeshSilence(msil)
|
2018-02-07 15:36:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2017-09-13 09:18:01 +00:00
|
|
|
}
|
2016-08-15 14:10:35 +00:00
|
|
|
|
2019-02-27 17:00:27 +00:00
|
|
|
if s.st.merge(msil, now) {
|
2019-02-27 11:33:46 +00:00
|
|
|
s.version++
|
|
|
|
}
|
2018-02-07 15:36:47 +00:00
|
|
|
s.broadcast(b)
|
2016-08-15 14:10:35 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-05-16 14:48:25 +00:00
|
|
|
// Set the specified silence. If a silence with the ID already exists and the modification
|
|
|
|
// modifies history, the old silence gets expired and a new one is created.
|
|
|
|
func (s *Silences) Set(sil *pb.Silence) (string, error) {
|
|
|
|
s.mtx.Lock()
|
|
|
|
defer s.mtx.Unlock()
|
2016-08-15 14:10:35 +00:00
|
|
|
|
2022-03-28 04:46:58 +00:00
|
|
|
now := s.nowUTC()
|
2017-05-16 14:48:25 +00:00
|
|
|
prev, ok := s.getSilence(sil.Id)
|
2017-04-18 08:46:40 +00:00
|
|
|
|
2017-05-16 14:48:25 +00:00
|
|
|
if sil.Id != "" && !ok {
|
|
|
|
return "", ErrNotFound
|
|
|
|
}
|
|
|
|
if ok {
|
|
|
|
if canUpdate(prev, sil, now) {
|
2019-02-27 17:00:27 +00:00
|
|
|
return sil.Id, s.setSilence(sil, now)
|
2017-05-16 14:48:25 +00:00
|
|
|
}
|
2022-03-28 04:46:58 +00:00
|
|
|
if getState(prev, s.nowUTC()) != types.SilenceStateExpired {
|
2017-05-16 14:48:25 +00:00
|
|
|
// We cannot update the silence, expire the old one.
|
|
|
|
if err := s.expire(prev.Id); err != nil {
|
|
|
|
return "", errors.Wrap(err, "expire previous silence")
|
|
|
|
}
|
|
|
|
}
|
2016-08-15 14:10:35 +00:00
|
|
|
}
|
2017-05-16 14:48:25 +00:00
|
|
|
// If we got here it's either a new silence or a replacing one.
|
2021-03-10 09:19:07 +00:00
|
|
|
uid, err := uuid.NewV4()
|
|
|
|
if err != nil {
|
|
|
|
return "", errors.Wrap(err, "generate uuid")
|
|
|
|
}
|
|
|
|
sil.Id = uid.String()
|
2016-08-15 14:10:35 +00:00
|
|
|
|
2017-05-16 14:48:25 +00:00
|
|
|
if sil.StartsAt.Before(now) {
|
|
|
|
sil.StartsAt = now
|
2016-08-15 14:10:35 +00:00
|
|
|
}
|
|
|
|
|
2019-02-27 17:00:27 +00:00
|
|
|
return sil.Id, s.setSilence(sil, now)
|
2017-05-16 14:48:25 +00:00
|
|
|
}
|
2016-08-15 14:10:35 +00:00
|
|
|
|
2017-05-16 14:48:25 +00:00
|
|
|
// canUpdate returns true if silence a can be updated to b without
|
|
|
|
// affecting the historic view of silencing.
|
|
|
|
func canUpdate(a, b *pb.Silence, now time.Time) bool {
|
|
|
|
if !reflect.DeepEqual(a.Matchers, b.Matchers) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
// Allowed timestamp modifications depend on the current time.
|
|
|
|
switch st := getState(a, now); st {
|
2017-11-07 10:36:30 +00:00
|
|
|
case types.SilenceStateActive:
|
2022-01-12 11:48:31 +00:00
|
|
|
if b.StartsAt.Unix() != a.StartsAt.Unix() {
|
2017-05-16 14:48:25 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
if b.EndsAt.Before(now) {
|
|
|
|
return false
|
|
|
|
}
|
2017-11-07 10:36:30 +00:00
|
|
|
case types.SilenceStatePending:
|
2017-05-16 14:48:25 +00:00
|
|
|
if b.StartsAt.Before(now) {
|
|
|
|
return false
|
|
|
|
}
|
2017-11-07 10:36:30 +00:00
|
|
|
case types.SilenceStateExpired:
|
2017-05-16 14:48:25 +00:00
|
|
|
return false
|
|
|
|
default:
|
|
|
|
panic("unknown silence state")
|
2016-08-15 14:10:35 +00:00
|
|
|
}
|
2017-05-16 14:48:25 +00:00
|
|
|
return true
|
2016-08-15 14:10:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Expire the silence with the given ID immediately.
|
|
|
|
func (s *Silences) Expire(id string) error {
|
|
|
|
s.mtx.Lock()
|
|
|
|
defer s.mtx.Unlock()
|
2017-05-16 14:48:25 +00:00
|
|
|
return s.expire(id)
|
2016-08-15 14:10:35 +00:00
|
|
|
}
|
|
|
|
|
2017-05-16 14:48:25 +00:00
|
|
|
// Expire the silence with the given ID immediately.
|
2022-02-22 12:34:21 +00:00
|
|
|
// It is idempotent, nil is returned if the silence already expired before it is GC'd.
|
|
|
|
// If the silence is not found an error is returned.
|
2017-05-16 14:48:25 +00:00
|
|
|
func (s *Silences) expire(id string) error {
|
2016-08-15 14:10:35 +00:00
|
|
|
sil, ok := s.getSilence(id)
|
|
|
|
if !ok {
|
|
|
|
return ErrNotFound
|
|
|
|
}
|
2017-05-22 07:27:57 +00:00
|
|
|
sil = cloneSilence(sil)
|
2022-03-28 04:46:58 +00:00
|
|
|
now := s.nowUTC()
|
2017-05-22 07:27:57 +00:00
|
|
|
|
|
|
|
switch getState(sil, now) {
|
2017-11-07 10:36:30 +00:00
|
|
|
case types.SilenceStateExpired:
|
2022-02-22 12:34:21 +00:00
|
|
|
return nil
|
2017-11-07 10:36:30 +00:00
|
|
|
case types.SilenceStateActive:
|
2017-05-22 07:27:57 +00:00
|
|
|
sil.EndsAt = now
|
2017-11-07 10:36:30 +00:00
|
|
|
case types.SilenceStatePending:
|
2017-05-29 16:03:31 +00:00
|
|
|
// Set both to now to make Silence move to "expired" state
|
|
|
|
sil.StartsAt = now
|
|
|
|
sil.EndsAt = now
|
2016-08-15 14:10:35 +00:00
|
|
|
}
|
|
|
|
|
2019-02-27 17:00:27 +00:00
|
|
|
return s.setSilence(sil, now)
|
2016-08-15 14:10:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// QueryParam expresses parameters along which silences are queried.
|
|
|
|
type QueryParam func(*query) error
|
|
|
|
|
|
|
|
type query struct {
|
|
|
|
ids []string
|
|
|
|
filters []silenceFilter
|
|
|
|
}
|
|
|
|
|
|
|
|
// silenceFilter is a function that returns true if a silence
|
|
|
|
// should be dropped from a result set for a given time.
|
2017-04-18 08:46:40 +00:00
|
|
|
type silenceFilter func(*pb.Silence, *Silences, time.Time) (bool, error)
|
2016-08-15 14:10:35 +00:00
|
|
|
|
|
|
|
// QIDs configures a query to select the given silence IDs.
|
|
|
|
func QIDs(ids ...string) QueryParam {
|
|
|
|
return func(q *query) error {
|
|
|
|
q.ids = append(q.ids, ids...)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// QMatches returns silences that match the given label set.
|
2016-09-08 14:02:27 +00:00
|
|
|
func QMatches(set model.LabelSet) QueryParam {
|
2016-08-15 14:10:35 +00:00
|
|
|
return func(q *query) error {
|
2017-04-18 08:46:40 +00:00
|
|
|
f := func(sil *pb.Silence, s *Silences, _ time.Time) (bool, error) {
|
2016-09-08 14:02:27 +00:00
|
|
|
m, err := s.mc.Get(sil)
|
|
|
|
if err != nil {
|
|
|
|
return true, err
|
2016-08-15 14:10:35 +00:00
|
|
|
}
|
2021-01-13 14:11:28 +00:00
|
|
|
return m.Matches(set), nil
|
2016-08-15 14:10:35 +00:00
|
|
|
}
|
|
|
|
q.filters = append(q.filters, f)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// getState returns a silence's SilenceState at the given timestamp.
|
2017-11-07 10:36:30 +00:00
|
|
|
func getState(sil *pb.Silence, ts time.Time) types.SilenceState {
|
2017-04-18 08:46:40 +00:00
|
|
|
if ts.Before(sil.StartsAt) {
|
2017-11-07 10:36:30 +00:00
|
|
|
return types.SilenceStatePending
|
2016-08-15 14:10:35 +00:00
|
|
|
}
|
2017-04-18 08:46:40 +00:00
|
|
|
if ts.After(sil.EndsAt) {
|
2017-11-07 10:36:30 +00:00
|
|
|
return types.SilenceStateExpired
|
2016-08-15 14:10:35 +00:00
|
|
|
}
|
2017-11-07 10:36:30 +00:00
|
|
|
return types.SilenceStateActive
|
2016-08-15 14:10:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// QState filters queried silences by the given states.
|
2017-11-07 10:36:30 +00:00
|
|
|
func QState(states ...types.SilenceState) QueryParam {
|
2016-08-15 14:10:35 +00:00
|
|
|
return func(q *query) error {
|
2017-04-18 08:46:40 +00:00
|
|
|
f := func(sil *pb.Silence, _ *Silences, now time.Time) (bool, error) {
|
2016-08-15 14:10:35 +00:00
|
|
|
s := getState(sil, now)
|
2016-08-30 09:58:27 +00:00
|
|
|
|
2016-08-15 14:10:35 +00:00
|
|
|
for _, ps := range states {
|
|
|
|
if s == ps {
|
2016-08-30 09:58:27 +00:00
|
|
|
return true, nil
|
2016-08-15 14:10:35 +00:00
|
|
|
}
|
|
|
|
}
|
2016-08-30 09:58:27 +00:00
|
|
|
return false, nil
|
2016-08-15 14:10:35 +00:00
|
|
|
}
|
|
|
|
q.filters = append(q.filters, f)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-16 14:48:25 +00:00
|
|
|
// QueryOne queries with the given parameters and returns the first result.
|
|
|
|
// Returns ErrNotFound if the query result is empty.
|
|
|
|
func (s *Silences) QueryOne(params ...QueryParam) (*pb.Silence, error) {
|
2019-02-27 11:33:46 +00:00
|
|
|
res, _, err := s.Query(params...)
|
2017-05-16 14:48:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if len(res) == 0 {
|
|
|
|
return nil, ErrNotFound
|
|
|
|
}
|
|
|
|
return res[0], nil
|
|
|
|
}
|
|
|
|
|
2019-02-27 11:33:46 +00:00
|
|
|
// Query for silences based on the given query parameters. It returns the
|
|
|
|
// resulting silences and the state version the result is based on.
|
|
|
|
func (s *Silences) Query(params ...QueryParam) ([]*pb.Silence, int, error) {
|
2016-09-19 19:56:12 +00:00
|
|
|
s.metrics.queriesTotal.Inc()
|
2019-02-28 12:04:05 +00:00
|
|
|
defer prometheus.NewTimer(s.metrics.queryDuration).ObserveDuration()
|
2016-09-19 19:56:12 +00:00
|
|
|
|
2019-02-28 12:04:05 +00:00
|
|
|
q := &query{}
|
|
|
|
for _, p := range params {
|
|
|
|
if err := p(q); err != nil {
|
|
|
|
s.metrics.queryErrorsTotal.Inc()
|
|
|
|
return nil, s.Version(), err
|
2016-09-19 19:56:12 +00:00
|
|
|
}
|
2019-02-28 12:04:05 +00:00
|
|
|
}
|
2022-03-28 04:46:58 +00:00
|
|
|
sils, version, err := s.query(q, s.nowUTC())
|
2016-08-15 14:10:35 +00:00
|
|
|
if err != nil {
|
2016-09-19 19:56:12 +00:00
|
|
|
s.metrics.queryErrorsTotal.Inc()
|
2016-08-15 14:10:35 +00:00
|
|
|
}
|
2019-02-27 11:33:46 +00:00
|
|
|
return sils, version, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Version of the silence state.
|
|
|
|
func (s *Silences) Version() int {
|
|
|
|
s.mtx.RLock()
|
|
|
|
defer s.mtx.RUnlock()
|
|
|
|
return s.version
|
2016-08-15 14:10:35 +00:00
|
|
|
}
|
|
|
|
|
2019-02-27 17:00:27 +00:00
|
|
|
// CountState counts silences by state.
|
2017-11-07 10:36:30 +00:00
|
|
|
func (s *Silences) CountState(states ...types.SilenceState) (int, error) {
|
2017-09-15 13:33:47 +00:00
|
|
|
// This could probably be optimized.
|
2019-02-27 11:33:46 +00:00
|
|
|
sils, _, err := s.Query(QState(states...))
|
2017-09-15 13:33:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return -1, err
|
|
|
|
}
|
|
|
|
return len(sils), nil
|
|
|
|
}
|
|
|
|
|
2019-02-27 11:33:46 +00:00
|
|
|
func (s *Silences) query(q *query, now time.Time) ([]*pb.Silence, int, error) {
|
|
|
|
// If we have no ID constraint, all silences are our base set. This and
|
|
|
|
// the use of post-filter functions is the trivial solution for now.
|
2016-08-15 14:10:35 +00:00
|
|
|
var res []*pb.Silence
|
|
|
|
|
2016-11-21 10:09:49 +00:00
|
|
|
s.mtx.Lock()
|
|
|
|
defer s.mtx.Unlock()
|
|
|
|
|
2016-08-15 14:10:35 +00:00
|
|
|
if q.ids != nil {
|
|
|
|
for _, id := range q.ids {
|
2018-02-07 15:36:47 +00:00
|
|
|
if s, ok := s.st[id]; ok {
|
2016-08-15 14:10:35 +00:00
|
|
|
res = append(res, s.Silence)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2018-02-07 15:36:47 +00:00
|
|
|
for _, sil := range s.st {
|
2016-08-15 14:10:35 +00:00
|
|
|
res = append(res, sil.Silence)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var resf []*pb.Silence
|
|
|
|
for _, sil := range res {
|
|
|
|
remove := false
|
|
|
|
for _, f := range q.filters {
|
2016-09-08 14:02:27 +00:00
|
|
|
ok, err := f(sil, s, now)
|
2016-08-15 14:10:35 +00:00
|
|
|
if err != nil {
|
2019-02-27 11:33:46 +00:00
|
|
|
return nil, s.version, err
|
2016-08-15 14:10:35 +00:00
|
|
|
}
|
|
|
|
if !ok {
|
|
|
|
remove = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !remove {
|
2017-05-16 14:48:25 +00:00
|
|
|
resf = append(resf, cloneSilence(sil))
|
2016-08-15 14:10:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-27 11:33:46 +00:00
|
|
|
return resf, s.version, nil
|
2016-08-15 14:10:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// loadSnapshot loads a snapshot generated by Snapshot() into the state.
|
|
|
|
// Any previous state is wiped.
|
|
|
|
func (s *Silences) loadSnapshot(r io.Reader) error {
|
2018-02-07 15:36:47 +00:00
|
|
|
st, err := decodeState(r)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, e := range st {
|
2017-05-16 14:48:25 +00:00
|
|
|
// Comments list was moved to a single comment. Upgrade on loading the snapshot.
|
2018-02-07 15:36:47 +00:00
|
|
|
if len(e.Silence.Comments) > 0 {
|
|
|
|
e.Silence.Comment = e.Silence.Comments[0].Comment
|
|
|
|
e.Silence.CreatedBy = e.Silence.Comments[0].Author
|
|
|
|
e.Silence.Comments = nil
|
2016-09-08 14:02:27 +00:00
|
|
|
}
|
2018-02-07 15:36:47 +00:00
|
|
|
st[e.Silence.Id] = e
|
2016-08-15 14:10:35 +00:00
|
|
|
}
|
2018-02-07 15:36:47 +00:00
|
|
|
s.mtx.Lock()
|
|
|
|
s.st = st
|
2019-02-27 11:33:46 +00:00
|
|
|
s.version++
|
2018-02-07 15:36:47 +00:00
|
|
|
s.mtx.Unlock()
|
2016-08-15 14:10:35 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Snapshot writes the full internal state into the writer and returns the number of bytes
|
|
|
|
// written.
|
2018-02-07 15:36:47 +00:00
|
|
|
func (s *Silences) Snapshot(w io.Writer) (int64, error) {
|
2016-09-19 19:56:12 +00:00
|
|
|
start := time.Now()
|
|
|
|
defer func() { s.metrics.snapshotDuration.Observe(time.Since(start).Seconds()) }()
|
|
|
|
|
2018-02-07 15:36:47 +00:00
|
|
|
s.mtx.RLock()
|
|
|
|
defer s.mtx.RUnlock()
|
2016-08-15 14:10:35 +00:00
|
|
|
|
2018-02-07 15:36:47 +00:00
|
|
|
b, err := s.st.MarshalBinary()
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
2016-08-15 14:10:35 +00:00
|
|
|
}
|
|
|
|
|
2018-02-07 15:36:47 +00:00
|
|
|
return io.Copy(w, bytes.NewReader(b))
|
2016-08-15 14:10:35 +00:00
|
|
|
}
|
|
|
|
|
2018-03-22 12:53:00 +00:00
|
|
|
// MarshalBinary serializes all silences.
|
2018-02-07 15:36:47 +00:00
|
|
|
func (s *Silences) MarshalBinary() ([]byte, error) {
|
|
|
|
s.mtx.Lock()
|
|
|
|
defer s.mtx.Unlock()
|
2016-08-15 14:10:35 +00:00
|
|
|
|
2018-02-07 15:36:47 +00:00
|
|
|
return s.st.MarshalBinary()
|
2016-08-15 14:10:35 +00:00
|
|
|
}
|
|
|
|
|
2018-03-22 12:53:00 +00:00
|
|
|
// Merge merges silence state received from the cluster with the local state.
|
2018-02-07 15:36:47 +00:00
|
|
|
func (s *Silences) Merge(b []byte) error {
|
|
|
|
st, err := decodeState(bytes.NewReader(b))
|
2016-08-15 14:10:35 +00:00
|
|
|
if err != nil {
|
2018-02-07 15:36:47 +00:00
|
|
|
return err
|
2016-08-15 14:10:35 +00:00
|
|
|
}
|
2018-02-07 15:36:47 +00:00
|
|
|
s.mtx.Lock()
|
|
|
|
defer s.mtx.Unlock()
|
2016-08-15 14:10:35 +00:00
|
|
|
|
2022-03-28 04:46:58 +00:00
|
|
|
now := s.nowUTC()
|
2018-11-21 10:40:57 +00:00
|
|
|
|
2018-02-07 15:36:47 +00:00
|
|
|
for _, e := range st {
|
2019-02-27 11:33:46 +00:00
|
|
|
if merged := s.st.merge(e, now); merged {
|
|
|
|
s.version++
|
|
|
|
if !cluster.OversizedMessage(b) {
|
|
|
|
// If this is the first we've seen the message and it's
|
|
|
|
// not oversized, gossip it to other nodes. We don't
|
|
|
|
// propagate oversized messages because they're sent to
|
|
|
|
// all nodes already.
|
|
|
|
s.broadcast(b)
|
|
|
|
s.metrics.propagatedMessagesTotal.Inc()
|
|
|
|
level.Debug(s.logger).Log("msg", "Gossiping new silence", "silence", e)
|
|
|
|
}
|
2018-06-08 09:48:42 +00:00
|
|
|
}
|
2016-08-15 14:10:35 +00:00
|
|
|
}
|
2018-02-07 15:36:47 +00:00
|
|
|
return nil
|
2016-08-15 14:10:35 +00:00
|
|
|
}
|
|
|
|
|
2019-02-27 17:00:27 +00:00
|
|
|
// SetBroadcast sets the provided function as the one creating data to be
|
|
|
|
// broadcast.
|
2018-02-07 15:36:47 +00:00
|
|
|
func (s *Silences) SetBroadcast(f func([]byte)) {
|
|
|
|
s.mtx.Lock()
|
|
|
|
s.broadcast = f
|
|
|
|
s.mtx.Unlock()
|
2017-09-13 09:18:01 +00:00
|
|
|
}
|
|
|
|
|
2018-02-07 15:36:47 +00:00
|
|
|
type state map[string]*pb.MeshSilence
|
2016-08-15 14:10:35 +00:00
|
|
|
|
2018-11-21 10:40:57 +00:00
|
|
|
func (s state) merge(e *pb.MeshSilence, now time.Time) bool {
|
2019-02-27 17:00:27 +00:00
|
|
|
id := e.Silence.Id
|
2018-11-21 10:40:57 +00:00
|
|
|
if e.ExpiresAt.Before(now) {
|
|
|
|
return false
|
|
|
|
}
|
2018-02-07 15:36:47 +00:00
|
|
|
// Comments list was moved to a single comment. Apply upgrade
|
|
|
|
// on silences received from peers.
|
|
|
|
if len(e.Silence.Comments) > 0 {
|
|
|
|
e.Silence.Comment = e.Silence.Comments[0].Comment
|
|
|
|
e.Silence.CreatedBy = e.Silence.Comments[0].Author
|
|
|
|
e.Silence.Comments = nil
|
2016-08-15 14:10:35 +00:00
|
|
|
}
|
2017-09-13 09:18:01 +00:00
|
|
|
|
2018-02-07 15:36:47 +00:00
|
|
|
prev, ok := s[id]
|
2018-06-08 09:48:42 +00:00
|
|
|
if !ok || prev.Silence.UpdatedAt.Before(e.Silence.UpdatedAt) {
|
2018-02-07 15:36:47 +00:00
|
|
|
s[id] = e
|
2018-06-08 09:48:42 +00:00
|
|
|
return true
|
2016-08-15 14:10:35 +00:00
|
|
|
}
|
2018-06-08 09:48:42 +00:00
|
|
|
return false
|
2016-08-15 14:10:35 +00:00
|
|
|
}
|
|
|
|
|
2018-02-07 15:36:47 +00:00
|
|
|
func (s state) MarshalBinary() ([]byte, error) {
|
|
|
|
var buf bytes.Buffer
|
2017-09-13 09:18:01 +00:00
|
|
|
|
2018-02-07 15:36:47 +00:00
|
|
|
for _, e := range s {
|
|
|
|
if _, err := pbutil.WriteDelimited(&buf, e); err != nil {
|
|
|
|
return nil, err
|
2016-08-15 14:10:35 +00:00
|
|
|
}
|
|
|
|
}
|
2018-02-07 15:36:47 +00:00
|
|
|
return buf.Bytes(), nil
|
2016-08-15 14:10:35 +00:00
|
|
|
}
|
|
|
|
|
2018-02-07 15:36:47 +00:00
|
|
|
func decodeState(r io.Reader) (state, error) {
|
|
|
|
st := state{}
|
|
|
|
for {
|
|
|
|
var s pb.MeshSilence
|
|
|
|
_, err := pbutil.ReadDelimited(r, &s)
|
|
|
|
if err == nil {
|
2018-04-10 08:12:05 +00:00
|
|
|
if s.Silence == nil {
|
|
|
|
return nil, ErrInvalidState
|
|
|
|
}
|
2018-02-07 15:36:47 +00:00
|
|
|
st[s.Silence.Id] = &s
|
2016-08-15 14:10:35 +00:00
|
|
|
continue
|
|
|
|
}
|
2018-02-07 15:36:47 +00:00
|
|
|
if err == io.EOF {
|
|
|
|
break
|
2016-08-15 14:10:35 +00:00
|
|
|
}
|
2018-02-07 15:36:47 +00:00
|
|
|
return nil, err
|
2016-08-15 14:10:35 +00:00
|
|
|
}
|
2018-02-07 15:36:47 +00:00
|
|
|
return st, nil
|
2016-08-15 14:10:35 +00:00
|
|
|
}
|
|
|
|
|
2018-03-22 12:53:00 +00:00
|
|
|
func marshalMeshSilence(e *pb.MeshSilence) ([]byte, error) {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
if _, err := pbutil.WriteDelimited(&buf, e); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return buf.Bytes(), nil
|
|
|
|
}
|
|
|
|
|
2016-08-15 14:10:35 +00:00
|
|
|
// replaceFile wraps a file that is moved to another filename on closing.
|
|
|
|
type replaceFile struct {
|
|
|
|
*os.File
|
|
|
|
filename string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *replaceFile) Close() error {
|
|
|
|
if err := f.File.Sync(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := f.File.Close(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return os.Rename(f.File.Name(), f.filename)
|
|
|
|
}
|
|
|
|
|
|
|
|
// openReplace opens a new temporary file that is moved to filename on closing.
|
|
|
|
func openReplace(filename string) (*replaceFile, error) {
|
|
|
|
tmpFilename := fmt.Sprintf("%s.%x", filename, uint64(rand.Int63()))
|
|
|
|
|
|
|
|
f, err := os.Create(tmpFilename)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
rf := &replaceFile{
|
|
|
|
File: f,
|
|
|
|
filename: filename,
|
|
|
|
}
|
|
|
|
return rf, nil
|
|
|
|
}
|