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

testkit,store/mockstore: make store image to accelerate unit test #42521

Merged
merged 27 commits into from
Jan 5, 2024
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
105426b
testkit,store/mockstore: make store image to accelerate unit test
tiancaiamao Mar 23, 2023
39220a9
tiny clean up
tiancaiamao Mar 23, 2023
70ac665
fix build
tiancaiamao Mar 23, 2023
8b5973f
fix build
tiancaiamao Mar 23, 2023
d0c02d2
fix CI
tiancaiamao Mar 24, 2023
550eeb3
Merge branch 'master' into issue-42434
tiancaiamao Mar 29, 2023
d8e303e
fix some test cases
tiancaiamao Mar 31, 2023
ce31edd
make bazel_prepare
tiancaiamao Mar 31, 2023
e9ef16d
Merge branch 'master' into issue-42434
tiancaiamao Apr 3, 2023
b889d02
fix test
tiancaiamao Apr 3, 2023
6e83733
Merge branch 'master' into issue-42434
tiancaiamao Apr 13, 2023
8582e48
Merge branch 'master' into issue-42434
tiancaiamao Apr 19, 2023
17f37ef
Merge branch 'master' into issue-42434
tiancaiamao Apr 19, 2023
4b1007f
Merge branch 'master' into issue-42434
tiancaiamao Apr 20, 2023
747eebe
Merge branch 'master' into issue-42434
tiancaiamao Jun 12, 2023
12e8756
resolve conflict
tiancaiamao Jun 12, 2023
a2d5f26
Merge branch 'master' into issue-42434
tiancaiamao Sep 4, 2023
7870d6b
make bazel_prepare
tiancaiamao Sep 4, 2023
54679f0
make fmt
tiancaiamao Sep 4, 2023
d8ad393
Merge branch 'master' into issue-42434
tiancaiamao Dec 31, 2023
fe94751
fix build
tiancaiamao Jan 1, 2024
6e6239a
resolve confict
tiancaiamao Jan 2, 2024
b2a94fd
Merge branch 'master' into issue-42434
tiancaiamao Jan 3, 2024
943064a
fix ci
tiancaiamao Jan 3, 2024
dd443a7
fix CI
tiancaiamao Jan 3, 2024
cce19c4
fix build
tiancaiamao Jan 3, 2024
52ecfbb
Merge branch 'master' into issue-42434
tiancaiamao Jan 5, 2024
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: 1 addition & 1 deletion ddl/table_split_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import (
)

func TestTableSplit(t *testing.T) {
store, err := mockstore.NewMockStore()
store, err := mockstore.NewMockStore(mockstore.WithStoreType(mockstore.EmbedUnistore))
require.NoError(t, err)
defer func() {
err := store.Close()
Expand Down
7 changes: 4 additions & 3 deletions executor/recover_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/pingcap/tidb/infoschema"
"github.com/pingcap/tidb/parser/auth"
"github.com/pingcap/tidb/sessionctx/variable"
"github.com/pingcap/tidb/store/mockstore"
"github.com/pingcap/tidb/testkit"
"github.com/pingcap/tidb/util/dbterror"
"github.com/pingcap/tidb/util/gcutil"
Expand Down Expand Up @@ -130,7 +131,7 @@ func TestFlashbackTable(t *testing.T) {
require.NoError(t, failpoint.Disable("github.com/pingcap/tidb/meta/autoid/mockAutoIDChange"))
}()

store := testkit.CreateMockStore(t)
store := testkit.CreateMockStore(t, mockstore.WithStoreType(mockstore.EmbedUnistore))

tk := testkit.NewTestKit(t, store)
tk.MustExec("create database if not exists test_flashback")
Expand Down Expand Up @@ -242,7 +243,7 @@ func TestFlashbackTable(t *testing.T) {
}

func TestRecoverTempTable(t *testing.T) {
store := testkit.CreateMockStore(t)
store := testkit.CreateMockStore(t, mockstore.WithStoreType(mockstore.EmbedUnistore))

tk := testkit.NewTestKit(t, store)
tk.MustExec("create database if not exists test_recover")
Expand Down Expand Up @@ -487,7 +488,7 @@ func TestFlashbackSchema(t *testing.T) {
require.NoError(t, failpoint.Disable("github.com/pingcap/tidb/meta/autoid/mockAutoIDChange"))
}()

store := testkit.CreateMockStore(t)
store := testkit.CreateMockStore(t, mockstore.WithStoreType(mockstore.EmbedUnistore))

tk := testkit.NewTestKit(t, store)
tk.MustExec("create database if not exists test_flashback")
Expand Down
1 change: 1 addition & 0 deletions expression/integration_test/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ go_test(
"//planner/core",
"//session",
"//sessionctx/variable",
"//store/mockstore",
"//table",
"//tablecodec",
"//testkit",
Expand Down
3 changes: 2 additions & 1 deletion expression/integration_test/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import (
plannercore "github.com/pingcap/tidb/planner/core"
"github.com/pingcap/tidb/session"
"github.com/pingcap/tidb/sessionctx/variable"
"github.com/pingcap/tidb/store/mockstore"
"github.com/pingcap/tidb/table"
"github.com/pingcap/tidb/tablecodec"
"github.com/pingcap/tidb/testkit"
Expand Down Expand Up @@ -244,7 +245,7 @@ func TestBuiltinFuncJsonPretty(t *testing.T) {

func TestGetLock(t *testing.T) {
ctx := context.Background()
store := testkit.CreateMockStore(t)
store := testkit.CreateMockStore(t, mockstore.WithStoreType(mockstore.EmbedUnistore))
tk := testkit.NewTestKit(t, store)

// No timeout specified
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ require (
github.com/nishanths/predeclared v0.2.2
github.com/opentracing/basictracer-go v1.0.0
github.com/opentracing/opentracing-go v1.2.0
github.com/otiai10/copy v1.2.0
github.com/phayes/freeport v0.0.0-20180830031419-95f893ade6f2
github.com/pingcap/badger v1.5.1-0.20230103063557-828f39b09b6d
github.com/pingcap/errors v0.11.5-0.20221009092201-b66cddb77c32
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -744,6 +744,7 @@ github.com/otiai10/copy v1.2.0/go.mod h1:rrF5dJ5F0t/EWSYODDu4j9/vEeYHMkc8jt0zJCh
github.com/otiai10/curr v0.0.0-20150429015615-9b4961190c95/go.mod h1:9qAhocn7zKJG+0mI8eUu6xqkFDYS2kb2saOteoSB3cE=
github.com/otiai10/curr v1.0.0/go.mod h1:LskTG5wDwr8Rs+nNQ+1LlxRjAtTZZjtJW4rMXl6j4vs=
github.com/otiai10/mint v1.3.0/go.mod h1:F5AjcsTsWUqX+Na9fpHb52P8pcRX2CI6A3ctIT91xUo=
github.com/otiai10/mint v1.3.1 h1:BCmzIS3n71sGfHB5NMNDB3lHYPz8fWSkCAErHed//qc=
github.com/otiai10/mint v1.3.1/go.mod h1:/yxELlJQ0ufhjUwhshSj+wFjZ78CnZ48/1wtmBH1OTc=
github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc=
github.com/pborman/getopt v0.0.0-20180729010549-6fdd0a2c7117/go.mod h1:85jBQOZwpVEaDAr341tbn15RS4fCAsIst0qp7i8ex1o=
Expand Down
12 changes: 6 additions & 6 deletions meta/autoid/autoid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func TestSignedAutoid(t *testing.T) {
require.NoError(t, failpoint.Disable("github.com/pingcap/tidb/meta/autoid/mockAutoIDChange"))
}()

store, err := mockstore.NewMockStore()
store, err := mockstore.NewMockStore(mockstore.WithStoreType(mockstore.EmbedUnistore))
require.NoError(t, err)
defer func() {
err := store.Close()
Expand Down Expand Up @@ -246,7 +246,7 @@ func TestUnsignedAutoid(t *testing.T) {
require.NoError(t, failpoint.Disable("github.com/pingcap/tidb/meta/autoid/mockAutoIDChange"))
}()

store, err := mockstore.NewMockStore()
store, err := mockstore.NewMockStore(mockstore.WithStoreType(mockstore.EmbedUnistore))
require.NoError(t, err)
defer func() {
err := store.Close()
Expand Down Expand Up @@ -404,7 +404,7 @@ func TestUnsignedAutoid(t *testing.T) {
// TestConcurrentAlloc is used for the test that
// multiple allocators allocate ID with the same table ID concurrently.
func TestConcurrentAlloc(t *testing.T) {
store, err := mockstore.NewMockStore()
store, err := mockstore.NewMockStore(mockstore.WithStoreType(mockstore.EmbedUnistore))
require.NoError(t, err)
defer func() {
err := store.Close()
Expand Down Expand Up @@ -495,7 +495,7 @@ func TestConcurrentAlloc(t *testing.T) {
// TestRollbackAlloc tests that when the allocation transaction commit failed,
// the local variable base and end doesn't change.
func TestRollbackAlloc(t *testing.T) {
store, err := mockstore.NewMockStore()
store, err := mockstore.NewMockStore(mockstore.WithStoreType(mockstore.EmbedUnistore))
require.NoError(t, err)
defer func() {
err := store.Close()
Expand Down Expand Up @@ -546,7 +546,7 @@ func TestAllocComputationIssue(t *testing.T) {
require.NoError(t, failpoint.Disable("github.com/pingcap/tidb/meta/autoid/mockAutoIDCustomize"))
}()

store, err := mockstore.NewMockStore()
store, err := mockstore.NewMockStore(mockstore.WithStoreType(mockstore.EmbedUnistore))
require.NoError(t, err)
defer func() {
err := store.Close()
Expand Down Expand Up @@ -597,7 +597,7 @@ func TestAllocComputationIssue(t *testing.T) {
}

func TestIssue40584(t *testing.T) {
store, err := mockstore.NewMockStore()
store, err := mockstore.NewMockStore(mockstore.WithStoreType(mockstore.EmbedUnistore))
require.NoError(t, err)
defer func() {
err := store.Close()
Expand Down
4 changes: 2 additions & 2 deletions meta/autoid/seq_autoid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import (
)

func TestSequenceAutoid(t *testing.T) {
store, err := mockstore.NewMockStore()
store, err := mockstore.NewMockStore(mockstore.WithStoreType(mockstore.EmbedUnistore))
require.NoError(t, err)
defer func() {
err := store.Close()
Expand Down Expand Up @@ -156,7 +156,7 @@ func TestSequenceAutoid(t *testing.T) {
}

func TestConcurrentAllocSequence(t *testing.T) {
store, err := mockstore.NewMockStore()
store, err := mockstore.NewMockStore(mockstore.WithStoreType(mockstore.EmbedUnistore))
require.NoError(t, err)
defer func() {
err := store.Close()
Expand Down
4 changes: 2 additions & 2 deletions meta/meta_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ func TestBackupAndRestoreAutoIDs(t *testing.T) {
}

func TestMeta(t *testing.T) {
store, err := mockstore.NewMockStore()
store, err := mockstore.NewMockStore(mockstore.WithStoreType(mockstore.EmbedUnistore))
require.NoError(t, err)

defer func() {
Expand Down Expand Up @@ -455,7 +455,7 @@ func TestMeta(t *testing.T) {
}

func TestSnapshot(t *testing.T) {
store, err := mockstore.NewMockStore()
store, err := mockstore.NewMockStore(mockstore.WithStoreType(mockstore.EmbedUnistore))
require.NoError(t, err)
defer func() {
err := store.Close()
Expand Down
14 changes: 7 additions & 7 deletions session/bootstrap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func globalVarsCount() int64 {
// We should make sure that the following session could finish the bootstrap process.
func TestBootstrapWithError(t *testing.T) {
ctx := context.Background()
store, err := mockstore.NewMockStore()
store, err := mockstore.NewMockStore(mockstore.WithStoreType(mockstore.EmbedUnistore))
require.NoError(t, err)
defer func() {
require.NoError(t, store.Close())
Expand Down Expand Up @@ -777,7 +777,7 @@ func TestAnalyzeVersionUpgradeFrom300To500(t *testing.T) {
}

func TestIndexMergeInNewCluster(t *testing.T) {
store, err := mockstore.NewMockStore()
store, err := mockstore.NewMockStore(mockstore.WithStoreType(mockstore.EmbedUnistore))
require.NoError(t, err)
// Indicates we are in a new cluster.
require.Equal(t, int64(notBootstrapped), getStoreBootstrapVersion(store))
Expand Down Expand Up @@ -955,7 +955,7 @@ func TestInitializeSQLFile(t *testing.T) {

func testEmptyInitSQLFile(t *testing.T) {
// An non-existent sql file would stop the bootstrap of the tidb cluster
store, err := mockstore.NewMockStore()
store, err := mockstore.NewMockStore(mockstore.WithStoreType(mockstore.EmbedUnistore))
require.NoError(t, err)
config.GetGlobalConfig().InitializeSQLFile = "non-existent.sql"
defer func() {
Expand Down Expand Up @@ -1156,7 +1156,7 @@ insert into test.t values ("abc"); -- invalid statement
`)
require.NoError(t, err)

store, err := mockstore.NewMockStore()
store, err := mockstore.NewMockStore(mockstore.WithStoreType(mockstore.EmbedUnistore))
require.NoError(t, err)
config.GetGlobalConfig().InitializeSQLFile = sqlFiles[0].Name()
defer func() {
Expand All @@ -1172,7 +1172,7 @@ insert into test.t values ("abc"); -- invalid statement
runBootstrapSQLFile = false

// Bootstrap with the second sql file, which would not been executed.
store, err = mockstore.NewMockStore()
store, err = mockstore.NewMockStore(mockstore.WithStoreType(mockstore.EmbedUnistore))
require.NoError(t, err)
defer func() {
require.NoError(t, store.Close())
Expand Down Expand Up @@ -1350,7 +1350,7 @@ func TestTiDBOptAdvancedJoinHintWhenUpgrading(t *testing.T) {
}

func TestTiDBOptAdvancedJoinHintInNewCluster(t *testing.T) {
store, err := mockstore.NewMockStore()
store, err := mockstore.NewMockStore(mockstore.WithStoreType(mockstore.EmbedUnistore))
require.NoError(t, err)
// Indicates we are in a new cluster.
require.Equal(t, int64(notBootstrapped), getStoreBootstrapVersion(store))
Expand All @@ -1376,7 +1376,7 @@ func TestTiDBOptAdvancedJoinHintInNewCluster(t *testing.T) {
}

func TestTiDBCostModelInNewCluster(t *testing.T) {
store, err := mockstore.NewMockStore()
store, err := mockstore.NewMockStore(mockstore.WithStoreType(mockstore.EmbedUnistore))
require.NoError(t, err)
// Indicates we are in a new cluster.
require.Equal(t, int64(notBootstrapped), getStoreBootstrapVersion(store))
Expand Down
3 changes: 2 additions & 1 deletion session/session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/pingcap/tidb/ddl"
"github.com/pingcap/tidb/metrics"
"github.com/pingcap/tidb/session"
"github.com/pingcap/tidb/store/mockstore"
"github.com/pingcap/tidb/tablecodec"
"github.com/pingcap/tidb/testkit"
"github.com/pingcap/tidb/testkit/external"
Expand Down Expand Up @@ -69,7 +70,7 @@ func TestMetaTableRegion(t *testing.T) {
enableSplitTableRegionVal := atomic.LoadUint32(&ddl.EnableSplitTableRegion)
atomic.StoreUint32(&ddl.EnableSplitTableRegion, 1)
defer atomic.StoreUint32(&ddl.EnableSplitTableRegion, enableSplitTableRegionVal)
store := testkit.CreateMockStore(t)
store := testkit.CreateMockStore(t, mockstore.WithStoreType(mockstore.EmbedUnistore))

tk := testkit.NewTestKit(t, store)

Expand Down
1 change: 1 addition & 0 deletions sessiontxn/staleread/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ go_test(
"//parser/auth",
"//sessionctx",
"//sessiontxn",
"//store/mockstore",
"//table/temptable",
"//testkit",
"//testkit/testsetup",
Expand Down
5 changes: 3 additions & 2 deletions sessiontxn/staleread/processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/pingcap/tidb/parser/ast"
"github.com/pingcap/tidb/sessionctx"
"github.com/pingcap/tidb/sessiontxn/staleread"
"github.com/pingcap/tidb/store/mockstore"
"github.com/pingcap/tidb/table/temptable"
"github.com/pingcap/tidb/testkit"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -103,7 +104,7 @@ func getCurrentExternalTimestamp(t *testing.T, tk *testkit.TestKit) uint64 {
}

func TestStaleReadProcessorWithSelectTable(t *testing.T) {
store := testkit.CreateMockStore(t)
store := testkit.CreateMockStore(t, mockstore.WithStoreType(mockstore.EmbedUnistore))
tk := testkit.NewTestKit(t, store)
tn := astTableWithAsOf(t, "")
p1 := genStaleReadPoint(t, tk)
Expand Down Expand Up @@ -229,7 +230,7 @@ func TestStaleReadProcessorWithSelectTable(t *testing.T) {
}

func TestStaleReadProcessorWithExecutePreparedStmt(t *testing.T) {
store := testkit.CreateMockStore(t)
store := testkit.CreateMockStore(t, mockstore.WithStoreType(mockstore.EmbedUnistore))
tk := testkit.NewTestKit(t, store)
p1 := genStaleReadPoint(t, tk)
//p2 := genStaleReadPoint(t, tk)
Expand Down
1 change: 1 addition & 0 deletions store/mockstore/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ go_library(
"//store/mockstore/mockstorage",
"//store/mockstore/unistore",
"//testkit/testenv",
"@com_github_otiai10_copy//:copy",
"@com_github_pingcap_errors//:errors",
"@com_github_tikv_client_go_v2//testutils",
"@com_github_tikv_client_go_v2//tikv",
Expand Down
35 changes: 35 additions & 0 deletions store/mockstore/mockstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@ package mockstore

import (
"net/url"
"os"
"path/filepath"
"strings"

cp "github.com/otiai10/copy"
"github.com/pingcap/errors"
"github.com/pingcap/tidb/config"
"github.com/pingcap/tidb/kv"
Expand Down Expand Up @@ -184,6 +187,13 @@ func NewMockStore(options ...MockTiKVStoreOption) (kv.Storage, error) {
case MockTiKV:
store, err = newMockTikvStore(&opt)
case EmbedUnistore:
if opt.path == "" && len(options) == 0 && ImageAvailable() {
// Create the store from the image.
if path, err := copyImage(); err == nil {
opt.path = path
}
}

store, err = newUnistore(&opt)
default:
panic("unsupported mockstore")
Expand All @@ -198,6 +208,31 @@ func NewMockStore(options ...MockTiKVStoreOption) (kv.Storage, error) {
return store, nil
}

// ImageFilePath is used by testing, it's the file path for the bootstraped store image.
const ImageFilePath = "/tmp/tidb-unistore-bootstraped-image/"

// ImageAvailable checks whether the store image file is available.
func ImageAvailable() bool {
_, err := os.ReadDir(ImageFilePath)
if err != nil {
return false
}
_, err = os.ReadDir(filepath.Join(ImageFilePath, "kv"))
return err == nil
}

func copyImage() (string, error) {
path, err := os.MkdirTemp("", "tidb-unistore-temp")
if err != nil {
return "", err
}
err = cp.Copy(ImageFilePath, path)
if err != nil {
return "", err
}
return path, nil
}

// BootstrapWithSingleStore initializes a Cluster with 1 Region and 1 Store.
func BootstrapWithSingleStore(cluster testutils.Cluster) (storeID, peerID, regionID uint64) {
switch x := cluster.(type) {
Expand Down
4 changes: 4 additions & 0 deletions store/mockstore/unistore/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ package unistore

import (
"os"
spath "path"
"strings"

"github.com/pingcap/errors"
usconf "github.com/pingcap/tidb/store/mockstore/unistore/config"
Expand All @@ -31,6 +33,8 @@ func New(path string) (*RPCClient, pd.Client, *Cluster, error) {
if path, err = os.MkdirTemp("", "tidb-unistore-temp"); err != nil {
return nil, nil, nil, err
}
}
if strings.HasPrefix(path, spath.Join(os.TempDir(), "tidb-unistore-temp")) {
persistent = false
}

Expand Down
4 changes: 2 additions & 2 deletions store/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ func TestGetSet(t *testing.T) {
}

func TestSeek(t *testing.T) {
store, err := mockstore.NewMockStore()
store, err := mockstore.NewMockStore(mockstore.WithStoreType(mockstore.EmbedUnistore))
require.NoError(t, err)
defer func() {
require.NoError(t, store.Close())
Expand Down Expand Up @@ -337,7 +337,7 @@ func TestSetNil(t *testing.T) {
}

func TestBasicSeek(t *testing.T) {
store, err := mockstore.NewMockStore()
store, err := mockstore.NewMockStore(mockstore.WithStoreType(mockstore.EmbedUnistore))
require.NoError(t, err)
defer func() {
require.NoError(t, store.Close())
Expand Down
2 changes: 1 addition & 1 deletion table/temptable/ddl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import (
)

func createTestSuite(t *testing.T) (sessionctx.Context, *temporaryTableDDL) {
store, err := mockstore.NewMockStore()
store, err := mockstore.NewMockStore(mockstore.WithStoreType(mockstore.EmbedUnistore))
require.NoError(t, err)

sctx := mock.NewContext()
Expand Down
Loading