Skip to content

Commit

Permalink
Fix default db opts
Browse files Browse the repository at this point in the history
Signed-off-by: Alexandros Filios <[email protected]>
  • Loading branch information
alexandrosfilios committed Jan 16, 2025
1 parent 64a9f90 commit 8486f7f
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 18 deletions.
2 changes: 2 additions & 0 deletions integration/nwo/fsc/node/core_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ fsc:
createSchema: {{ NodeKVSPersistence.SQL.CreateSchema }}
tablePrefix: {{ NodeKVSPersistence.SQL.TablePrefix }}
maxOpenConns: {{ NodeKVSPersistence.SQL.MaxOpenConns }}
maxIdleConns: 3
maxIdleTime: 45s
{{- else }}
path: {{ NodeKVSPath }}
SyncWrites: false
Expand Down
2 changes: 2 additions & 0 deletions platform/view/services/db/driver/memory/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ var (
SkipCreateTable: false,
SkipPragmas: false,
MaxOpenConns: 10,
MaxIdleConns: common.CopyPtr(common.DefaultMaxIdleConns),
MaxIdleTime: common.CopyPtr(common.DefaultMaxIdleTime),
}
)

Expand Down
4 changes: 2 additions & 2 deletions platform/view/services/db/driver/sql/common/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,8 +337,8 @@ type Opts struct {
SkipCreateTable bool
SkipPragmas bool
MaxOpenConns int
MaxIdleConns int
MaxIdleTime time.Duration
MaxIdleConns *int
MaxIdleTime *time.Duration
}

func generateParamSet(offset, count int) string {
Expand Down
18 changes: 14 additions & 4 deletions platform/view/services/db/driver/sql/common/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ import (
"github.com/pkg/errors"
)

const (
DefaultMaxIdleConns = 2
DefaultMaxIdleTime = time.Minute
)

type TableNameCreator struct {
prefix string
r *regexp.Regexp
Expand Down Expand Up @@ -92,11 +97,11 @@ func GetOpts(config driver.Config, optsKey string) (*Opts, error) {
if opts.DataSource == "" {
return nil, notSetError(optsKey + ".dataSource")
}
if !config.IsSet(optsKey + ".maxIdleConns") {
opts.MaxIdleConns = 2 // go default
if opts.MaxIdleConns == nil {
opts.MaxIdleConns = CopyPtr(DefaultMaxIdleConns) // go default
}
if !config.IsSet(optsKey + ".maxIdleTime") {
opts.MaxIdleTime = time.Minute
if opts.MaxIdleTime == nil {
opts.MaxIdleTime = CopyPtr(DefaultMaxIdleTime)
}

return &opts, nil
Expand All @@ -113,6 +118,11 @@ func QueryUnique[T any](db *sql.DB, query string, args ...any) (T, error) {
return result, err
}

func CopyPtr[T any](t T) *T {
v := t
return &v
}

func notSetError(key string) error {
return fmt.Errorf(
"either %s in core.yaml or the %s environment variable must be specified", key,
Expand Down
6 changes: 6 additions & 0 deletions platform/view/services/db/driver/sql/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,5 +146,11 @@ func getOps(config driver.Config) (common.Opts, error) {
if opts.TablePrefix == "" {
opts.TablePrefix = "fsc"
}
if opts.MaxIdleTime == nil {
opts.MaxIdleTime = common.CopyPtr(common.DefaultMaxIdleTime)
}
if opts.MaxIdleConns == nil {
opts.MaxIdleConns = common.CopyPtr(common.DefaultMaxIdleConns)
}
return *opts, nil
}
16 changes: 12 additions & 4 deletions platform/view/services/db/driver/sql/postgres/persistence.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,36 @@ import (
"time"

"github.com/hyperledger-labs/fabric-smart-client/platform/common/services/logging"
"github.com/hyperledger-labs/fabric-smart-client/platform/view/services/db/driver/sql/common"
_ "github.com/jackc/pgx/v5/stdlib"
)

var logger = logging.MustGetLogger("view-sdk.db.postgres")

const driverName = "pgx"

func OpenDB(dataSourceName string, maxOpenConns, maxIdleConns int, maxIdleTime time.Duration) (*sql.DB, error) {
func OpenDB(dataSourceName string, maxOpenConns int, maxIdleConns *int, maxIdleTime *time.Duration) (*sql.DB, error) {
db, err := sql.Open(driverName, dataSourceName)
if err != nil {
logger.Error(err)
return nil, fmt.Errorf("can't open %s database: %w", driverName, err)
}

if maxIdleConns == nil {
maxIdleConns = common.CopyPtr(common.DefaultMaxIdleConns)
}
if maxIdleTime == nil {
maxIdleTime = common.CopyPtr(common.DefaultMaxIdleTime)
}

db.SetMaxOpenConns(maxOpenConns)
db.SetMaxIdleConns(maxIdleConns)
db.SetConnMaxIdleTime(maxIdleTime)
db.SetMaxIdleConns(*maxIdleConns)
db.SetConnMaxIdleTime(*maxIdleTime)

if err = db.Ping(); err != nil {
return nil, err
}
logger.Infof("connected to [%s] for reads, max open connections: %d", driverName, maxOpenConns)
logger.Infof("connected to [%s] for reads, max open connections: %d, max idle connections: %d, max idle time: %v", driverName, maxOpenConns, maxIdleConns, maxIdleTime)

logger.Info("using same db for writes")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ type dbObject interface {
type persistenceConstructor[V dbObject] func(common2.Opts, string) (V, error)

func initPersistence[V dbObject](constructor persistenceConstructor[V], pgConnStr, name string, maxOpenConns, maxIdleConns int, maxIdleTime time.Duration) (V, error) {
p, err := constructor(common2.Opts{DataSource: pgConnStr, MaxOpenConns: maxOpenConns, MaxIdleConns: maxIdleConns, MaxIdleTime: maxIdleTime}, name)
p, err := constructor(common2.Opts{DataSource: pgConnStr, MaxOpenConns: maxOpenConns, MaxIdleConns: &maxIdleConns, MaxIdleTime: &maxIdleTime}, name)
if err != nil {
return utils.Zero[V](), err
}
Expand Down
23 changes: 18 additions & 5 deletions platform/view/services/db/driver/sql/sqlite/persistence.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ import (
"time"

"github.com/hyperledger-labs/fabric-smart-client/platform/common/services/logging"
"github.com/hyperledger-labs/fabric-smart-client/platform/view/services/db/driver/sql/common"
)

const (
maxIdleConnsWrite = 1
maxIdleTimeWrite time.Duration = 0
)

const sqlitePragmas = `
Expand All @@ -30,34 +36,41 @@ const driverName = "sqlite"

var logger = logging.MustGetLogger("view-sdk.db.sqlite")

func openDB(dataSourceName string, maxOpenConns, maxIdleConns int, maxIdleTime time.Duration, skipPragmas bool) (*sql.DB, *sql.DB, error) {
func openDB(dataSourceName string, maxOpenConns int, maxIdleConns *int, maxIdleTime *time.Duration, skipPragmas bool) (*sql.DB, *sql.DB, error) {
logger.Infof("Opening read db [%v]", dataSourceName)
readDB, err := OpenDB(dataSourceName, maxOpenConns, maxIdleConns, maxIdleTime, skipPragmas)
if err != nil {
return nil, nil, fmt.Errorf("can't open read %s database: %w", driverName, err)
}
logger.Infof("Opening write db [%v]", dataSourceName)
writeDB, err := OpenDB(dataSourceName, 1, 1, 0, skipPragmas)
writeDB, err := OpenDB(dataSourceName, 1, common.CopyPtr(maxIdleConnsWrite), common.CopyPtr(maxIdleTimeWrite), skipPragmas)
if err != nil {
return nil, nil, fmt.Errorf("can't open write %s database: %w", driverName, err)
}
return readDB, writeDB, nil
}

func OpenDB(dataSourceName string, maxOpenConns, maxIdleConns int, maxIdleTime time.Duration, skipPragmas bool) (*sql.DB, error) {
func OpenDB(dataSourceName string, maxOpenConns int, maxIdleConns *int, maxIdleTime *time.Duration, skipPragmas bool) (*sql.DB, error) {
// Create directories if they do not exist to avoid error "out of memory (14)", see below
path := getDir(dataSourceName)
if err := os.MkdirAll(path, 0777); err != nil {
logger.Warnf("failed creating dir [%s]: %s", path, err)
}

if maxIdleConns == nil {
maxIdleConns = common.CopyPtr(common.DefaultMaxIdleConns)
}
if maxIdleTime == nil {
maxIdleTime = common.CopyPtr(common.DefaultMaxIdleTime)
}

db, err := sql.Open(driverName, dataSourceName)
if err != nil {
return nil, fmt.Errorf("can't open %s database: %w", driverName, err)
}
db.SetMaxOpenConns(maxOpenConns)
db.SetMaxIdleConns(maxIdleConns)
db.SetConnMaxIdleTime(maxIdleTime)
db.SetMaxIdleConns(*maxIdleConns)
db.SetConnMaxIdleTime(*maxIdleTime)

if err = db.Ping(); err != nil && strings.Contains(err.Error(), "out of memory (14)") {
return nil, fmt.Errorf("can't open %s database, does the folder exist?", driverName)
Expand Down
7 changes: 5 additions & 2 deletions platform/view/services/db/driver/sql/sqlite/test_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ package sqlite
import (
"fmt"
"path"
"time"

"github.com/hyperledger-labs/fabric-smart-client/platform/view/services/db/driver"
common2 "github.com/hyperledger-labs/fabric-smart-client/platform/view/services/db/driver/sql/common"
Expand Down Expand Up @@ -69,9 +70,11 @@ func (t *TestDriver) NewBinding(dataSourceName string, config driver.Config) (dr
}

func unversionedOpts(name string, tempDir string) common2.Opts {
return common2.Opts{DataSource: fmt.Sprintf("file:%s.sqlite?_pragma=busy_timeout(1000)", path.Join(tempDir, name))}
maxIdleConns, maxIdleTime := 2, 1*time.Minute
return common2.Opts{DataSource: fmt.Sprintf("file:%s.sqlite?_pragma=busy_timeout(1000)", path.Join(tempDir, name)), MaxIdleConns: &maxIdleConns, MaxIdleTime: &maxIdleTime}
}

func versionedOpts(name string, tempDir string) common2.Opts {
return common2.Opts{DataSource: fmt.Sprintf("%s.sqlite", path.Join(tempDir, name))}
maxIdleConns, maxIdleTime := 2, 1*time.Minute
return common2.Opts{DataSource: fmt.Sprintf("%s.sqlite", path.Join(tempDir, name)), MaxIdleConns: &maxIdleConns, MaxIdleTime: &maxIdleTime}
}

0 comments on commit 8486f7f

Please sign in to comment.