Skip to content

Commit 71bc625

Browse files
authored
statistics: gc the statistics correctly after drop the database (pingcap#57309) (pingcap#58179)
close pingcap#57230
1 parent 02bdc2b commit 71bc625

File tree

3 files changed

+51
-2
lines changed

3 files changed

+51
-2
lines changed

pkg/statistics/handle/ddl/BUILD.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ go_test(
3535
timeout = "short",
3636
srcs = ["ddl_test.go"],
3737
flaky = True,
38-
shard_count = 19,
38+
shard_count = 20,
3939
deps = [
4040
":ddl",
4141
"//pkg/ddl/notifier",

pkg/statistics/handle/ddl/ddl.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,18 @@ func (h *ddlHandlerImpl) HandleDDLEvent(s *notifier.SchemaChangeEvent) error {
181181
case model.ActionAddIndex:
182182
// No need to update the stats meta for the adding index event.
183183
case model.ActionDropSchema:
184-
// TODO: handle the drop schema event.
184+
miniDBInfo := s.GetDropSchemaInfo()
185+
intest.Assert(miniDBInfo != nil)
186+
for _, table := range miniDBInfo.Tables {
187+
// Try best effort to update the stats meta version for gc.
188+
if err := h.statsWriter.UpdateStatsMetaVersionForGC(table.ID); err != nil {
189+
logutil.StatsLogger().Error(
190+
"Failed to update stats meta version for gc",
191+
zap.Int64("tableID", table.ID),
192+
zap.Error(err),
193+
)
194+
}
195+
}
185196
default:
186197
intest.Assert(false)
187198
logutil.StatsLogger().Error("Unhandled schema change event", zap.Stringer("type", s))

pkg/statistics/handle/ddl/ddl_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1306,6 +1306,44 @@ func TestAddPartitioning(t *testing.T) {
13061306
)
13071307
}
13081308

1309+
func TestDropSchema(t *testing.T) {
1310+
store, dom := testkit.CreateMockStoreAndDomain(t)
1311+
tk := testkit.NewTestKit(t, store)
1312+
1313+
tk.MustExec("use test")
1314+
tk.MustExec("create table t (c1 int)")
1315+
h := dom.StatsHandle()
1316+
tk.MustExec("insert into t values (1)")
1317+
require.NoError(t, h.DumpStatsDeltaToKV(true))
1318+
1319+
is := dom.InfoSchema()
1320+
tbl, err := is.TableByName(context.Background(), pmodel.NewCIStr("test"), pmodel.NewCIStr("t"))
1321+
require.NoError(t, err)
1322+
tableInfo := tbl.Meta()
1323+
// Check the current stats meta version.
1324+
rows := tk.MustQuery(
1325+
"select version from mysql.stats_meta where table_id = ?",
1326+
tableInfo.ID,
1327+
).Rows()
1328+
require.Len(t, rows, 1)
1329+
version := rows[0][0].(string)
1330+
1331+
tk.MustExec("drop database test")
1332+
1333+
// Handle the drop schema event.
1334+
dropSchemaEvent := findEvent(h.DDLEventCh(), model.ActionDropSchema)
1335+
err = h.HandleDDLEvent(dropSchemaEvent)
1336+
require.NoError(t, err)
1337+
1338+
// Check the stats meta version after drop schema.
1339+
rows = tk.MustQuery(
1340+
"select version from mysql.stats_meta where table_id = ?",
1341+
tableInfo.ID,
1342+
).Rows()
1343+
require.Len(t, rows, 1)
1344+
require.NotEqual(t, version, rows[0][0].(string))
1345+
}
1346+
13091347
func findEvent(eventCh <-chan *notifier.SchemaChangeEvent, eventType model.ActionType) *notifier.SchemaChangeEvent {
13101348
// Find the target event.
13111349
for {

0 commit comments

Comments
 (0)