github.com/matrixorigin/matrixone@v1.2.0/pkg/vm/engine/tae/txn/txnimpl/cmdmgr.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 txnimpl
    16  
    17  import (
    18  	"time"
    19  
    20  	"github.com/matrixorigin/matrixone/pkg/logutil"
    21  	"go.uber.org/zap"
    22  
    23  	// "github.com/matrixorigin/matrixone/pkg/vm/engine/tae/common"
    24  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/iface/txnif"
    25  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/logstore/entry"
    26  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/txn/txnbase"
    27  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/wal"
    28  )
    29  
    30  type commandManager struct {
    31  	cmd    *txnbase.TxnCmd
    32  	lsn    uint64
    33  	csn    uint32
    34  	driver wal.Driver
    35  }
    36  
    37  func newCommandManager(driver wal.Driver, maxMessageSize uint64) *commandManager {
    38  	return &commandManager{
    39  		cmd:    txnbase.NewTxnCmd(maxMessageSize),
    40  		driver: driver,
    41  	}
    42  }
    43  
    44  func (mgr *commandManager) GetCSN() uint32 {
    45  	return mgr.csn
    46  }
    47  
    48  func (mgr *commandManager) AddInternalCmd(cmd txnif.TxnCmd) {
    49  	mgr.cmd.AddCmd(cmd)
    50  }
    51  
    52  func (mgr *commandManager) AddCmd(cmd txnif.TxnCmd) {
    53  	mgr.cmd.AddCmd(cmd)
    54  	mgr.csn++
    55  }
    56  
    57  func (mgr *commandManager) ApplyTxnRecord(txn txnif.AsyncTxn) (logEntry entry.Entry, err error) {
    58  	if mgr.driver == nil {
    59  		return
    60  	}
    61  	t1 := time.Now()
    62  	mgr.cmd.SetTxn(txn)
    63  	var buf []byte
    64  	if buf, err = mgr.cmd.MarshalBinary(); err != nil {
    65  		return
    66  	}
    67  	// logutil.Info("", common.OperationField("suxi-replay-cmd"),
    68  	// common.OperandField(mgr.cmd.Desc()))
    69  	logEntry = entry.GetBase()
    70  	logEntry.SetType(IOET_WALEntry_TxnRecord)
    71  	if err = logEntry.SetPayload(buf); err != nil {
    72  		return
    73  	}
    74  	info := &entry.Info{
    75  		Group: wal.GroupPrepare,
    76  	}
    77  	logEntry.SetInfo(info)
    78  	t2 := time.Now()
    79  	mgr.lsn, err = mgr.driver.AppendEntry(wal.GroupPrepare, logEntry)
    80  	t3 := time.Now()
    81  	if t3.Sub(t1) > time.Millisecond*500 {
    82  		logutil.Warn(
    83  			"SLOW-LOG",
    84  			zap.String("txn", txn.String()),
    85  			zap.Duration("make-log-entry-duration", t3.Sub(t1)),
    86  		)
    87  	}
    88  	if t3.Sub(t2) > time.Millisecond*20 {
    89  		logutil.Warn(
    90  			"SLOW-LOG",
    91  			zap.Duration("append-log-entry-duration", t3.Sub(t1)),
    92  			zap.String("txn", txn.String()),
    93  		)
    94  	}
    95  	logutil.Debugf("ApplyTxnRecord LSN=%d, Size=%d", mgr.lsn, len(buf))
    96  	return
    97  }