Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(notifier): incorrect sending alerts to subscriptions #1109

Merged
merged 6 commits into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion notifier/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,11 @@ func (scheduler *StandardScheduler) ScheduleNotification(params moira.SchedulerP
next time.Time
throttled bool
)

now := scheduler.clock.NowUTC()
if params.SendFail > 0 {
next = now.Add(scheduler.config.ReschedulingDelay)
throttled = params.ThrottledOld
next, throttled = scheduler.calculateNextDelivery(next, &params.Event, logger)
} else {
if params.Event.State == moira.StateTEST {
next = now
Expand All @@ -62,6 +63,7 @@ func (scheduler *StandardScheduler) ScheduleNotification(params moira.SchedulerP
next, throttled = scheduler.calculateNextDelivery(now, &params.Event, logger)
}
}

notification := &moira.ScheduledNotification{
Event: params.Event,
Trigger: params.Trigger,
Expand All @@ -78,6 +80,7 @@ func (scheduler *StandardScheduler) ScheduleNotification(params moira.SchedulerP
Int64("notification_timestamp_unix", next.Unix()).
Int64("notification_created_at_unix", now.Unix()).
Msg("Scheduled notification")

return notification
}

Expand Down
75 changes: 74 additions & 1 deletion notifier/scheduler_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package notifier

import (
"errors"
"fmt"
"testing"
"time"
Expand Down Expand Up @@ -45,13 +46,23 @@ func TestThrottling(t *testing.T) {
SubscriptionID: &subID,
}

subscription := moira.SubscriptionData{
ID: "SubscriptionID-000000000000001",
Enabled: true,
Tags: []string{"test-tag"},
Contacts: []string{"ContactID-000000000000001"},
ThrottlingEnabled: true,
Schedule: schedule5,
}

mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()
dataBase := mock_moira_alert.NewMockDatabase(mockCtrl)
logger, _ := logging.GetLogger("Scheduler")
metrics2 := metrics.ConfigureNotifierMetrics(metrics.NewDummyRegistry(), "notifier")

now := time.Now()
next := now.Add(10 * time.Minute)
systemClock := mock_clock.NewMockClock(mockCtrl)
scheduler := NewScheduler(dataBase, logger, metrics2, SchedulerConfig{ReschedulingDelay: time.Minute}, systemClock)

Expand Down Expand Up @@ -84,6 +95,51 @@ func TestThrottling(t *testing.T) {
expected2.SendFail = 1
expected2.Timestamp = now.Add(time.Minute).Unix()
systemClock.EXPECT().NowUTC().Return(now).Times(1)
dataBase.EXPECT().GetTriggerThrottling(params2.Event.TriggerID).Return(now, now)
dataBase.EXPECT().GetSubscription(*params2.Event.SubscriptionID).Return(subscription, nil)
dataBase.EXPECT().GetNotificationEventCount(event.TriggerID, now.Unix()).Return(int64(0))
dataBase.EXPECT().GetNotificationEventCount(event.TriggerID, now.Unix()).Return(int64(0))

notification := scheduler.ScheduleNotification(params2, logger)
So(notification, ShouldResemble, &expected2)
})

Convey("Test sendFail more that 0, and no throttling, but subscription doesn't exists, should send message in one minute", t, func() {
params2 := params
params2.ThrottledOld = false
params2.SendFail = 1
testErr := errors.New("subscription doesn't exist")

expected2 := expected
expected2.SendFail = 1
expected2.Timestamp = now.Add(time.Minute).Unix()
systemClock.EXPECT().NowUTC().Return(now).Times(1)
dataBase.EXPECT().GetTriggerThrottling(params2.Event.TriggerID).Return(now, now)
dataBase.EXPECT().GetSubscription(*params2.Event.SubscriptionID).Return(moira.SubscriptionData{}, testErr)

notification := scheduler.ScheduleNotification(params2, logger)
So(notification, ShouldResemble, &expected2)
})

Convey("Test sendFail more that 0, and no throttling, but the subscription schedule postpones the dispatch time, should send message in one minute", t, func() {
params2 := params
params2.ThrottledOld = false
params2.SendFail = 1

// 2015-09-02, 01:00:00 GMT+03:00
testNow := time.Unix(1441144800, 0)
testSubscription := subscription
testSubscription.ThrottlingEnabled = false
testSubscription.Schedule = schedule3

expected2 := expected
expected2.SendFail = 1
// 2015-09-02, 02:00:00 GMT+03:00
expected2.Timestamp = time.Unix(1441148400, 0).Unix()
expected2.CreatedAt = testNow.Unix()
systemClock.EXPECT().NowUTC().Return(testNow).Times(1)
dataBase.EXPECT().GetTriggerThrottling(params2.Event.TriggerID).Return(testNow, testNow)
dataBase.EXPECT().GetSubscription(*params2.Event.SubscriptionID).Return(testSubscription, nil)

notification := scheduler.ScheduleNotification(params2, logger)
So(notification, ShouldResemble, &expected2)
Expand All @@ -96,9 +152,11 @@ func TestThrottling(t *testing.T) {

expected2 := expected
expected2.SendFail = 3
expected2.Timestamp = now.Add(time.Minute).Unix()
expected2.Timestamp = now.Add(10 * time.Minute).Unix()
expected2.Throttled = true
systemClock.EXPECT().NowUTC().Return(now).Times(1)
dataBase.EXPECT().GetTriggerThrottling(params2.Event.TriggerID).Return(next, now)
dataBase.EXPECT().GetSubscription(*params2.Event.SubscriptionID).Return(subscription, nil)

notification := scheduler.ScheduleNotification(params2, logger)
So(notification, ShouldResemble, &expected2)
Expand Down Expand Up @@ -504,3 +562,18 @@ var schedule4 = moira.ScheduleData{
{Enabled: true},
},
}

var schedule5 = moira.ScheduleData{
StartOffset: 0, // 00:00
EndOffset: 1440, // 24:00
TimezoneOffset: -180, // (GMT +3)
Days: []moira.ScheduleDataDay{
{Enabled: true},
{Enabled: true},
{Enabled: true},
{Enabled: true},
{Enabled: true},
{Enabled: true},
{Enabled: true},
},
}
Loading