From 5c78ae486f9ec5205134c79c3e15da53956cc0a4 Mon Sep 17 00:00:00 2001 From: almostinf Date: Thu, 3 Oct 2024 12:04:06 +0300 Subject: [PATCH 1/5] add calculate next delivery with send fails more than zero --- notifier/scheduler.go | 6 +++++- notifier/scheduler_test.go | 33 ++++++++++++++++++++++++++++++++- 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/notifier/scheduler.go b/notifier/scheduler.go index d0348bc75..d1e7c8a66 100644 --- a/notifier/scheduler.go +++ b/notifier/scheduler.go @@ -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, ¶ms.Event, logger) } else { if params.Event.State == moira.StateTEST { next = now @@ -62,6 +63,7 @@ func (scheduler *StandardScheduler) ScheduleNotification(params moira.SchedulerP next, throttled = scheduler.calculateNextDelivery(now, ¶ms.Event, logger) } } + notification := &moira.ScheduledNotification{ Event: params.Event, Trigger: params.Trigger, @@ -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 } @@ -95,6 +98,7 @@ func (scheduler *StandardScheduler) calculateNextDelivery(now time.Time, event * next, beginning := scheduler.database.GetTriggerThrottling(event.TriggerID) + fmt.Println(now, next, beginning) if next.After(now) { alarmFatigue = true } else { diff --git a/notifier/scheduler_test.go b/notifier/scheduler_test.go index e148503bb..bc2e606cb 100644 --- a/notifier/scheduler_test.go +++ b/notifier/scheduler_test.go @@ -45,6 +45,15 @@ 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) @@ -52,6 +61,7 @@ func TestThrottling(t *testing.T) { 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) @@ -84,6 +94,10 @@ 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) @@ -96,9 +110,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) @@ -504,3 +520,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}, + }, +} From dbed1d886fb5bce64db4778c8b213baf4989b12a Mon Sep 17 00:00:00 2001 From: almostinf Date: Thu, 3 Oct 2024 12:06:21 +0300 Subject: [PATCH 2/5] remove useless fmt --- notifier/scheduler.go | 1 - 1 file changed, 1 deletion(-) diff --git a/notifier/scheduler.go b/notifier/scheduler.go index d1e7c8a66..446c1d376 100644 --- a/notifier/scheduler.go +++ b/notifier/scheduler.go @@ -98,7 +98,6 @@ func (scheduler *StandardScheduler) calculateNextDelivery(now time.Time, event * next, beginning := scheduler.database.GetTriggerThrottling(event.TriggerID) - fmt.Println(now, next, beginning) if next.After(now) { alarmFatigue = true } else { From 9f28f498e0c9ae6e214dfc9d309c327fc146a5e7 Mon Sep 17 00:00:00 2001 From: almostinf Date: Sun, 27 Oct 2024 21:25:04 +0300 Subject: [PATCH 3/5] merge with master and gofmt --- notifier/scheduler_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/notifier/scheduler_test.go b/notifier/scheduler_test.go index bc2e606cb..7337c2d04 100644 --- a/notifier/scheduler_test.go +++ b/notifier/scheduler_test.go @@ -61,7 +61,7 @@ func TestThrottling(t *testing.T) { metrics2 := metrics.ConfigureNotifierMetrics(metrics.NewDummyRegistry(), "notifier") now := time.Now() - next := now.Add(10*time.Minute) + next := now.Add(10 * time.Minute) systemClock := mock_clock.NewMockClock(mockCtrl) scheduler := NewScheduler(dataBase, logger, metrics2, SchedulerConfig{ReschedulingDelay: time.Minute}, systemClock) @@ -110,7 +110,7 @@ func TestThrottling(t *testing.T) { expected2 := expected expected2.SendFail = 3 - expected2.Timestamp = now.Add(10*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) From 327aa0306c9ed7ceec514418d98c2e78b754a3ed Mon Sep 17 00:00:00 2001 From: almostinf Date: Sun, 27 Oct 2024 22:24:35 +0300 Subject: [PATCH 4/5] add more tests to scheduler --- notifier/scheduler_test.go | 42 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/notifier/scheduler_test.go b/notifier/scheduler_test.go index 7337c2d04..f2d7666d0 100644 --- a/notifier/scheduler_test.go +++ b/notifier/scheduler_test.go @@ -1,6 +1,7 @@ package notifier import ( + "errors" "fmt" "testing" "time" @@ -103,6 +104,47 @@ func TestThrottling(t *testing.T) { 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 + now := 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 = now.Unix() + systemClock.EXPECT().NowUTC().Return(now).Times(1) + dataBase.EXPECT().GetTriggerThrottling(params2.Event.TriggerID).Return(now, now) + dataBase.EXPECT().GetSubscription(*params2.Event.SubscriptionID).Return(testSubscription, nil) + + notification := scheduler.ScheduleNotification(params2, logger) + So(notification, ShouldResemble, &expected2) + }) + Convey("Test sendFail more than 0, and has throttling, should send message in one minute", t, func() { params2 := params params2.ThrottledOld = true From 1c659d27a19301b60d67d2d3f5ccfbc3c1e29d05 Mon Sep 17 00:00:00 2001 From: almostinf Date: Sun, 27 Oct 2024 22:28:10 +0300 Subject: [PATCH 5/5] fix linter --- notifier/scheduler_test.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/notifier/scheduler_test.go b/notifier/scheduler_test.go index f2d7666d0..79d3b9681 100644 --- a/notifier/scheduler_test.go +++ b/notifier/scheduler_test.go @@ -127,7 +127,7 @@ func TestThrottling(t *testing.T) { params2.SendFail = 1 // 2015-09-02, 01:00:00 GMT+03:00 - now := time.Unix(1441144800, 0) + testNow := time.Unix(1441144800, 0) testSubscription := subscription testSubscription.ThrottlingEnabled = false testSubscription.Schedule = schedule3 @@ -136,9 +136,9 @@ func TestThrottling(t *testing.T) { expected2.SendFail = 1 // 2015-09-02, 02:00:00 GMT+03:00 expected2.Timestamp = time.Unix(1441148400, 0).Unix() - expected2.CreatedAt = now.Unix() - systemClock.EXPECT().NowUTC().Return(now).Times(1) - dataBase.EXPECT().GetTriggerThrottling(params2.Event.TriggerID).Return(now, now) + 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)