Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: DB sessions are recreated whenever controller configmap updates. Fixes #10498 #10734

Merged
merged 3 commits into from
Mar 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions persist/sqldb/sqldb.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,7 @@ func CreatePostGresDBSession(kubectlConfig kubernetes.Interface, namespace strin
if err != nil {
return nil, "", err
}

if persistPool != nil {
session.SetMaxOpenConns(persistPool.MaxOpenConns)
session.SetMaxIdleConns(persistPool.MaxIdleConns)
session.SetConnMaxLifetime(time.Duration(persistPool.ConnMaxLifetime))
}
session = ConfigureDBSession(session, persistPool)
return session, cfg.TableName, nil
}

Expand Down Expand Up @@ -135,12 +130,7 @@ func CreateMySQLDBSession(kubectlConfig kubernetes.Interface, namespace string,
if err != nil {
return nil, "", err
}

if persistPool != nil {
session.SetMaxOpenConns(persistPool.MaxOpenConns)
session.SetMaxIdleConns(persistPool.MaxIdleConns)
session.SetConnMaxLifetime(time.Duration(persistPool.ConnMaxLifetime))
}
session = ConfigureDBSession(session, persistPool)
// this is needed to make MySQL run in a Golang-compatible UTF-8 character set.
_, err = session.Exec("SET NAMES 'utf8mb4'")
if err != nil {
Expand All @@ -152,3 +142,13 @@ func CreateMySQLDBSession(kubectlConfig kubernetes.Interface, namespace string,
}
return session, cfg.TableName, nil
}

// ConfigureDBSession configures the DB session
func ConfigureDBSession(session sqlbuilder.Database, persistPool *config.ConnectionPool) sqlbuilder.Database {
Copy link
Member Author

@terrytangyuan terrytangyuan Mar 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a separate function because once we reused a session, we still want to update the configurations based on configmap changes in updateConfig().

if persistPool != nil {
session.SetMaxOpenConns(persistPool.MaxOpenConns)
session.SetMaxIdleConns(persistPool.MaxIdleConns)
session.SetConnMaxLifetime(time.Duration(persistPool.ConnMaxLifetime))
}
return session
}
30 changes: 16 additions & 14 deletions workflow/controller/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,31 +23,33 @@ func (wfc *WorkflowController) updateConfig() error {
return err
}
log.Info("Configuration:\n" + string(bytes))
wfc.session = nil
wfc.artifactRepositories = artifactrepositories.New(wfc.kubeclientset, wfc.namespace, &wfc.Config.ArtifactRepository)
wfc.offloadNodeStatusRepo = sqldb.ExplosiveOffloadNodeStatusRepo
wfc.wfArchive = sqldb.NullWorkflowArchive
wfc.archiveLabelSelector = labels.Everything()
persistence := wfc.Config.Persistence
if persistence != nil {
log.Info("Persistence configuration enabled")
session, tableName, err := sqldb.CreateDBSession(wfc.kubeclientset, wfc.namespace, persistence)
if err != nil {
return err
}
log.Info("Persistence Session created successfully")
if !persistence.SkipMigration {
err = sqldb.NewMigrate(session, persistence.GetClusterName(), tableName).Exec(context.Background())
var tableName string
if wfc.session == nil {
session, tableName, err := sqldb.CreateDBSession(wfc.kubeclientset, wfc.namespace, persistence)
if err != nil {
return err
}
} else {
log.Info("DB migration is disabled")
log.Info("Persistence Session created successfully")
if !persistence.SkipMigration {
err = sqldb.NewMigrate(session, persistence.GetClusterName(), tableName).Exec(context.Background())
if err != nil {
return err
}
} else {
log.Info("DB migration is disabled")
}
wfc.session = session
}

wfc.session = session
sqldb.ConfigureDBSession(wfc.session, persistence.ConnectionPool)
if persistence.NodeStatusOffload {
wfc.offloadNodeStatusRepo, err = sqldb.NewOffloadNodeStatusRepo(session, persistence.GetClusterName(), tableName)
wfc.offloadNodeStatusRepo, err = sqldb.NewOffloadNodeStatusRepo(wfc.session, persistence.GetClusterName(), tableName)
if err != nil {
return err
}
Expand All @@ -62,7 +64,7 @@ func (wfc *WorkflowController) updateConfig() error {
if err != nil {
return err
}
wfc.wfArchive = sqldb.NewWorkflowArchive(session, persistence.GetClusterName(), wfc.managedNamespace, instanceIDService)
wfc.wfArchive = sqldb.NewWorkflowArchive(wfc.session, persistence.GetClusterName(), wfc.managedNamespace, instanceIDService)
log.Info("Workflow archiving is enabled")
} else {
log.Info("Workflow archiving is disabled")
Expand Down