未验证 提交 6bdf1d96 编写于 作者: martianzhang's avatar martianzhang 提交者: GitHub

Merge pull request #72 from liipx/master

fix `-cleanup-test-database` hangup without `-query`
...@@ -83,6 +83,7 @@ func main() { ...@@ -83,6 +83,7 @@ func main() {
// 使用 -cleanup-test-database 命令手动清理残余的 optimizer_xxx 数据库 // 使用 -cleanup-test-database 命令手动清理残余的 optimizer_xxx 数据库
if common.Config.CleanupTestDatabase { if common.Config.CleanupTestDatabase {
vEnv.CleanupTestDatabase() vEnv.CleanupTestDatabase()
return
} }
// 如果使用到测试环境,在这里环境清理 // 如果使用到测试环境,在这里环境清理
......
...@@ -174,10 +174,10 @@ func startSampling(conn, localConn mysql.Conn, database, table string, factor fl ...@@ -174,10 +174,10 @@ func startSampling(conn, localConn mysql.Conn, database, table string, factor fl
// str = "" // str = ""
case []byte: case []byte:
// 先尝试转成数字,如果报错则转换成string // 先尝试转成数字,如果报错则转换成string
v, err := row.Int64Err(i) if v, err := row.Int64Err(i); err != nil {
values[i] = strconv.FormatInt(v, 10)
if err != nil {
values[i] = string(data) values[i] = string(data)
} else {
values[i] = strconv.FormatInt(v, 10)
} }
case time.Time: case time.Time:
values[i] = mysql.TimeString(data) values[i] = mysql.TimeString(data)
......
...@@ -153,8 +153,14 @@ func (ve VirtualEnv) CleanUp() bool { ...@@ -153,8 +153,14 @@ func (ve VirtualEnv) CleanUp() bool {
// CleanupTestDatabase 清除一小时前的环境 // CleanupTestDatabase 清除一小时前的环境
func (ve *VirtualEnv) CleanupTestDatabase() { func (ve *VirtualEnv) CleanupTestDatabase() {
common.Log.Debug("CleanupTestDatabase ...") common.Log.Debug("CleanupTestDatabase ...")
dbs, err := ve.Query("show databases like 'optimizer%'") dbs, err := ve.Query("show databases like 'optimizer%%'")
if err == nil { if err != nil {
common.Log.Error("CleanupTestDatabase failed Error:%s", err.Error())
return
}
// TODO: 1 hour should be config-able
minHour := 1
for _, row := range dbs.Rows { for _, row := range dbs.Rows {
testDatabase := row.Str(0) testDatabase := row.Str(0)
// test temporary database format `optimizer_YYMMDDHHmmss_randomString(16)` // test temporary database format `optimizer_YYMMDDHHmmss_randomString(16)`
...@@ -168,22 +174,21 @@ func (ve *VirtualEnv) CleanupTestDatabase() { ...@@ -168,22 +174,21 @@ func (ve *VirtualEnv) CleanupTestDatabase() {
common.Log.Error("CleanupTestDatabase compute pastTime Error: %s", err.Error()) common.Log.Error("CleanupTestDatabase compute pastTime Error: %s", err.Error())
continue continue
} }
// TODO: 1 hour should be config-able
subHour := time.Now().Sub(pastTime).Hours() subHour := time.Now().Sub(pastTime).Hours()
if subHour > 1 { if subHour > float64(minHour) {
_, err := ve.Query("drop database %s", testDatabase) if _, err := ve.Query("drop database %s", testDatabase); err != nil {
if err != nil {
common.Log.Error("CleanupTestDatabase failed Error: %s", err.Error()) common.Log.Error("CleanupTestDatabase failed Error: %s", err.Error())
continue continue
} }
common.Log.Debug("CleanupTestDatabase drop database %s success", testDatabase) common.Log.Debug("CleanupTestDatabase drop database %s success", testDatabase)
} else { continue
common.Log.Debug("CleanupTestDatabase by pass database %s, less than %d hours", testDatabase, subHour)
} }
common.Log.Debug("CleanupTestDatabase by pass database %s, %.2f less than %d hours", testDatabase, subHour, minHour)
} }
} else {
common.Log.Error("CleanupTestDatabase failed Error:%s", err.Error()) common.Log.Debug("CleanupTestDatabase done")
} return
} }
// BuildVirtualEnv rEnv为SQL源环境,DB使用的信息从接口获取 // BuildVirtualEnv rEnv为SQL源环境,DB使用的信息从接口获取
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册