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

Local cache to reuse db connection #40

Merged
merged 1 commit into from
Jul 10, 2018
Merged
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
21 changes: 19 additions & 2 deletions atlas-db-controller/databaseSchemaResource.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"

"github.com/golang-migrate/migrate"
"github.com/golang-migrate/migrate/database"
"github.com/golang/glog"
atlas "github.com/infobloxopen/atlas-db/pkg/apis/db/v1alpha1"
"k8s.io/apimachinery/pkg/api/errors"
Expand All @@ -12,6 +13,8 @@ import (
"k8s.io/client-go/tools/cache"
)

var dbDriverMap = make(map[string]database.Driver)

func (c *Controller) syncSchema(key string) error {
glog.Infof("Schema key: %v", key)

Expand Down Expand Up @@ -116,15 +119,29 @@ func (c *Controller) syncSchema(key string) error {
}
}

mgrt, err := migrate.New(gitURL, dsn)
// migrate package is not closing the dbconnnection so using a local cache reuse dbconnection.
// TODO when same resource with different dsn arrives; need to unset local cache during schema resource deletion.
dbDriver, ok := dbDriverMap[schema.Namespace+schema.Name]
if !ok {
dbDriver, err = database.Open(dsn)
if err != nil {
schemaStatusMsg = fmt.Sprintf("failed to open dbconnection: %s", err)
c.updateDatabaseSchemaStatus(key, schema, StateError, schemaStatusMsg)
err = fmt.Errorf(schemaStatusMsg)
runtime.HandleError(err)
return err
}
dbDriverMap[schema.Namespace+schema.Name] = dbDriver
}

mgrt, err := migrate.NewWithDatabaseInstance(gitURL, dbName, dbDriver)
if err != nil {
schemaStatusMsg = fmt.Sprintf("failed to initialize migrate engine: %s", err)
c.updateDatabaseSchemaStatus(key, schema, StateError, schemaStatusMsg)
err = fmt.Errorf(schemaStatusMsg)
runtime.HandleError(err)
return err
}
defer mgrt.Close()

ver, dirt, err := mgrt.Version()
if err != nil {
Expand Down