github.com/matrixorigin/matrixone@v0.7.0/pkg/vm/engine/tae/logstore/samples/replay/main.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 main
    16  
    17  import (
    18  	"bytes"
    19  	"os"
    20  	"time"
    21  
    22  	"github.com/matrixorigin/matrixone/pkg/logutil"
    23  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/common"
    24  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/logstore/entry"
    25  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/logstore/store"
    26  )
    27  
    28  var sampleDir = "/tmp/logstoreSample/replay"
    29  var name = "replay"
    30  
    31  func init() {
    32  	os.RemoveAll(sampleDir)
    33  }
    34  
    35  func main() {
    36  	s := store.NewStoreWithBatchStoreDriver(sampleDir, name, nil)
    37  	var bs bytes.Buffer
    38  	for i := 0; i < 3000; i++ {
    39  		bs.WriteString("helloyou")
    40  	}
    41  	buf := bs.Bytes()
    42  
    43  	for i := 0; i < 5000; i++ {
    44  		tid := string(common.NewTxnIDAllocator().Alloc())
    45  		e1 := entry.GetBase()
    46  		uncommitInfo := &entry.Info{
    47  			Group:     10,
    48  			Uncommits: tid,
    49  		}
    50  		e1.SetType(entry.ETUncommitted)
    51  		e1.SetInfo(uncommitInfo)
    52  		n, err := common.LogAllocator.Alloc(common.K * 100)
    53  		if err != nil {
    54  			panic(err)
    55  		}
    56  		copy(n, buf)
    57  		err = e1.UnmarshalFromNode(n, true)
    58  		if err != nil {
    59  			panic(err)
    60  		}
    61  		_, err = s.Append(10, e1)
    62  		if err != nil {
    63  			panic(err)
    64  		}
    65  
    66  		txnInfo := &entry.Info{
    67  			Group: 11,
    68  			TxnId: tid,
    69  		}
    70  		e2 := entry.GetBase()
    71  		e2.SetType(entry.ETTxn)
    72  		e2.SetInfo(txnInfo)
    73  		n, err = common.LogAllocator.Alloc(common.K * 100)
    74  		if err != nil {
    75  			panic(err)
    76  		}
    77  		copy(n, buf)
    78  		err = e2.UnmarshalFromNode(n, true)
    79  		if err != nil {
    80  			panic(err)
    81  		}
    82  		cmtLsn, err := s.Append(11, e2)
    83  		if err != nil {
    84  			panic(err)
    85  		}
    86  
    87  		cmd := entry.CommandInfo{
    88  			Size:       2,
    89  			CommandIds: []uint32{0, 1},
    90  		}
    91  		cmds := make(map[uint64]entry.CommandInfo)
    92  		cmds[cmtLsn] = cmd
    93  		info := &entry.Info{
    94  			Group: entry.GTCKp,
    95  			Checkpoints: []*entry.CkpRanges{{
    96  				Group:   11,
    97  				Command: cmds,
    98  			}},
    99  		}
   100  		e3 := entry.GetBase()
   101  		e3.SetType(entry.ETCheckpoint)
   102  		e3.SetInfo(info)
   103  		_, err = s.Append(entry.GTCKp, e3)
   104  		if err != nil {
   105  			panic(err)
   106  		}
   107  		err = e3.WaitDone()
   108  		if err != nil {
   109  			panic(err)
   110  		}
   111  		err = e1.WaitDone()
   112  		if err != nil {
   113  			panic(err)
   114  		}
   115  		e1.Free()
   116  		e2.Free()
   117  		e3.Free()
   118  	}
   119  
   120  	err := s.Close()
   121  	if err != nil {
   122  		panic(err)
   123  	}
   124  
   125  	t0 := time.Now()
   126  
   127  	s = store.NewStoreWithBatchStoreDriver(sampleDir, name, nil)
   128  	a := func(group uint32, commitId uint64, payload []byte, typ uint16, info any) {
   129  		// fmt.Printf("%s", payload)
   130  	}
   131  	err = s.Replay(a)
   132  	if err != nil {
   133  		panic(err)
   134  	}
   135  
   136  	logutil.Infof("Open and replay takes %v", time.Since(t0))
   137  }