github.com/matrixorigin/matrixone@v1.2.0/pkg/vm/engine/tae/wal/driver.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 wal
    16  
    17  import (
    18  	"context"
    19  	"sync"
    20  	"time"
    21  
    22  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/logstore/driver/batchstoredriver"
    23  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/logstore/driver/logservicedriver"
    24  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/logstore/store"
    25  )
    26  
    27  type DriverConfig struct {
    28  	BatchStoreConfig   *batchstoredriver.StoreCfg
    29  	CheckpointDuration time.Duration
    30  }
    31  
    32  type walDriver struct {
    33  	sync.RWMutex
    34  	impl store.Store
    35  	own  bool
    36  
    37  	ckpDuration   time.Duration
    38  	cancelfn      context.CancelFunc
    39  	cancelContext context.Context
    40  	wg            sync.WaitGroup
    41  }
    42  
    43  func NewDriverWithLogservice(ctx context.Context, factory logservicedriver.LogServiceClientFactory) Driver {
    44  	ckpDuration := time.Second * 5
    45  	impl := store.NewStoreWithLogserviceDriver(factory)
    46  	driver := NewDriverWithStore(ctx, impl, true, ckpDuration)
    47  	return driver
    48  }
    49  
    50  func NewDriverWithBatchStore(ctx context.Context, dir, name string, cfg *DriverConfig) Driver {
    51  	var batchStoreCfg *batchstoredriver.StoreCfg
    52  	ckpDuration := time.Second * 5
    53  	if cfg != nil {
    54  		batchStoreCfg = cfg.BatchStoreConfig
    55  		ckpDuration = cfg.CheckpointDuration
    56  	}
    57  	impl := store.NewStoreWithBatchStoreDriver(dir, name, batchStoreCfg)
    58  	driver := NewDriverWithStore(ctx, impl, true, ckpDuration)
    59  	return driver
    60  }
    61  
    62  func NewDriverWithStore(ctx context.Context, impl store.Store, own bool, ckpDuration time.Duration) Driver {
    63  	if ckpDuration == 0 {
    64  		ckpDuration = time.Second
    65  	}
    66  	driver := &walDriver{
    67  		impl:        impl,
    68  		own:         own,
    69  		wg:          sync.WaitGroup{},
    70  		ckpDuration: ckpDuration,
    71  	}
    72  	driver.cancelContext, driver.cancelfn = context.WithCancel(ctx)
    73  	driver.wg.Add(1)
    74  	return driver
    75  }
    76  func (driver *walDriver) Start() {}
    77  func (driver *walDriver) GetCheckpointed() uint64 {
    78  	return driver.impl.GetCheckpointed(GroupPrepare)
    79  }
    80  func (driver *walDriver) replayhandle(handle store.ApplyHandle) store.ApplyHandle {
    81  	return func(group uint32, commitId uint64, payload []byte, typ uint16, info any) {
    82  		handle(group, commitId, payload, typ, nil)
    83  	}
    84  }
    85  func (driver *walDriver) Replay(handle store.ApplyHandle) error {
    86  	return driver.impl.Replay(driver.replayhandle(handle))
    87  }
    88  
    89  func (driver *walDriver) GetPenddingCnt() uint64 {
    90  	return driver.impl.GetPendding(GroupPrepare)
    91  }
    92  
    93  func (driver *walDriver) GetCurrSeqNum() uint64 {
    94  	return driver.impl.GetCurrSeqNum(GroupPrepare)
    95  }
    96  
    97  func (driver *walDriver) LoadEntry(groupID uint32, lsn uint64) (LogEntry, error) {
    98  	return driver.impl.Load(groupID, lsn)
    99  }
   100  
   101  func (driver *walDriver) AppendEntry(group uint32, e LogEntry) (uint64, error) {
   102  	id, err := driver.impl.Append(group, e)
   103  	return id, err
   104  }
   105  
   106  func (driver *walDriver) Close() error {
   107  	driver.cancelfn()
   108  	if driver.own {
   109  		return driver.impl.Close()
   110  	}
   111  	driver.wg.Wait()
   112  	return nil
   113  }