github.com/whtcorpsinc/milevadb-prod@v0.0.0-20211104133533-f57f4be3b597/dbs/mock.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://wwm.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 dbs 15 16 import ( 17 "context" 18 "sync/atomic" 19 "time" 20 21 "github.com/whtcorpsinc/errors" 22 "github.com/whtcorpsinc/failpoint" 23 "github.com/whtcorpsinc/BerolinaSQL/ast" 24 "github.com/whtcorpsinc/BerolinaSQL/charset" 25 "github.com/whtcorpsinc/BerolinaSQL/perceptron" 26 "github.com/whtcorpsinc/milevadb/dbs/soliton" 27 "github.com/whtcorpsinc/milevadb/stochastikctx" 28 "go.etcd.io/etcd/clientv3" 29 ) 30 31 var _ soliton.SchemaSyncer = &MockSchemaSyncer{} 32 33 const mockCheckVersInterval = 2 * time.Millisecond 34 35 // MockSchemaSyncer is a mock schemaReplicant syncer, it is exported for tesing. 36 type MockSchemaSyncer struct { 37 selfSchemaVersion int64 38 globalVerCh chan clientv3.WatchResponse 39 mockStochastik chan struct{} 40 } 41 42 // NewMockSchemaSyncer creates a new mock SchemaSyncer. 43 func NewMockSchemaSyncer() soliton.SchemaSyncer { 44 return &MockSchemaSyncer{} 45 } 46 47 // Init implements SchemaSyncer.Init interface. 48 func (s *MockSchemaSyncer) Init(ctx context.Context) error { 49 s.globalVerCh = make(chan clientv3.WatchResponse, 1) 50 s.mockStochastik = make(chan struct{}, 1) 51 return nil 52 } 53 54 // GlobalVersionCh implements SchemaSyncer.GlobalVersionCh interface. 55 func (s *MockSchemaSyncer) GlobalVersionCh() clientv3.WatchChan { 56 return s.globalVerCh 57 } 58 59 // WatchGlobalSchemaVer implements SchemaSyncer.WatchGlobalSchemaVer interface. 60 func (s *MockSchemaSyncer) WatchGlobalSchemaVer(context.Context) {} 61 62 // UFIDelateSelfVersion implements SchemaSyncer.UFIDelateSelfVersion interface. 63 func (s *MockSchemaSyncer) UFIDelateSelfVersion(ctx context.Context, version int64) error { 64 atomic.StoreInt64(&s.selfSchemaVersion, version) 65 return nil 66 } 67 68 // Done implements SchemaSyncer.Done interface. 69 func (s *MockSchemaSyncer) Done() <-chan struct{} { 70 return s.mockStochastik 71 } 72 73 // CloseStochastik mockStochastik, it is exported for testing. 74 func (s *MockSchemaSyncer) CloseStochastik() { 75 close(s.mockStochastik) 76 } 77 78 // Restart implements SchemaSyncer.Restart interface. 79 func (s *MockSchemaSyncer) Restart(_ context.Context) error { 80 s.mockStochastik = make(chan struct{}, 1) 81 return nil 82 } 83 84 // TenantUFIDelateGlobalVersion implements SchemaSyncer.TenantUFIDelateGlobalVersion interface. 85 func (s *MockSchemaSyncer) TenantUFIDelateGlobalVersion(ctx context.Context, version int64) error { 86 select { 87 case s.globalVerCh <- clientv3.WatchResponse{}: 88 default: 89 } 90 return nil 91 } 92 93 // MustGetGlobalVersion implements SchemaSyncer.MustGetGlobalVersion interface. 94 func (s *MockSchemaSyncer) MustGetGlobalVersion(ctx context.Context) (int64, error) { 95 return 0, nil 96 } 97 98 // TenantCheckAllVersions implements SchemaSyncer.TenantCheckAllVersions interface. 99 func (s *MockSchemaSyncer) TenantCheckAllVersions(ctx context.Context, latestVer int64) error { 100 ticker := time.NewTicker(mockCheckVersInterval) 101 defer ticker.Stop() 102 103 for { 104 select { 105 case <-ctx.Done(): 106 failpoint.Inject("checkTenantCheckAllVersionsWaitTime", func(v failpoint.Value) { 107 if v.(bool) { 108 panic("shouldn't happen") 109 } 110 }) 111 return errors.Trace(ctx.Err()) 112 case <-ticker.C: 113 ver := atomic.LoadInt64(&s.selfSchemaVersion) 114 if ver >= latestVer { 115 return nil 116 } 117 } 118 } 119 } 120 121 // NotifyCleanExpiredPaths implements SchemaSyncer.NotifyCleanExpiredPaths interface. 122 func (s *MockSchemaSyncer) NotifyCleanExpiredPaths() bool { return true } 123 124 // StartCleanWork implements SchemaSyncer.StartCleanWork interface. 125 func (s *MockSchemaSyncer) StartCleanWork() {} 126 127 // Close implements SchemaSyncer.Close interface. 128 func (s *MockSchemaSyncer) Close() {} 129 130 type mockDelRange struct { 131 } 132 133 // newMockDelRangeManager creates a mock delRangeManager only used for test. 134 func newMockDelRangeManager() delRangeManager { 135 return &mockDelRange{} 136 } 137 138 // addDelRangeJob implements delRangeManager interface. 139 func (dr *mockDelRange) addDelRangeJob(job *perceptron.Job) error { 140 return nil 141 } 142 143 // removeFromGCDeleteRange implements delRangeManager interface. 144 func (dr *mockDelRange) removeFromGCDeleteRange(jobID int64, blockIDs []int64) error { 145 return nil 146 } 147 148 // start implements delRangeManager interface. 149 func (dr *mockDelRange) start() {} 150 151 // clear implements delRangeManager interface. 152 func (dr *mockDelRange) clear() {} 153 154 // MockBlockInfo mocks a causet info by create causet stmt ast and a specified causet id. 155 func MockBlockInfo(ctx stochastikctx.Context, stmt *ast.CreateBlockStmt, blockID int64) (*perceptron.BlockInfo, error) { 156 chs, defCausl := charset.GetDefaultCharsetAndDefCauslate() 157 defcaus, newConstraints, err := buildDeferredCausetsAndConstraints(ctx, stmt.DefCauss, stmt.Constraints, chs, defCausl) 158 if err != nil { 159 return nil, errors.Trace(err) 160 } 161 tbl, err := buildBlockInfo(ctx, stmt.Block.Name, defcaus, newConstraints, "", "") 162 if err != nil { 163 return nil, errors.Trace(err) 164 } 165 tbl.ID = blockID 166 167 // The specified charset will be handled in handleBlockOptions 168 if err = handleBlockOptions(stmt.Options, tbl); err != nil { 169 return nil, errors.Trace(err) 170 } 171 172 return tbl, nil 173 }