|
| 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 | +} |
0 commit comments