2016-08-18 09:27:59 +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.
|
|
|
|
|
|
|
|
// Package nflog implements a garbage-collected and snapshottable append-only log of
|
|
|
|
// active/resolved notifications. Each log entry stores the active/resolved state,
|
|
|
|
// the notified receiver, and a hash digest of the notification's identifying contents.
|
2018-01-10 13:53:57 +00:00
|
|
|
// The log can be queried along different parameters.
|
2016-08-11 09:33:59 +00:00
|
|
|
package nflog
|
|
|
|
|
|
|
|
import (
|
2016-08-12 12:28:07 +00:00
|
|
|
"bytes"
|
2016-08-11 09:33:59 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2016-08-12 13:15:16 +00:00
|
|
|
"math/rand"
|
|
|
|
"os"
|
2016-08-11 09:33:59 +00:00
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2024-10-22 15:52:54 +00:00
|
|
|
"github.com/coder/quartz"
|
2021-07-30 08:11:43 +00:00
|
|
|
"github.com/go-kit/log"
|
|
|
|
"github.com/go-kit/log/level"
|
2016-08-11 09:33:59 +00:00
|
|
|
"github.com/matttproud/golang_protobuf_extensions/pbutil"
|
2022-03-25 16:59:51 +00:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
|
|
2018-06-15 11:40:21 +00:00
|
|
|
"github.com/prometheus/alertmanager/cluster"
|
2016-08-11 09:33:59 +00:00
|
|
|
pb "github.com/prometheus/alertmanager/nflog/nflogpb"
|
|
|
|
)
|
|
|
|
|
2016-08-18 09:27:59 +00:00
|
|
|
// ErrNotFound is returned for empty query results.
|
2016-08-11 09:33:59 +00:00
|
|
|
var ErrNotFound = errors.New("not found")
|
|
|
|
|
2018-04-10 08:11:40 +00:00
|
|
|
// ErrInvalidState is returned if the state isn't valid.
|
2019-09-25 08:31:01 +00:00
|
|
|
var ErrInvalidState = errors.New("invalid state")
|
2018-04-10 08:11:40 +00:00
|
|
|
|
2016-08-11 09:33:59 +00:00
|
|
|
// query currently allows filtering by and/or receiver group key.
|
|
|
|
// It is configured via QueryParameter functions.
|
|
|
|
//
|
2024-03-21 11:26:46 +00:00
|
|
|
// TODO(fabxc): Future versions could allow querying a certain receiver,
|
2016-08-11 09:33:59 +00:00
|
|
|
// group or a given time interval.
|
|
|
|
type query struct {
|
|
|
|
recv *pb.Receiver
|
2017-04-21 09:43:12 +00:00
|
|
|
groupKey string
|
2016-08-11 09:33:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// QueryParam is a function that modifies a query to incorporate
|
|
|
|
// a set of parameters. Returns an error for invalid or conflicting
|
|
|
|
// parameters.
|
|
|
|
type QueryParam func(*query) error
|
|
|
|
|
|
|
|
// QReceiver adds a receiver parameter to a query.
|
|
|
|
func QReceiver(r *pb.Receiver) QueryParam {
|
|
|
|
return func(q *query) error {
|
|
|
|
q.recv = r
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// QGroupKey adds a group key as querying argument.
|
2017-04-21 09:43:12 +00:00
|
|
|
func QGroupKey(gk string) QueryParam {
|
2016-08-11 09:33:59 +00:00
|
|
|
return func(q *query) error {
|
|
|
|
q.groupKey = gk
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-19 16:39:03 +00:00
|
|
|
// Log holds the notification log state for alerts that have been notified.
|
2018-02-07 15:36:47 +00:00
|
|
|
type Log struct {
|
2024-10-22 15:52:54 +00:00
|
|
|
clock quartz.Clock
|
2023-01-19 16:39:03 +00:00
|
|
|
|
2016-08-16 06:44:16 +00:00
|
|
|
logger log.Logger
|
2016-11-21 14:22:35 +00:00
|
|
|
metrics *metrics
|
2016-08-12 13:15:16 +00:00
|
|
|
retention time.Duration
|
|
|
|
|
2016-08-11 09:33:59 +00:00
|
|
|
// For now we only store the most recently added log entry.
|
|
|
|
// The key is a serialized concatenation of group key and receiver.
|
2023-01-19 16:39:03 +00:00
|
|
|
mtx sync.RWMutex
|
|
|
|
st state
|
|
|
|
broadcast func([]byte)
|
2016-08-11 09:33:59 +00:00
|
|
|
}
|
|
|
|
|
2021-09-06 10:49:39 +00:00
|
|
|
// MaintenanceFunc represents the function to run as part of the periodic maintenance for the nflog.
|
|
|
|
// It returns the size of the snapshot taken or an error if it failed.
|
|
|
|
type MaintenanceFunc func() (int64, error)
|
|
|
|
|
2016-11-21 14:22:35 +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
|
|
|
|
propagatedMessagesTotal prometheus.Counter
|
2023-03-08 10:29:05 +00:00
|
|
|
maintenanceTotal prometheus.Counter
|
|
|
|
maintenanceErrorsTotal prometheus.Counter
|
2016-11-21 14:22:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func newMetrics(r prometheus.Registerer) *metrics {
|
|
|
|
m := &metrics{}
|
|
|
|
|
|
|
|
m.gcDuration = prometheus.NewSummary(prometheus.SummaryOpts{
|
2019-06-11 23:41:15 +00:00
|
|
|
Name: "alertmanager_nflog_gc_duration_seconds",
|
|
|
|
Help: "Duration of the last notification log garbage collection cycle.",
|
|
|
|
Objectives: map[float64]float64{},
|
2016-11-21 14:22:35 +00:00
|
|
|
})
|
|
|
|
m.snapshotDuration = prometheus.NewSummary(prometheus.SummaryOpts{
|
2019-06-11 23:41:15 +00:00
|
|
|
Name: "alertmanager_nflog_snapshot_duration_seconds",
|
|
|
|
Help: "Duration of the last notification log snapshot.",
|
|
|
|
Objectives: map[float64]float64{},
|
2016-11-21 14:22:35 +00:00
|
|
|
})
|
2018-01-10 13:53:57 +00:00
|
|
|
m.snapshotSize = prometheus.NewGauge(prometheus.GaugeOpts{
|
|
|
|
Name: "alertmanager_nflog_snapshot_size_bytes",
|
|
|
|
Help: "Size of the last notification log snapshot in bytes.",
|
|
|
|
})
|
2023-03-08 10:29:05 +00:00
|
|
|
m.maintenanceTotal = prometheus.NewCounter(prometheus.CounterOpts{
|
|
|
|
Name: "alertmanager_nflog_maintenance_total",
|
|
|
|
Help: "How many maintenances were executed for the notification log.",
|
|
|
|
})
|
|
|
|
m.maintenanceErrorsTotal = prometheus.NewCounter(prometheus.CounterOpts{
|
|
|
|
Name: "alertmanager_nflog_maintenance_errors_total",
|
|
|
|
Help: "How many maintenances were executed for the notification log that failed.",
|
|
|
|
})
|
2016-11-21 14:22:35 +00:00
|
|
|
m.queriesTotal = prometheus.NewCounter(prometheus.CounterOpts{
|
|
|
|
Name: "alertmanager_nflog_queries_total",
|
|
|
|
Help: "Number of notification log queries were received.",
|
|
|
|
})
|
|
|
|
m.queryErrorsTotal = prometheus.NewCounter(prometheus.CounterOpts{
|
|
|
|
Name: "alertmanager_nflog_query_errors_total",
|
|
|
|
Help: "Number notification log received queries that failed.",
|
|
|
|
})
|
|
|
|
m.queryDuration = prometheus.NewHistogram(prometheus.HistogramOpts{
|
2024-02-29 14:53:47 +00:00
|
|
|
Name: "alertmanager_nflog_query_duration_seconds",
|
|
|
|
Help: "Duration of notification log query evaluation.",
|
|
|
|
Buckets: prometheus.DefBuckets,
|
|
|
|
NativeHistogramBucketFactor: 1.1,
|
|
|
|
NativeHistogramMaxBucketNumber: 100,
|
|
|
|
NativeHistogramMinResetDuration: 1 * time.Hour,
|
2016-11-21 14:22:35 +00:00
|
|
|
})
|
2018-06-08 09:48:42 +00:00
|
|
|
m.propagatedMessagesTotal = prometheus.NewCounter(prometheus.CounterOpts{
|
|
|
|
Name: "alertmanager_nflog_gossip_messages_propagated_total",
|
|
|
|
Help: "Number of received gossip messages that have been further gossiped.",
|
|
|
|
})
|
2016-11-21 14:22:35 +00:00
|
|
|
|
|
|
|
if r != nil {
|
|
|
|
r.MustRegister(
|
|
|
|
m.gcDuration,
|
|
|
|
m.snapshotDuration,
|
2018-06-12 11:59:48 +00:00
|
|
|
m.snapshotSize,
|
2016-11-21 14:22:35 +00:00
|
|
|
m.queriesTotal,
|
|
|
|
m.queryErrorsTotal,
|
|
|
|
m.queryDuration,
|
2018-06-08 09:48:42 +00:00
|
|
|
m.propagatedMessagesTotal,
|
2023-03-08 10:29:05 +00:00
|
|
|
m.maintenanceTotal,
|
|
|
|
m.maintenanceErrorsTotal,
|
2016-11-21 14:22:35 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
2018-02-07 15:36:47 +00:00
|
|
|
type state map[string]*pb.MeshEntry
|
|
|
|
|
|
|
|
func (s state) clone() state {
|
|
|
|
c := make(state, len(s))
|
|
|
|
for k, v := range s {
|
|
|
|
c[k] = v
|
|
|
|
}
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2018-06-08 09:48:42 +00:00
|
|
|
// merge returns true or false whether the MeshEntry was merged or
|
|
|
|
// not. This information is used to decide to gossip the message further.
|
2018-11-21 10:40:57 +00:00
|
|
|
func (s state) merge(e *pb.MeshEntry, now time.Time) bool {
|
|
|
|
if e.ExpiresAt.Before(now) {
|
|
|
|
return false
|
|
|
|
}
|
2018-02-07 15:36:47 +00:00
|
|
|
k := stateKey(string(e.Entry.GroupKey), e.Entry.Receiver)
|
|
|
|
|
|
|
|
prev, ok := s[k]
|
|
|
|
if !ok || prev.Entry.Timestamp.Before(e.Entry.Timestamp) {
|
|
|
|
s[k] = e
|
2018-06-08 09:48:42 +00:00
|
|
|
return true
|
2018-02-07 15:36:47 +00:00
|
|
|
}
|
2018-06-08 09:48:42 +00:00
|
|
|
return false
|
2018-02-07 15:36:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s state) MarshalBinary() ([]byte, error) {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
|
|
|
|
for _, e := range s {
|
|
|
|
if _, err := pbutil.WriteDelimited(&buf, e); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return buf.Bytes(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func decodeState(r io.Reader) (state, error) {
|
|
|
|
st := state{}
|
|
|
|
for {
|
|
|
|
var e pb.MeshEntry
|
|
|
|
_, err := pbutil.ReadDelimited(r, &e)
|
|
|
|
if err == nil {
|
2018-04-10 08:11:40 +00:00
|
|
|
if e.Entry == nil || e.Entry.Receiver == nil {
|
|
|
|
return nil, ErrInvalidState
|
|
|
|
}
|
2018-02-07 15:36:47 +00:00
|
|
|
st[stateKey(string(e.Entry.GroupKey), e.Entry.Receiver)] = &e
|
|
|
|
continue
|
|
|
|
}
|
2023-11-24 21:17:35 +00:00
|
|
|
if errors.Is(err, io.EOF) {
|
2018-02-07 15:36:47 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return st, nil
|
|
|
|
}
|
|
|
|
|
2018-03-22 12:53:00 +00:00
|
|
|
func marshalMeshEntry(e *pb.MeshEntry) ([]byte, error) {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
if _, err := pbutil.WriteDelimited(&buf, e); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return buf.Bytes(), nil
|
|
|
|
}
|
|
|
|
|
2023-01-19 16:39:03 +00:00
|
|
|
// Options configures a new Log implementation.
|
|
|
|
type Options struct {
|
|
|
|
SnapshotReader io.Reader
|
|
|
|
SnapshotFile string
|
|
|
|
|
|
|
|
Retention time.Duration
|
|
|
|
|
|
|
|
Logger log.Logger
|
|
|
|
Metrics prometheus.Registerer
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *Options) validate() error {
|
|
|
|
if o.SnapshotFile != "" && o.SnapshotReader != nil {
|
|
|
|
return errors.New("only one of SnapshotFile and SnapshotReader must be set")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-08-11 09:33:59 +00:00
|
|
|
// New creates a new notification log based on the provided options.
|
|
|
|
// The snapshot is loaded into the Log if it is set.
|
2023-01-19 16:39:03 +00:00
|
|
|
func New(o Options) (*Log, error) {
|
|
|
|
if err := o.validate(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-02-07 15:36:47 +00:00
|
|
|
l := &Log{
|
2024-10-22 15:52:54 +00:00
|
|
|
clock: quartz.NewReal(),
|
2023-01-19 16:39:03 +00:00
|
|
|
retention: o.Retention,
|
2018-02-07 15:36:47 +00:00
|
|
|
logger: log.NewNopLogger(),
|
|
|
|
st: state{},
|
|
|
|
broadcast: func([]byte) {},
|
2023-01-19 16:39:03 +00:00
|
|
|
metrics: newMetrics(o.Metrics),
|
2016-11-21 14:22:35 +00:00
|
|
|
}
|
|
|
|
|
2023-01-19 16:39:03 +00:00
|
|
|
if o.Logger != nil {
|
|
|
|
l.logger = o.Logger
|
|
|
|
}
|
2016-08-16 12:09:06 +00:00
|
|
|
|
2023-01-19 16:39:03 +00:00
|
|
|
if o.SnapshotFile != "" {
|
|
|
|
if r, err := os.Open(o.SnapshotFile); err != nil {
|
|
|
|
if !os.IsNotExist(err) {
|
|
|
|
return nil, err
|
2016-08-16 12:09:06 +00:00
|
|
|
}
|
2023-01-19 16:39:03 +00:00
|
|
|
level.Debug(l.logger).Log("msg", "notification log snapshot file doesn't exist", "err", err)
|
|
|
|
} else {
|
|
|
|
o.SnapshotReader = r
|
|
|
|
defer r.Close()
|
2016-08-11 09:33:59 +00:00
|
|
|
}
|
|
|
|
}
|
2016-11-21 14:22:35 +00:00
|
|
|
|
2023-01-19 16:39:03 +00:00
|
|
|
if o.SnapshotReader != nil {
|
|
|
|
if err := l.loadSnapshot(o.SnapshotReader); err != nil {
|
|
|
|
return l, err
|
|
|
|
}
|
|
|
|
}
|
2016-08-12 13:15:16 +00:00
|
|
|
|
2016-08-11 09:33:59 +00:00
|
|
|
return l, nil
|
|
|
|
}
|
|
|
|
|
2023-01-19 16:39:03 +00:00
|
|
|
func (l *Log) now() time.Time {
|
|
|
|
return l.clock.Now()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Maintenance garbage collects the notification log state at the given interval. If the snapshot
|
|
|
|
// file is set, a snapshot is written to it afterwards.
|
|
|
|
// Terminates on receiving from stopc.
|
|
|
|
// If not nil, the last argument is an override for what to do as part of the maintenance - for advanced usage.
|
|
|
|
func (l *Log) Maintenance(interval time.Duration, snapf string, stopc <-chan struct{}, override MaintenanceFunc) {
|
|
|
|
if interval == 0 || stopc == nil {
|
|
|
|
level.Error(l.logger).Log("msg", "interval or stop signal are missing - not running maintenance")
|
2016-08-12 13:15:16 +00:00
|
|
|
return
|
|
|
|
}
|
2024-10-22 15:52:54 +00:00
|
|
|
t := l.clock.NewTicker(interval)
|
2016-08-12 13:15:16 +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-12 13:15:16 +00:00
|
|
|
if _, err := l.GC(); err != nil {
|
2021-09-06 10:49:39 +00:00
|
|
|
return size, err
|
2016-08-12 13:15:16 +00:00
|
|
|
}
|
2023-01-19 16:39:03 +00:00
|
|
|
if snapf == "" {
|
2021-09-06 10:49:39 +00:00
|
|
|
return size, nil
|
2016-08-12 13:15:16 +00:00
|
|
|
}
|
2023-01-19 16:39:03 +00:00
|
|
|
f, err := openReplace(snapf)
|
2016-08-12 13:15:16 +00:00
|
|
|
if err != nil {
|
2021-09-06 10:49:39 +00:00
|
|
|
return size, err
|
2016-08-12 13:15:16 +00:00
|
|
|
}
|
2018-01-10 13:53:57 +00:00
|
|
|
if size, err = l.Snapshot(f); err != nil {
|
2023-01-19 16:39:03 +00:00
|
|
|
f.Close()
|
2021-09-06 10:49:39 +00:00
|
|
|
return size, err
|
2016-08-12 13:15:16 +00:00
|
|
|
}
|
2021-09-06 10:49:39 +00:00
|
|
|
return size, f.Close()
|
|
|
|
}
|
|
|
|
|
2023-01-19 16:39:03 +00:00
|
|
|
if override != nil {
|
|
|
|
doMaintenance = override
|
2021-09-06 10:49:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
runMaintenance := func(do func() (int64, error)) error {
|
2023-03-08 10:29:05 +00:00
|
|
|
l.metrics.maintenanceTotal.Inc()
|
2023-01-19 16:39:03 +00:00
|
|
|
start := l.now().UTC()
|
2021-09-06 10:49:39 +00:00
|
|
|
level.Debug(l.logger).Log("msg", "Running maintenance")
|
|
|
|
size, err := do()
|
|
|
|
l.metrics.snapshotSize.Set(float64(size))
|
2023-03-08 10:29:05 +00:00
|
|
|
if err != nil {
|
|
|
|
l.metrics.maintenanceErrorsTotal.Inc()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
level.Debug(l.logger).Log("msg", "Maintenance done", "duration", l.now().Sub(start), "size", size)
|
|
|
|
return nil
|
2016-08-12 13:15:16 +00:00
|
|
|
}
|
|
|
|
|
2016-08-19 10:01:16 +00:00
|
|
|
Loop:
|
2016-08-12 13:15:16 +00:00
|
|
|
for {
|
|
|
|
select {
|
2023-01-19 16:39:03 +00:00
|
|
|
case <-stopc:
|
2016-08-19 10:01:16 +00:00
|
|
|
break Loop
|
2016-08-12 13:15:16 +00:00
|
|
|
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.Error(l.logger).Log("msg", "Running maintenance failed", "err", err)
|
2016-08-16 06:44:16 +00:00
|
|
|
}
|
2016-08-12 13:15:16 +00:00
|
|
|
}
|
|
|
|
}
|
2023-01-19 16:39:03 +00:00
|
|
|
|
2016-08-16 06:44:16 +00:00
|
|
|
// No need to run final maintenance if we don't want to snapshot.
|
2023-01-19 16:39:03 +00:00
|
|
|
if snapf == "" {
|
2016-08-16 06:44:16 +00:00
|
|
|
return
|
|
|
|
}
|
2021-09-06 10:49:39 +00:00
|
|
|
if err := runMaintenance(doMaintenance); err != nil {
|
2017-10-22 05:59:33 +00:00
|
|
|
level.Error(l.logger).Log("msg", "Creating shutdown snapshot failed", "err", err)
|
2016-08-16 06:44:16 +00:00
|
|
|
}
|
2016-08-12 13:15:16 +00:00
|
|
|
}
|
|
|
|
|
2017-04-21 09:43:12 +00:00
|
|
|
func receiverKey(r *pb.Receiver) string {
|
|
|
|
return fmt.Sprintf("%s/%s/%d", r.GroupName, r.Integration, r.Idx)
|
|
|
|
}
|
|
|
|
|
2016-08-12 14:48:09 +00:00
|
|
|
// stateKey returns a string key for a log entry consisting of the group key
|
2016-08-12 12:28:07 +00:00
|
|
|
// and receiver.
|
2017-04-21 09:43:12 +00:00
|
|
|
func stateKey(k string, r *pb.Receiver) string {
|
|
|
|
return fmt.Sprintf("%s:%s", k, receiverKey(r))
|
2016-08-12 12:28:07 +00:00
|
|
|
}
|
|
|
|
|
2022-07-05 10:09:18 +00:00
|
|
|
func (l *Log) Log(r *pb.Receiver, gkey string, firingAlerts, resolvedAlerts []uint64, expiry time.Duration) error {
|
2016-08-12 12:28:07 +00:00
|
|
|
// Write all st with the same timestamp.
|
2016-08-11 09:33:59 +00:00
|
|
|
now := l.now()
|
2016-08-12 12:28:07 +00:00
|
|
|
key := stateKey(gkey, r)
|
2016-08-11 09:33:59 +00:00
|
|
|
|
|
|
|
l.mtx.Lock()
|
|
|
|
defer l.mtx.Unlock()
|
|
|
|
|
2016-08-12 12:28:07 +00:00
|
|
|
if prevle, ok := l.st[key]; ok {
|
2016-08-11 09:33:59 +00:00
|
|
|
// Entry already exists, only overwrite if timestamp is newer.
|
2017-04-18 08:03:57 +00:00
|
|
|
// This may happen with raciness or clock-drift across AM nodes.
|
|
|
|
if prevle.Entry.Timestamp.After(now) {
|
2016-08-11 09:33:59 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-05 10:09:18 +00:00
|
|
|
expiresAt := now.Add(l.retention)
|
|
|
|
if expiry > 0 && l.retention > expiry {
|
|
|
|
expiresAt = now.Add(expiry)
|
|
|
|
}
|
|
|
|
|
2016-08-19 10:20:04 +00:00
|
|
|
e := &pb.MeshEntry{
|
2016-08-11 09:33:59 +00:00
|
|
|
Entry: &pb.Entry{
|
2017-03-13 12:44:36 +00:00
|
|
|
Receiver: r,
|
2017-04-21 09:43:12 +00:00
|
|
|
GroupKey: []byte(gkey),
|
2017-04-18 08:03:57 +00:00
|
|
|
Timestamp: now,
|
2017-03-13 12:44:36 +00:00
|
|
|
FiringAlerts: firingAlerts,
|
|
|
|
ResolvedAlerts: resolvedAlerts,
|
2016-08-11 09:33:59 +00:00
|
|
|
},
|
2022-07-05 10:09:18 +00:00
|
|
|
ExpiresAt: expiresAt,
|
2016-08-11 09:33:59 +00:00
|
|
|
}
|
2018-02-07 15:36:47 +00:00
|
|
|
|
2018-03-22 12:53:00 +00:00
|
|
|
b, err := marshalMeshEntry(e)
|
2018-02-07 15:36:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2017-11-01 09:34:40 +00:00
|
|
|
}
|
2018-11-21 10:40:57 +00:00
|
|
|
l.st.merge(e, l.now())
|
2018-02-07 15:36:47 +00:00
|
|
|
l.broadcast(b)
|
2016-08-19 10:20:04 +00:00
|
|
|
|
2016-08-11 09:33:59 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-08-12 13:15:16 +00:00
|
|
|
// GC implements the Log interface.
|
2018-02-07 15:36:47 +00:00
|
|
|
func (l *Log) GC() (int, error) {
|
2016-11-21 14:22:35 +00:00
|
|
|
start := time.Now()
|
|
|
|
defer func() { l.metrics.gcDuration.Observe(time.Since(start).Seconds()) }()
|
|
|
|
|
2016-08-11 09:33:59 +00:00
|
|
|
now := l.now()
|
|
|
|
var n int
|
|
|
|
|
2016-08-12 12:28:07 +00:00
|
|
|
l.mtx.Lock()
|
|
|
|
defer l.mtx.Unlock()
|
|
|
|
|
|
|
|
for k, le := range l.st {
|
2017-04-18 08:03:57 +00:00
|
|
|
if le.ExpiresAt.IsZero() {
|
|
|
|
return n, errors.New("unexpected zero expiration timestamp")
|
|
|
|
}
|
|
|
|
if !le.ExpiresAt.After(now) {
|
2016-08-12 12:28:07 +00:00
|
|
|
delete(l.st, k)
|
2016-08-11 09:33:59 +00:00
|
|
|
n++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return n, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Query implements the Log interface.
|
2018-02-07 15:36:47 +00:00
|
|
|
func (l *Log) Query(params ...QueryParam) ([]*pb.Entry, error) {
|
2016-11-21 14:22:35 +00:00
|
|
|
start := time.Now()
|
|
|
|
l.metrics.queriesTotal.Inc()
|
|
|
|
|
|
|
|
entries, err := func() ([]*pb.Entry, error) {
|
|
|
|
q := &query{}
|
|
|
|
for _, p := range params {
|
|
|
|
if err := p(q); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// TODO(fabxc): For now our only query mode is the most recent entry for a
|
|
|
|
// receiver/group_key combination.
|
2017-04-21 09:43:12 +00:00
|
|
|
if q.recv == nil || q.groupKey == "" {
|
2016-11-21 14:22:35 +00:00
|
|
|
// TODO(fabxc): allow more complex queries in the future.
|
|
|
|
// How to enable pagination?
|
|
|
|
return nil, errors.New("no query parameters specified")
|
2016-08-11 09:33:59 +00:00
|
|
|
}
|
|
|
|
|
2016-11-21 14:22:35 +00:00
|
|
|
l.mtx.RLock()
|
|
|
|
defer l.mtx.RUnlock()
|
2016-08-11 09:33:59 +00:00
|
|
|
|
2016-11-21 14:22:35 +00:00
|
|
|
if le, ok := l.st[stateKey(q.groupKey, q.recv)]; ok {
|
|
|
|
return []*pb.Entry{le.Entry}, nil
|
|
|
|
}
|
|
|
|
return nil, ErrNotFound
|
|
|
|
}()
|
|
|
|
if err != nil {
|
|
|
|
l.metrics.queryErrorsTotal.Inc()
|
2016-08-11 09:33:59 +00:00
|
|
|
}
|
2016-11-21 14:22:35 +00:00
|
|
|
l.metrics.queryDuration.Observe(time.Since(start).Seconds())
|
|
|
|
return entries, err
|
2016-08-11 09:33:59 +00:00
|
|
|
}
|
|
|
|
|
2016-08-12 12:28:07 +00:00
|
|
|
// loadSnapshot loads a snapshot generated by Snapshot() into the state.
|
2018-02-07 15:36:47 +00:00
|
|
|
func (l *Log) loadSnapshot(r io.Reader) error {
|
|
|
|
st, err := decodeState(r)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2016-08-11 09:33:59 +00:00
|
|
|
}
|
2018-02-07 15:36:47 +00:00
|
|
|
|
|
|
|
l.mtx.Lock()
|
2016-08-12 13:15:16 +00:00
|
|
|
l.st = st
|
2018-02-07 15:36:47 +00:00
|
|
|
l.mtx.Unlock()
|
2016-08-11 09:33:59 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Snapshot implements the Log interface.
|
2018-02-07 15:36:47 +00:00
|
|
|
func (l *Log) Snapshot(w io.Writer) (int64, error) {
|
2016-11-21 14:22:35 +00:00
|
|
|
start := time.Now()
|
|
|
|
defer func() { l.metrics.snapshotDuration.Observe(time.Since(start).Seconds()) }()
|
|
|
|
|
2016-08-11 09:33:59 +00:00
|
|
|
l.mtx.RLock()
|
|
|
|
defer l.mtx.RUnlock()
|
|
|
|
|
2018-02-07 15:36:47 +00:00
|
|
|
b, err := l.st.MarshalBinary()
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
2016-08-11 09:33:59 +00:00
|
|
|
}
|
2016-08-12 12:28:07 +00:00
|
|
|
|
2018-02-07 15:36:47 +00:00
|
|
|
return io.Copy(w, bytes.NewReader(b))
|
2016-08-12 12:28:07 +00:00
|
|
|
}
|
|
|
|
|
2018-02-07 15:36:47 +00:00
|
|
|
// MarshalBinary serializes all contents of the notification log.
|
|
|
|
func (l *Log) MarshalBinary() ([]byte, error) {
|
2016-08-12 12:28:07 +00:00
|
|
|
l.mtx.Lock()
|
|
|
|
defer l.mtx.Unlock()
|
|
|
|
|
2018-02-07 15:36:47 +00:00
|
|
|
return l.st.MarshalBinary()
|
2016-08-12 12:28:07 +00:00
|
|
|
}
|
|
|
|
|
2018-03-22 12:53:00 +00:00
|
|
|
// Merge merges notification log state received from the cluster with the local state.
|
2018-02-07 15:36:47 +00:00
|
|
|
func (l *Log) Merge(b []byte) error {
|
|
|
|
st, err := decodeState(bytes.NewReader(b))
|
2016-08-12 12:28:07 +00:00
|
|
|
if err != nil {
|
2018-02-07 15:36:47 +00:00
|
|
|
return err
|
2016-08-12 12:28:07 +00:00
|
|
|
}
|
|
|
|
l.mtx.Lock()
|
|
|
|
defer l.mtx.Unlock()
|
2018-11-21 10:40:57 +00:00
|
|
|
now := l.now()
|
2016-08-12 12:28:07 +00:00
|
|
|
|
2018-02-07 15:36:47 +00:00
|
|
|
for _, e := range st {
|
2018-11-21 10:40:57 +00:00
|
|
|
if merged := l.st.merge(e, now); merged && !cluster.OversizedMessage(b) {
|
2018-06-15 11:40:21 +00:00
|
|
|
// 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.
|
2018-06-08 09:48:42 +00:00
|
|
|
l.broadcast(b)
|
|
|
|
l.metrics.propagatedMessagesTotal.Inc()
|
|
|
|
level.Debug(l.logger).Log("msg", "gossiping new entry", "entry", e)
|
|
|
|
}
|
2016-08-12 12:28:07 +00:00
|
|
|
}
|
2018-02-07 15:36:47 +00:00
|
|
|
return nil
|
2016-08-12 14:48:09 +00:00
|
|
|
}
|
|
|
|
|
2018-02-09 10:06:51 +00:00
|
|
|
// SetBroadcast sets a broadcast callback that will be invoked with serialized state
|
|
|
|
// on updates.
|
2018-02-07 15:36:47 +00:00
|
|
|
func (l *Log) SetBroadcast(f func([]byte)) {
|
|
|
|
l.mtx.Lock()
|
|
|
|
l.broadcast = f
|
|
|
|
l.mtx.Unlock()
|
2016-08-12 12:28:07 +00:00
|
|
|
}
|
2016-08-12 13:15:16 +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
|
|
|
|
}
|