github.com/matrixorigin/matrixone@v1.2.0/pkg/vm/engine/tae/logstore/driver/logservicedriver/driver_test.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  	"fmt"
    19  	"strconv"
    20  	"strings"
    21  	"testing"
    22  
    23  	"github.com/lni/vfs"
    24  	"github.com/matrixorigin/matrixone/pkg/common/runtime"
    25  	"github.com/matrixorigin/matrixone/pkg/logservice"
    26  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/logstore/driver/entry"
    27  
    28  	// "github.com/matrixorigin/matrixone/pkg/vm/engine/tae/logstore/entry"
    29  	"github.com/stretchr/testify/assert"
    30  )
    31  
    32  func initTest(t *testing.T) (*logservice.Service, *logservice.ClientConfig) {
    33  	runtime.SetupProcessLevelRuntime(runtime.DefaultRuntime())
    34  	fs := vfs.NewStrictMem()
    35  	service, ccfg, err := logservice.NewTestService(fs)
    36  	assert.NoError(t, err)
    37  	return service, &ccfg
    38  }
    39  
    40  func restartDriver(t *testing.T, d *LogServiceDriver, h func(*entry.Entry)) *LogServiceDriver {
    41  	assert.NoError(t, d.Close())
    42  	t.Log("Addr:")
    43  	// preAddr:=d.addr
    44  	for lsn, intervals := range d.addr {
    45  		t.Logf("%d %v", lsn, intervals)
    46  	}
    47  	// preLsns:=d.validLsn
    48  	t.Logf("Valid lsn: %v", d.validLsn)
    49  	t.Logf("Driver Lsn %d, Syncing %d, Synced %d", d.driverLsn, d.syncing, d.synced)
    50  	t.Logf("Truncated %d", d.truncating.Load())
    51  	t.Logf("LSTruncated %d", d.truncatedLogserviceLsn)
    52  	d = NewLogServiceDriver(d.config)
    53  	tempLsn := uint64(0)
    54  	err := d.Replay(func(e *entry.Entry) {
    55  		if e.Lsn <= tempLsn {
    56  			panic("logic err")
    57  		}
    58  		tempLsn = e.Lsn
    59  		if h != nil {
    60  			h(e)
    61  		}
    62  	})
    63  	assert.NoError(t, err)
    64  	t.Log("Addr:")
    65  	for lsn, intervals := range d.addr {
    66  		t.Logf("%d %v", lsn, intervals)
    67  	}
    68  	// assert.Equal(t,len(preAddr),len(d.addr))
    69  	// for lsn,intervals := range preAddr{
    70  	// 	replayedInterval,ok:=d.addr[lsn]
    71  	// 	assert.True(t,ok)
    72  	// 	assert.Equal(t,intervals.Intervals[0].Start,replayedInterval.Intervals[0].Start)
    73  	// 	assert.Equal(t,intervals.Intervals[0].End,replayedInterval.Intervals[0].End)
    74  	// }
    75  	t.Logf("Valid lsn: %v", d.validLsn)
    76  	// assert.Equal(t,preLsns.GetCardinality(),d.validLsn.GetCardinality())
    77  	t.Logf("Driver Lsn %d, Syncing %d, Synced %d", d.driverLsn, d.syncing, d.synced)
    78  	t.Logf("Truncated %d", d.truncating.Load())
    79  	t.Logf("LSTruncated %d", d.truncatedLogserviceLsn)
    80  	return d
    81  }
    82  
    83  func TestReplay1(t *testing.T) {
    84  	// t.Skip("debug")
    85  	service, ccfg := initTest(t)
    86  	defer service.Close()
    87  
    88  	cfg := NewTestConfig(ccfg)
    89  	driver := NewLogServiceDriver(cfg)
    90  
    91  	entryCount := 10000
    92  	entries := make([]*entry.Entry, entryCount)
    93  
    94  	for i := 0; i < entryCount; i++ {
    95  		payload := []byte(fmt.Sprintf("payload %d", i))
    96  		e := entry.MockEntryWithPayload(payload)
    97  		driver.Append(e)
    98  		entries[i] = e
    99  	}
   100  
   101  	for _, e := range entries {
   102  		e.WaitDone()
   103  	}
   104  
   105  	// i := 0
   106  	// h := func(e *entry.Entry) {
   107  	// 	payload := []byte(fmt.Sprintf("payload %d", i))
   108  	// 	assert.Equal(t, payload, e.Entry.GetPayload())
   109  	// 	i++
   110  	// }
   111  
   112  	driver = restartDriver(t, driver, nil)
   113  
   114  	for _, e := range entries {
   115  		e.Entry.Free()
   116  	}
   117  
   118  	driver.Close()
   119  }
   120  
   121  func TestReplay2(t *testing.T) {
   122  	t.Skip("debug")
   123  
   124  	service, ccfg := initTest(t)
   125  	defer service.Close()
   126  
   127  	cfg := NewTestConfig(ccfg)
   128  	cfg.RecordSize = 100
   129  	driver := NewLogServiceDriver(cfg)
   130  
   131  	entryCount := 10000
   132  	entries := make([]*entry.Entry, entryCount)
   133  
   134  	for i := 0; i < entryCount; i++ {
   135  		payload := []byte(fmt.Sprintf("payload %d", i))
   136  		e := entry.MockEntryWithPayload(payload)
   137  		driver.Append(e)
   138  		entries[i] = e
   139  	}
   140  
   141  	synced := driver.getSynced()
   142  	driver.Truncate(synced)
   143  
   144  	for i, e := range entries {
   145  		e.WaitDone()
   146  		assert.Equal(t, uint64(i+1), e.Lsn)
   147  	}
   148  
   149  	truncated, err := driver.GetTruncated()
   150  	i := truncated
   151  	t.Logf("truncate %d", i)
   152  	assert.NoError(t, err)
   153  	h := func(e *entry.Entry) {
   154  		entryPayload := e.Entry.GetPayload()
   155  		strs := strings.Split(string(entryPayload), " ")
   156  		id, err := strconv.Atoi(strs[1])
   157  		assert.NoError(t, err)
   158  		if id <= int(truncated) {
   159  			return
   160  		}
   161  
   162  		payload := []byte(fmt.Sprintf("payload %d", i))
   163  		assert.Equal(t, payload, entryPayload)
   164  		i++
   165  	}
   166  
   167  	driver = restartDriver(t, driver, h)
   168  
   169  	for _, e := range entries {
   170  		e.Entry.Free()
   171  	}
   172  
   173  	driver.Close()
   174  }