Skip to content

Commit 402ebc4

Browse files
authored
tests(ticdc): fix bank test (#11407) (#11810)
close #11806
1 parent 24a948c commit 402ebc4

File tree

3 files changed

+74
-2
lines changed

3 files changed

+74
-2
lines changed

cdc/owner/ddl_manager.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,8 @@ func (m *ddlManager) tick(
293293
continue
294294
}
295295

296+
// Note: do not change the key words in the log, it is used to search the
297+
// FinishTS of the DDL job. Some integration tests and users depend on it.
296298
log.Info("handle a ddl job",
297299
zap.String("namespace", m.changfeedID.Namespace),
298300
zap.String("changefeed", m.changfeedID.ID),

tests/integration_tests/bank/case.go

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,18 @@
1414
package main
1515

1616
import (
17+
"bufio"
1718
"context"
1819
"database/sql"
1920
"encoding/json"
2021
"fmt"
2122
"io"
2223
"math/rand"
2324
"net/http"
25+
"os"
26+
"path/filepath"
27+
"regexp"
28+
"strconv"
2429
"strings"
2530
"sync/atomic"
2631
"time"
@@ -623,7 +628,8 @@ func getDownStreamSyncedEndTs(ctx context.Context, db *sql.DB, tidbAPIEndpoint,
623628
log.Error("get downstream sync end ts failed due to timeout", zap.String("table", tableName), zap.Error(ctx.Err()))
624629
return 0, ctx.Err()
625630
case <-time.After(2 * time.Second):
626-
result, ok := tryGetEndTs(db, tidbAPIEndpoint, tableName)
631+
// result, ok := tryGetEndTs(db, tidbAPIEndpoint, tableName)
632+
result, ok := tryGetEndTsFromLog(db, tableName)
627633
if ok {
628634
return result, nil
629635
}
@@ -675,3 +681,65 @@ func tryGetEndTs(db *sql.DB, tidbAPIEndpoint, tableName string) (result uint64,
675681
zap.Uint64("ts", ddlJob[0].Binlog.FinishedTS))
676682
return ddlJob[0].Binlog.FinishedTS, true
677683
}
684+
685+
func tryGetEndTsFromLog(_ *sql.DB, tableName string) (result uint64, ok bool) {
686+
log.Info("try parse finishedTs from ticdc log", zap.String("tableName", tableName))
687+
688+
logFilePath := "/tmp/tidb_cdc_test/bank"
689+
cdcLogFiles := make([]string, 0)
690+
// walk all file with cdc prefix
691+
err := filepath.WalkDir(logFilePath, func(path string, d os.DirEntry, err error) error {
692+
if err != nil {
693+
return err
694+
}
695+
if !d.IsDir() {
696+
if strings.Contains(d.Name(), "down") && strings.Contains(d.Name(), "cdc") && strings.Contains(d.Name(), "log") {
697+
cdcLogFiles = append(cdcLogFiles, path)
698+
fmt.Println(path)
699+
}
700+
}
701+
return nil
702+
})
703+
if err != nil {
704+
log.Error("Failed to walk dir: %v", zap.Error(err))
705+
}
706+
log.Info("total files", zap.Any("file", cdcLogFiles))
707+
708+
logRegex := regexp.MustCompile(`handle a ddl job`)
709+
tableNameRegex := regexp.MustCompile(tableName + "`")
710+
timeStampRegex := regexp.MustCompile(`finishedTs=([0-9]+)`)
711+
for _, f := range cdcLogFiles {
712+
file, err := os.Open(f)
713+
if err != nil {
714+
log.Error("Failed to open file: %v", zap.Error(err))
715+
}
716+
defer file.Close()
717+
718+
reader := bufio.NewReader(file)
719+
for {
720+
bs, _, err := reader.ReadLine()
721+
if err != nil {
722+
if err != io.EOF {
723+
fmt.Printf("Error reading file: %v\n", err)
724+
}
725+
return 0, false
726+
}
727+
line := string(bs)
728+
if !logRegex.MatchString(line) || !tableNameRegex.MatchString(line) {
729+
continue
730+
}
731+
732+
matches := timeStampRegex.FindStringSubmatch(line)
733+
if len(matches) > 1 {
734+
fmt.Println("found first match line, Match Result: ", matches[1], ", line: ", line)
735+
// convert to uint64
736+
result, err := strconv.ParseUint(matches[1], 10, 64)
737+
if err != nil {
738+
log.Error("Failed to parse uint64: %v", zap.Error(err))
739+
}
740+
return result, true
741+
}
742+
}
743+
}
744+
return 0, false
745+
}

tests/integration_tests/bank/run.sh

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@ function prepare() {
2020
run_sql "CREATE DATABASE bank" ${DOWN_TIDB_HOST} ${DOWN_TIDB_PORT}
2121

2222
run_cdc_server --workdir $WORK_DIR --binary $CDC_BINARY
23-
2423
run_cdc_cli changefeed create --sink-uri="mysql://root@${DOWN_TIDB_HOST}:${DOWN_TIDB_PORT}/"
24+
25+
run_cdc_server --workdir $WORK_DIR --binary $CDC_BINARY --addr "127.0.0.1:8400" --pd "http://${DOWN_PD_HOST}:${DOWN_PD_PORT}" --logsuffix "down"
26+
run_cdc_cli changefeed create --sink-uri="blackhole://" -c "changefeed-for-find-finished-ts" --server "http://127.0.0.1:8400"
2527
}
2628

2729
trap stop_tidb_cluster EXIT

0 commit comments

Comments
 (0)