Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

statistic: solve write write conflicts of mysql.stats_histograms #23831

Closed
wants to merge 1 commit into from
Closed
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
2 changes: 1 addition & 1 deletion statistics/handle/handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -688,7 +688,7 @@ func (h *Handle) FlushStats() {
}
}
if err := h.DumpStatsDeltaToKV(DumpAll); err != nil {
logutil.BgLogger().Error("[stats] dump stats delta fail", zap.Error(err))
logutil.BgLogger().Error("[stats] dump stats all fail", zap.Error(err))
Copy link
Contributor

Choose a reason for hiding this comment

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

Why?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Why?

There call h.DumpStatsDeltaToKV(DumpAll) but log message is delta

}
if err := h.DumpStatsFeedbackToKV(); err != nil {
logutil.BgLogger().Error("[stats] dump stats feedback fail", zap.Error(err))
Expand Down
17 changes: 15 additions & 2 deletions statistics/handle/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,7 @@ func (h *Handle) dumpTableStatCountToKV(id int64, delta variable.TableDelta) (up
return
}

func (h *Handle) dumpTableStatColSizeToKV(id int64, delta variable.TableDelta) error {
func (h *Handle) dumpTableStatColSizeToKV(id int64, delta variable.TableDelta) (err error) {
if len(delta.ColSize) == 0 {
return nil
}
Expand All @@ -509,9 +509,22 @@ func (h *Handle) dumpTableStatColSizeToKV(id int64, delta variable.TableDelta) e
if len(values) == 0 {
return nil
}

h.mu.Lock()
defer h.mu.Unlock()
ctx := context.TODO()
exec := h.mu.ctx.(sqlexec.SQLExecutor)

_, err = exec.ExecuteInternal(ctx, "begin pessimistic")
if err != nil {
return errors.Trace(err)
}
defer func() {
err = finishTransaction(ctx, exec, err)
}()
sql := fmt.Sprintf("insert into mysql.stats_histograms (table_id, is_index, hist_id, distinct_count, tot_col_size) "+
"values %s on duplicate key update tot_col_size = tot_col_size + values(tot_col_size)", strings.Join(values, ","))
_, _, err := h.execRestrictedSQL(context.Background(), sql)
_, err = exec.ExecuteInternal(ctx, sql)
return errors.Trace(err)
}

Expand Down