github.com/matrixorigin/matrixone@v1.2.0/pkg/vm/engine/tae/logstore/driver/logservicedriver/truncate.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 logservicedriver 16 17 import ( 18 "context" 19 20 "github.com/matrixorigin/matrixone/pkg/logutil" 21 // "time" 22 ) 23 24 // driver lsn -> entry lsn 25 func (d *LogServiceDriver) Truncate(lsn uint64) error { 26 if lsn > d.truncating.Load() { 27 d.truncating.Store(lsn) 28 } 29 _, err := d.truncateQueue.Enqueue(struct{}{}) 30 if err != nil { 31 panic(err) 32 } 33 return nil 34 } 35 36 func (d *LogServiceDriver) GetTruncated() (lsn uint64, err error) { 37 lsn = d.truncating.Load() 38 return 39 } 40 41 func (d *LogServiceDriver) onTruncate(items ...any) { 42 d.doTruncate() 43 } 44 45 func (d *LogServiceDriver) doTruncate() { 46 target := d.truncating.Load() 47 lastServiceLsn := d.truncatedLogserviceLsn 48 lsn := lastServiceLsn 49 //TODO use valid lsn 50 next := d.getNextValidLogserviceLsn(lsn) 51 for d.isToTruncate(next, target) { 52 lsn = next 53 next = d.getNextValidLogserviceLsn(lsn) 54 if next <= lsn { 55 break 56 } 57 } 58 if lsn == lastServiceLsn { 59 return 60 } 61 d.truncateLogservice(lsn) 62 d.truncatedLogserviceLsn = lsn 63 d.gcAddr(lsn) 64 } 65 66 func (d *LogServiceDriver) truncateLogservice(lsn uint64) { 67 client, err := d.clientPool.Get() 68 if err == ErrClientPoolClosed { 69 return 70 } 71 if err != nil { 72 panic(err) 73 } 74 defer d.clientPool.Put(client) 75 ctx, cancel := context.WithTimeout(context.Background(), d.config.TruncateDuration) 76 err = client.c.Truncate(ctx, lsn) 77 cancel() 78 if err != nil { 79 err = RetryWithTimeout(d.config.RetryTimeout, func() (shouldReturn bool) { 80 logutil.Infof("LogService Driver: retry truncate, err is %v", err) 81 ctx, cancel := context.WithTimeout(context.Background(), d.config.TruncateDuration) 82 err = client.c.Truncate(ctx, lsn) 83 cancel() 84 return err == nil 85 }) 86 if err != nil { 87 panic(err) 88 } 89 } 90 logutil.Infof("LogService Driver: Truncate %d", lsn) 91 } 92 func (d *LogServiceDriver) getLogserviceTruncate() (lsn uint64) { 93 client, err := d.clientPool.Get() 94 if err == ErrClientPoolClosed { 95 return 96 } 97 if err != nil { 98 panic(err) 99 } 100 defer d.clientPool.Put(client) 101 ctx, cancel := context.WithTimeout(context.Background(), d.config.GetTruncateDuration) 102 lsn, err = client.c.GetTruncatedLsn(ctx) 103 cancel() 104 if err != nil { 105 err = RetryWithTimeout(d.config.RetryTimeout, func() (shouldReturn bool) { 106 logutil.Infof("LogService Driver: retry gettruncate, err is %v", err) 107 ctx, cancel := context.WithTimeout(context.Background(), d.config.GetTruncateDuration) 108 lsn, err = client.c.GetTruncatedLsn(ctx) 109 cancel() 110 return err == nil 111 }) 112 if err != nil { 113 panic(err) 114 } 115 } 116 logutil.Infof("Logservice Driver: Get Truncate %d", lsn) 117 return 118 }