github.com/matrixorigin/matrixone@v0.7.0/pkg/txn/client/client.go (about)

     1  // Copyright 2022 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 client
    16  
    17  import (
    18  	"github.com/matrixorigin/matrixone/pkg/common/runtime"
    19  	"github.com/matrixorigin/matrixone/pkg/pb/txn"
    20  	"github.com/matrixorigin/matrixone/pkg/txn/rpc"
    21  )
    22  
    23  // WithTxnIDGenerator setup txn id generator
    24  func WithTxnIDGenerator(generator TxnIDGenerator) TxnClientCreateOption {
    25  	return func(tc *txnClient) {
    26  		tc.generator = generator
    27  	}
    28  }
    29  
    30  var _ TxnClient = (*txnClient)(nil)
    31  
    32  type txnClient struct {
    33  	rt        runtime.Runtime
    34  	sender    rpc.TxnSender
    35  	generator TxnIDGenerator
    36  }
    37  
    38  // NewTxnClient create a txn client with TxnSender and Options
    39  func NewTxnClient(
    40  	rt runtime.Runtime,
    41  	sender rpc.TxnSender,
    42  	options ...TxnClientCreateOption) TxnClient {
    43  	c := &txnClient{rt: rt, sender: sender}
    44  	for _, opt := range options {
    45  		opt(c)
    46  	}
    47  	c.adjust()
    48  	return c
    49  }
    50  
    51  func (client *txnClient) adjust() {
    52  	if client.generator == nil {
    53  		client.generator = newUUIDTxnIDGenerator()
    54  	}
    55  	if client.rt.Clock() == nil {
    56  		panic("txn clock not set")
    57  	}
    58  }
    59  
    60  func (client *txnClient) New(options ...TxnOption) (TxnOperator, error) {
    61  	txnMeta := txn.TxnMeta{}
    62  	txnMeta.ID = client.generator.Generate()
    63  
    64  	now, _ := client.rt.Clock().Now()
    65  	// TODO: Consider how to handle clock offsets. If use Clock-SI, can use the current
    66  	// time minus the maximum clock offset as the transaction's snapshotTimestamp to avoid
    67  	// conflicts due to clock uncertainty.
    68  	txnMeta.SnapshotTS = now
    69  	options = append(options, WithTxnCNCoordinator())
    70  	return newTxnOperator(client.rt, client.sender, txnMeta, options...), nil
    71  }
    72  
    73  func (client *txnClient) NewWithSnapshot(snapshot []byte) (TxnOperator, error) {
    74  	return newTxnOperatorWithSnapshot(client.rt, client.sender, snapshot)
    75  }
    76  
    77  func (client *txnClient) Close() error {
    78  	return client.sender.Close()
    79  }