-
Notifications
You must be signed in to change notification settings - Fork 5.9k
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
statistics: handle deleted tables correctly in the PQ #57649
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -57,7 +57,7 @@ func (f *AnalysisJobFactory) CreateNonPartitionedTableAnalysisJob( | |
tblInfo *model.TableInfo, | ||
tblStats *statistics.Table, | ||
) AnalysisJob { | ||
if !tblStats.IsEligibleForAnalysis() { | ||
if tblStats == nil || !tblStats.IsEligibleForAnalysis() { | ||
return nil | ||
} | ||
|
||
|
@@ -92,7 +92,7 @@ func (f *AnalysisJobFactory) CreateStaticPartitionAnalysisJob( | |
partitionID int64, | ||
partitionStats *statistics.Table, | ||
) AnalysisJob { | ||
if !partitionStats.IsEligibleForAnalysis() { | ||
if partitionStats == nil || !partitionStats.IsEligibleForAnalysis() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto |
||
return nil | ||
} | ||
|
||
|
@@ -128,7 +128,7 @@ func (f *AnalysisJobFactory) CreateDynamicPartitionedTableAnalysisJob( | |
globalTblStats *statistics.Table, | ||
partitionStats map[PartitionIDAndName]*statistics.Table, | ||
) AnalysisJob { | ||
if !globalTblStats.IsEligibleForAnalysis() { | ||
if globalTblStats == nil || !globalTblStats.IsEligibleForAnalysis() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto |
||
return nil | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -644,14 +644,16 @@ func (pq *AnalysisPriorityQueue) RefreshLastAnalysisDuration() { | |
zap.Int64("tableID", job.GetTableID()), | ||
zap.String("job", job.String()), | ||
) | ||
// TODO: Remove this after handling the DDL event. | ||
// Delete the job from the queue since its table is missing. This is a safeguard - | ||
// DDL events should have already cleaned up jobs for dropped tables. | ||
err := pq.syncFields.inner.delete(job) | ||
if err != nil { | ||
statslogutil.StatsLogger().Error("Failed to delete job from priority queue", | ||
zap.Error(err), | ||
zap.String("job", job.String()), | ||
) | ||
} | ||
continue | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix the |
||
} | ||
indicators.LastAnalysisDuration = jobFactory.GetTableLastAnalyzeDuration(tableStats) | ||
job.SetIndicators(indicators) | ||
|
@@ -852,9 +854,9 @@ func (pq *AnalysisPriorityQueue) Close() { | |
pq.syncFields.initialized = false | ||
// The rest fields will be reset when the priority queue is initialized. | ||
// But we do it here for double safety. | ||
pq.syncFields.inner = nil | ||
pq.syncFields.runningJobs = nil | ||
pq.syncFields.mustRetryJobs = nil | ||
pq.syncFields.inner = newHeap() | ||
Rustin170506 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
pq.syncFields.runningJobs = make(map[int64]struct{}) | ||
pq.syncFields.mustRetryJobs = make(map[int64]struct{}) | ||
pq.syncFields.lastDMLUpdateFetchTimestamp = 0 | ||
pq.syncFields.cancel = nil | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,7 @@ import ( | |
"testing" | ||
"time" | ||
|
||
"github.com/pingcap/tidb/pkg/meta/model" | ||
pmodel "github.com/pingcap/tidb/pkg/parser/model" | ||
"github.com/pingcap/tidb/pkg/sessionctx" | ||
"github.com/pingcap/tidb/pkg/statistics" | ||
|
@@ -608,3 +609,47 @@ func TestPQCanBeClosedAndReInitialized(t *testing.T) { | |
// Check if the priority queue is initialized. | ||
require.True(t, pq.IsInitialized()) | ||
} | ||
|
||
func TestPQHandlesTableDeletionGracefully(t *testing.T) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Test case for: |
||
store, dom := testkit.CreateMockStoreAndDomain(t) | ||
handle := dom.StatsHandle() | ||
|
||
tk := testkit.NewTestKit(t, store) | ||
tk.MustExec("use test") | ||
tk.MustExec("create table t1 (a int)") | ||
tk.MustExec("insert into t1 values (1)") | ||
statistics.AutoAnalyzeMinCnt = 0 | ||
defer func() { | ||
statistics.AutoAnalyzeMinCnt = 1000 | ||
}() | ||
|
||
ctx := context.Background() | ||
require.NoError(t, handle.DumpStatsDeltaToKV(true)) | ||
require.NoError(t, handle.Update(ctx, dom.InfoSchema())) | ||
pq := priorityqueue.NewAnalysisPriorityQueue(handle) | ||
defer pq.Close() | ||
require.NoError(t, pq.Initialize()) | ||
|
||
// Check the priority queue is not empty. | ||
l, err := pq.Len() | ||
require.NoError(t, err) | ||
require.NotEqual(t, 0, l) | ||
|
||
tbl, err := dom.InfoSchema().TableByName(ctx, pmodel.NewCIStr("test"), pmodel.NewCIStr("t1")) | ||
require.NoError(t, err) | ||
|
||
// Drop the table and mock the table stats is removed from the cache. | ||
tk.MustExec("drop table t1") | ||
deleteEvent := findEvent(handle.DDLEventCh(), model.ActionDropTable) | ||
require.NotNil(t, deleteEvent) | ||
require.NoError(t, handle.HandleDDLEvent(deleteEvent)) | ||
require.NoError(t, handle.Update(ctx, dom.InfoSchema())) | ||
|
||
// Make sure handle.Get() returns false. | ||
_, ok := handle.Get(tbl.Meta().ID) | ||
require.False(t, ok) | ||
|
||
require.NotPanics(t, func() { | ||
pq.RefreshLastAnalysisDuration() | ||
}) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We didn't encounter any issues here, just double-checking.