github.com/whtcorpsinc/MilevaDB-Prod@v0.0.0-20211104133533-f57f4be3b597/soliton/mock/context.go (about) 1 // Copyright 2020 WHTCORPS INC, 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 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 // Package mock is just for test only. 15 package mock 16 17 import ( 18 "context" 19 "fmt" 20 "time" 21 22 "github.com/whtcorpsinc/BerolinaSQL/perceptron" 23 "github.com/whtcorpsinc/errors" 24 "github.com/whtcorpsinc/fidelpb/go-binlog" 25 "github.com/whtcorpsinc/milevadb/ekv" 26 "github.com/whtcorpsinc/milevadb/soliton" 27 "github.com/whtcorpsinc/milevadb/soliton/disk" 28 "github.com/whtcorpsinc/milevadb/soliton/ekvcache" 29 "github.com/whtcorpsinc/milevadb/soliton/memory" 30 "github.com/whtcorpsinc/milevadb/soliton/sqlexec" 31 "github.com/whtcorpsinc/milevadb/stochastikctx" 32 "github.com/whtcorpsinc/milevadb/stochastikctx/variable" 33 "github.com/whtcorpsinc/milevadb/tenant" 34 ) 35 36 var _ stochastikctx.Context = (*Context)(nil) 37 var _ sqlexec.ALLEGROSQLInterlockingDirectorate = (*Context)(nil) 38 39 // Context represents mocked stochastikctx.Context. 40 type Context struct { 41 values map[fmt.Stringer]interface{} 42 txn wrapTxn // mock global variable 43 CausetStore ekv.CausetStorage // mock global variable 44 stochastikVars *variable.StochastikVars 45 ctx context.Context 46 cancel context.CancelFunc 47 sm soliton.StochastikManager 48 pcache *ekvcache.SimpleLRUCache 49 } 50 51 type wrapTxn struct { 52 ekv.Transaction 53 } 54 55 func (txn *wrapTxn) Valid() bool { 56 return txn.Transaction != nil && txn.Transaction.Valid() 57 } 58 59 // InterDircute implements sqlexec.ALLEGROSQLInterlockingDirectorate InterDircute interface. 60 func (c *Context) InterDircute(ctx context.Context, allegrosql string) ([]sqlexec.RecordSet, error) { 61 return nil, errors.Errorf("Not Support.") 62 } 63 64 // InterDircuteInternal implements sqlexec.ALLEGROSQLInterlockingDirectorate InterDircuteInternal interface. 65 func (c *Context) InterDircuteInternal(ctx context.Context, allegrosql string) ([]sqlexec.RecordSet, error) { 66 return nil, errors.Errorf("Not Support.") 67 } 68 69 type mockDBSTenantChecker struct{} 70 71 func (c *mockDBSTenantChecker) IsTenant() bool { return true } 72 73 // DBSTenantChecker returns tenant.DBSTenantChecker. 74 func (c *Context) DBSTenantChecker() tenant.DBSTenantChecker { 75 return &mockDBSTenantChecker{} 76 } 77 78 // SetValue implements stochastikctx.Context SetValue interface. 79 func (c *Context) SetValue(key fmt.Stringer, value interface{}) { 80 c.values[key] = value 81 } 82 83 // Value implements stochastikctx.Context Value interface. 84 func (c *Context) Value(key fmt.Stringer) interface{} { 85 value := c.values[key] 86 return value 87 } 88 89 // ClearValue implements stochastikctx.Context ClearValue interface. 90 func (c *Context) ClearValue(key fmt.Stringer) { 91 delete(c.values, key) 92 } 93 94 // HasDirtyContent implements stochastikctx.Context ClearValue interface. 95 func (c *Context) HasDirtyContent(tid int64) bool { 96 return false 97 } 98 99 // GetStochastikVars implements the stochastikctx.Context GetStochastikVars interface. 100 func (c *Context) GetStochastikVars() *variable.StochastikVars { 101 return c.stochastikVars 102 } 103 104 // Txn implements stochastikctx.Context Txn interface. 105 func (c *Context) Txn(bool) (ekv.Transaction, error) { 106 return &c.txn, nil 107 } 108 109 // GetClient implements stochastikctx.Context GetClient interface. 110 func (c *Context) GetClient() ekv.Client { 111 if c.CausetStore == nil { 112 return nil 113 } 114 return c.CausetStore.GetClient() 115 } 116 117 // GetGlobalSysVar implements GlobalVarAccessor GetGlobalSysVar interface. 118 func (c *Context) GetGlobalSysVar(ctx stochastikctx.Context, name string) (string, error) { 119 v := variable.GetSysVar(name) 120 if v == nil { 121 return "", variable.ErrUnknownSystemVar.GenWithStackByArgs(name) 122 } 123 return v.Value, nil 124 } 125 126 // SetGlobalSysVar implements GlobalVarAccessor SetGlobalSysVar interface. 127 func (c *Context) SetGlobalSysVar(ctx stochastikctx.Context, name string, value string) error { 128 v := variable.GetSysVar(name) 129 if v == nil { 130 return variable.ErrUnknownSystemVar.GenWithStackByArgs(name) 131 } 132 v.Value = value 133 return nil 134 } 135 136 // PreparedCausetCache implements the stochastikctx.Context interface. 137 func (c *Context) PreparedCausetCache() *ekvcache.SimpleLRUCache { 138 return c.pcache 139 } 140 141 // NewTxn implements the stochastikctx.Context interface. 142 func (c *Context) NewTxn(context.Context) error { 143 if c.CausetStore == nil { 144 return errors.New("causetstore is not set") 145 } 146 if c.txn.Valid() { 147 err := c.txn.Commit(c.ctx) 148 if err != nil { 149 return errors.Trace(err) 150 } 151 } 152 153 txn, err := c.CausetStore.Begin() 154 if err != nil { 155 return errors.Trace(err) 156 } 157 c.txn.Transaction = txn 158 return nil 159 } 160 161 // RefreshTxnCtx implements the stochastikctx.Context interface. 162 func (c *Context) RefreshTxnCtx(ctx context.Context) error { 163 return errors.Trace(c.NewTxn(ctx)) 164 } 165 166 // InitTxnWithStartTS implements the stochastikctx.Context interface with startTS. 167 func (c *Context) InitTxnWithStartTS(startTS uint64) error { 168 if c.txn.Valid() { 169 return nil 170 } 171 if c.CausetStore != nil { 172 txn, err := c.CausetStore.BeginWithStartTS(startTS) 173 if err != nil { 174 return errors.Trace(err) 175 } 176 c.txn.Transaction = txn 177 } 178 return nil 179 } 180 181 // GetStore gets the causetstore of stochastik. 182 func (c *Context) GetStore() ekv.CausetStorage { 183 return c.CausetStore 184 } 185 186 // GetStochastikManager implements the stochastikctx.Context interface. 187 func (c *Context) GetStochastikManager() soliton.StochastikManager { 188 return c.sm 189 } 190 191 // SetStochastikManager set the stochastik manager. 192 func (c *Context) SetStochastikManager(sm soliton.StochastikManager) { 193 c.sm = sm 194 } 195 196 // Cancel implements the Stochastik interface. 197 func (c *Context) Cancel() { 198 c.cancel() 199 } 200 201 // GoCtx returns standard stochastikctx.Context that bind with current transaction. 202 func (c *Context) GoCtx() context.Context { 203 return c.ctx 204 } 205 206 // StoreQueryFeedback stores the query feedback. 207 func (c *Context) StoreQueryFeedback(_ interface{}) {} 208 209 // StmtCommit implements the stochastikctx.Context interface. 210 func (c *Context) StmtCommit() {} 211 212 // StmtRollback implements the stochastikctx.Context interface. 213 func (c *Context) StmtRollback() { 214 } 215 216 // StmtGetMutation implements the stochastikctx.Context interface. 217 func (c *Context) StmtGetMutation(blockID int64) *binlog.BlockMutation { 218 return nil 219 } 220 221 // StmtAddDirtyBlockOP implements the stochastikctx.Context interface. 222 func (c *Context) StmtAddDirtyBlockOP(op int, tid int64, handle ekv.Handle) { 223 } 224 225 // AddBlockLock implements the stochastikctx.Context interface. 226 func (c *Context) AddBlockLock(_ []perceptron.BlockLockTpInfo) { 227 } 228 229 // ReleaseBlockLocks implements the stochastikctx.Context interface. 230 func (c *Context) ReleaseBlockLocks(locks []perceptron.BlockLockTpInfo) { 231 } 232 233 // ReleaseBlockLockByBlockIDs implements the stochastikctx.Context interface. 234 func (c *Context) ReleaseBlockLockByBlockIDs(blockIDs []int64) { 235 } 236 237 // CheckBlockLocked implements the stochastikctx.Context interface. 238 func (c *Context) CheckBlockLocked(_ int64) (bool, perceptron.BlockLockType) { 239 return false, perceptron.BlockLockNone 240 } 241 242 // GetAllBlockLocks implements the stochastikctx.Context interface. 243 func (c *Context) GetAllBlockLocks() []perceptron.BlockLockTpInfo { 244 return nil 245 } 246 247 // ReleaseAllBlockLocks implements the stochastikctx.Context interface. 248 func (c *Context) ReleaseAllBlockLocks() { 249 } 250 251 // HasLockedBlocks implements the stochastikctx.Context interface. 252 func (c *Context) HasLockedBlocks() bool { 253 return false 254 } 255 256 // PrepareTSFuture implements the stochastikctx.Context interface. 257 func (c *Context) PrepareTSFuture(ctx context.Context) { 258 } 259 260 // Close implements the stochastikctx.Context interface. 261 func (c *Context) Close() { 262 } 263 264 // NewContext creates a new mocked stochastikctx.Context. 265 func NewContext() *Context { 266 ctx, cancel := context.WithCancel(context.Background()) 267 sctx := &Context{ 268 values: make(map[fmt.Stringer]interface{}), 269 stochastikVars: variable.NewStochastikVars(), 270 ctx: ctx, 271 cancel: cancel, 272 } 273 sctx.stochastikVars.InitChunkSize = 2 274 sctx.stochastikVars.MaxChunkSize = 32 275 sctx.stochastikVars.StmtCtx.TimeZone = time.UTC 276 sctx.stochastikVars.StmtCtx.MemTracker = memory.NewTracker(-1, -1) 277 sctx.stochastikVars.StmtCtx.DiskTracker = disk.NewTracker(-1, -1) 278 sctx.stochastikVars.GlobalVarsAccessor = variable.NewMockGlobalAccessor() 279 if err := sctx.GetStochastikVars().SetSystemVar(variable.MaxAllowedPacket, "67108864"); err != nil { 280 panic(err) 281 } 282 return sctx 283 } 284 285 // HookKeyForTest is as alias, used by context.WithValue. 286 // golint forbits using string type as key in context.WithValue. 287 type HookKeyForTest string