Skip to content
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
55 changes: 31 additions & 24 deletions pkg/cloudcommon/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,29 +39,7 @@ import (
"yunion.io/x/onecloud/pkg/util/dbutils"
)

func InitDB(options *common_options.DBOptions) {
if options.DebugSqlchemy {
log.Warningf("debug Sqlchemy is turned on")
sqlchemy.DEBUG_SQLCHEMY = true
}

log.Infof("Registered SQL drivers: %s", strings.Join(sql.Drivers(), ", "))

consts.QueryOffsetOptimization = options.QueryOffsetOptimization

if options.HistoricalUniqueName {
consts.EnableHistoricalUniqueName()
} else {
consts.DisableHistoricalUniqueName()
}

if options.OpsLogMaxKeepMonths > 0 {
consts.SetSplitableMaxKeepMonths(options.OpsLogMaxKeepMonths)
}
if options.SplitableMaxDurationHours > 0 {
consts.SetSplitableMaxDurationHours(options.SplitableMaxDurationHours)
}

func InitDBConn(options *common_options.DBOptions) {
dialect, sqlStr, err := options.GetDBConnection()
if err != nil {
log.Fatalf("Invalid SqlConnection string: %s error: %v", options.SqlConnection, err)
Expand Down Expand Up @@ -103,8 +81,10 @@ func InitDB(options *common_options.DBOptions) {
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()
func InitClickhouseConn(options *common_options.DBOptions) {
dialect, sqlStr, err := options.GetClickhouseConnStr()
if err == nil {
// connect to clickcloud
// force convert sqlstr from clickhouse v2 to v1
Expand All @@ -126,6 +106,33 @@ func InitDB(options *common_options.DBOptions) {
consts.OpsLogWithClickhouse = true
}
}
}

func InitDB(options *common_options.DBOptions) {
if options.DebugSqlchemy {
log.Warningf("debug Sqlchemy is turned on")
sqlchemy.DEBUG_SQLCHEMY = true
}

log.Infof("Registered SQL drivers: %s", strings.Join(sql.Drivers(), ", "))

consts.QueryOffsetOptimization = options.QueryOffsetOptimization

if options.HistoricalUniqueName {
consts.EnableHistoricalUniqueName()
} else {
consts.DisableHistoricalUniqueName()
}

if options.OpsLogMaxKeepMonths > 0 {
consts.SetSplitableMaxKeepMonths(options.OpsLogMaxKeepMonths)
}
if options.SplitableMaxDurationHours > 0 {
consts.SetSplitableMaxDurationHours(options.SplitableMaxDurationHours)
}

InitDBConn(options)
InitClickhouseConn(options)

switch options.LockmanMethod {
case common_options.LockMethodInMemory, "":
Expand Down
27 changes: 11 additions & 16 deletions pkg/dns/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ package dns

import (
"context"
"database/sql"
"errors"
"fmt"
"strconv"
"strings"
Expand All @@ -33,13 +31,15 @@ import (

"yunion.io/x/jsonutils"
"yunion.io/x/log"
"yunion.io/x/pkg/utils"
"yunion.io/x/pkg/errors"
"yunion.io/x/sqlchemy"
_ "yunion.io/x/sqlchemy/backends"

api "yunion.io/x/onecloud/pkg/apis/compute"
identity_api "yunion.io/x/onecloud/pkg/apis/identity"
"yunion.io/x/onecloud/pkg/cloudcommon"
"yunion.io/x/onecloud/pkg/cloudcommon/db"
common_options "yunion.io/x/onecloud/pkg/cloudcommon/options"
"yunion.io/x/onecloud/pkg/compute/models"
"yunion.io/x/onecloud/pkg/mcclient"
"yunion.io/x/onecloud/pkg/mcclient/auth"
Expand Down Expand Up @@ -99,17 +99,12 @@ func New() *SRegionDNS {
}

func (r *SRegionDNS) initDB(c *caddy.Controller) error {
dialect, sqlStr, err := utils.TransSQLAchemyURL(r.SqlConnection)
if err != nil {
return err
}
sqlDb, err := sql.Open(dialect, sqlStr)
if err != nil {
return err
options := &common_options.DBOptions{
SqlConnection: r.SqlConnection,
}
sqlDb.SetMaxOpenConns(defaultDbMaxOpenConn)
sqlDb.SetMaxIdleConns(defaultDbMaxIdleConn)
sqlchemy.SetDB(sqlDb)

cloudcommon.InitDBConn(options)

db.InitAllManagers()

c.OnShutdown(func() error {
Expand Down Expand Up @@ -206,9 +201,9 @@ func (r *SRegionDNS) ServeDNS(ctx context.Context, w dns.ResponseWriter, rmsg *d
}

var (
errRefused = errors.New("refused the query")
errNotFound = errors.New("not found")
errCallNext = errors.New("continue to next")
errRefused = errors.Error("refused the query")
errNotFound = errors.Error("not found")
errCallNext = errors.Error("continue to next")
)

// Services implements the ServiceBackend interface
Expand Down