Skip to content

Commit 36b257c

Browse files
committed
Merge branch 'master' into rename-explaintest
2 parents 75d5589 + 02ceded commit 36b257c

File tree

190 files changed

+16217
-12347
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

190 files changed

+16217
-12347
lines changed

.codecov.yml

+18-10
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,23 @@ coverage:
88
round: down
99
range: "65...90"
1010

11+
# status:
12+
# project:
13+
# default:
14+
# threshold: 0.2 #Allow the coverage to drop by threshold%, and posting a success status.
15+
# patch:
16+
# default:
17+
# target: 0% # trial operation
18+
19+
# Ref: https://docs.codecov.com/docs/commit-status#disabling-a-status
1120
status:
12-
project:
13-
default:
14-
threshold: 0.2 #Allow the coverage to drop by threshold%, and posting a success status.
15-
patch:
16-
default:
17-
target: 0% # trial operation
21+
project: off # disable it
22+
patch: off # disable it
1823
changes: no
1924

25+
# Ref: https://docs.codecov.com/docs/github-checks#disabling-github-checks-completely-via-yaml
26+
github_checks: false
27+
2028
parsers:
2129
gcov:
2230
branch_detection:
@@ -43,7 +51,7 @@ component_management:
4351
- component_id: component_parser
4452
name: parser
4553
paths:
46-
- parser/**
54+
- parser/**
4755
- component_id: component_br
4856
name: br
4957
paths:
@@ -52,7 +60,7 @@ component_management:
5260

5361
flag_management:
5462
default_rules: # the rules that will be followed for any flag added, generally
55-
carryforward: false
63+
carryforward: true
5664
statuses:
5765
- type: project
5866
target: auto
@@ -61,11 +69,11 @@ flag_management:
6169

6270
ignore:
6371
- "LICENSES"
64-
- "*_test.go"
72+
- "*_test.go"
6573
- ".git"
6674
- "*.yml"
6775
- "*.md"
68-
- "cmd/.*"
76+
- "cmd/.*"
6977
- "docs/.*"
7078
- "vendor/.*"
7179
- "ddl/failtest/.*"

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,7 @@ bazel_test: failpoint-enable bazel_prepare
493493
-//tests/globalkilltest/... -//tests/readonlytest/... -//br/pkg/task:task_test -//tests/realtikvtest/...
494494

495495

496-
bazel_coverage_test: check-bazel-prepare failpoint-enable bazel_ci_simple_prepare
496+
bazel_coverage_test: failpoint-enable bazel_ci_simple_prepare
497497
bazel $(BAZEL_GLOBAL_CONFIG) --nohome_rc coverage $(BAZEL_CMD_CONFIG) --jobs=35 --build_tests_only --test_keep_going=false \
498498
--@io_bazel_rules_go//go/config:cover_format=go_cover --define gotags=deadlock,intest \
499499
-- //... -//cmd/... -//tests/graceshutdown/... \

bindinfo/internal/testutil.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,6 @@ func UtilNormalizeWithDefaultDB(t *testing.T, sql string) (normalized, digest st
3535
testParser := parser.New()
3636
stmt, err := testParser.ParseOneStmt(sql, "", "")
3737
require.NoError(t, err)
38-
normalized, digestResult := parser.NormalizeDigest(utilparser.RestoreWithDefaultDB(stmt, "test", ""))
38+
normalized, digestResult := parser.NormalizeDigestForBinding(utilparser.RestoreWithDefaultDB(stmt, "test", ""))
3939
return normalized, digestResult.String()
4040
}

bindinfo/tests/BUILD.bazel

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ go_test(
99
],
1010
flaky = True,
1111
race = "on",
12-
shard_count = 34,
12+
shard_count = 35,
1313
deps = [
1414
"//bindinfo",
1515
"//bindinfo/internal",

bindinfo/tests/bind_test.go

+47-3
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,7 @@ func TestBindingSymbolList(t *testing.T) {
414414
require.True(t, tk.MustUseIndex("select a, b from t where a = 3 limit 1, 100", "ib(b)"))
415415

416416
// Normalize
417-
sql, hash := parser.NormalizeDigest("select a, b from test . t where a = 1 limit 0, 1")
417+
sql, hash := parser.NormalizeDigestForBinding("select a, b from test . t where a = 1 limit 0, 1")
418418

419419
bindData := dom.BindHandle().GetBindRecord(hash.String(), sql, "test")
420420
require.NotNil(t, bindData)
@@ -429,6 +429,50 @@ func TestBindingSymbolList(t *testing.T) {
429429
require.NotNil(t, bind.UpdateTime)
430430
}
431431

432+
// TestBindingInListWithSingleLiteral tests sql with "IN (Lit)", fixes #44298
433+
func TestBindingInListWithSingleLiteral(t *testing.T) {
434+
store, dom := testkit.CreateMockStoreAndDomain(t)
435+
436+
tk := testkit.NewTestKit(t, store)
437+
tk.MustExec("use test")
438+
tk.MustExec("drop table if exists t")
439+
tk.MustExec("create table t(a int, b int, INDEX ia (a), INDEX ib (b));")
440+
tk.MustExec("insert into t value(1, 1);")
441+
442+
// GIVEN
443+
sqlcmd := "select a, b from t where a in (1)"
444+
binding := `create global binding for select a, b from t where a in (1, 2, 3) using select a, b from t use index (ib) where a in (1, 2, 3)`
445+
446+
// before binding
447+
tk.MustQuery(sqlcmd)
448+
require.Equal(t, "t:ia", tk.Session().GetSessionVars().StmtCtx.IndexNames[0])
449+
require.True(t, tk.MustUseIndex(sqlcmd, "ia(a)"))
450+
451+
tk.MustExec(binding)
452+
453+
// after binding
454+
tk.MustQuery(sqlcmd)
455+
require.Equal(t, "t:ib", tk.Session().GetSessionVars().StmtCtx.IndexNames[0])
456+
require.True(t, tk.MustUseIndex(sqlcmd, "ib(b)"))
457+
458+
tk.MustQuery("select @@last_plan_from_binding").Check(testkit.Rows("1"))
459+
460+
// Normalize
461+
sql, hash := parser.NormalizeDigestForBinding("select a, b from test . t where a in (1)")
462+
463+
bindData := dom.BindHandle().GetBindRecord(hash.String(), sql, "test")
464+
require.NotNil(t, bindData)
465+
require.Equal(t, "select `a` , `b` from `test` . `t` where `a` in ( ... )", bindData.OriginalSQL)
466+
bind := bindData.Bindings[0]
467+
require.Equal(t, "SELECT `a`,`b` FROM `test`.`t` USE INDEX (`ib`) WHERE `a` IN (1,2,3)", bind.BindSQL)
468+
require.Equal(t, "test", bindData.Db)
469+
require.Equal(t, bindinfo.Enabled, bind.Status)
470+
require.NotNil(t, bind.Charset)
471+
require.NotNil(t, bind.Collation)
472+
require.NotNil(t, bind.CreateTime)
473+
require.NotNil(t, bind.UpdateTime)
474+
}
475+
432476
func TestDMLSQLBind(t *testing.T) {
433477
store := testkit.CreateMockStore(t)
434478

@@ -538,7 +582,7 @@ func TestErrorBind(t *testing.T) {
538582
_, err := tk.Exec("create global binding for select * from t where i>100 using select * from t use index(index_t) where i>100")
539583
require.NoError(t, err, "err %v", err)
540584

541-
sql, hash := parser.NormalizeDigest("select * from test . t where i > ?")
585+
sql, hash := parser.NormalizeDigestForBinding("select * from test . t where i > ?")
542586
bindData := dom.BindHandle().GetBindRecord(hash.String(), sql, "test")
543587
require.NotNil(t, bindData)
544588
require.Equal(t, "select * from `test` . `t` where `i` > ?", bindData.OriginalSQL)
@@ -1304,7 +1348,7 @@ func TestBindSQLDigest(t *testing.T) {
13041348
parser4binding := parser.New()
13051349
originNode, err := parser4binding.ParseOneStmt(c.origin, "utf8mb4", "utf8mb4_general_ci")
13061350
require.NoError(t, err)
1307-
_, sqlDigestWithDB := parser.NormalizeDigest(utilparser.RestoreWithDefaultDB(originNode, "test", c.origin))
1351+
_, sqlDigestWithDB := parser.NormalizeDigestForBinding(utilparser.RestoreWithDefaultDB(originNode, "test", c.origin))
13081352
require.Equal(t, res[0][9], sqlDigestWithDB.String())
13091353
}
13101354
}

br/pkg/lightning/backend/backend.go

+16
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ type EngineConfig struct {
8484
TableInfo *checkpoints.TidbTableInfo
8585
// local backend specified configuration
8686
Local LocalEngineConfig
87+
// local backend external engine specified configuration
88+
External *ExternalEngineConfig
8789
// KeepSortDir indicates whether to keep the temporary sort directory
8890
// when opening the engine, instead of removing it.
8991
KeepSortDir bool
@@ -99,6 +101,20 @@ type LocalEngineConfig struct {
99101
CompactConcurrency int
100102
}
101103

104+
// ExternalEngineConfig is the configuration used for local backend external engine.
105+
type ExternalEngineConfig struct {
106+
StorageURI string
107+
DataFiles []string
108+
StatFiles []string
109+
MinKey []byte
110+
MaxKey []byte
111+
SplitKeys [][]byte
112+
// TotalFileSize can be an estimated value.
113+
TotalFileSize int64
114+
// TotalKVCount can be an estimated value.
115+
TotalKVCount int64
116+
}
117+
102118
// CheckCtx contains all parameters used in CheckRequirements
103119
type CheckCtx struct {
104120
DBMetas []*mydump.MDDatabaseMeta

br/pkg/lightning/backend/external/BUILD.bazel

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ go_test(
5353
],
5454
embed = [":external"],
5555
flaky = True,
56-
shard_count = 28,
56+
shard_count = 31,
5757
deps = [
5858
"//br/pkg/lightning/backend/kv",
5959
"//br/pkg/lightning/common",

br/pkg/lightning/backend/external/engine.go

+22-9
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ type Engine struct {
3838
storage storage.ExternalStorage
3939
dataFiles []string
4040
statsFiles []string
41+
minKey []byte
42+
maxKey []byte
4143
splitKeys [][]byte
4244
bufPool *membuf.Pool
4345

@@ -49,8 +51,8 @@ type Engine struct {
4951
dupDetectOpt common.DupDetectOpt
5052
ts uint64
5153

52-
totalKVSize int64
53-
totalKVLength int64
54+
totalKVSize int64
55+
totalKVCount int64
5456

5557
importedKVSize *atomic.Int64
5658
importedKVCount *atomic.Int64
@@ -61,26 +63,32 @@ func NewExternalEngine(
6163
storage storage.ExternalStorage,
6264
dataFiles []string,
6365
statsFiles []string,
66+
minKey []byte,
67+
maxKey []byte,
68+
splitKeys [][]byte,
6469
keyAdapter common.KeyAdapter,
6570
duplicateDetection bool,
6671
duplicateDB *pebble.DB,
6772
dupDetectOpt common.DupDetectOpt,
6873
ts uint64,
6974
totalKVSize int64,
70-
totakKVLength int64,
75+
totalKVCount int64,
7176
) common.Engine {
7277
return &Engine{
7378
storage: storage,
7479
dataFiles: dataFiles,
7580
statsFiles: statsFiles,
81+
minKey: minKey,
82+
maxKey: maxKey,
83+
splitKeys: splitKeys,
7684
bufPool: membuf.NewPool(),
7785
keyAdapter: keyAdapter,
7886
duplicateDetection: duplicateDetection,
7987
duplicateDB: duplicateDB,
8088
dupDetectOpt: dupDetectOpt,
8189
ts: ts,
8290
totalKVSize: totalKVSize,
83-
totalKVLength: totakKVLength,
91+
totalKVCount: totalKVCount,
8492
importedKVSize: atomic.NewInt64(0),
8593
importedKVCount: atomic.NewInt64(0),
8694
}
@@ -177,13 +185,13 @@ func (e *Engine) createMergeIter(ctx context.Context, start kv.Key) (*MergeKVIte
177185
return iter, nil
178186
}
179187

180-
// KVStatistics returns the total kv size and total kv length.
181-
func (e *Engine) KVStatistics() (totalKVSize int64, totalKVLength int64) {
182-
return e.totalKVSize, e.totalKVLength
188+
// KVStatistics returns the total kv size and total kv count.
189+
func (e *Engine) KVStatistics() (totalKVSize int64, totalKVCount int64) {
190+
return e.totalKVSize, e.totalKVCount
183191
}
184192

185-
// ImportedStatistics returns the imported kv size and imported kv length.
186-
func (e *Engine) ImportedStatistics() (importedKVSize int64, importedKVLength int64) {
193+
// ImportedStatistics returns the imported kv size and imported kv count.
194+
func (e *Engine) ImportedStatistics() (importedSize int64, importedKVCount int64) {
187195
return e.importedKVSize.Load(), e.importedKVCount.Load()
188196
}
189197

@@ -192,6 +200,11 @@ func (e *Engine) ID() string {
192200
return "external"
193201
}
194202

203+
// GetKeyRange implements common.Engine.
204+
func (e *Engine) GetKeyRange() (firstKey []byte, lastKey []byte, err error) {
205+
return e.minKey, e.maxKey, nil
206+
}
207+
195208
// SplitRanges split the ranges by split keys provided by external engine.
196209
func (e *Engine) SplitRanges(
197210
startKey, endKey []byte,

br/pkg/lightning/backend/external/util.go

+48
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"context"
2020
"fmt"
2121
"path/filepath"
22+
"slices"
2223
"sort"
2324
"strings"
2425

@@ -199,3 +200,50 @@ func MockExternalEngineWithWriter(
199200
}
200201
return GetAllFileNames(ctx, storage, subDir)
201202
}
203+
204+
// EndpointTp is the type of Endpoint.Key.
205+
type EndpointTp int
206+
207+
const (
208+
// ExclusiveEnd represents "..., Endpoint.Key)".
209+
ExclusiveEnd EndpointTp = iota
210+
// InclusiveStart represents "[Endpoint.Key, ...".
211+
InclusiveStart
212+
// InclusiveEnd represents "..., Endpoint.Key]".
213+
InclusiveEnd
214+
)
215+
216+
// Endpoint represents an endpoint of an interval which can be used by GetMaxOverlapping.
217+
type Endpoint struct {
218+
Key []byte
219+
Tp EndpointTp
220+
Weight uint64 // all EndpointTp use positive weight
221+
}
222+
223+
// GetMaxOverlapping returns the maximum overlapping weight treating given
224+
// `points` as endpoints of intervals. `points` are not required to be sorted,
225+
// and will be sorted in-place in this function.
226+
func GetMaxOverlapping(points []Endpoint) int {
227+
slices.SortFunc(points, func(i, j Endpoint) int {
228+
if cmp := bytes.Compare(i.Key, j.Key); cmp != 0 {
229+
return cmp
230+
}
231+
return int(i.Tp) - int(j.Tp)
232+
})
233+
var maxWeight uint64
234+
var curWeight uint64
235+
for _, p := range points {
236+
switch p.Tp {
237+
case InclusiveStart:
238+
curWeight += p.Weight
239+
case ExclusiveEnd:
240+
curWeight -= p.Weight
241+
case InclusiveEnd:
242+
curWeight -= p.Weight
243+
}
244+
if curWeight > maxWeight {
245+
maxWeight = curWeight
246+
}
247+
}
248+
return int(maxWeight)
249+
}

br/pkg/lightning/backend/external/util_test.go

+31
Original file line numberDiff line numberDiff line change
@@ -209,3 +209,34 @@ func TestCleanUpFiles(t *testing.T) {
209209
require.Equal(t, []string(nil), statFiles)
210210
require.Equal(t, []string(nil), dataFiles)
211211
}
212+
213+
func TestGetMaxOverlapping(t *testing.T) {
214+
// [1, 3), [2, 4)
215+
points := []Endpoint{
216+
{Key: []byte{1}, Tp: InclusiveStart, Weight: 1},
217+
{Key: []byte{3}, Tp: ExclusiveEnd, Weight: 1},
218+
{Key: []byte{2}, Tp: InclusiveStart, Weight: 1},
219+
{Key: []byte{4}, Tp: ExclusiveEnd, Weight: 1},
220+
}
221+
require.Equal(t, 2, GetMaxOverlapping(points))
222+
// [1, 3), [2, 4), [3, 5)
223+
points = []Endpoint{
224+
{Key: []byte{1}, Tp: InclusiveStart, Weight: 1},
225+
{Key: []byte{3}, Tp: ExclusiveEnd, Weight: 1},
226+
{Key: []byte{2}, Tp: InclusiveStart, Weight: 1},
227+
{Key: []byte{4}, Tp: ExclusiveEnd, Weight: 1},
228+
{Key: []byte{3}, Tp: InclusiveStart, Weight: 1},
229+
{Key: []byte{5}, Tp: ExclusiveEnd, Weight: 1},
230+
}
231+
require.Equal(t, 2, GetMaxOverlapping(points))
232+
// [1, 3], [2, 4], [3, 5]
233+
points = []Endpoint{
234+
{Key: []byte{1}, Tp: InclusiveStart, Weight: 1},
235+
{Key: []byte{3}, Tp: InclusiveEnd, Weight: 1},
236+
{Key: []byte{2}, Tp: InclusiveStart, Weight: 1},
237+
{Key: []byte{4}, Tp: InclusiveEnd, Weight: 1},
238+
{Key: []byte{3}, Tp: InclusiveStart, Weight: 1},
239+
{Key: []byte{5}, Tp: InclusiveEnd, Weight: 1},
240+
}
241+
require.Equal(t, 3, GetMaxOverlapping(points))
242+
}

0 commit comments

Comments
 (0)