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

ddl: check the key existence on original index #40749

Merged
merged 37 commits into from
Feb 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
3b95add
ddl: check the key existence on original index
tangenta Jan 20, 2023
c5468ad
Merge branch 'master' into add-index-check-exist
hawkingrei Jan 20, 2023
df2fb49
add a test for insert on duplicate update
tangenta Jan 28, 2023
576c374
update comment
tangenta Jan 28, 2023
bd84f66
Merge remote-tracking branch 'upstream/master' into add-index-check-e…
tangenta Jan 29, 2023
6a9b48e
fix replace and insert on duplicate update issues
tangenta Jan 30, 2023
a224037
fix replace and insert on duplicate update issues
tangenta Jan 31, 2023
c0548a4
make context.Context as the first arg of a function
tangenta Jan 31, 2023
f220a48
Merge remote-tracking branch 'upstream/master' into add-index-check-e…
tangenta Jan 31, 2023
549e14b
fix admin check
tangenta Jan 31, 2023
5f1f47a
Merge branch 'master' into add-index-check-exist
tangenta Jan 31, 2023
fc12610
fix integration test
tangenta Feb 1, 2023
9303b0b
Merge branch 'master' into add-index-check-exist
tangenta Feb 1, 2023
1874a4e
add some comments
tangenta Feb 1, 2023
dda1225
address comment
tangenta Feb 2, 2023
0e36c80
fix merging case
tangenta Feb 2, 2023
ee5cbd7
fix linter
tangenta Feb 2, 2023
098b834
Merge remote-tracking branch 'upstream/master' into add-index-check-e…
tangenta Feb 7, 2023
b9c995b
record all the operations to the temp index value
tangenta Feb 7, 2023
c2040b1
fix linter
tangenta Feb 7, 2023
495466b
Merge branch 'master' into add-index-check-exist
tangenta Feb 7, 2023
c3984c4
update bazel
tangenta Feb 7, 2023
1c6e9d6
fix panic on clustered index table
tangenta Feb 8, 2023
bd7d17a
address comment
tangenta Feb 8, 2023
5323e89
Merge branch 'master' into add-index-check-exist
tangenta Feb 8, 2023
7f4a3c4
print value to log when inconsistency detected
tangenta Feb 8, 2023
9dfe8af
fix insert ignore may introduce invalid temp index value
tangenta Feb 8, 2023
2865dd8
Merge branch 'master' into add-index-check-exist
tangenta Feb 8, 2023
86d5a9c
move failpoint to a common place for distributed reorg
tangenta Feb 8, 2023
c365c3e
remove debug info and refine
tangenta Feb 8, 2023
bfe6db6
Merge remote-tracking branch 'upstream/master' into add-index-check-e…
tangenta Feb 8, 2023
f51ce7f
fix build
tangenta Feb 8, 2023
0e75f01
should not return during iterating temp index value elem
tangenta Feb 8, 2023
8d1ed64
add more test for TempIndexValue encoding
tangenta Feb 8, 2023
5d739db
Merge remote-tracking branch 'upstream/master' into add-index-check-e…
tangenta Feb 8, 2023
29aaa12
Merge branch 'master' into add-index-check-exist
tangenta Feb 9, 2023
5945d0e
Merge branch 'master' into add-index-check-exist
purelind Feb 9, 2023
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
15 changes: 15 additions & 0 deletions ddl/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -964,6 +964,15 @@ func convertToKeyExistsErr(originErr error, idxInfo *model.IndexInfo, tblInfo *m
func runReorgJobAndHandleErr(w *worker, d *ddlCtx, t *meta.Meta, job *model.Job,
tbl table.Table, indexInfo *model.IndexInfo, mergingTmpIdx bool) (done bool, ver int64, err error) {
elements := []*meta.Element{{ID: indexInfo.ID, TypeKey: meta.IndexElementKey}}

failpoint.Inject("mockDMLExecutionStateMerging", func(val failpoint.Value) {
//nolint:forcetypeassert
if val.(bool) && indexInfo.BackfillState == model.BackfillStateMerging &&
MockDMLExecutionStateMerging != nil {
MockDMLExecutionStateMerging()
}
})

sctx, err1 := w.sessPool.get()
if err1 != nil {
err = err1
Expand Down Expand Up @@ -1791,6 +1800,12 @@ func (w *addIndexWorker) BackfillDataInTxn(handleRange reorgBackfillTask) (taskC
// MockDMLExecution is only used for test.
var MockDMLExecution func()

// MockDMLExecutionMerging is only used for test.
var MockDMLExecutionMerging func()

// MockDMLExecutionStateMerging is only used for test.
var MockDMLExecutionStateMerging func()

func (w *worker) addPhysicalTableIndex(t table.PhysicalTable, reorgInfo *reorgInfo) error {
if reorgInfo.mergingTmpIdx {
logutil.BgLogger().Info("[ddl] start to merge temp index", zap.String("job", reorgInfo.Job.String()), zap.String("reorgInfo", reorgInfo.String()))
Expand Down
72 changes: 44 additions & 28 deletions ddl/index_merge_tmp.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"time"

"github.com/pingcap/errors"
"github.com/pingcap/failpoint"
"github.com/pingcap/tidb/kv"
"github.com/pingcap/tidb/parser/model"
"github.com/pingcap/tidb/sessionctx/variable"
Expand Down Expand Up @@ -198,6 +199,12 @@ func (w *mergeIndexWorker) BackfillDataInTxn(taskRange reorgBackfillTask) (taskC
return nil
})

failpoint.Inject("mockDMLExecutionMerging", func(val failpoint.Value) {
//nolint:forcetypeassert
if val.(bool) && MockDMLExecutionMerging != nil {
MockDMLExecutionMerging()
}
})
logSlowOperations(time.Since(oprStartTime), "AddIndexMergeDataInTxn", 3000)
return
}
Expand Down Expand Up @@ -252,40 +259,49 @@ func (w *mergeIndexWorker) fetchTempIndexVals(txn kv.Transaction, taskRange reor
return false, nil
}

originVal, handle, isDelete, unique, keyVer := tablecodec.DecodeTempIndexValue(rawValue, isCommonHandle)
if keyVer == tables.TempIndexKeyTypeMerge || keyVer == tables.TempIndexKeyTypeDelete {
// For 'm' version kvs, they are double-written.
// For 'd' version kvs, they are written in the delete-only state and can be dropped safely.
return true, nil
tempIdxVal, err := tablecodec.DecodeTempIndexValue(rawValue, isCommonHandle)
if err != nil {
return false, err
}
tempIdxVal = tempIdxVal.FilterOverwritten()
Benjamin2037 marked this conversation as resolved.
Show resolved Hide resolved

// Extract the operations on the original index and replay them later.
Benjamin2037 marked this conversation as resolved.
Show resolved Hide resolved
for _, elem := range tempIdxVal {
if elem.KeyVer == tables.TempIndexKeyTypeMerge || elem.KeyVer == tables.TempIndexKeyTypeDelete {
// For 'm' version kvs, they are double-written.
// For 'd' version kvs, they are written in the delete-only state and can be dropped safely.
continue
}

if handle == nil {
// If the handle is not found in the value of the temp index, it means
// 1) This is not a deletion marker, the handle is in the key or the origin value.
// 2) This is a deletion marker, but the handle is in the key of temp index.
handle, err = tablecodec.DecodeIndexHandle(indexKey, originVal, len(w.index.Meta().Columns))
if err != nil {
return false, err
if elem.Handle == nil {
// If the handle is not found in the value of the temp index, it means
// 1) This is not a deletion marker, the handle is in the key or the origin value.
// 2) This is a deletion marker, but the handle is in the key of temp index.
elem.Handle, err = tablecodec.DecodeIndexHandle(indexKey, elem.Value, len(w.index.Meta().Columns))
if err != nil {
return false, err
}
}
}

originIdxKey := make([]byte, len(indexKey))
copy(originIdxKey, indexKey)
tablecodec.TempIndexKey2IndexKey(w.index.Meta().ID, originIdxKey)
originIdxKey := make([]byte, len(indexKey))
copy(originIdxKey, indexKey)
tablecodec.TempIndexKey2IndexKey(w.index.Meta().ID, originIdxKey)

idxRecord := &temporaryIndexRecord{
handle: handle,
delete: isDelete,
unique: unique,
skip: false,
}
if !isDelete {
idxRecord.vals = originVal
idxRecord.distinct = tablecodec.IndexKVIsUnique(originVal)
idxRecord := &temporaryIndexRecord{
handle: elem.Handle,
delete: elem.Delete,
unique: elem.Distinct,
skip: false,
}
if !elem.Delete {
idxRecord.vals = elem.Value
idxRecord.distinct = tablecodec.IndexKVIsUnique(elem.Value)
}
w.tmpIdxRecords = append(w.tmpIdxRecords, idxRecord)
w.originIdxKeys = append(w.originIdxKeys, originIdxKey)
w.tmpIdxKeys = append(w.tmpIdxKeys, indexKey)
}
w.tmpIdxRecords = append(w.tmpIdxRecords, idxRecord)
w.originIdxKeys = append(w.originIdxKeys, originIdxKey)
w.tmpIdxKeys = append(w.tmpIdxKeys, indexKey)

lastKey = indexKey
return true, nil
})
Expand Down
1 change: 1 addition & 0 deletions ddl/indexmergetest/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ go_test(
"//ddl/internal/callback",
"//ddl/testutil",
"//domain",
"//errno",
"//kv",
"//meta/autoid",
"//parser/model",
Expand Down
Loading