github.com/matrixorigin/matrixone@v0.7.0/pkg/vm/engine/tae/logstore/driver/batchstoredriver/vinfo.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 "context" 19 "sync" 20 21 // "github.com/matrixorigin/matrixone/pkg/logutil" 22 // "github.com/matrixorigin/matrixone/pkg/vm/engine/tae/logstore/entry" 23 24 "github.com/RoaringBitmap/roaring/roaring64" 25 "github.com/matrixorigin/matrixone/pkg/common/moerr" 26 ) 27 28 var ( 29 ErrVFileGroupNotExist = moerr.NewInternalErrorNoCtx("vfile: group not existed") 30 ErrVFileLsnNotExist = moerr.NewInternalErrorNoCtx("vfile: lsn not existed") 31 ErrVFileOffsetTimeOut = moerr.NewInternalErrorNoCtx("get vfile offset timeout") 32 ErrReadMetaFailed = moerr.NewInternalErrorNoCtx("read meta failed") 33 ) 34 35 type vInfo struct { 36 vf *vFile 37 38 Addrs map[uint64]int //lsn-offset //r 39 addrmu *sync.RWMutex 40 addrCond sync.Cond 41 42 logQueue chan any 43 flushWg sync.WaitGroup 44 flushCtx context.Context 45 flushCancel context.CancelFunc 46 } 47 48 type VFileUncommitInfo struct { 49 Index *roaring64.Bitmap 50 Addr *VFileAddress 51 } 52 53 type VFileAddress struct { 54 LSN uint64 55 Version int 56 Offset int 57 } 58 59 func newVInfo(vf *vFile) *vInfo { 60 info := &vInfo{ 61 Addrs: make(map[uint64]int), 62 addrmu: &sync.RWMutex{}, 63 addrCond: *sync.NewCond(new(sync.Mutex)), 64 vf: vf, 65 66 logQueue: make(chan any, DefaultMaxCommitSize*100), 67 } 68 info.flushCtx, info.flushCancel = context.WithCancel(context.Background()) 69 go info.logLoop() 70 return info 71 } 72 73 func (info *vInfo) String() string { 74 s := "" 75 // info.groupmu.RLock() 76 // for gid, g := range info.groups { 77 // s = fmt.Sprintf("%s%d-%s\n", s, gid, g.String()) 78 // } 79 // info.groupmu.RUnlock() 80 return s 81 } 82 func (info *vInfo) onReplay(offset int, lsn uint64) { 83 info.Addrs[lsn] = offset 84 } 85 func (info *vInfo) logLoop() { 86 infos := make([]any, 0, DefaultMaxCommitSize) 87 for { 88 select { 89 case <-info.flushCtx.Done(): 90 return 91 case entryInfo := <-info.logQueue: 92 infos = append(infos, entryInfo) 93 Left: 94 for i := 0; i < DefaultMaxCommitSize-1; i++ { 95 select { 96 case entryInfo = <-info.logQueue: 97 infos = append(infos, entryInfo) 98 default: 99 break Left 100 } 101 } 102 info.onLog(infos) 103 infos = infos[:0] 104 } 105 } 106 } 107 108 func (info *vInfo) onLog(infos []any) { 109 for _, vi := range infos { 110 info.addrmu.Lock() 111 addr := vi.(*VFileAddress) 112 info.Addrs[addr.LSN] = addr.Offset 113 info.addrmu.Unlock() 114 } 115 info.addrCond.L.Lock() 116 info.addrCond.Broadcast() 117 info.addrCond.L.Unlock() 118 info.flushWg.Add(-1 * len(infos)) 119 } 120 121 func (info *vInfo) close() { 122 info.flushWg.Wait() 123 info.flushCancel() 124 } 125 126 func (info *vInfo) Log(v any) error { 127 if v == nil { 128 return nil 129 } 130 info.flushWg.Add(1) 131 info.logQueue <- v.(*VFileAddress) 132 return nil 133 } 134 135 func (info *vInfo) GetOffsetByLSN(lsn uint64) (int, error) { 136 info.addrmu.RLock() 137 defer info.addrmu.RUnlock() 138 offset, ok := info.Addrs[lsn] 139 if !ok { 140 return 0, ErrVFileLsnNotExist 141 } 142 return offset, nil 143 }