Skip to content

Commit 09cb02f

Browse files
RidRisRti-chi-bot
authored andcommitted
This is an automated cherry-pick of pingcap#55044
Signed-off-by: ti-chi-bot <[email protected]>
1 parent 5fe6f23 commit 09cb02f

File tree

10 files changed

+402
-0
lines changed

10 files changed

+402
-0
lines changed

br/pkg/errors/errors.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ var (
6868
ErrRestoreIncompatibleSys = errors.Normalize("incompatible system table", errors.RFCCodeText("BR:Restore:ErrRestoreIncompatibleSys"))
6969
ErrUnsupportedSystemTable = errors.Normalize("the system table isn't supported for restoring yet", errors.RFCCodeText("BR:Restore:ErrUnsupportedSysTable"))
7070
ErrDatabasesAlreadyExisted = errors.Normalize("databases already existed in restored cluster", errors.RFCCodeText("BR:Restore:ErrDatabasesAlreadyExisted"))
71+
ErrTablesAlreadyExisted = errors.Normalize("tables already existed in restored cluster", errors.RFCCodeText("BR:Restore:ErrTablesAlreadyExisted"))
7172

7273
// ErrStreamLogTaskExist is the error when stream log task already exists, because of supporting single task currently.
7374
ErrStreamLogTaskExist = errors.Normalize("stream task already exists", errors.RFCCodeText("BR:Stream:ErrStreamLogTaskExist"))

br/pkg/glue/glue.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@ import (
1313
pd "github.com/tikv/pd/client"
1414
)
1515

16+
type GlueClient int
17+
18+
const (
19+
ClientCLP GlueClient = iota
20+
ClientSql
21+
)
22+
1623
// Glue is an abstraction of TiDB function calls used in BR.
1724
type Glue interface {
1825
GetDomain(store kv.Storage) (*domain.Domain, error)
@@ -36,6 +43,9 @@ type Glue interface {
3643
// we can close domain as soon as possible.
3744
// and we must reuse the exists session and don't close it in SQL backup job.
3845
UseOneShotSession(store kv.Storage, closeDomain bool, fn func(se Session) error) error
46+
47+
// GetClient returns the client type of the glue
48+
GetClient() GlueClient
3949
}
4050

4151
// Session is an abstraction of the session.Session interface.

br/pkg/gluetidb/glue.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,10 @@ func (g Glue) UseOneShotSession(store kv.Storage, closeDomain bool, fn func(glue
157157
return nil
158158
}
159159

160+
func (Glue) GetClient() glue.GlueClient {
161+
return glue.ClientCLP
162+
}
163+
160164
// GetSessionCtx implements glue.Glue
161165
func (gs *tidbSession) GetSessionCtx() sessionctx.Context {
162166
return gs.se

br/pkg/gluetidb/mock/mock.go

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
// Copyright 2024 PingCAP, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package mock
16+
17+
import (
18+
"context"
19+
"log"
20+
21+
"github.com/pingcap/tidb/br/pkg/glue"
22+
"github.com/pingcap/tidb/pkg/ddl"
23+
"github.com/pingcap/tidb/pkg/domain"
24+
"github.com/pingcap/tidb/pkg/kv"
25+
"github.com/pingcap/tidb/pkg/parser/model"
26+
sessiontypes "github.com/pingcap/tidb/pkg/session/types"
27+
"github.com/pingcap/tidb/pkg/sessionctx"
28+
pd "github.com/tikv/pd/client"
29+
)
30+
31+
// mockSession is used for test.
32+
type mockSession struct {
33+
se sessiontypes.Session
34+
globalVars map[string]string
35+
}
36+
37+
// GetSessionCtx implements glue.Glue
38+
func (s *mockSession) GetSessionCtx() sessionctx.Context {
39+
return s.se
40+
}
41+
42+
// Execute implements glue.Session.
43+
func (s *mockSession) Execute(ctx context.Context, sql string) error {
44+
return s.ExecuteInternal(ctx, sql)
45+
}
46+
47+
func (s *mockSession) ExecuteInternal(ctx context.Context, sql string, args ...any) error {
48+
ctx = kv.WithInternalSourceType(ctx, kv.InternalTxnBR)
49+
rs, err := s.se.ExecuteInternal(ctx, sql, args...)
50+
if err != nil {
51+
return err
52+
}
53+
// Some of SQLs (like ADMIN RECOVER INDEX) may lazily take effect
54+
// when we are polling the result set.
55+
// At least call `next` once for triggering theirs side effect.
56+
// (Maybe we'd better drain all returned rows?)
57+
if rs != nil {
58+
//nolint: errcheck
59+
defer rs.Close()
60+
c := rs.NewChunk(nil)
61+
if err := rs.Next(ctx, c); err != nil {
62+
return nil
63+
}
64+
}
65+
return nil
66+
}
67+
68+
// CreateDatabase implements glue.Session.
69+
func (*mockSession) CreateDatabase(_ context.Context, _ *model.DBInfo) error {
70+
log.Fatal("unimplemented CreateDatabase for mock session")
71+
return nil
72+
}
73+
74+
// CreatePlacementPolicy implements glue.Session.
75+
func (*mockSession) CreatePlacementPolicy(_ context.Context, _ *model.PolicyInfo) error {
76+
log.Fatal("unimplemented CreateDatabase for mock session")
77+
return nil
78+
}
79+
80+
// CreateTables implements glue.BatchCreateTableSession.
81+
func (*mockSession) CreateTables(_ context.Context, _ map[string][]*model.TableInfo,
82+
_ ...ddl.CreateTableOption) error {
83+
log.Fatal("unimplemented CreateDatabase for mock session")
84+
return nil
85+
}
86+
87+
// CreateTable implements glue.Session.
88+
func (*mockSession) CreateTable(_ context.Context, _ model.CIStr,
89+
_ *model.TableInfo, _ ...ddl.CreateTableOption) error {
90+
log.Fatal("unimplemented CreateDatabase for mock session")
91+
return nil
92+
}
93+
94+
// Close implements glue.Session.
95+
func (s *mockSession) Close() {
96+
s.se.Close()
97+
}
98+
99+
// GetGlobalVariables implements glue.Session.
100+
func (s *mockSession) GetGlobalVariable(name string) (string, error) {
101+
if ret, ok := s.globalVars[name]; ok {
102+
return ret, nil
103+
}
104+
return "True", nil
105+
}
106+
107+
// MockGlue only used for test
108+
type MockGlue struct {
109+
se sessiontypes.Session
110+
GlobalVars map[string]string
111+
}
112+
113+
func (m *MockGlue) SetSession(se sessiontypes.Session) {
114+
m.se = se
115+
}
116+
117+
// GetDomain implements glue.Glue.
118+
func (*MockGlue) GetDomain(store kv.Storage) (*domain.Domain, error) {
119+
return nil, nil
120+
}
121+
122+
// CreateSession implements glue.Glue.
123+
func (m *MockGlue) CreateSession(store kv.Storage) (glue.Session, error) {
124+
glueSession := &mockSession{
125+
se: m.se,
126+
globalVars: m.GlobalVars,
127+
}
128+
return glueSession, nil
129+
}
130+
131+
// Open implements glue.Glue.
132+
func (*MockGlue) Open(path string, option pd.SecurityOption) (kv.Storage, error) {
133+
return nil, nil
134+
}
135+
136+
// OwnsStorage implements glue.Glue.
137+
func (*MockGlue) OwnsStorage() bool {
138+
return true
139+
}
140+
141+
// StartProgress implements glue.Glue.
142+
func (*MockGlue) StartProgress(ctx context.Context, cmdName string, total int64, redirectLog bool) glue.Progress {
143+
return nil
144+
}
145+
146+
// Record implements glue.Glue.
147+
func (*MockGlue) Record(name string, value uint64) {
148+
}
149+
150+
// GetVersion implements glue.Glue.
151+
func (*MockGlue) GetVersion() string {
152+
return "mock glue"
153+
}
154+
155+
// UseOneShotSession implements glue.Glue.
156+
func (m *MockGlue) UseOneShotSession(store kv.Storage, closeDomain bool, fn func(glue.Session) error) error {
157+
glueSession := &mockSession{
158+
se: m.se,
159+
}
160+
return fn(glueSession)
161+
}
162+
163+
func (*MockGlue) GetClient() glue.GlueClient {
164+
return glue.ClientCLP
165+
}

br/pkg/gluetikv/glue.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,7 @@ func (Glue) GetVersion() string {
7373
func (g Glue) UseOneShotSession(store kv.Storage, closeDomain bool, fn func(glue.Session) error) error {
7474
return nil
7575
}
76+
77+
func (Glue) GetClient() glue.GlueClient {
78+
return glue.ClientCLP
79+
}

0 commit comments

Comments
 (0)