github.com/matrixorigin/matrixone@v1.2.0/pkg/vm/engine/tae/logstore/store/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 store
    16  
    17  import (
    18  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/logstore/driver/entry"
    19  )
    20  
    21  func (w *StoreImpl) Replay(h ApplyHandle) error {
    22  	err := w.driver.Replay(func(e *entry.Entry) {
    23  		err := w.replayEntry(e, h)
    24  		if err != nil {
    25  			panic(err)
    26  		}
    27  	})
    28  	if err != nil {
    29  		panic(err)
    30  	}
    31  	lsn, err := w.driver.GetTruncated()
    32  	if err != nil {
    33  		panic(err)
    34  	}
    35  	w.StoreInfo.onCheckpoint()
    36  	w.driverCheckpointed = lsn
    37  	w.driverCheckpointing.Store(lsn)
    38  	for g, lsn := range w.syncing {
    39  		w.walCurrentLsn[g] = lsn
    40  		w.synced[g] = lsn
    41  	}
    42  	for g, ckped := range w.checkpointed {
    43  		if w.walCurrentLsn[g] == 0 {
    44  			w.walCurrentLsn[g] = ckped
    45  			w.synced[g] = ckped
    46  		}
    47  		if w.minLsn[g] <= w.driverCheckpointed {
    48  			minLsn := w.minLsn[g]
    49  			for ; minLsn <= ckped+1; minLsn++ {
    50  				drLsn, err := w.getDriverLsn(g, minLsn)
    51  				if err == nil && drLsn > w.driverCheckpointed {
    52  					break
    53  				}
    54  			}
    55  			w.minLsn[g] = minLsn
    56  		}
    57  	}
    58  	return nil
    59  }
    60  
    61  func (w *StoreImpl) onReplayLsn(g uint32, lsn uint64) {
    62  	_, ok := w.minLsn[g]
    63  	if !ok {
    64  		w.minLsn[g] = lsn
    65  	}
    66  }
    67  
    68  func (w *StoreImpl) replayEntry(e *entry.Entry, h ApplyHandle) error {
    69  	walEntry := e.Entry
    70  	info := e.Info
    71  	switch info.Group {
    72  	case GroupInternal:
    73  		w.unmarshalPostCommitEntry(walEntry.GetPayload())
    74  		w.checkpointed[GroupCKP] = info.TargetLsn
    75  		return nil
    76  	case GroupCKP:
    77  		w.logCheckpointInfo(info)
    78  	}
    79  	w.logDriverLsn(e)
    80  	w.onReplayLsn(info.Group, info.GroupLSN)
    81  	h(info.Group, info.GroupLSN, walEntry.GetPayload(), walEntry.GetType(), walEntry.GetInfo())
    82  	return nil
    83  }