Skip to content

Commit

Permalink
remove unnecessary tests and debug code
Browse files Browse the repository at this point in the history
Signed-off-by: ekexium <ekexium@gmail.com>
  • Loading branch information
ekexium committed Dec 14, 2021
1 parent a9f1cc2 commit 91c8c51
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 140 deletions.
2 changes: 1 addition & 1 deletion kv/kv.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ type MemBuffer interface {
// Size returns sum of keys and values length.
Size() int

// RemoveFromBuffer remove the entry from the buffer. It's used for testing.
// RemoveFromBuffer removes the entry from the buffer. It's used for testing.
RemoveFromBuffer(Key)
}

Expand Down
86 changes: 0 additions & 86 deletions session/defend_test.go

This file was deleted.

42 changes: 32 additions & 10 deletions session/session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"strings"
"sync"
"sync/atomic"
"testing"
"time"

"github.com/docker/go-units"
Expand Down Expand Up @@ -61,6 +62,7 @@ import (
"github.com/pingcap/tidb/store/mockstore/mockcopr"
"github.com/pingcap/tidb/table/tables"
"github.com/pingcap/tidb/tablecodec"
testkit2 "github.com/pingcap/tidb/testkit"
"github.com/pingcap/tidb/types"
"github.com/pingcap/tidb/util/collate"
"github.com/pingcap/tidb/util/sqlexec"
Expand Down Expand Up @@ -5884,14 +5886,34 @@ func (s *testSessionSuite) TestSameNameObjectWithLocalTemporaryTable(c *C) {
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin"))
}

func (s *testSessionSuite2) TestDefend24029(c *C) {
tk := testkit.NewTestKitWithInit(c, s.store)
collate.SetNewCollationEnabledForTest(true)
defer collate.SetNewCollationEnabledForTest(false)
tk.MustExec("create table t(a int key, b varchar(20) collate utf8mb4_unicode_ci, c varchar(20) collate utf8mb4_general_ci, unique key idx_b_c(b, c));")
c.Assert(failpoint.Enable("github.com/pingcap/tidb/tablecodec/injectNeedRestoredData", "return(false)"), IsNil)
_, err := tk.Exec("insert into t values (4, 'd', 'F');")
c.Assert(err, NotNil)
c.Assert(strings.Contains(err.Error(), "inconsistent index values"), IsTrue)
c.Assert(failpoint.Disable("github.com/pingcap/tidb/tablecodec/injectNeedRestoredData"), IsNil)
func TestCorrupt(t *testing.T) {
store, close := testkit2.CreateMockStore(t)
defer close()
tk := testkit2.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec(`drop table if exists t1`)
tk.MustExec("set global tidb_enable_mutation_checker = true;")
tk.MustExec("set tidb_enable_mutation_checker = true;")
tk.MustQuery("select @@tidb_enable_mutation_checker").Check(testkit2.Rows("1"))
tk.MustExec(`CREATE TABLE t1653 (c1 VARCHAR(10), c1377 VARCHAR(10), KEY i1654 (c1, c1377), KEY i1655 (c1377, c1))`)
failpoint.Enable("github.com/pingcap/tidb/table/tables/corruptMutations", "return(\"missingIndex\")")
tk.MustExec("begin")
tk.MustExec(`insert into t1653 set c1 = 'a', c1377 = 'b'`)
tk.MustExec(`insert into t1653 values('aa', 'bb')`)
tk.MustExec("commit")
failpoint.Disable("github.com/pingcap/tidb/table/tables/corruptMutations")
}

func TestCheckInTxn(t *testing.T) {
store, close := testkit2.CreateMockStore(t)
defer close()
tk := testkit2.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec(`drop table if exists t`)
tk.MustExec("set global tidb_enable_mutation_checker = true;")
tk.MustExec("create table t(id int, v varchar(20), unique key i1(id))")
tk.MustExec("insert into t values (1, 'a')")
tk.MustExec("begin")
tk.MustExec("insert into t values (1, 'd'), (3, 'f') on duplicate key update v='x'")
tk.MustExec("commit")
}
84 changes: 45 additions & 39 deletions table/tables/mutation_checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,10 @@
package tables

import (
"encoding/hex"
"crypto/rand"
"fmt"
"math/rand"
"math/big"
"strings"
"time"

"github.com/pingcap/errors"
"github.com/pingcap/failpoint"
Expand Down Expand Up @@ -70,10 +69,15 @@ type columnMaps struct {
func CheckDataConsistency(
txn kv.Transaction, sessVars *variable.SessionVars, t *TableCommon,
rowToInsert, rowToRemove []types.Datum, memBuffer kv.MemBuffer, sh kv.StagingHandle,
) error {
) (err error) {
failpoint.Inject("corruptMutations", func(commands failpoint.Value) {
corruptMutations(t, txn, sh, commands.(string))
if e := corruptMutations(t, txn, sh, commands.(string)); e != nil {
err = e
}
})
if err != nil {
return err
}

if t.Meta().GetPartitionInfo() != nil {
return nil
Expand All @@ -82,21 +86,11 @@ func CheckDataConsistency(
// some implementations of MemBuffer doesn't support staging, e.g. that in br/pkg/lightning/backend/kv
return nil
}
indexMutations, rowInsertion, _, err := collectTableMutationsFromBufferStage(t, memBuffer, sh)
indexMutations, rowInsertion, err := collectTableMutationsFromBufferStage(t, memBuffer, sh)
if err != nil {
return errors.Trace(err)
}

failpoint.Inject("printMutation", func() {
fmt.Println("------------------------------------------------")
fmt.Printf("row to insert: %+v\nrow to remove: %+v\n", rowToInsert, rowToRemove)
fmt.Printf("row insertion: key: %v, value: %v\n", hex.EncodeToString(rowInsertion.key),
hex.EncodeToString(rowInsertion.value))
for _, im := range indexMutations {
fmt.Printf("index mutation: key: %v, value: %v\n", hex.EncodeToString(im.key), hex.EncodeToString(im.value))
}
})

columnMaps := getColumnMaps(txn, t)

if rowToInsert != nil {
Expand Down Expand Up @@ -274,14 +268,14 @@ func checkRowInsertionConsistency(
}

// collectTableMutationsFromBufferStage collects mutations of the current table from the mem buffer stage
// It returns: (1) all index mutations (2) the only row insertion (3) the only row deletion
// If there are no row insertions/deletions, return nil
// It returns: (1) all index mutations (2) the only row insertion
// If there are no row insertions, the 2nd returned value is nil
// If there are multiple row insertions, an error is returned
func collectTableMutationsFromBufferStage(t *TableCommon, memBuffer kv.MemBuffer, sh kv.StagingHandle) (
[]mutation, mutation, mutation, error,
[]mutation, mutation, error,
) {
indexMutations := make([]mutation, 0)
var rowInsertion, rowDeletion mutation
var rowInsertion mutation
var err error
inspector := func(key kv.Key, flags kv.KeyFlags, data []byte) {
// only check the current table
Expand All @@ -296,16 +290,7 @@ func collectTableMutationsFromBufferStage(t *TableCommon, memBuffer kv.MemBuffer
"multiple row mutations added/mutated, one = %+v, another = %+v", rowInsertion, m,
)
}
} else {
if rowDeletion.key == nil {
rowDeletion = m
} else {
err = errors.Errorf(
"multiple row mutations deleted, one = %+v, another = %+v", rowDeletion, m,
)
}
}

} else {
_, m.indexID, _, err = tablecodec.DecodeIndexKey(m.key)
if err != nil {
Expand All @@ -316,7 +301,7 @@ func collectTableMutationsFromBufferStage(t *TableCommon, memBuffer kv.MemBuffer
}
}
memBuffer.InspectStage(sh, inspector)
return indexMutations, rowInsertion, rowDeletion, err
return indexMutations, rowInsertion, err
}

// compareIndexData compares the decoded index data with the input data.
Expand Down Expand Up @@ -410,9 +395,8 @@ func getOrBuildColumnMaps(
func corruptMutations(t *TableCommon, txn kv.Transaction, sh kv.StagingHandle, cmds string) error {
commands := strings.Split(cmds, ",")
memBuffer := txn.GetMemBuffer()
rand := rand.New(rand.NewSource(time.Now().UnixNano()))

indexMutations, _, _, err := collectTableMutationsFromBufferStage(t, memBuffer, sh)
indexMutations, _, err := collectTableMutationsFromBufferStage(t, memBuffer, sh)
if err != nil {
return errors.Trace(err)
}
Expand All @@ -426,11 +410,17 @@ func corruptMutations(t *TableCommon, txn kv.Transaction, sh kv.StagingHandle, c
if len(indexMutations) == 0 {
continue
}
indexMutation := indexMutations[rand.Intn(len(indexMutations))]
r, err := rand.Int(rand.Reader, big.NewInt(int64(len(indexMutations))))
if err != nil {
return errors.Trace(err)
}
indexMutation := indexMutations[r.Int64()]
key := make([]byte, len(indexMutation.key))
copy(key, indexMutation.key)
key[len(key)-1] += 1
memBuffer.Set(key, indexMutation.value)
if err := memBuffer.Set(key, indexMutation.value); err != nil {
return errors.Trace(err)
}
}
//case "extraIndexByHandle":
// {
Expand All @@ -443,7 +433,11 @@ func corruptMutations(t *TableCommon, txn kv.Transaction, sh kv.StagingHandle, c
if len(indexMutations) == 0 {
continue
}
indexMutation := indexMutations[rand.Intn(len(indexMutations))]
r, err := rand.Int(rand.Reader, big.NewInt(int64(len(indexMutations))))
if err != nil {
return errors.Trace(err)
}
indexMutation := indexMutations[r.Int64()]
memBuffer.RemoveFromBuffer(indexMutation.key)
}
case "corruptIndexKey":
Expand All @@ -453,11 +447,17 @@ func corruptMutations(t *TableCommon, txn kv.Transaction, sh kv.StagingHandle, c
if len(indexMutations) == 0 {
continue
}
indexMutation := indexMutations[rand.Intn(len(indexMutations))]
r, err := rand.Int(rand.Reader, big.NewInt(int64(len(indexMutations))))
if err != nil {
return errors.Trace(err)
}
indexMutation := indexMutations[r.Int64()]
key := indexMutation.key
memBuffer.RemoveFromBuffer(key)
key[len(key)-1] += 1
memBuffer.Set(key, indexMutation.value)
if err := memBuffer.Set(key, indexMutation.value); err != nil {
return errors.Trace(err)
}
}
//case "corruptIndexKeyByHandle":
// {
Expand All @@ -469,12 +469,18 @@ func corruptMutations(t *TableCommon, txn kv.Transaction, sh kv.StagingHandle, c
if len(indexMutations) == 0 {
continue
}
indexMutation := indexMutations[rand.Intn(len(indexMutations))]
r, err := rand.Int(rand.Reader, big.NewInt(int64(len(indexMutations))))
if err != nil {
return errors.Trace(err)
}
indexMutation := indexMutations[r.Int64()]
value := indexMutation.value
if len(value) > 0 {
value[len(value)-1] += 1
}
memBuffer.Set(indexMutation.key, value)
if err := memBuffer.Set(indexMutation.key, value); err != nil {
return errors.Trace(err)
}
}
//case "corruptIndexValueCommonHandle":
// {
Expand Down
4 changes: 0 additions & 4 deletions tablecodec/tablecodec.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package tablecodec
import (
"bytes"
"encoding/binary"
"github.com/pingcap/failpoint"
"math"
"strings"
"time"
Expand Down Expand Up @@ -1130,9 +1129,6 @@ func GenIndexKey(sc *stmtctx.StatementContext, tblInfo *model.TableInfo, idxInfo
// | Besides, if the collation of b is _bin, then restored data is an integer indicate the spaces are truncated. Then we use sortKey
// | and the restored data together to restore original data.
func GenIndexValuePortal(sc *stmtctx.StatementContext, tblInfo *model.TableInfo, idxInfo *model.IndexInfo, needRestoredData bool, distinct bool, untouched bool, indexedValues []types.Datum, h kv.Handle, partitionID int64, restoredData []types.Datum) ([]byte, error) {
failpoint.Inject("injectNeedRestoredData", func(val failpoint.Value) {
needRestoredData = val.(bool)
})
if tblInfo.IsCommonHandle && tblInfo.CommonHandleVersion == 1 {
return GenIndexValueForClusteredIndexVersion1(sc, tblInfo, idxInfo, needRestoredData, distinct, untouched, indexedValues, h, partitionID, restoredData)
}
Expand Down

0 comments on commit 91c8c51

Please sign in to comment.