From 6c47c87024f0df356d89bd9bb787eb1a9c3dabde Mon Sep 17 00:00:00 2001 From: liipx Date: Fri, 2 Nov 2018 11:50:15 +0800 Subject: [PATCH] fix -cleanup-test-database with out query --- cmd/soar/soar.go | 1 + database/sampling.go | 6 ++--- env/env.go | 59 ++++++++++++++++++++++++-------------------- 3 files changed, 36 insertions(+), 30 deletions(-) diff --git a/cmd/soar/soar.go b/cmd/soar/soar.go index 54ef231..33c2e93 100644 --- a/cmd/soar/soar.go +++ b/cmd/soar/soar.go @@ -83,6 +83,7 @@ func main() { // 使用 -cleanup-test-database 命令手动清理残余的 optimizer_xxx 数据库 if common.Config.CleanupTestDatabase { vEnv.CleanupTestDatabase() + return } // 如果使用到测试环境,在这里环境清理 diff --git a/database/sampling.go b/database/sampling.go index 6190c18..81645f9 100644 --- a/database/sampling.go +++ b/database/sampling.go @@ -174,10 +174,10 @@ func startSampling(conn, localConn mysql.Conn, database, table string, factor fl // str = "" case []byte: // 先尝试转成数字,如果报错则转换成string - v, err := row.Int64Err(i) - values[i] = strconv.FormatInt(v, 10) - if err != nil { + if v, err := row.Int64Err(i); err != nil { values[i] = string(data) + } else { + values[i] = strconv.FormatInt(v, 10) } case time.Time: values[i] = mysql.TimeString(data) diff --git a/env/env.go b/env/env.go index 5b59789..770204c 100644 --- a/env/env.go +++ b/env/env.go @@ -153,37 +153,42 @@ func (ve VirtualEnv) CleanUp() bool { // CleanupTestDatabase 清除一小时前的环境 func (ve *VirtualEnv) CleanupTestDatabase() { common.Log.Debug("CleanupTestDatabase ...") - dbs, err := ve.Query("show databases like 'optimizer%'") - if err == nil { - for _, row := range dbs.Rows { - testDatabase := row.Str(0) - // test temporary database format `optimizer_YYMMDDHHmmss_randomString(16)` - if len(testDatabase) != 39 { - common.Log.Debug("CleanupTestDatabase by pass %s", testDatabase) - continue - } - s := strings.Split(testDatabase, "_") - pastTime, err := time.Parse("060102150405", s[1]) - if err != nil { - common.Log.Error("CleanupTestDatabase compute pastTime Error: %s", err.Error()) + dbs, err := ve.Query("show databases like 'optimizer%%'") + 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 { + testDatabase := row.Str(0) + // test temporary database format `optimizer_YYMMDDHHmmss_randomString(16)` + if len(testDatabase) != 39 { + common.Log.Debug("CleanupTestDatabase by pass %s", testDatabase) + continue + } + s := strings.Split(testDatabase, "_") + pastTime, err := time.Parse("060102150405", s[1]) + if err != nil { + common.Log.Error("CleanupTestDatabase compute pastTime Error: %s", err.Error()) + continue + } + + subHour := time.Now().Sub(pastTime).Hours() + if subHour > float64(minHour) { + if _, err := ve.Query("drop database %s", testDatabase); err != nil { + common.Log.Error("CleanupTestDatabase failed Error: %s", err.Error()) continue } - // TODO: 1 hour should be config-able - subHour := time.Now().Sub(pastTime).Hours() - if subHour > 1 { - _, err := ve.Query("drop database %s", testDatabase) - if err != nil { - common.Log.Error("CleanupTestDatabase failed Error: %s", err.Error()) - continue - } - common.Log.Debug("CleanupTestDatabase drop database %s success", testDatabase) - } else { - common.Log.Debug("CleanupTestDatabase by pass database %s, less than %d hours", testDatabase, subHour) - } + common.Log.Debug("CleanupTestDatabase drop database %s success", testDatabase) + continue } - } else { - common.Log.Error("CleanupTestDatabase failed Error:%s", err.Error()) + common.Log.Debug("CleanupTestDatabase by pass database %s, %.2f less than %d hours", testDatabase, subHour, minHour) } + + common.Log.Debug("CleanupTestDatabase done") + return } // BuildVirtualEnv rEnv为SQL源环境,DB使用的信息从接口获取 -- GitLab