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

fix panic when cant openDB on tiup-bench #1557

Merged
merged 1 commit into from
Sep 18, 2021
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
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