Skip to content

Commit

Permalink
add transaction heuristic limit to notification config
Browse files Browse the repository at this point in the history
  • Loading branch information
almostinf committed Oct 24, 2023
1 parent 0ae961a commit 9c82f26
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 16 deletions.
10 changes: 7 additions & 3 deletions cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,18 @@ type NotificationConfig struct {
TransactionTimeout string `yaml:"transaction_timeout"`
// TransactionMaxRetries defines the maximum number of attempts to make a transaction
TransactionMaxRetries int `yaml:"transaction_max_retries"`
// TransactionHeuristicLimit maximum allowable limit, after this limit all notifications
// without limit will be taken
TransactionHeuristicLimit int64 `yaml:"transaction_heuristic_limit"`
}

// GetSettings returns notification storage configuration
func (notificationConfig *NotificationConfig) GetSettings() redis.NotificationConfig {
return redis.NotificationConfig{
DelayedTime: to.Duration(notificationConfig.DelayedTime),
TransactionTimeout: to.Duration(notificationConfig.TransactionTimeout),
TransactionMaxRetries: notificationConfig.TransactionMaxRetries,
DelayedTime: to.Duration(notificationConfig.DelayedTime),
TransactionTimeout: to.Duration(notificationConfig.TransactionTimeout),
TransactionMaxRetries: notificationConfig.TransactionMaxRetries,
TransactionHeuristicLimit: notificationConfig.TransactionHeuristicLimit,
}
}

Expand Down
3 changes: 3 additions & 0 deletions database/redis/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,7 @@ type NotificationConfig struct {
TransactionTimeout time.Duration
// TransactionMaxRetries defines the maximum number of attempts to make a transaction
TransactionMaxRetries int
// TransactionHeuristicLimit maximum allowable limit, after this limit all notifications
// without limit will be taken
TransactionHeuristicLimit int64
}
14 changes: 8 additions & 6 deletions database/redis/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,10 @@ func NewTestDatabase(logger moira.Logger) *DbConnector {
NotificationHistoryQueryLimit: 1000,
},
NotificationConfig{
DelayedTime: time.Minute,
TransactionTimeout: 200 * time.Millisecond,
TransactionMaxRetries: 10,
DelayedTime: time.Minute,
TransactionTimeout: 200 * time.Millisecond,
TransactionMaxRetries: 10,
TransactionHeuristicLimit: 10000,
},
testSource)
}
Expand All @@ -112,9 +113,10 @@ func NewTestDatabaseWithIncorrectConfig(logger moira.Logger) *DbConnector {
NotificationHistoryQueryLimit: 1000,
},
NotificationConfig{
DelayedTime: time.Minute,
TransactionTimeout: 200 * time.Millisecond,
TransactionMaxRetries: 10,
DelayedTime: time.Minute,
TransactionTimeout: 200 * time.Millisecond,
TransactionMaxRetries: 10,
TransactionHeuristicLimit: 10000,
},
testSource)
}
Expand Down
9 changes: 2 additions & 7 deletions database/redis/notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,6 @@ import (
"github.com/moira-alert/moira/database/redis/reply"
)

const (
transactionTriesLimit = 10
transactionHeuristicLimit = 10000
)

// Custom error for transaction error
type transactionError struct{}

Expand Down Expand Up @@ -155,7 +150,7 @@ func (connector *DbConnector) FetchNotifications(to int64, limit int64) ([]*moir
}

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

Expand All @@ -180,7 +175,7 @@ func (connector *DbConnector) fetchNotificationsWithLimit(to int64, limit int64)
// fetchNotifecationsWithLimitDo uses WATCH, so transaction may fail and will retry it
// see https://redis.io/topics/transactions

for i := 0; i < transactionTriesLimit; i++ {
for i := 0; i < connector.notification.TransactionMaxRetries; i++ {
res, err := connector.fetchNotificationsWithLimitDo(to, limit)

if err == nil {
Expand Down

0 comments on commit 9c82f26

Please sign in to comment.