Skip to content

Commit

Permalink
perf(filter): Allow stale reads in filter
Browse files Browse the repository at this point in the history
Allow reading from slave replicas when fetching all patterns for
patterns index. Add dialer test case.
  • Loading branch information
kodmi authored and beevee committed Mar 29, 2020
1 parent 0864279 commit 8f4f81e
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 6 deletions.
6 changes: 4 additions & 2 deletions cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ type RedisConfig struct {
// Redis node port
Port string `yaml:"port"`
// Redis database
DB int `yaml:"dbid"`
ConnectionLimit int `yaml:"connection_limit"`
DB int `yaml:"dbid"`
ConnectionLimit int `yaml:"connection_limit"`
AllowSlaveReads bool `yaml:"allow_slave_reads"`
}

// GetSettings returns redis config parsed from moira config files
Expand All @@ -41,6 +42,7 @@ func (config *RedisConfig) GetSettings() redis.Config {
Port: config.Port,
DB: config.DB,
ConnectionLimit: config.ConnectionLimit,
AllowSlaveReads: config.AllowSlaveReads,
}
}

Expand Down
5 changes: 2 additions & 3 deletions database/redis/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ type DbConnector struct {
metricsCache *cache.Cache
sync *redsync.Redsync
source DBSource

slaveConnector *DbConnector
slaveConnector *DbConnector
}

// NewDatabase creates Redis pool based on config
Expand Down Expand Up @@ -144,7 +143,7 @@ func createPoolDialers(logger moira.Logger, config Config) (mainDialer, slaveDia
db: config.DB,
dialTimeout: dialTimeout,
}
return
return mainDialer, nil
}

func (connector *DbConnector) makePubSubConnection(channel string) (*redis.PubSubConn, error) {
Expand Down
14 changes: 14 additions & 0 deletions database/redis/database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,20 @@ func TestAllowStale(t *testing.T) {
So(staleDatabase, ShouldPointTo, database)
})

Convey("When using sentinel with slave reads disabled, returns same db", func() {
sentinelConfig := Config{
MasterName: "mstr",
SentinelAddresses: []string{"addr.ru"},
DB: 0,
AllowSlaveReads: false,
}
database := newTestDatabase(logger, sentinelConfig)

staleDatabase := database.AllowStale()

So(staleDatabase, ShouldPointTo, database)
})

Convey("When using sentinel, returns slave db instance, retains references", func() {
sentinelConfig := Config{
MasterName: "mstr",
Expand Down
2 changes: 1 addition & 1 deletion filter/patterns_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func NewPatternStorage(database moira.Database, metrics *metrics.FilterMetrics,

// Refresh builds pattern's indexes from redis data
func (storage *PatternStorage) Refresh() error {
newPatterns, err := storage.database.GetPatterns()
newPatterns, err := storage.database.AllowStale().GetPatterns()
if err != nil {
return err
}
Expand Down
1 change: 1 addition & 0 deletions filter/patterns_storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ func TestProcessIncomingMetric(t *testing.T) {

mockCtrl := gomock.NewController(t)
database := mock_moira_alert.NewMockDatabase(mockCtrl)
database.EXPECT().AllowStale().AnyTimes().Return(database)
logger, _ := logging.GetLogger("Scheduler")

Convey("Create new pattern storage, GetPatterns returns error, should error", t, func() {
Expand Down
1 change: 1 addition & 0 deletions perfomance_tests/filter/filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ func BenchmarkProcessIncomingMetric(b *testing.B) {

mockCtrl := gomock.NewController(b)
database := mock_moira_alert.NewMockDatabase(mockCtrl)
database.EXPECT().AllowStale().AnyTimes().Return(database)
logger, _ := logging.GetLogger("Benchmark")

database.EXPECT().GetPatterns().Return(patterns, nil)
Expand Down

0 comments on commit 8f4f81e

Please sign in to comment.