Skip to content

Commit

Permalink
fix panic when cant openDB on tiup-bench (#1557)
Browse files Browse the repository at this point in the history
  • Loading branch information
nexustar authored Sep 18, 2021
1 parent 2e089a2 commit 20455db
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
8 changes: 5 additions & 3 deletions components/bench/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,30 +51,32 @@ func closeDB() {
globalDB = nil
}

func openDB() {
func openDB() error {
// TODO: support other drivers
var tmpDB *sql.DB
ds := fmt.Sprintf("%s:%s@tcp(%s:%d)/", user, password, host, port)
dsn := ds + dbName
var err error
globalDB, err = sql.Open(mysqlDriver, dsn)
if err != nil {
panic(err)
return err
}
if err := globalDB.Ping(); err != nil {
errString := err.Error()
if strings.Contains(errString, unknownDB) {
tmpDB, _ = sql.Open(mysqlDriver, ds)
defer tmpDB.Close()
if _, err := tmpDB.Exec(createDBDDL + dbName); err != nil {
panic(fmt.Errorf("failed to create database, err %v", err))
return fmt.Errorf("failed to create database, err %v", err)
}
} else {
globalDB = nil
return err
}
} else {
globalDB.SetMaxIdleConns(threads + 1)
}
return nil
}

func main() {
Expand Down
6 changes: 5 additions & 1 deletion components/bench/tpcc.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ func executeTpcc(action string) {
}
runtime.GOMAXPROCS(maxProcs)

openDB()
if err := openDB(); err != nil {
fmt.Println(err)
fmt.Println("Cannot open database, pleae check it (ip/port/username/password)")
os.Exit(1)
}

tpccConfig.DBName = dbName
tpccConfig.Threads = threads
Expand Down
8 changes: 7 additions & 1 deletion components/bench/tpch.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package main

import (
"context"
"fmt"
"os"
"strings"

"github.com/pingcap/go-tpc/tpch"
Expand All @@ -11,7 +13,11 @@ import (
var tpchConfig tpch.Config

func executeTpch(action string, _ []string) {
openDB()
if err := openDB(); err != nil {
fmt.Println(err)
fmt.Println("Cannot open database, pleae check it (ip/port/username/password)")
os.Exit(1)
}
defer closeDB()

tpchConfig.DBName = dbName
Expand Down

0 comments on commit 20455db

Please sign in to comment.