Skip to content
Open
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
19 changes: 16 additions & 3 deletions pkg/executor/infoschema_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -2519,11 +2519,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
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)
}
if !chunkRow.IsNull(6) {
Expand All @@ -2547,11 +2551,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 @@ -2588,6 +2591,7 @@ func dataForAnalyzeStatusHelper(ctx context.Context, e *memtableRetriever, sctx

func getRemainDurationForAnalyzeStatusHelper(
ctx context.Context,
<<<<<<< HEAD
sctx sessionctx.Context, startTime *types.Time,
dbName, tableName, partitionName string, processedRows int64) (*time.Duration, float64, float64, error) {
var remainingDuration = time.Duration(0)
Expand All @@ -2599,6 +2603,15 @@ func getRemainDurationForAnalyzeStatusHelper(
return nil, percentage, totalCnt, err
}
duration := time.Now().UTC().Sub(start)
=======
sctx sessionctx.Context, startTimeUTC *time.Time,
dbName, tableName, partitionName string, processedRows int64,
) (_ *time.Duration, percentage, totalCnt float64, err error) {
remainingDuration := time.Duration(0)
if startTimeUTC != nil {
// time.Time.Sub uses the actual instant.
duration := time.Since(*startTimeUTC)
>>>>>>> 4d20c4a037f (executor: keep analyze status remaining time in UTC (#67229))
if intest.InTest {
if val := ctx.Value(AnalyzeProgressTest); val != nil {
remainingDuration, percentage = calRemainInfoForAnalyzeStatus(ctx, int64(totalCnt), processedRows, duration)
Expand Down
47 changes: 47 additions & 0 deletions pkg/executor/show_stats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,53 @@ func TestShowAnalyzeStatus(t *testing.T) {
tk.MustExec("alter table t2 add index idx(b)")
tk.MustExec("analyze table t2 index idx")
rows = tk.MustQuery("show analyze status").Rows()
<<<<<<< HEAD
require.Len(t, rows, 2)
require.Equal(t, "merge global stats for test.t2's index idx", rows[0][3])
=======
require.Len(t, rows, 3)
jobInfos = []string{rows[0][3].(string), rows[1][3].(string), rows[2][3].(string)}
require.ElementsMatch(t, []string{
"merge global stats for test.t2's index idx",
"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))
>>>>>>> 4d20c4a037f (executor: keep analyze status remaining time in UTC (#67229))
}