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

lightning: support detect duplicate keys on specific index ID #37507

Merged
merged 7 commits into from
Aug 31, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 2 additions & 0 deletions br/pkg/lightning/backend/kv/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,8 @@ type SessionOptions struct {
SysVars map[string]string
// a seed used for tableKvEncoder's auto random bits value
AutoRandomSeed int64
// IndexID is used by the DuplicateManager. Only the key range with the specified index ID is scanned.
IndexID int64
}

// NewSession creates a new trimmed down Session matching the options.
Expand Down
28 changes: 28 additions & 0 deletions br/pkg/lightning/backend/local/duplicate.go
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,7 @@ type DuplicateManager struct {
logger log.Logger
concurrency int
hasDupe *atomic.Bool
indexID int64
}

// NewDuplicateManager creates a new DuplicateManager.
Expand Down Expand Up @@ -430,6 +431,7 @@ func NewDuplicateManager(
logger: logger,
concurrency: concurrency,
hasDupe: hasDupe,
indexID: sessOpts.IndexID,
}, nil
}

Expand Down Expand Up @@ -547,6 +549,9 @@ type dupTask struct {
}

func (m *DuplicateManager) buildDupTasks() ([]dupTask, error) {
if m.indexID != 0 {
return m.buildIndexDupTasks()
}
keyRanges, err := tableHandleKeyRanges(m.tbl.Meta())
if err != nil {
return nil, errors.Trace(err)
Expand Down Expand Up @@ -579,6 +584,29 @@ func (m *DuplicateManager) buildDupTasks() ([]dupTask, error) {
return tasks, nil
}

func (m *DuplicateManager) buildIndexDupTasks() ([]dupTask, error) {
sleepymole marked this conversation as resolved.
Show resolved Hide resolved
for _, indexInfo := range m.tbl.Meta().Indices {
if m.indexID != indexInfo.ID {
continue
}
keyRanges, err := tableIndexKeyRanges(m.tbl.Meta(), indexInfo)
if err != nil {
return nil, errors.Trace(err)
}
tasks := make([]dupTask, 0, len(keyRanges))
for _, kr := range keyRanges {
tableID := tablecodec.DecodeTableID(kr.StartKey)
tasks = append(tasks, dupTask{
KeyRange: kr,
tableID: tableID,
indexInfo: indexInfo,
})
}
return tasks, nil
}
return nil, nil
}

func (m *DuplicateManager) splitLocalDupTaskByKeys(
task dupTask,
dupDB *pebble.DB,
Expand Down