Skip to content

Commit

Permalink
merge origin/master into refactor/change-alert-parts-calculations
Browse files Browse the repository at this point in the history
  • Loading branch information
AleksandrMatsko committed Oct 15, 2024
2 parents 1ebe417 + 0fd0b04 commit 26ba0b4
Show file tree
Hide file tree
Showing 16 changed files with 856 additions and 813 deletions.
2 changes: 1 addition & 1 deletion api/controller/triggers.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func CreateTrigger(dataBase moira.Database, trigger *dto.TriggerModel, timeSerie
return nil, api.ErrorInternalServer(err)
}
if exists {
return nil, api.ErrorInvalidRequest(fmt.Errorf("trigger with this ID already exists"))
return nil, api.ErrorInvalidRequest(fmt.Errorf("trigger with this ID (%s) already exists", trigger.ID))
}
}
resp, err := saveTrigger(dataBase, trigger.ToMoiraTrigger(), trigger.ID, timeSeriesNames)
Expand Down
5 changes: 3 additions & 2 deletions api/controller/triggers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,12 @@ func TestCreateTrigger(t *testing.T) {
})

Convey("Trigger already exists", t, func() {
triggerModel := dto.TriggerModel{ID: uuid.Must(uuid.NewV4()).String()}
triggerId := uuid.Must(uuid.NewV4()).String()
triggerModel := dto.TriggerModel{ID: triggerId}
trigger := triggerModel.ToMoiraTrigger()
dataBase.EXPECT().GetTrigger(triggerModel.ID).Return(*trigger, nil)
resp, err := CreateTrigger(dataBase, &triggerModel, make(map[string]bool))
So(err, ShouldResemble, api.ErrorInvalidRequest(fmt.Errorf("trigger with this ID already exists")))
So(err, ShouldResemble, api.ErrorInvalidRequest(fmt.Errorf("trigger with this ID (%s) already exists", triggerId)))
So(resp, ShouldBeNil)
})

Expand Down
11 changes: 7 additions & 4 deletions database/redis/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,11 @@ func NewDatabase(logger moira.Logger, config DatabaseConfig, nh NotificationHist

// NewTestDatabase use it only for tests.
func NewTestDatabase(logger moira.Logger) *DbConnector {
return NewDatabase(logger, DatabaseConfig{
Addrs: []string{"0.0.0.0:6379"},
},
return NewDatabase(
logger, DatabaseConfig{
Addrs: []string{"0.0.0.0:6379"},
MetricsTTL: time.Hour,
},
NotificationHistoryConfig{
NotificationHistoryTTL: time.Hour * 48,
},
Expand All @@ -104,7 +106,8 @@ func NewTestDatabase(logger moira.Logger) *DbConnector {
TransactionHeuristicLimit: 10000,
ResaveTime: 30 * time.Second,
},
testSource)
testSource,
)
}

// NewTestDatabaseWithIncorrectConfig use it only for tests.
Expand Down
19 changes: 11 additions & 8 deletions database/redis/notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,17 @@ import (
"strings"
"time"

"github.com/moira-alert/moira/notifier"

"github.com/go-redis/redis/v8"

"github.com/moira-alert/moira"
"github.com/moira-alert/moira/database/redis/reply"
)

// Separate const to prevent cyclic dependencies.
// Original const is declared in notifier package, notifier depends on all metric source packages.
// Thus it prevents us from using database in tests for local metric source.
const notificationsLimitUnlimited = int64(-1)

type notificationTypes struct {
Valid, ToRemove, ToResaveNew, ToResaveOld []*moira.ScheduledNotification
}
Expand Down Expand Up @@ -294,8 +297,8 @@ func (connector *DbConnector) FetchNotifications(to int64, limit int64) ([]*moir
}

// No limit
if limit == notifier.NotificationsLimitUnlimited {
return connector.fetchNotifications(to, notifier.NotificationsLimitUnlimited)
if limit == notificationsLimitUnlimited {
return connector.fetchNotifications(to, notificationsLimitUnlimited)
}

count, err := connector.notificationsCount(to)
Expand All @@ -305,7 +308,7 @@ func (connector *DbConnector) FetchNotifications(to int64, limit int64) ([]*moir

// Hope count will be not greater then limit when we call fetchNotificationsNoLimit
if limit > connector.notification.TransactionHeuristicLimit && count < limit/2 {
return connector.fetchNotifications(to, notifier.NotificationsLimitUnlimited)
return connector.fetchNotifications(to, notificationsLimitUnlimited)
}

return connector.fetchNotifications(to, limit)
Expand Down Expand Up @@ -354,7 +357,7 @@ func (connector *DbConnector) fetchNotifications(to int64, limit int64) ([]*moir
// sorted by timestamp in one transaction with or without limit, depending on whether limit is nil.
func getNotificationsInTxWithLimit(ctx context.Context, tx *redis.Tx, to int64, limit int64) ([]*moira.ScheduledNotification, error) {
var rng *redis.ZRangeBy
if limit != notifier.NotificationsLimitUnlimited {
if limit != notificationsLimitUnlimited {
rng = &redis.ZRangeBy{Min: "-inf", Max: strconv.FormatInt(to, 10), Offset: 0, Count: limit}
} else {
rng = &redis.ZRangeBy{Min: "-inf", Max: strconv.FormatInt(to, 10)}
Expand Down Expand Up @@ -393,15 +396,15 @@ func getLimitedNotifications(

limitedNotifications := notifications

if limit != notifier.NotificationsLimitUnlimited {
if limit != notificationsLimitUnlimited {
limitedNotifications = limitNotifications(notifications)
lastTs := limitedNotifications[len(limitedNotifications)-1].Timestamp

if len(notifications) == len(limitedNotifications) {
// this means that all notifications have same timestamp,
// we hope that all notifications with same timestamp should fit our memory
var err error
limitedNotifications, err = getNotificationsInTxWithLimit(ctx, tx, lastTs, notifier.NotificationsLimitUnlimited)
limitedNotifications, err = getNotificationsInTxWithLimit(ctx, tx, lastTs, notificationsLimitUnlimited)
if err != nil {
return nil, fmt.Errorf("failed to get notification without limit in transaction: %w", err)
}
Expand Down
31 changes: 15 additions & 16 deletions database/redis/notification_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/moira-alert/moira"
"github.com/moira-alert/moira/clock"
logging "github.com/moira-alert/moira/logging/zerolog_adapter"
"github.com/moira-alert/moira/notifier"
"github.com/stretchr/testify/assert"

. "github.com/smartystreets/goconvey/convey"
Expand Down Expand Up @@ -59,7 +58,7 @@ func TestScheduledNotification(t *testing.T) {
})

Convey("Test fetch notifications", func() {
actual, err := database.FetchNotifications(now-database.getDelayedTimeInSeconds(), notifier.NotificationsLimitUnlimited) //nolint
actual, err := database.FetchNotifications(now-database.getDelayedTimeInSeconds(), notificationsLimitUnlimited) //nolint
So(err, ShouldBeNil)
So(actual, ShouldResemble, []*moira.ScheduledNotification{&notificationOld})

Expand All @@ -68,7 +67,7 @@ func TestScheduledNotification(t *testing.T) {
So(total, ShouldEqual, 2)
So(actual, ShouldResemble, []*moira.ScheduledNotification{&notification, &notificationNew})

actual, err = database.FetchNotifications(now+database.getDelayedTimeInSeconds(), notifier.NotificationsLimitUnlimited) //nolint
actual, err = database.FetchNotifications(now+database.getDelayedTimeInSeconds(), notificationsLimitUnlimited) //nolint
So(err, ShouldBeNil)
So(actual, ShouldResemble, []*moira.ScheduledNotification{&notification, &notificationNew})

Expand Down Expand Up @@ -128,7 +127,7 @@ func TestScheduledNotification(t *testing.T) {
So(total, ShouldEqual, 0)
So(actual, ShouldResemble, []*moira.ScheduledNotification{})

actual, err = database.FetchNotifications(now+database.getDelayedTimeInSeconds(), notifier.NotificationsLimitUnlimited) //nolint
actual, err = database.FetchNotifications(now+database.getDelayedTimeInSeconds(), notificationsLimitUnlimited) //nolint
So(err, ShouldBeNil)
So(actual, ShouldResemble, []*moira.ScheduledNotification{})
})
Expand Down Expand Up @@ -167,7 +166,7 @@ func TestScheduledNotification(t *testing.T) {
So(total, ShouldEqual, 0)
So(actual, ShouldResemble, []*moira.ScheduledNotification{})

actual, err = database.FetchNotifications(now+database.getDelayedTimeInSeconds(), notifier.NotificationsLimitUnlimited) //nolint
actual, err = database.FetchNotifications(now+database.getDelayedTimeInSeconds(), notificationsLimitUnlimited) //nolint
So(err, ShouldBeNil)
So(actual, ShouldResemble, []*moira.ScheduledNotification{})
})
Expand Down Expand Up @@ -197,7 +196,7 @@ func TestScheduledNotificationErrorConnection(t *testing.T) {
So(err, ShouldNotBeNil)
So(total, ShouldEqual, 0)

actual2, err := database.FetchNotifications(0, notifier.NotificationsLimitUnlimited)
actual2, err := database.FetchNotifications(0, notificationsLimitUnlimited)
So(err, ShouldNotBeNil)
So(actual2, ShouldBeNil)

Expand Down Expand Up @@ -284,7 +283,7 @@ func TestFetchNotifications(t *testing.T) {

Convey("Test fetch notifications without limit", func() {
addNotifications(database, []moira.ScheduledNotification{notification, notificationNew, notificationOld})
actual, err := database.FetchNotifications(now+database.getDelayedTimeInSeconds(), notifier.NotificationsLimitUnlimited) //nolint
actual, err := database.FetchNotifications(now+database.getDelayedTimeInSeconds(), notificationsLimitUnlimited) //nolint
So(err, ShouldBeNil)
So(actual, ShouldResemble, []*moira.ScheduledNotification{&notificationOld, &notification, &notificationNew})

Expand Down Expand Up @@ -330,7 +329,7 @@ func TestGetNotificationsInTxWithLimit(t *testing.T) {
Convey("Test with zero notifications without limit", func() {
addNotifications(database, []moira.ScheduledNotification{})
err := client.Watch(ctx, func(tx *redis.Tx) error {
actual, err := getNotificationsInTxWithLimit(ctx, tx, now+database.getDelayedTimeInSeconds()*2, notifier.NotificationsLimitUnlimited)
actual, err := getNotificationsInTxWithLimit(ctx, tx, now+database.getDelayedTimeInSeconds()*2, notificationsLimitUnlimited)
So(err, ShouldBeNil)
So(actual, ShouldResemble, []*moira.ScheduledNotification{})
return nil
Expand All @@ -344,7 +343,7 @@ func TestGetNotificationsInTxWithLimit(t *testing.T) {
Convey("Test all notifications without limit", func() {
addNotifications(database, []moira.ScheduledNotification{notification, notificationNew, notificationOld})
err := client.Watch(ctx, func(tx *redis.Tx) error {
actual, err := getNotificationsInTxWithLimit(ctx, tx, now+database.getDelayedTimeInSeconds()*2, notifier.NotificationsLimitUnlimited)
actual, err := getNotificationsInTxWithLimit(ctx, tx, now+database.getDelayedTimeInSeconds()*2, notificationsLimitUnlimited)
So(err, ShouldBeNil)
So(actual, ShouldResemble, []*moira.ScheduledNotification{&notificationOld, &notification, &notificationNew})
return nil
Expand Down Expand Up @@ -418,7 +417,7 @@ func TestGetLimitedNotifications(t *testing.T) {
Convey("Test all notifications with different timestamps without limit", func() {
notifications := []*moira.ScheduledNotification{&notificationOld, &notification, &notificationNew}
err := client.Watch(ctx, func(tx *redis.Tx) error {
actual, err := getLimitedNotifications(ctx, tx, notifier.NotificationsLimitUnlimited, notifications)
actual, err := getLimitedNotifications(ctx, tx, notificationsLimitUnlimited, notifications)
So(err, ShouldBeNil)
So(actual, ShouldResemble, []*moira.ScheduledNotification{&notificationOld, &notification, &notificationNew})
return nil
Expand Down Expand Up @@ -913,7 +912,7 @@ func TestFetchNotificationsDo(t *testing.T) {

Convey("Without limit", func() {
addNotifications(database, []moira.ScheduledNotification{notification, notificationNew, notificationOld})
actual, err := database.fetchNotificationsDo(now+database.getDelayedTimeInSeconds(), notifier.NotificationsLimitUnlimited)
actual, err := database.fetchNotificationsDo(now+database.getDelayedTimeInSeconds(), notificationsLimitUnlimited)
So(err, ShouldBeNil)
So(actual, ShouldResemble, []*moira.ScheduledNotification{&notificationOld, &notification, &notificationNew})

Expand All @@ -936,7 +935,7 @@ func TestFetchNotificationsDo(t *testing.T) {

Convey("Without limit", func() {
addNotifications(database, []moira.ScheduledNotification{})
actual, err := database.fetchNotificationsDo(now+database.getDelayedTimeInSeconds(), notifier.NotificationsLimitUnlimited)
actual, err := database.fetchNotificationsDo(now+database.getDelayedTimeInSeconds(), notificationsLimitUnlimited)
So(err, ShouldBeNil)
So(actual, ShouldResemble, []*moira.ScheduledNotification{})

Expand All @@ -947,7 +946,7 @@ func TestFetchNotificationsDo(t *testing.T) {

Convey("Test all notification with ts and without limit in db", func() {
addNotifications(database, []moira.ScheduledNotification{notification, notificationNew, notificationOld, notification4})
actual, err := database.fetchNotificationsDo(now+database.getDelayedTimeInSeconds(), notifier.NotificationsLimitUnlimited)
actual, err := database.fetchNotificationsDo(now+database.getDelayedTimeInSeconds(), notificationsLimitUnlimited)
So(err, ShouldBeNil)
So(actual, ShouldResemble, []*moira.ScheduledNotification{&notificationOld, &notification4, &notification, &notificationNew})

Expand Down Expand Up @@ -1016,7 +1015,7 @@ func TestFetchNotificationsDo(t *testing.T) {

Convey("Without limit", func() {
addNotifications(database, []moira.ScheduledNotification{notificationOld, notificationOld2, notification, notificationNew, notificationNew2, notificationNew3})
actual, err := database.fetchNotificationsDo(now+database.getDelayedTimeInSeconds()+3, notifier.NotificationsLimitUnlimited)
actual, err := database.fetchNotificationsDo(now+database.getDelayedTimeInSeconds()+3, notificationsLimitUnlimited)
So(err, ShouldBeNil)
So(actual, ShouldResemble, []*moira.ScheduledNotification{&notificationOld, &notificationOld2, &notification, &notificationNew, &notificationNew3})

Expand Down Expand Up @@ -1052,7 +1051,7 @@ func TestFetchNotificationsDo(t *testing.T) {

Convey("Without limit", func() {
addNotifications(database, []moira.ScheduledNotification{notificationOld, notificationOld2, notification, notificationNew, notificationNew2, notificationNew3})
actual, err := database.fetchNotificationsDo(now+database.getDelayedTimeInSeconds()+3, notifier.NotificationsLimitUnlimited)
actual, err := database.fetchNotificationsDo(now+database.getDelayedTimeInSeconds()+3, notificationsLimitUnlimited)
So(err, ShouldBeNil)
So(actual, ShouldResemble, []*moira.ScheduledNotification{&notificationOld, &notificationOld2, &notification, &notificationNew, &notificationNew2})

Expand Down Expand Up @@ -1092,7 +1091,7 @@ func TestFetchNotificationsDo(t *testing.T) {

Convey("without limit", func() {
addNotifications(database, []moira.ScheduledNotification{notificationOld, notificationOld2, notification, notificationNew, notificationNew2, notificationNew3})
actual, err := database.fetchNotificationsDo(now+database.getDelayedTimeInSeconds()+3, notifier.NotificationsLimitUnlimited)
actual, err := database.fetchNotificationsDo(now+database.getDelayedTimeInSeconds()+3, notificationsLimitUnlimited)
So(err, ShouldBeNil)
So(actual, ShouldResemble, []*moira.ScheduledNotification{&notificationOld, &notificationOld2, &notification, &notificationNew, &notificationNew3})

Expand Down
Loading

0 comments on commit 26ba0b4

Please sign in to comment.