Skip to content
Merged
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
18 changes: 8 additions & 10 deletions pkg/cdc/table_scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"sync"
"time"

"github.com/matrixorigin/matrixone/pkg/common/moerr"
"github.com/matrixorigin/matrixone/pkg/objectio"

"go.uber.org/zap"
Expand Down Expand Up @@ -58,6 +57,7 @@ var GetTableDetector = func(cnUUID string) *TableDetector {
CallBackTableName: make(map[string][]string),
SubscribedTableNames: make(map[string][]string),
}
detector.scanTableFn = detector.scanTable
})
return detector
}
Expand Down Expand Up @@ -88,6 +88,8 @@ type TableDetector struct {
// tablename -> [taska, taskb ...]
SubscribedTableNames map[string][]string

scanTableFn func() error

// to make sure there is at most only one handleNewTables running, so the truncate info will not be lost
handling bool
lastMp map[uint32]TblMap
Expand Down Expand Up @@ -216,19 +218,19 @@ func (s *TableDetector) scanTableLoop(ctx context.Context) {
s.scanAndProcess(ctx)
case <-retryTicker.C:
s.mu.Lock()
if s.handling || s.lastMp == nil {
s.mu.Unlock()
handling, lastMp := s.handling, s.lastMp
s.mu.Unlock()
if handling || lastMp == nil {
continue
}
s.mu.Unlock()

go s.processCallback(ctx, s.lastMp)
go s.processCallback(ctx, lastMp)
}
}
}

func (s *TableDetector) scanAndProcess(ctx context.Context) {
if err := s.scanTable(); err != nil {
if err := s.scanTableFn(); err != nil {
logutil.Error("CDC-TableDetector-Scan-Error", zap.Error(err))
return
}
Expand Down Expand Up @@ -264,10 +266,6 @@ func (s *TableDetector) processCallback(ctx context.Context, tables map[uint32]T
}

func (s *TableDetector) scanTable() error {
if objectio.CDCScanTableErrInjected() {
return moerr.NewInternalError(context.Background(), "CDC_SCANTABLE_ERR")
}

ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
var (
Expand Down
27 changes: 26 additions & 1 deletion pkg/cdc/table_scanner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,13 +162,38 @@ func TestScanAndProcess(t *testing.T) {
exec: nil,
}

tables := map[uint32]TblMap{
1: {
"db1.tbl1": &DbTableInfo{
SourceDbId: 1,
SourceDbName: "db1",
SourceTblId: 1001,
SourceTblName: "tbl1",
SourceCreateSql: "create table tbl1 (a int)",
IdChanged: false,
},
},
}
scanCount := 0
td.scanTableFn = func() error {
td.mu.Lock()
if scanCount%5 == 0 {
td.lastMp = tables
} else {
td.lastMp = nil
}
td.mu.Unlock()
scanCount++
return nil
}

fault.Enable()
objectio.SimpleInject(objectio.FJ_CDCScanTableErr)
rm, _ := objectio.InjectCDCScanTable("fast scan")
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
go td.scanTableLoop(ctx)
time.Sleep(2 * time.Millisecond)
time.Sleep(200 * time.Millisecond)
defer rm()
td.scanAndProcess(context.Background())
fault.Disable()
Expand Down
Loading