/
collector.go
483 lines (426 loc) · 11.3 KB
/
collector.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
// Copyright 2021 Matrix Origin
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package logtail
import (
"bytes"
"context"
"fmt"
"sync"
"sync/atomic"
"time"
"github.com/matrixorigin/matrixone/pkg/common/moerr"
"github.com/matrixorigin/matrixone/pkg/container/types"
"github.com/matrixorigin/matrixone/pkg/logutil"
"github.com/matrixorigin/matrixone/pkg/objectio"
"github.com/matrixorigin/matrixone/pkg/txn/clock"
"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/catalog"
"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/model"
"github.com/tidwall/btree"
)
type TempFilter struct {
sync.RWMutex
m map[uint64]bool
}
type TempFKey struct{}
func (f *TempFilter) Add(id uint64) {
f.Lock()
defer f.Unlock()
f.m[id] = true
}
func (f *TempFilter) Check(id uint64) (skip bool) {
f.Lock()
defer f.Unlock()
if _, ok := f.m[id]; ok {
delete(f.m, id)
return true
}
return false
}
var TempF *TempFilter
func init() {
TempF = &TempFilter{
m: make(map[uint64]bool),
}
}
type Collector interface {
String() string
Run(lag time.Duration)
ScanInRange(from, to types.TS) (*DirtyTreeEntry, int)
ScanInRangePruned(from, to types.TS) *DirtyTreeEntry
IsCommitted(from, to types.TS) bool
GetAndRefreshMerged() *DirtyTreeEntry
Merge() *DirtyTreeEntry
GetMaxLSN(from, to types.TS) uint64
Init(maxts types.TS)
}
type DirtyEntryInterceptor = catalog.Processor
type DirtyTreeEntry struct {
sync.RWMutex
start, end types.TS
tree *model.Tree
}
func NewEmptyDirtyTreeEntry() *DirtyTreeEntry {
return &DirtyTreeEntry{
tree: model.NewTree(),
}
}
func NewDirtyTreeEntry(start, end types.TS, tree *model.Tree) *DirtyTreeEntry {
entry := NewEmptyDirtyTreeEntry()
entry.start = start
entry.end = end
entry.tree = tree
return entry
}
func (entry *DirtyTreeEntry) Merge(o *DirtyTreeEntry) {
if entry.start.Greater(o.start) {
entry.start = o.start
}
if entry.end.Less(o.end) {
entry.end = o.end
}
entry.tree.Merge(o.tree)
}
func (entry *DirtyTreeEntry) IsEmpty() bool {
return entry.tree.IsEmpty()
}
func (entry *DirtyTreeEntry) GetTimeRange() (from, to types.TS) {
return entry.start, entry.end
}
func (entry *DirtyTreeEntry) GetTree() (tree *model.Tree) {
return entry.tree
}
func (entry *DirtyTreeEntry) String() string {
var buf bytes.Buffer
_, _ = buf.WriteString(
fmt.Sprintf("DirtyTreeEntry[%s=>%s]\n",
entry.start.ToString(),
entry.end.ToString()))
_, _ = buf.WriteString(entry.tree.String())
return buf.String()
}
type dirtyCollector struct {
// sourcer
sourcer *Manager
// context
catalog *catalog.Catalog
clock *types.TsAlloctor
interceptor DirtyEntryInterceptor
// storage
storage struct {
sync.RWMutex
entries *btree.BTreeG[*DirtyTreeEntry]
maxTs types.TS
}
merged atomic.Pointer[DirtyTreeEntry]
}
func NewDirtyCollector(
sourcer *Manager,
clock clock.Clock,
catalog *catalog.Catalog,
interceptor DirtyEntryInterceptor) *dirtyCollector {
collector := &dirtyCollector{
sourcer: sourcer,
catalog: catalog,
interceptor: interceptor,
clock: types.NewTsAlloctor(clock),
}
collector.storage.entries = btree.NewBTreeGOptions(
func(a, b *DirtyTreeEntry) bool {
return a.start.Less(b.start) && a.end.Less(b.end)
}, btree.Options{
NoLocks: true,
})
collector.merged.Store(NewEmptyDirtyTreeEntry())
return collector
}
func (d *dirtyCollector) Init(maxts types.TS) {
d.storage.maxTs = maxts
}
func (d *dirtyCollector) Run(lag time.Duration) {
from, to := d.findRange(lag)
// stale range found, skip this run
if to.IsEmpty() {
return
}
d.rangeScanAndUpdate(from, to)
d.cleanupStorage()
d.GetAndRefreshMerged()
}
func (d *dirtyCollector) ScanInRangePruned(from, to types.TS) (
tree *DirtyTreeEntry) {
tree, _ = d.ScanInRange(from, to)
ctx := context.WithValue(context.Background(), TempFKey{}, 42)
if err := d.tryCompactTree(ctx, d.interceptor, tree.tree, from, to); err != nil {
panic(err)
}
return
}
func (d *dirtyCollector) GetMaxLSN(from, to types.TS) uint64 {
reader := d.sourcer.GetReader(from, to)
return reader.GetMaxLSN()
}
func (d *dirtyCollector) ScanInRange(from, to types.TS) (
entry *DirtyTreeEntry, count int) {
reader := d.sourcer.GetReader(from, to)
tree, count := reader.GetDirty()
// make a entry
entry = &DirtyTreeEntry{
start: from,
end: to,
tree: tree,
}
return
}
func (d *dirtyCollector) IsCommitted(from, to types.TS) bool {
reader := d.sourcer.GetReader(from, to)
return reader.IsCommitted()
}
// DirtyCount returns unflushed table, Object, block count
func (d *dirtyCollector) DirtyCount() (tblCnt, objCnt, blkCnt int) {
merged := d.GetAndRefreshMerged()
tblCnt = merged.tree.TableCount()
for _, tblTree := range merged.tree.Tables {
objCnt += len(tblTree.Objs)
for _, objTree := range tblTree.Objs {
blkCnt += len(objTree.Blks)
}
}
return
}
func (d *dirtyCollector) String() string {
merged := d.GetAndRefreshMerged()
return merged.tree.String()
}
func (d *dirtyCollector) GetAndRefreshMerged() (merged *DirtyTreeEntry) {
merged = d.merged.Load()
d.storage.RLock()
maxTs := d.storage.maxTs
d.storage.RUnlock()
if maxTs.LessEq(merged.end) {
return
}
merged = d.Merge()
d.tryUpdateMerged(merged)
return
}
func (d *dirtyCollector) Merge() *DirtyTreeEntry {
// get storage snapshot and work on it
snapshot, maxTs := d.getStorageSnapshot()
merged := NewEmptyDirtyTreeEntry()
merged.end = maxTs
// scan base on the snapshot
// merge all trees of the entry
snapshot.Scan(func(entry *DirtyTreeEntry) bool {
entry.RLock()
defer entry.RUnlock()
merged.tree.Merge(entry.tree)
return true
})
return merged
}
func (d *dirtyCollector) tryUpdateMerged(merged *DirtyTreeEntry) (updated bool) {
var old *DirtyTreeEntry
for {
old = d.merged.Load()
if old.end.GreaterEq(merged.end) {
break
}
if d.merged.CompareAndSwap(old, merged) {
updated = true
break
}
}
return
}
func (d *dirtyCollector) findRange(lagDuration time.Duration) (from, to types.TS) {
now := d.clock.Alloc()
// a deliberate lag is made here for flushing and checkpoint to
// avoid fierce competition on the very new ablock, whose PrepareCompact probably
// returns false
lag := types.BuildTS(now.Physical()-int64(lagDuration), now.Logical())
d.storage.RLock()
defer d.storage.RUnlock()
if lag.LessEq(d.storage.maxTs) {
return
}
from, to = d.storage.maxTs.Next(), lag
return
}
func (d *dirtyCollector) rangeScanAndUpdate(from, to types.TS) (updated bool) {
entry, _ := d.ScanInRange(from, to)
// try to store the entry
updated = d.tryStoreEntry(entry)
return
}
func (d *dirtyCollector) tryStoreEntry(entry *DirtyTreeEntry) (ok bool) {
ok = true
d.storage.Lock()
defer d.storage.Unlock()
// storage was updated before
if !entry.start.Equal(d.storage.maxTs.Next()) {
ok = false
return
}
// update storage maxTs
d.storage.maxTs = entry.end
// don't store empty entry
if entry.tree.IsEmpty() {
return
}
d.storage.entries.Set(entry)
return
}
func (d *dirtyCollector) getStorageSnapshot() (ss *btree.BTreeG[*DirtyTreeEntry], ts types.TS) {
d.storage.Lock()
defer d.storage.Unlock()
ss = d.storage.entries.Copy()
ts = d.storage.maxTs
return
}
// Scan current dirty entries, remove all flushed or not found ones, and drive interceptor on remaining block entries.
func (d *dirtyCollector) cleanupStorage() {
toDeletes := make([]*DirtyTreeEntry, 0)
// get a snapshot of entries
entries, _ := d.getStorageSnapshot()
// scan all entries in the storage
// try compact the dirty tree for each entry
// if the dirty tree is empty, delete the specified entry from the storage
entries.Scan(func(entry *DirtyTreeEntry) bool {
entry.Lock()
defer entry.Unlock()
// dirty blocks within the time range has been flushed
// exclude the related dirty tree from the foreset
if entry.tree.IsEmpty() {
toDeletes = append(toDeletes, entry)
return true
}
if err := d.tryCompactTree(context.Background(), d.interceptor, entry.tree, entry.start, entry.end); err != nil {
logutil.Warnf("error: interceptor on dirty tree: %v", err)
}
if entry.tree.IsEmpty() {
toDeletes = append(toDeletes, entry)
}
return true
})
if len(toDeletes) == 0 {
return
}
// remove entries with empty dirty tree from the storage
d.storage.Lock()
defer d.storage.Unlock()
for _, tree := range toDeletes {
d.storage.entries.Delete(tree)
}
}
// iter the tree and call interceptor to process block. flushed block, empty obj and table will be removed from the tree
func (d *dirtyCollector) tryCompactTree(
ctx context.Context,
interceptor DirtyEntryInterceptor,
tree *model.Tree, from, to types.TS) (err error) {
var (
db *catalog.DBEntry
tbl *catalog.TableEntry
obj *catalog.ObjectEntry
blk *catalog.BlockEntry
)
for id, dirtyTable := range tree.Tables {
// remove empty tables
if dirtyTable.Compact() {
tree.Shrink(id)
continue
}
if db, err = d.catalog.GetDatabaseByID(dirtyTable.DbID); err != nil {
if moerr.IsMoErrCode(err, moerr.OkExpectedEOB) {
tree.Shrink(id)
err = nil
continue
}
break
}
if tbl, err = db.GetTableEntryByID(dirtyTable.ID); err != nil {
if moerr.IsMoErrCode(err, moerr.OkExpectedEOB) {
tree.Shrink(id)
err = nil
continue
}
break
}
tbl.Stats.RLock()
lastFlush := tbl.Stats.LastFlush
if lastFlush.GreaterEq(to) {
tree.Shrink(id)
tbl.Stats.RUnlock()
continue
}
tbl.Stats.RUnlock()
if x := ctx.Value(TempFKey{}); x != nil && TempF.Check(tbl.ID) {
logutil.Infof("temp filter skip table %v-%v", tbl.ID, tbl.GetLastestSchema().Name)
tree.Shrink(id)
continue
}
for id, dirtyObj := range dirtyTable.Objs {
// remove empty objs
if dirtyObj.IsEmpty() {
dirtyTable.Shrink(id)
continue
}
if obj, err = tbl.GetObjectByID(dirtyObj.ID); err != nil {
if moerr.IsMoErrCode(err, moerr.OkExpectedEOB) {
dirtyTable.Shrink(id)
err = nil
continue
}
return
}
for id := range dirtyObj.Blks {
bid := objectio.NewBlockidWithObjectID(dirtyObj.ID, id)
if blk, err = obj.GetBlockEntryByID(bid); err != nil {
if moerr.IsMoErrCode(err, moerr.OkExpectedEOB) {
dirtyObj.Shrink(bid)
err = nil
continue
}
return
}
if blk.GetBlockData().RunCalibration() == 0 {
// TODO: may be put it to post replay process
// FIXME
if blk.HasPersistedData() {
blk.GetBlockData().TryUpgrade()
}
dirtyObj.Shrink(bid)
continue
}
if !blk.IsAppendable() {
newFrom := from
if lastFlush.Greater(newFrom) {
newFrom = lastFlush
}
// sometimes, delchain is no cleared after flushing table tail.
// the reason is still unknown, but here bumping the check from ts to lastFlush is correct anyway.
found, _ := blk.GetBlockData().HasDeleteIntentsPreparedIn(newFrom, to)
if !found {
dirtyObj.Shrink(bid)
continue
}
}
if err = interceptor.OnBlock(blk); err != nil {
return
}
}
}
}
tree.Compact()
return
}