Alerting: Persist notification log and silences to the database (#39005)
* Alerting: Persist notification log and silences to the database This removes the dependency of having persistent disk to run grafana alerting. Instead of regularly flushing the notification log and silences to disk we now flush the binary content of those files to the database encoded as a base64 string.
This commit is contained in:
@@ -2,6 +2,8 @@ package notifier
|
||||
|
||||
import (
|
||||
"context"
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
"github.com/grafana/grafana/pkg/services/ngalert/models"
|
||||
"github.com/grafana/grafana/pkg/services/ngalert/store"
|
||||
@@ -54,3 +56,76 @@ type FakeOrgStore struct {
|
||||
func (f *FakeOrgStore) GetOrgs(_ context.Context) ([]int64, error) {
|
||||
return f.orgs, nil
|
||||
}
|
||||
|
||||
type FakeKVStore struct {
|
||||
mtx sync.Mutex
|
||||
store map[int64]map[string]map[string]string
|
||||
}
|
||||
|
||||
func newFakeKVStore(t *testing.T) *FakeKVStore {
|
||||
t.Helper()
|
||||
|
||||
return &FakeKVStore{
|
||||
store: map[int64]map[string]map[string]string{},
|
||||
}
|
||||
}
|
||||
|
||||
func (fkv *FakeKVStore) Get(_ context.Context, orgId int64, namespace string, key string) (string, bool, error) {
|
||||
fkv.mtx.Lock()
|
||||
defer fkv.mtx.Unlock()
|
||||
org, ok := fkv.store[orgId]
|
||||
if !ok {
|
||||
return "", false, nil
|
||||
}
|
||||
k, ok := org[namespace]
|
||||
if !ok {
|
||||
return "", false, nil
|
||||
}
|
||||
|
||||
v, ok := k[key]
|
||||
if !ok {
|
||||
return "", false, nil
|
||||
}
|
||||
|
||||
return v, true, nil
|
||||
}
|
||||
func (fkv *FakeKVStore) Set(_ context.Context, orgId int64, namespace string, key string, value string) error {
|
||||
fkv.mtx.Lock()
|
||||
defer fkv.mtx.Unlock()
|
||||
org, ok := fkv.store[orgId]
|
||||
if !ok {
|
||||
fkv.store[orgId] = map[string]map[string]string{}
|
||||
}
|
||||
_, ok = org[namespace]
|
||||
if !ok {
|
||||
fkv.store[orgId][namespace] = map[string]string{}
|
||||
}
|
||||
|
||||
fkv.store[orgId][namespace][key] = value
|
||||
|
||||
return nil
|
||||
}
|
||||
func (fkv *FakeKVStore) Del(_ context.Context, orgId int64, namespace string, key string) error {
|
||||
fkv.mtx.Lock()
|
||||
defer fkv.mtx.Unlock()
|
||||
org, ok := fkv.store[orgId]
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
_, ok = org[namespace]
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
|
||||
delete(fkv.store[orgId][namespace], key)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
type fakeState struct {
|
||||
data string
|
||||
}
|
||||
|
||||
func (fs *fakeState) MarshalBinary() ([]byte, error) {
|
||||
return []byte(fs.data), nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user