diff --git a/cmd/soar/soar.go b/cmd/soar/soar.go index 54ef231d48b47f41d8f238d86c85d662db9ca211..33c2e93c89402282dd0847b8f2988d39ccf94b83 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 6190c18ee513a9e1560bf34de35128c1babfad60..81645f921fd5a96da052ff5ccbecfa8b0abcb318 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 5b597893f5d7fccc81931df61cc17431f72879e5..770204cbadf6a4bc638ffcdfc19f0d21c1f08f29 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使用的信息从接口获取