Skip to content

Commit f22811f

Browse files
tpch: support add invert index during preparing
Signed-off-by: Lloyd-Pottiger <[email protected]>
1 parent a22ae88 commit f22811f

File tree

7 files changed

+215
-271
lines changed

7 files changed

+215
-271
lines changed

cmd/go-tpc/tpch.go

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"runtime"
99
"strings"
1010

11+
"github.com/coreos/go-semver/semver"
1112
"github.com/pingcap/go-tpc/pkg/util"
1213
"github.com/pingcap/go-tpc/tpch"
1314
"github.com/spf13/cobra"
@@ -29,8 +30,13 @@ var queryTuningVars = []struct {
2930

3031
// isSysVarSupported determines if a system variable is supported in given TiDB version
3132
// TODO: Every known sys var should have a minimal supported version and be checked individually. For now we just assume all sys vars are supported since 7.1.0.
32-
func isSysVarSupported(ver util.SemVersion, sysVar string) bool {
33-
return ver.Compare(util.SemVersion{Major: 7, Minor: 1, Patch: 0}) >= 0
33+
func isSysVarSupported(ver *semver.Version, _ string) bool {
34+
return ver.Compare(semver.Version{Major: 7, Minor: 1, Patch: 0}) >= 0
35+
}
36+
37+
// isInvertedIndexSupported checks if inverted index is supported in given TiDB version
38+
func isInvertedIndexSupported(ver semver.Version) bool {
39+
return ver.Compare(semver.Version{Major: 9, Minor: 0, Patch: 0, PreRelease: "beta.1"}) > 0
3440
}
3541

3642
func executeTpch(action string) {
@@ -45,23 +51,37 @@ func executeTpch(action string) {
4551
runtime.GOMAXPROCS(maxProcs)
4652
}
4753

48-
if action == "run" && driver == mysqlDriver && tpchConfig.EnableQueryTuning {
54+
isTiDB := false
55+
var TiDBSemVer *semver.Version
56+
if driver == mysqlDriver {
4957
serverVer, err := getServerVersion(globalDB)
5058
if err != nil {
5159
panic(fmt.Errorf("get server version failed: %v", err))
5260
}
5361
fmt.Printf("Server version: %s\n", serverVer)
62+
TiDBSemVer, isTiDB = util.NewTiDBSemVersion(serverVer)
63+
}
5464

55-
if semVer, ok := util.NewTiDBSemVersion(serverVer); ok {
56-
fmt.Printf("Enabling query tuning for TiDB version %s.\n", semVer.String())
57-
if err := setTiDBQueryTuningVars(globalDB, semVer); err != nil {
65+
if action == "run" && tpchConfig.EnableQueryTuning {
66+
if isTiDB {
67+
fmt.Printf("Enabling query tuning for TiDB version %s.\n", TiDBSemVer.String())
68+
if err := setTiDBQueryTuningVars(globalDB, TiDBSemVer); err != nil {
5869
panic(fmt.Errorf("set session variables failed: %v", err))
5970
}
6071
} else {
6172
fmt.Printf("Query tuning is enabled(by default) but server version doesn't appear to be TiDB, skipping tuning.\n")
6273
}
6374
}
6475

76+
if tpchConfig.AddInvertedIndex {
77+
if !isTiDB || !isInvertedIndexSupported(*TiDBSemVer) || tpchConfig.TiFlashReplica == 0 {
78+
fmt.Printf("Inverted index is only supported when TiDB version is > 9.0.0-beta.1 and TiFlash replica > 0.\n")
79+
tpchConfig.AddInvertedIndex = false
80+
} else {
81+
tpchConfig.AddInvertedIndex = true
82+
}
83+
}
84+
6585
tpchConfig.PlanReplayerConfig.Host = hosts[0]
6686
tpchConfig.PlanReplayerConfig.StatusPort = statusPort
6787

@@ -85,7 +105,7 @@ func getServerVersion(db *sql.DB) (string, error) {
85105
return version, err
86106
}
87107

88-
func setTiDBQueryTuningVars(db *sql.DB, ver util.SemVersion) error {
108+
func setTiDBQueryTuningVars(db *sql.DB, ver *semver.Version) error {
89109
for _, v := range queryTuningVars {
90110
if isSysVarSupported(ver, v.name) {
91111
if _, err := db.Exec(fmt.Sprintf("SET SESSION %s = %s", v.name, v.value)); err != nil {
@@ -189,6 +209,10 @@ func registerTpch(root *cobra.Command) {
189209
"enable-query-tuning",
190210
true,
191211
"Tune queries by setting some session variables known effective for tpch")
212+
cmdRun.PersistentFlags().BoolVar(&tpchConfig.AddInvertedIndex,
213+
"add-inverted-index",
214+
true,
215+
"Add some inverted index to accelerate queries")
192216

193217
var cmdCleanup = &cobra.Command{
194218
Use: "cleanup",

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ go 1.21
44

55
require (
66
github.com/HdrHistogram/hdrhistogram-go v1.0.0
7+
github.com/coreos/go-semver v0.3.1
78
github.com/go-sql-driver/mysql v1.7.1
89
github.com/jedib0t/go-pretty v4.3.0+incompatible
910
github.com/lib/pq v1.10.6

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ github.com/coreos/etcd v3.3.10+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc
4646
github.com/coreos/etcd v3.3.13+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc32PjwdhPthX9715RE=
4747
github.com/coreos/go-semver v0.2.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk=
4848
github.com/coreos/go-semver v0.3.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk=
49+
github.com/coreos/go-semver v0.3.1 h1:yi21YpKnrx1gt5R+la8n5WgS0kCrsPp33dmEyHReZr4=
50+
github.com/coreos/go-semver v0.3.1/go.mod h1:irMmmIw/7yzSRPWryHsK7EYSg09caPQL03VsM8rvUec=
4951
github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4=
5052
github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f/go.mod h1:E3G3o1h8I7cfcXa63jLwjI0eiQQMgzzUDFVpN/nH/eA=
5153
github.com/cpuguy83/go-md2man/v2 v2.0.0/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=

pkg/util/version.go

Lines changed: 23 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -3,76 +3,51 @@ package util
33
import (
44
"strconv"
55
"strings"
6-
)
76

8-
type SemVersion struct {
9-
Major int
10-
Minor int
11-
Patch int
12-
}
7+
"github.com/coreos/go-semver/semver"
8+
)
139

1410
// @version is the `SELECT VERSION()` output of TiDB
15-
func NewTiDBSemVersion(version string) (SemVersion, bool) {
11+
func NewTiDBSemVersion(version string) (*semver.Version, bool) {
1612
isTiDB := strings.Contains(strings.ToLower(version), "tidb")
1713
if !isTiDB {
18-
return SemVersion{}, false
14+
return nil, false
1915
}
2016

2117
verItems := strings.Split(version, "-v")
2218
if len(verItems) < 2 {
23-
return SemVersion{}, false
19+
return nil, false
20+
}
21+
verParts := strings.Split(verItems[1], "-")
22+
verStr := verParts[0]
23+
var preReleaseStr string
24+
if len(verParts) > 1 {
25+
preReleaseStr = verParts[1]
2426
}
25-
verStr := strings.Split(verItems[1], "-")[0]
2627

2728
parts := strings.Split(verStr, ".")
2829
if len(parts) < 3 {
29-
return SemVersion{}, false
30+
return nil, false
3031
}
3132

32-
major, err := strconv.Atoi(parts[0])
33+
major, err := strconv.ParseInt(parts[0], 10, 64)
3334
if err != nil {
34-
return SemVersion{}, false
35+
return nil, false
3536
}
36-
minor, err := strconv.Atoi(parts[1])
37+
minor, err := strconv.ParseInt(parts[1], 10, 64)
3738
if err != nil {
38-
return SemVersion{}, false
39+
return nil, false
3940
}
4041

41-
patch, err := strconv.Atoi(parts[2])
42+
patch, err := strconv.ParseInt(parts[2], 10, 64)
4243
if err != nil {
43-
return SemVersion{}, false
44+
return nil, false
4445
}
4546

46-
return SemVersion{
47-
Major: major,
48-
Minor: minor,
49-
Patch: patch,
47+
return &semver.Version{
48+
Major: major,
49+
Minor: minor,
50+
Patch: patch,
51+
PreRelease: semver.PreRelease(preReleaseStr),
5052
}, true
5153
}
52-
53-
func (s SemVersion) String() string {
54-
return strconv.Itoa(s.Major) + "." + strconv.Itoa(s.Minor) + "." + strconv.Itoa(s.Patch)
55-
}
56-
57-
func (s SemVersion) Compare(other SemVersion) int {
58-
signum := func(x int) int {
59-
if x > 0 {
60-
return 1
61-
}
62-
if x < 0 {
63-
return -1
64-
}
65-
return 0
66-
}
67-
68-
if diff := s.Major - other.Major; diff != 0 {
69-
return signum(diff)
70-
}
71-
if diff := s.Minor - other.Minor; diff != 0 {
72-
return signum(diff)
73-
}
74-
if diff := s.Patch - other.Patch; diff != 0 {
75-
return signum(diff)
76-
}
77-
return 0
78-
}

pkg/util/version_test.go

Lines changed: 15 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -3,62 +3,68 @@ package util
33
import (
44
"testing"
55

6+
"github.com/coreos/go-semver/semver"
67
"github.com/stretchr/testify/assert"
78
)
89

910
func TestNewTiDBSemVersion(t *testing.T) {
1011
testCases := []struct {
1112
name string
1213
input string
13-
expected SemVersion
14+
expected *semver.Version
1415
ok bool
1516
}{
1617
{
1718
name: "normal case with addition",
1819
input: "5.7.25-TiDB-v7.1.0-alpha",
19-
expected: SemVersion{Major: 7, Minor: 1, Patch: 0},
20+
expected: &semver.Version{Major: 7, Minor: 1, Patch: 0, PreRelease: "alpha"},
2021
ok: true,
2122
},
2223
{
2324
name: "version without addition",
2425
input: "5.7.25-TiDB-v7.4.1",
25-
expected: SemVersion{Major: 7, Minor: 4, Patch: 1},
26+
expected: &semver.Version{Major: 7, Minor: 4, Patch: 1, PreRelease: ""},
2627
ok: true,
2728
},
2829
{
2930
name: "multi-part addition",
3031
input: "5.7.25-TiDB-v6.5.3-beta.2",
31-
expected: SemVersion{Major: 6, Minor: 5, Patch: 3},
32+
expected: &semver.Version{Major: 6, Minor: 5, Patch: 3, PreRelease: "beta.2"},
3233
ok: true,
3334
},
3435
{
3536
name: "empty addition due to trailing hyphen",
3637
input: "5.7.25-TiDB-v7.1.0-",
37-
expected: SemVersion{Major: 7, Minor: 1, Patch: 0},
38+
expected: &semver.Version{Major: 7, Minor: 1, Patch: 0, PreRelease: ""},
39+
ok: true,
40+
},
41+
{
42+
input: "8.0.11-TiDB-v9.0.0-beta.1.pre-547-g4d34cac",
43+
expected: &semver.Version{Major: 9, Minor: 0, Patch: 0, PreRelease: "beta.1.pre"},
3844
ok: true,
3945
},
4046
{
4147
name: "non-tidb database",
4248
input: "MySQL 8.0.35",
43-
expected: SemVersion{},
49+
expected: nil,
4450
ok: false,
4551
},
4652
{
4753
name: "missing version prefix",
4854
input: "TiDB-7.2.0",
49-
expected: SemVersion{},
55+
expected: nil,
5056
ok: false,
5157
},
5258
{
5359
name: "invalid patch version",
5460
input: "5.7.25-TiDB-v7.1.x",
55-
expected: SemVersion{},
61+
expected: nil,
5662
ok: false,
5763
},
5864
{
5965
name: "insufficient version parts",
6066
input: "5.7.25-TiDB-v7.1",
61-
expected: SemVersion{},
67+
expected: nil,
6268
ok: false,
6369
},
6470
}
@@ -73,78 +79,3 @@ func TestNewTiDBSemVersion(t *testing.T) {
7379
})
7480
}
7581
}
76-
77-
func TestSemVersionCompare(t *testing.T) {
78-
testCases := []struct {
79-
name string
80-
version1 SemVersion
81-
version2 SemVersion
82-
expected int
83-
}{
84-
{
85-
name: "major version greater",
86-
version1: SemVersion{Major: 8, Minor: 0, Patch: 0},
87-
version2: SemVersion{Major: 7, Minor: 5, Patch: 10},
88-
expected: 1,
89-
},
90-
{
91-
name: "major version less",
92-
version1: SemVersion{Major: 6, Minor: 9, Patch: 9},
93-
version2: SemVersion{Major: 7, Minor: 0, Patch: 0},
94-
expected: -1,
95-
},
96-
{
97-
name: "major same, minor greater",
98-
version1: SemVersion{Major: 7, Minor: 2, Patch: 0},
99-
version2: SemVersion{Major: 7, Minor: 1, Patch: 5},
100-
expected: 1,
101-
},
102-
{
103-
name: "major same, minor less",
104-
version1: SemVersion{Major: 7, Minor: 1, Patch: 10},
105-
version2: SemVersion{Major: 7, Minor: 2, Patch: 0},
106-
expected: -1,
107-
},
108-
{
109-
name: "major and minor same, patch greater",
110-
version1: SemVersion{Major: 7, Minor: 1, Patch: 5},
111-
version2: SemVersion{Major: 7, Minor: 1, Patch: 0},
112-
expected: 1,
113-
},
114-
{
115-
name: "major and minor same, patch less",
116-
version1: SemVersion{Major: 7, Minor: 1, Patch: 0},
117-
version2: SemVersion{Major: 7, Minor: 1, Patch: 1},
118-
expected: -1,
119-
},
120-
{
121-
name: "identical versions",
122-
version1: SemVersion{Major: 7, Minor: 1, Patch: 0},
123-
version2: SemVersion{Major: 7, Minor: 1, Patch: 0},
124-
expected: 0,
125-
},
126-
{
127-
name: "extreme version differences",
128-
version1: SemVersion{Major: 10, Minor: 0, Patch: 0},
129-
version2: SemVersion{Major: 1, Minor: 99, Patch: 99},
130-
expected: 1,
131-
},
132-
}
133-
134-
for _, tc := range testCases {
135-
t.Run(tc.name, func(t *testing.T) {
136-
result := tc.version1.Compare(tc.version2)
137-
if result != tc.expected {
138-
t.Errorf("Expected %v.Compare(%v) = %v, got %v",
139-
tc.version1, tc.version2, tc.expected, result)
140-
}
141-
142-
reverseResult := tc.version2.Compare(tc.version1)
143-
expectedReverse := -tc.expected
144-
if reverseResult != expectedReverse {
145-
t.Errorf("Expected %v.Compare(%v) = %v, got %v",
146-
tc.version2, tc.version1, expectedReverse, reverseResult)
147-
}
148-
})
149-
}
150-
}

0 commit comments

Comments
 (0)