Skip to content

Commit

Permalink
Add DB connection configs (#60)
Browse files Browse the repository at this point in the history
* Add DB connection config

* Add testdata file

* Add a small comment update for the treatment service

---------

Co-authored-by: Krithika Sundararajan <krithika.sundararajan@go-jek.com>
  • Loading branch information
krithika369 and Krithika Sundararajan authored Jan 31, 2023
1 parent e987ab2 commit 2182325
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 17 deletions.
6 changes: 6 additions & 0 deletions management-service/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package config

import (
"fmt"
"time"

"github.com/gojek/mlp/api/pkg/instrumentation/newrelic"
"github.com/gojek/mlp/api/pkg/instrumentation/sentry"
Expand Down Expand Up @@ -40,6 +41,11 @@ type DatabaseConfig struct {
Password string `default:"xp"`
Database string `default:"xp"`
MigrationsPath string `default:"file://database/db-migrations"`

ConnMaxIdleTime time.Duration `default:"0s"`
ConnMaxLifetime time.Duration `default:"0s"`
MaxIdleConns int `default:"0"`
MaxOpenConns int `default:"0"`
}

// MLPConfig captures the configuration used to connect to the MLP API server
Expand Down
36 changes: 24 additions & 12 deletions management-service/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package config
import (
"strings"
"testing"
"time"

"github.com/gojek/mlp/api/pkg/instrumentation/newrelic"
"github.com/gojek/mlp/api/pkg/instrumentation/sentry"
Expand All @@ -11,6 +12,7 @@ import (
)

func TestDefaultConfigs(t *testing.T) {
zeroSecond, _ := time.ParseDuration("0s")
emptyInterfaceMap := make(map[string]interface{})
emptyStringMap := make(map[string]string)
defaultCfg := Config{
Expand All @@ -21,12 +23,16 @@ func TestDefaultConfigs(t *testing.T) {
URL: "",
},
DbConfig: &DatabaseConfig{
Host: "localhost",
Port: 5432,
User: "xp",
Password: "xp",
Database: "xp",
MigrationsPath: "file://database/db-migrations",
Host: "localhost",
Port: 5432,
User: "xp",
Password: "xp",
Database: "xp",
MigrationsPath: "file://database/db-migrations",
ConnMaxIdleTime: zeroSecond,
ConnMaxLifetime: zeroSecond,
MaxIdleConns: 0,
MaxOpenConns: 0,
},
SegmenterConfig: make(map[string]interface{}),
MLPConfig: &MLPConfig{
Expand Down Expand Up @@ -64,6 +70,8 @@ func TestDefaultConfigs(t *testing.T) {
// TestLoadConfigFiles verifies that when multiple configs are passed in
// they are consumed in the correct order
func TestLoadConfigFiles(t *testing.T) {
oneSecond, _ := time.ParseDuration("1s")
twoSecond, _ := time.ParseDuration("2s")
tests := []struct {
name string
configFiles []string
Expand All @@ -81,12 +89,16 @@ func TestLoadConfigFiles(t *testing.T) {
URL: "test-authz-server",
},
DbConfig: &DatabaseConfig{
Host: "localhost",
Port: 5432,
User: "admin",
Password: "password",
Database: "xp",
MigrationsPath: "file://test-db-migrations",
Host: "localhost",
Port: 5432,
User: "admin",
Password: "password",
Database: "xp",
MigrationsPath: "file://test-db-migrations",
ConnMaxIdleTime: oneSecond,
ConnMaxLifetime: twoSecond,
MaxIdleConns: 3,
MaxOpenConns: 4,
},
SegmenterConfig: map[string]interface{}{
"s2_ids": map[string]interface{}{
Expand Down
17 changes: 16 additions & 1 deletion management-service/database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,25 @@ func ConnectionString(cfg *config.DatabaseConfig) string {
}

func Open(cfg *config.DatabaseConfig) (*gorm.DB, error) {
return gorm.Open(pg.Open(ConnectionString(cfg)),
db, err := gorm.Open(pg.Open(ConnectionString(cfg)),
&gorm.Config{
Logger: logger.Default.LogMode(logger.Silent),
})
if err != nil {
return nil, err
}

// Get the underlying SQL DB and apply connection properties
sqlDB, err := db.DB()
if err != nil {
return nil, err
}
sqlDB.SetConnMaxIdleTime(cfg.ConnMaxIdleTime)
sqlDB.SetConnMaxLifetime(cfg.ConnMaxLifetime)
sqlDB.SetMaxIdleConns(cfg.MaxIdleConns)
sqlDB.SetMaxOpenConns(cfg.MaxOpenConns)

return db, nil
}

func Migrate(cfg *config.DatabaseConfig) error {
Expand Down
3 changes: 1 addition & 2 deletions management-service/services/configuration_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ type configurationService struct {
}

func NewConfigurationService(cfg *config.Config) ConfigurationService {
var segmenterConfig schema.SegmenterConfig
segmenterConfig = cfg.SegmenterConfig
var segmenterConfig schema.SegmenterConfig = cfg.SegmenterConfig

return &configurationService{
treatmentServiceConfig: schema.TreatmentServiceConfig{
Expand Down
4 changes: 4 additions & 0 deletions management-service/testdata/config1.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ DbConfig:
User: user
Password: password
MigrationsPath: file://test-db-migrations
ConnMaxIdleTime: 1s
ConnMaxLifetime: 2s
MaxIdleConns: 3
MaxOpenConns: 4

MLPConfig:
URL: test-mlp-url
Expand Down
6 changes: 4 additions & 2 deletions treatment-service/services/experiment_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,13 @@ func (es *experimentService) GetExperiment(
func(matches []*models.ExperimentMatch) []*models.ExperimentMatch {
return es.filterByMatchStrength(matches, projectSettings.Segmenters.Names)
},
// Resolve granularity of Segmenters
// Resolve lookup order. At this point, comparing by each segmenter, we should be left with one or more
// experiments which are either all exact or all weak. Where there are multiple transformed values returned
// by the segmenter, we pick the first transformed value that has a match, to filter the pool of experiments.
func(matches []*models.ExperimentMatch) []*models.ExperimentMatch {
return es.filterByLookupOrder(matches, requestFilter, projectSettings.Segmenters.Names, segmentersTypeMapping)
},
// Resolve tiers - at this point, we should ideally only be left with 1 experiment or 2
// Resolve tiers. At this point, we should ideally only be left with 1 experiment or 2
// (in different tiers), based on the orthogonality rules enforced by the management service.
es.filterByTierPriority,
}
Expand Down

0 comments on commit 2182325

Please sign in to comment.