diff --git a/pkg/cloudcommon/database.go b/pkg/cloudcommon/database.go index 8f415c1bc3..38aceddfd3 100644 --- a/pkg/cloudcommon/database.go +++ b/pkg/cloudcommon/database.go @@ -94,6 +94,16 @@ func InitDB(options *common_options.DBOptions) { } sqlchemy.SetDBWithNameBackend(dbConn, sqlchemy.DefaultDB, backend) + if options.DbMaxWaitTimeoutSeconds <= 300 { + options.DbMaxWaitTimeoutSeconds = 3600 + } + // ConnMaxLifetime is the maximum amount of time a connection may be reused. + // mysql default max_waitimeout is 28800 seconds, 1 hour should be enough + // but if user set a customized mysql max_waittimeout, the value should be adjusted accordingly + dbConn.SetConnMaxLifetime(time.Duration(options.DbMaxWaitTimeoutSeconds) * time.Second) + // ConnMaxIdleTime should be half of ConnMaxLifetime + dbConn.SetConnMaxIdleTime(time.Duration(options.DbMaxWaitTimeoutSeconds/2) * time.Second) + dialect, sqlStr, err = options.GetClickhouseConnStr() if err == nil { // connect to clickcloud diff --git a/pkg/cloudcommon/options/options.go b/pkg/cloudcommon/options/options.go index adca07ab99..6c2415e443 100644 --- a/pkg/cloudcommon/options/options.go +++ b/pkg/cloudcommon/options/options.go @@ -165,6 +165,8 @@ type DBOptions struct { Clickhouse string `help:"Connection string for click house"` + DbMaxWaitTimeoutSeconds int `help:"max wait timeout for db connection, default 1 hour" default:"3600"` + OpsLogWithClickhouse bool `help:"store operation logs with clickhouse" default:"false"` EnableDBChecksumTables bool `help:"Enable DB tables with record checksum for consistency"` DBChecksumSkipInit bool `help:"Skip DB tables with record checksum calculation when init" default:"false"`