github.com/matrixorigin/matrixone@v0.7.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 } 64 65 func (d *LogServiceDriver) truncateLogservice(lsn uint64) { 66 client, err := d.clientPool.Get() 67 if err == ErrClientPoolClosed { 68 return 69 } 70 if err != nil { 71 panic(err) 72 } 73 defer d.clientPool.Put(client) 74 ctx, cancel := context.WithTimeout(context.Background(), d.config.TruncateDuration) 75 err = client.c.Truncate(ctx, lsn) 76 cancel() 77 if err != nil { 78 err = RetryWithTimeout(d.config.RetryTimeout, func() (shouldReturn bool) { 79 logutil.Infof("LogService Driver: retry truncate, err is %v", err) 80 ctx, cancel := context.WithTimeout(context.Background(), d.config.TruncateDuration) 81 err = client.c.Truncate(ctx, lsn) 82 cancel() 83 return err == nil 84 }) 85 if err != nil { 86 panic(err) 87 } 88 } 89 logutil.Infof("LogService Driver: Truncate %d", lsn) 90 } 91 func (d *LogServiceDriver) getLogserviceTruncate() (lsn uint64) { 92 client, err := d.clientPool.Get() 93 if err == ErrClientPoolClosed { 94 return 95 } 96 if err != nil { 97 panic(err) 98 } 99 defer d.clientPool.Put(client) 100 ctx, cancel := context.WithTimeout(context.Background(), d.config.GetTruncateDuration) 101 lsn, err = client.c.GetTruncatedLsn(ctx) 102 cancel() 103 if err != nil { 104 err = RetryWithTimeout(d.config.RetryTimeout, func() (shouldReturn bool) { 105 logutil.Infof("LogService Driver: retry gettruncate, err is %v", err) 106 ctx, cancel := context.WithTimeout(context.Background(), d.config.GetTruncateDuration) 107 lsn, err = client.c.GetTruncatedLsn(ctx) 108 cancel() 109 return err == nil 110 }) 111 if err != nil { 112 panic(err) 113 } 114 } 115 logutil.Infof("Logservice Driver: Get Truncate %d", lsn) 116 return 117 }