github.com/matrixorigin/matrixone@v1.2.0/pkg/vm/engine/tae/logstore/driver/batchstoredriver/replay.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  	"time"
    20  
    21  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/common"
    22  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/logstore/driver"
    23  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/logstore/driver/entry"
    24  )
    25  
    26  // wal ckping
    27  
    28  type replayer struct {
    29  	version int
    30  	pos     int
    31  
    32  	applyEntry driver.ApplyHandle
    33  
    34  	//syncbase
    35  	addrs  map[int]*common.ClosedIntervals
    36  	maxlsn uint64
    37  	minlsn uint64
    38  
    39  	readDuration  time.Duration
    40  	applyDuration time.Duration
    41  }
    42  
    43  func newReplayer(h driver.ApplyHandle) *replayer {
    44  	return &replayer{
    45  		addrs:      make(map[int]*common.ClosedIntervals),
    46  		applyEntry: h,
    47  		minlsn:     math.MaxUint64,
    48  	}
    49  }
    50  
    51  func (r *replayer) updateaddrs(version int, lsn uint64) {
    52  	interval, ok := r.addrs[version]
    53  	if !ok {
    54  		interval = common.NewClosedIntervals()
    55  		r.addrs[version] = interval
    56  	}
    57  	interval.TryMerge(*common.NewClosedIntervalsByInt(lsn))
    58  }
    59  
    60  func (r *replayer) updateGroupLSN(lsn uint64) {
    61  	if lsn > r.maxlsn {
    62  		r.maxlsn = lsn
    63  	}
    64  	if lsn < r.minlsn {
    65  		r.minlsn = lsn
    66  	}
    67  }
    68  
    69  func (r *replayer) onReplayEntry(e *entry.Entry) error {
    70  	e.SetInfo()
    71  	info := e.Info
    72  	if info == nil {
    73  		return nil
    74  	}
    75  	r.updateaddrs(r.version, e.Lsn)
    76  	r.updateGroupLSN(e.Lsn)
    77  	return nil
    78  }
    79  
    80  func (r *replayer) replayHandler(vfile *vFile) error {
    81  	if vfile.version != r.version {
    82  		r.pos = 0
    83  		r.version = vfile.version
    84  	}
    85  	e := entry.NewEmptyEntry()
    86  	t0 := time.Now()
    87  	_, err := e.ReadAt(vfile.File, r.pos)
    88  	r.readDuration += time.Since(t0)
    89  	if err != nil {
    90  		return err
    91  	}
    92  	if err := r.onReplayEntry(e); err != nil {
    93  		return err
    94  	}
    95  	vfile.onReplay(r.pos, e.Lsn)
    96  	t0 = time.Now()
    97  	r.applyEntry(e)
    98  	r.applyDuration += time.Since(t0)
    99  	r.pos += e.GetSize()
   100  	e.Entry.Free()
   101  	return nil
   102  }