Skip to content
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

Filter shadow indexes upsert #187

Merged
merged 1 commit into from
Sep 26, 2024
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
11 changes: 11 additions & 0 deletions pkg/ccr/ingest_binlog_job.go
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,11 @@ func (j *IngestBinlogJob) preparePartition(srcTableId, destTableId int64, partit
if j.srcMeta.IsIndexDropped(indexId) {
continue
}
if featureFilterShadowIndexesUpsert {
if _, ok := j.ccrJob.progress.ShadowIndexes[indexId]; ok {
continue
}
}
srcIndexMeta, ok := srcIndexIdMap[indexId]
if !ok {
j.setError(xerror.Errorf(xerror.Meta, "index id %v not found in src meta", indexId))
Expand All @@ -407,6 +412,12 @@ func (j *IngestBinlogJob) preparePartition(srcTableId, destTableId int64, partit
log.Infof("skip the dropped index %d", indexId)
continue
}
if featureFilterShadowIndexesUpsert {
if _, ok := j.ccrJob.progress.ShadowIndexes[indexId]; ok {
log.Infof("skip the shadow index %d", indexId)
continue
}
}

srcIndexMeta := srcIndexIdMap[indexId]
destIndexMeta := destIndexNameMap[getSrcIndexName(job, srcIndexMeta)]
Expand Down
28 changes: 28 additions & 0 deletions pkg/ccr/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ var (
featureAtomicRestore bool
featureCreateViewDropExists bool
featureReplaceNotMatchedWithAlias bool
featureFilterShadowIndexesUpsert bool
)

func init() {
Expand All @@ -56,6 +57,8 @@ func init() {
"drop the exists view if exists, when sync the creating view binlog")
flag.BoolVar(&featureReplaceNotMatchedWithAlias, "feature_replace_not_matched_with_alias", false,
"replace signature not matched tables with table alias during the full sync")
flag.BoolVar(&featureFilterShadowIndexesUpsert, "feature_filter_shadow_indexes_upsert", false,
"filter the upsert to the shadow indexes")
}

type SyncType int
Expand Down Expand Up @@ -887,6 +890,7 @@ func (j *Job) fullSync() error {
}

j.progress.TableMapping = tableMapping
j.progress.ShadowIndexes = nil
j.progress.NextWithPersist(j.progress.CommitSeq, DBTablesIncrementalSync, Done, "")
case TableSync:
if destTable, err := j.destMeta.UpdateTable(j.Dest.Table, 0); err != nil {
Expand All @@ -901,6 +905,7 @@ func (j *Job) fullSync() error {

j.progress.TableCommitSeqMap = nil
j.progress.TableMapping = nil
j.progress.ShadowIndexes = nil
j.progress.NextWithPersist(j.progress.CommitSeq, TableIncrementalSync, Done, "")
default:
return xerror.Errorf(xerror.Normal, "invalid sync type %d", j.SyncType)
Expand Down Expand Up @@ -1459,6 +1464,24 @@ func (j *Job) handleAlterJob(binlog *festruct.TBinlog) error {
}

if !alterJob.IsFinished() {
switch alterJob.JobState {
case record.ALTER_JOB_STATE_PENDING:
// Once the schema change step to WAITING_TXN, the upsert to the shadow indexes is allowed,
// but the dest indexes of the downstream cluster hasn't been created.
//
// To filter the upsert to the shadow indexes, save the shadow index ids here.
if j.progress.ShadowIndexes == nil {
j.progress.ShadowIndexes = make(map[int64]int64)
}
for shadowIndexId, originIndexId := range alterJob.ShadowIndexes {
j.progress.ShadowIndexes[shadowIndexId] = originIndexId
}
case record.ALTER_JOB_STATE_CANCELLED:
// clear the shadow indexes
for shadowIndexId := range alterJob.ShadowIndexes {
delete(j.progress.ShadowIndexes, shadowIndexId)
}
}
return nil
}

Expand All @@ -1471,6 +1494,11 @@ func (j *Job) handleAlterJob(binlog *festruct.TBinlog) error {
}

if featureSchemaChangePartialSync && alterJob.Type == record.ALTER_JOB_SCHEMA_CHANGE {
// Once partial snapshot finished, the shadow indexes will be convert to normal indexes.
for shadowIndexId := range alterJob.ShadowIndexes {
delete(j.progress.ShadowIndexes, shadowIndexId)
}

replaceTable := true
return j.newPartialSnapshot(alterJob.TableName, nil, replaceTable)
}
Expand Down
4 changes: 4 additions & 0 deletions pkg/ccr/job_progress.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,9 @@ type JobProgress struct {
// The tables need to be replaced rather than dropped during sync.
TableAliases map[string]string `json:"table_aliases,omitempty"`

// The shadow indexes of the pending schema changes
ShadowIndexes map[int64]int64 `json:"shadow_index_map"`

// Some fields to save the unix epoch time of the key timepoint.
CreatedAt int64 `json:"created_at,omitempty"`
FullSyncStartAt int64 `json:"full_sync_start_at,omitempty"`
Expand Down Expand Up @@ -194,6 +197,7 @@ func NewJobProgress(jobName string, syncType SyncType, db storage.DB) *JobProgre
PersistData: "",
PartialSyncData: nil,
TableAliases: nil,
ShadowIndexes: nil,

CreatedAt: time.Now().Unix(),
FullSyncStartAt: 0,
Expand Down
23 changes: 15 additions & 8 deletions pkg/ccr/record/alter_job_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,23 @@ import (
const (
ALTER_JOB_SCHEMA_CHANGE = "SCHEMA_CHANGE"
ALTER_JOB_ROLLUP = "ROLLUP"

ALTER_JOB_STATE_PENDING = "PENDING"
ALTER_JOB_STATE_WAITING_TXN = "WAITING_TXN"
ALTER_JOB_STATE_RUNNING = "RUNNING"
ALTER_JOB_STATE_FINISHED = "FINISHED"
ALTER_JOB_STATE_CANCELLED = "CANCELLED"
)

type AlterJobV2 struct {
Type string `json:"type"`
DbId int64 `json:"dbId"`
TableId int64 `json:"tableId"`
TableName string `json:"tableName"`
JobId int64 `json:"jobId"`
JobState string `json:"jobState"`
RawSql string `json:"rawSql"`
Type string `json:"type"`
DbId int64 `json:"dbId"`
TableId int64 `json:"tableId"`
TableName string `json:"tableName"`
JobId int64 `json:"jobId"`
JobState string `json:"jobState"`
RawSql string `json:"rawSql"`
ShadowIndexes map[int64]int64 `json:"iim"`
}

func NewAlterJobV2FromJson(data string) (*AlterJobV2, error) {
Expand Down Expand Up @@ -47,7 +54,7 @@ func NewAlterJobV2FromJson(data string) (*AlterJobV2, error) {
}

func (a *AlterJobV2) IsFinished() bool {
return a.JobState == "FINISHED"
return a.JobState == ALTER_JOB_STATE_FINISHED
}

// Stringer
Expand Down
Loading