@@ -16,10 +16,10 @@ package gc
1616
1717import (
1818 "context"
19-
2019 "github.com/matrixorigin/matrixone/pkg/common/bloomfilter"
2120 "github.com/matrixorigin/matrixone/pkg/common/malloc"
2221 "github.com/matrixorigin/matrixone/pkg/logutil"
22+ "github.com/matrixorigin/matrixone/pkg/vm/engine/tae/db/checkpoint"
2323 "go.uber.org/zap"
2424
2525 "github.com/matrixorigin/matrixone/pkg/container/types"
@@ -71,6 +71,7 @@ type CheckpointBasedGCJob struct {
7171 ts * types.TS
7272 globalCkpLoc objectio.Location
7373 globalCkpVer uint32
74+ checkpointCli checkpoint.Runner // Added to access catalog
7475
7576 result struct {
7677 vecToGC * vector.Vector
@@ -87,6 +88,7 @@ func NewCheckpointBasedGCJob(
8788 accountSnapshots map [uint32 ][]types.TS ,
8889 iscpTables map [uint64 ]types.TS ,
8990 snapshotMeta * logtail.SnapshotMeta ,
91+ checkpointCli checkpoint.Runner ,
9092 buffer * containers.OneSchemaBatchBuffer ,
9193 isOwner bool ,
9294 mp * mpool.MPool ,
@@ -103,6 +105,7 @@ func NewCheckpointBasedGCJob(
103105 globalCkpLoc : globalCkpLoc ,
104106 globalCkpVer : gckpVersion ,
105107 iscpTables : iscpTables ,
108+ checkpointCli : checkpointCli ,
106109 }
107110 for _ , opt := range opts {
108111 opt (e )
@@ -173,6 +176,7 @@ func (e *CheckpointBasedGCJob) Execute(ctx context.Context) error {
173176 e .snapshotMeta ,
174177 transObjects ,
175178 e .iscpTables ,
179+ e .checkpointCli ,
176180 )
177181 if err != nil {
178182 return err
@@ -316,17 +320,58 @@ func MakeBloomfilterCoarseFilter(
316320 }, nil
317321}
318322
323+ // buildTableExistenceMap creates a combined map of table IDs from both snapshotMeta and catalog
324+ // This avoids the need for locking on every table existence check
325+ func buildTableExistenceMap (snapshotMeta * logtail.SnapshotMeta , checkpointCli checkpoint.Runner ) (map [uint64 ]bool , error ) {
326+ // First, copy all table IDs from snapshotMeta
327+ tableExistenceMap := snapshotMeta .GetAllTableIDs ()
328+ catalog := checkpointCli .GetCatalog ()
329+ count := len (tableExistenceMap )
330+ defer func () {
331+ logutil .Info ("GC-TRACE-TABLE-LIST" ,
332+ zap .Int ("gc-table-count" , count ),
333+ zap .Int ("all-table-count" , len (tableExistenceMap )))
334+ }()
335+ if catalog == nil {
336+ return tableExistenceMap , nil
337+ }
338+ it := catalog .MakeDBIt (true )
339+ for ; it .Valid (); it .Next () {
340+ db := it .Get ().GetPayload ()
341+
342+ itTable := db .MakeTableIt (true )
343+ for itTable .Valid () {
344+ table := itTable .Get ().GetPayload ()
345+ drop := table .GetDeleteAtLocked ()
346+ if drop .IsEmpty () {
347+ tableID := table .GetID ()
348+ tableExistenceMap [tableID ] = true
349+ }
350+ itTable .Next ()
351+ }
352+ }
353+
354+ return tableExistenceMap , nil
355+ }
356+
319357func MakeSnapshotAndPitrFineFilter (
320358 ts * types.TS ,
321359 accountSnapshots map [uint32 ][]types.TS ,
322360 pitrs * logtail.PitrInfo ,
323361 snapshotMeta * logtail.SnapshotMeta ,
324362 transObjects map [string ]map [uint64 ]* ObjectEntry ,
325363 iscpTables map [uint64 ]types.TS ,
364+ checkpointCli checkpoint.Runner ,
326365) (
327366 filter FilterFn ,
328367 err error ,
329368) {
369+ // Build combined table existence map from both snapshotMeta and catalog
370+ tableExistenceMap , err := buildTableExistenceMap (snapshotMeta , checkpointCli )
371+ if err != nil {
372+ return nil , err
373+ }
374+
330375 tableSnapshots , tablePitrs := snapshotMeta .AccountToTableSnapshots (
331376 accountSnapshots ,
332377 pitrs ,
@@ -354,8 +399,8 @@ func MakeSnapshotAndPitrFineFilter(
354399 if transObjects [name ] != nil {
355400 tables := transObjects [name ]
356401 if entry := tables [tableID ]; entry != nil {
357- // Check if the table still exists
358- _ , ok := snapshotMeta . GetAccountId ( tableID )
402+ // Check if the table still exists using the combined map
403+ ok := tableExistenceMap [ tableID ]
359404 // The table has not been dropped, and the dropTS is empty, so it cannot be deleted.
360405 if entry .dropTS .IsEmpty () && ok {
361406 continue
0 commit comments