Skip to content

Commit

Permalink
fix(checker): Add cap on metric fetching to prevent checker OOM
Browse files Browse the repository at this point in the history
Closes #190
  • Loading branch information
beevee committed Mar 26, 2020
1 parent ea96709 commit 1293c25
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
8 changes: 7 additions & 1 deletion checker/trigger_checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ import (
"github.com/moira-alert/moira/metrics"
)

const (
// Prevents OOM when a trigger has enormous TTL,
// see https://github.com/moira-alert/moira/pull/519 for details
safeMetricFetchIntervalSeconds = 7 * 24 * 3600
)

// TriggerChecker represents data, used for handling new trigger state
type TriggerChecker struct {
database moira.Database
Expand Down Expand Up @@ -101,7 +107,7 @@ func getTTLState(triggerTTLState *moira.TTLState) moira.TTLState {

func calculateFrom(lastCheckTimestamp, triggerTTL int64) int64 {
if triggerTTL != 0 {
return lastCheckTimestamp - triggerTTL
return lastCheckTimestamp - moira.MinInt64(triggerTTL, safeMetricFetchIntervalSeconds)
}
return lastCheckTimestamp - 600
}
25 changes: 25 additions & 0 deletions checker/trigger_checker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package checker

import (
"fmt"
"math"
"testing"

"github.com/golang/mock/gomock"
Expand Down Expand Up @@ -195,4 +196,28 @@ func TestInitTriggerChecker(t *testing.T) {
}
So(*actual, ShouldResemble, expected)
})

trigger.TTL = math.MaxInt64

Convey("Test trigger with enormous ttl", t, func() {
dataBase.EXPECT().GetTrigger(triggerID).Return(trigger, nil)
dataBase.EXPECT().GetTriggerLastCheck(triggerID).Return(lastCheck, nil)
actual, err := MakeTriggerChecker(triggerID, dataBase, logger, config, metricSource.CreateMetricSourceProvider(localSource, nil), &metrics.CheckerMetrics{})
So(err, ShouldBeNil)

expected := TriggerChecker{
triggerID: triggerID,
database: dataBase,
config: config,
source: localSource,
logger: logger,
trigger: &trigger,
ttl: math.MaxInt64,
ttlState: moira.TTLStateNODATA,
lastCheck: &lastCheck,
from: lastCheck.Timestamp - safeMetricFetchIntervalSeconds,
until: actual.until,
}
So(*actual, ShouldResemble, expected)
})
}
7 changes: 7 additions & 0 deletions helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,3 +195,10 @@ func ChunkSlice(original []string, chunkSize int) (divided [][]string) {
func RoundToNearestRetention(ts, retention int64) int64 {
return (ts + retention/2) / retention * retention
}

func MinInt64(a, b int64) int64 {
if a < b {
return a
}
return b
}

0 comments on commit 1293c25

Please sign in to comment.