Skip to content
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
20 changes: 10 additions & 10 deletions pkg/executor/infoschema_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -2548,11 +2548,15 @@ func dataForAnalyzeStatusHelper(ctx context.Context, e *memtableRetriever, sctx
jobInfo := chunkRow.GetString(3)
processedRows := chunkRow.GetInt64(4)
var startTime, endTime any
// startTime and endTime use the local timezone for displaying.
// startTimeUTC is used to calculate the remaining duration of the job.
var startTimeUTC *time.Time
Comment thread
0xPoe marked this conversation as resolved.
if !chunkRow.IsNull(5) {
t, err := chunkRow.GetTime(5).GoTime(time.UTC)
if err != nil {
return nil, err
}
startTimeUTC = &t
startTime = types.NewTime(types.FromGoTime(t.In(sctx.GetSessionVars().TimeZone)), mysql.TypeDatetime, 0)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is where the original bug comes form. We use the session time, but we applied it as UTC in later usage.

}
if !chunkRow.IsNull(6) {
Expand All @@ -2576,11 +2580,10 @@ func dataForAnalyzeStatusHelper(ctx context.Context, e *memtableRetriever, sctx

var remainDurationStr, progressDouble, estimatedRowCntStr any
if state == statistics.AnalyzeRunning && !strings.HasPrefix(jobInfo, "merge global stats") {
startTime, ok := startTime.(types.Time)
if !ok {
if startTimeUTC == nil {
return nil, errors.New("invalid start time")
}
remainingDuration, progress, estimatedRowCnt, remainDurationErr := getRemainDurationForAnalyzeStatusHelper(ctx, sctx, &startTime,
remainingDuration, progress, estimatedRowCnt, remainDurationErr := getRemainDurationForAnalyzeStatusHelper(ctx, sctx, startTimeUTC,
dbName, tableName, partitionName, processedRows)
if remainDurationErr != nil {
logutil.BgLogger().Warn("get remaining duration failed", zap.Error(remainDurationErr))
Expand Down Expand Up @@ -2617,16 +2620,13 @@ func dataForAnalyzeStatusHelper(ctx context.Context, e *memtableRetriever, sctx

func getRemainDurationForAnalyzeStatusHelper(
ctx context.Context,
sctx sessionctx.Context, startTime *types.Time,
sctx sessionctx.Context, startTimeUTC *time.Time,
dbName, tableName, partitionName string, processedRows int64,
) (_ *time.Duration, percentage, totalCnt float64, err error) {
remainingDuration := time.Duration(0)
if startTime != nil {
start, err := startTime.GoTime(time.UTC)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Buggy code!

if err != nil {
return nil, percentage, totalCnt, err
}
duration := time.Now().UTC().Sub(start)
if startTimeUTC != nil {
// time.Time.Sub uses the actual instant.
duration := time.Since(*startTimeUTC)
if intest.InTest {
if val := ctx.Value(AnalyzeProgressTest); val != nil {
remainingDuration, percentage = calRemainInfoForAnalyzeStatus(ctx, int64(totalCnt), processedRows, duration)
Expand Down
37 changes: 37 additions & 0 deletions pkg/executor/show_stats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -399,4 +399,41 @@ func TestShowAnalyzeStatus(t *testing.T) {
"merge global stats for test.t2 columns",
"analyze table all indexes, all columns with 256 buckets, 100 topn, 1 samplerate",
}, jobInfos)

tk.MustExec("delete from mysql.analyze_jobs")
tk.MustExec("drop table if exists t3")
tk.MustExec("create table t3 (a int, b int, primary key(a))")
tk.MustExec(`insert into t3 values (1, 1), (2, 2)`)
tk.MustExec("analyze table t3")
tk.MustExec("delete from mysql.analyze_jobs")

originalTZ := tk.MustQuery("select @@time_zone").Rows()[0][0]
defer func() {
tk.MustExec("set @@time_zone = ?", originalTZ)
}()
tk.MustExec("set @@time_zone = '+08:00'")
tk.MustExec(`insert into mysql.analyze_jobs (
table_schema,
table_name,
partition_name,
job_info,
processed_rows,
start_time,
state,
instance
) values (
'test',
't3',
'',
'analyze table all indexes, all columns with 256 buckets, 100 topn, 1 samplerate',
1,
CURRENT_TIMESTAMP - INTERVAL 1 MINUTE,
'running',
'127.0.0.1:4000'
)`)
rows = tk.MustQuery("show analyze status where table_name = 't3' and state = 'running'").Rows()
require.Len(t, rows, 1)
remainingDuration, err := time.ParseDuration(rows[0][11].(string))
require.NoError(t, err)
require.GreaterOrEqual(t, remainingDuration, time.Duration(0))
}
Loading