Skip to content

Commit

Permalink
change write table order to avoid deadlock
Browse files Browse the repository at this point in the history
  • Loading branch information
time-and-fate committed Dec 9, 2021
1 parent d9ef107 commit cb26fbe
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions statistics/handle/handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -1469,6 +1469,15 @@ func (h *Handle) InsertExtendedStats(statsName string, colIDs []int64, tp int, t
return errors.Errorf("extended statistics '%s' with same type on same columns already exists", statsName)
}
}
txn, err := h.mu.ctx.Txn(true)
if err != nil {
return errors.Trace(err)
}
version := txn.StartTS()
// Bump version in `mysql.stats_meta` to trigger stats cache refresh.
if _, err = exec.ExecuteInternal(ctx, "UPDATE mysql.stats_meta SET version = %? WHERE table_id = %?", version, tableID); err != nil {
return err
}
// Remove the existing 'deleted' records.
if _, err = exec.ExecuteInternal(ctx, "DELETE FROM mysql.stats_extended WHERE name = %? and table_id = %?", statsName, tableID); err != nil {
return err
Expand All @@ -1479,17 +1488,10 @@ func (h *Handle) InsertExtendedStats(statsName string, colIDs []int64, tp int, t
// the record from the table, tidb-b should delete the cached item synchronously. While for tidb-c, it has to wait for
// next `Update()` to remove the cached item then.
h.removeExtendedStatsItem(tableID, statsName)
txn, err := h.mu.ctx.Txn(true)
if err != nil {
return errors.Trace(err)
}
version := txn.StartTS()
const sql = "INSERT INTO mysql.stats_extended(name, type, table_id, column_ids, version, status) VALUES (%?, %?, %?, %?, %?, %?)"
if _, err = exec.ExecuteInternal(ctx, sql, statsName, tp, tableID, strColIDs, version, StatsStatusInited); err != nil {
return err
}
// Bump version in `mysql.stats_meta` to trigger stats cache refresh.
_, err = exec.ExecuteInternal(ctx, "UPDATE mysql.stats_meta SET version = %? WHERE table_id = %?", version, tableID)
return
}

Expand Down Expand Up @@ -1529,10 +1531,10 @@ func (h *Handle) MarkExtendedStatsDeleted(statsName string, tableID int64, ifExi
return errors.Trace(err)
}
version := txn.StartTS()
if _, err = exec.ExecuteInternal(ctx, "UPDATE mysql.stats_extended SET version = %?, status = %? WHERE name = %? and table_id = %?", version, StatsStatusDeleted, statsName, tableID); err != nil {
if _, err = exec.ExecuteInternal(ctx, "UPDATE mysql.stats_meta SET version = %? WHERE table_id = %?", version, tableID); err != nil {
return err
}
if _, err = exec.ExecuteInternal(ctx, "UPDATE mysql.stats_meta SET version = %? WHERE table_id = %?", version, tableID); err != nil {
if _, err = exec.ExecuteInternal(ctx, "UPDATE mysql.stats_extended SET version = %?, status = %? WHERE name = %? and table_id = %?", version, StatsStatusDeleted, statsName, tableID); err != nil {
return err
}
return nil
Expand Down

0 comments on commit cb26fbe

Please sign in to comment.