github.com/matrixorigin/matrixone@v0.7.0/pkg/vm/engine/tae/logstore/driver/batchstoredriver/syncbase.go (about) 1 // Copyright 2021 Matrix Origin 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 batchstoredriver 16 17 import ( 18 "math" 19 "sync" 20 "sync/atomic" 21 22 // "github.com/matrixorigin/matrixone/pkg/logutil" 23 "github.com/matrixorigin/matrixone/pkg/common/moerr" 24 "github.com/matrixorigin/matrixone/pkg/vm/engine/tae/common" 25 "github.com/matrixorigin/matrixone/pkg/vm/engine/tae/logstore/driver/entry" 26 ) 27 28 var ( 29 ErrGroupNotExist = moerr.NewInternalErrorNoCtx("group not existed") 30 ErrLsnNotExist = moerr.NewInternalErrorNoCtx("lsn not existed") 31 ErrVFileVersionTimeOut = moerr.NewInternalErrorNoCtx("get vfile version timeout") 32 ErrLsnCheckpointed = moerr.NewInternalErrorNoCtx("lsn has been checkpointed") 33 ) 34 35 type syncBase struct { 36 *sync.RWMutex 37 Lsn uint64 38 lsnmu sync.RWMutex 39 checkpointing atomic.Uint64 40 ckpmu *sync.RWMutex 41 truncatedVersion int 42 syncing uint64 43 synced uint64 44 addrs map[int]*common.ClosedIntervals //group-version-glsn range 45 addrmu sync.RWMutex 46 commitCond sync.Cond 47 } 48 49 func newSyncBase() *syncBase { 50 return &syncBase{ 51 lsnmu: sync.RWMutex{}, 52 addrs: make(map[int]*common.ClosedIntervals), 53 addrmu: sync.RWMutex{}, 54 ckpmu: &sync.RWMutex{}, 55 commitCond: *sync.NewCond(new(sync.Mutex)), 56 } 57 } 58 59 func (base *syncBase) GetVersionByGLSN(lsn uint64) (int, error) { 60 base.addrmu.RLock() 61 defer base.addrmu.RUnlock() 62 for ver, interval := range base.addrs { 63 if interval.Contains(*common.NewClosedIntervalsByInt(lsn)) { 64 return ver, nil 65 } 66 } 67 // for ver, lsns := range base.addrs { 68 // logutil.Infof("versionsMap %d %v", ver, lsns) 69 // } 70 return 0, ErrLsnNotExist 71 } 72 73 func (base *syncBase) OnEntryReceived(v *entry.Entry) error { 74 base.syncing = v.Lsn 75 base.addrmu.Lock() 76 defer base.addrmu.Unlock() 77 addr := v.Ctx.(*VFileAddress) 78 interval, ok := base.addrs[addr.Version] 79 if !ok { 80 interval = common.NewClosedIntervals() 81 base.addrs[addr.Version] = interval 82 } 83 interval.TryMerge(*common.NewClosedIntervalsByInt(v.Lsn)) 84 return nil 85 } 86 87 func (base *syncBase) GetTruncated() (uint64, error) { 88 lsn := base.checkpointing.Load() 89 return lsn, nil 90 } 91 92 func (base *syncBase) OnCommit() { 93 base.commitCond.L.Lock() 94 base.commitCond.Broadcast() 95 base.commitCond.L.Unlock() 96 97 if base.syncing > base.synced { 98 base.synced = base.syncing 99 } 100 } 101 102 func (base *syncBase) AllocateLsn() uint64 { 103 base.lsnmu.Lock() 104 defer base.lsnmu.Unlock() 105 base.Lsn++ 106 return base.Lsn 107 } 108 109 func (base *syncBase) GetCurrSeqNum() uint64 { 110 base.lsnmu.RLock() 111 defer base.lsnmu.RUnlock() 112 return base.Lsn 113 } 114 115 func (base *syncBase) onTruncatedFile(id int) { 116 base.truncatedVersion = id 117 } 118 119 func (base *syncBase) onReplay(r *replayer) { 120 base.Lsn = r.maxlsn 121 base.synced = r.maxlsn 122 base.syncing = r.maxlsn 123 if r.minlsn != math.MaxUint64 { 124 base.checkpointing.Store(r.minlsn) 125 } 126 }