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

     1  // Copyright 2021 - 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 taestorage
    16  
    17  import (
    18  	"context"
    19  
    20  	"go.uber.org/multierr"
    21  
    22  	"github.com/matrixorigin/matrixone/pkg/common/runtime"
    23  	"github.com/matrixorigin/matrixone/pkg/fileservice"
    24  	"github.com/matrixorigin/matrixone/pkg/logservice"
    25  	"github.com/matrixorigin/matrixone/pkg/pb/metadata"
    26  	"github.com/matrixorigin/matrixone/pkg/pb/timestamp"
    27  	"github.com/matrixorigin/matrixone/pkg/pb/txn"
    28  	"github.com/matrixorigin/matrixone/pkg/txn/storage"
    29  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/iface/rpchandle"
    30  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/logstore/driver/logservicedriver"
    31  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/logtail"
    32  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/logtail/service"
    33  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/options"
    34  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/rpc"
    35  )
    36  
    37  type taeStorage struct {
    38  	shard         metadata.DNShard
    39  	taeHandler    rpchandle.Handler
    40  	logtailServer *service.LogtailServer
    41  }
    42  
    43  var _ storage.TxnStorage = (*taeStorage)(nil)
    44  
    45  func NewTAEStorage(
    46  	dataDir string,
    47  	shard metadata.DNShard,
    48  	factory logservice.ClientFactory,
    49  	fs fileservice.FileService,
    50  	rt runtime.Runtime,
    51  	ckpCfg *options.CheckpointCfg,
    52  	logtailServerAddr string,
    53  	logtailServerCfg *options.LogtailServerCfg,
    54  	logStore options.LogstoreType,
    55  ) (*taeStorage, error) {
    56  	opt := &options.Options{
    57  		Clock:         rt.Clock(),
    58  		Fs:            fs,
    59  		Lc:            logservicedriver.LogServiceClientFactory(factory),
    60  		Shard:         shard,
    61  		CheckpointCfg: ckpCfg,
    62  		LogStoreT:     logStore,
    63  	}
    64  
    65  	taeHandler := rpc.NewTAEHandle(dataDir, opt)
    66  	tae := taeHandler.GetTxnEngine().GetTAE(context.Background())
    67  	logtailer := logtail.NewLogtailer(tae.BGCheckpointRunner, tae.LogtailMgr, tae.Catalog)
    68  	server, err := service.NewLogtailServer(logtailServerAddr, logtailServerCfg, logtailer, rt)
    69  	if err != nil {
    70  		return nil, err
    71  	}
    72  
    73  	return &taeStorage{
    74  		shard:         shard,
    75  		taeHandler:    taeHandler,
    76  		logtailServer: server,
    77  	}, nil
    78  }
    79  
    80  var _ storage.TxnStorage = new(taeStorage)
    81  
    82  // Start starts logtail push service.
    83  func (s *taeStorage) Start() error {
    84  	return s.logtailServer.Start()
    85  }
    86  
    87  // Close implements storage.TxnTAEStorage
    88  func (s *taeStorage) Close(ctx context.Context) error {
    89  	if err := s.logtailServer.Close(); err != nil {
    90  		return multierr.Append(err, s.taeHandler.HandleClose(ctx))
    91  	}
    92  	return s.taeHandler.HandleClose(ctx)
    93  }
    94  
    95  // Commit implements storage.TxnTAEStorage
    96  func (s *taeStorage) Commit(ctx context.Context, txnMeta txn.TxnMeta) error {
    97  	return s.taeHandler.HandleCommit(ctx, txnMeta)
    98  }
    99  
   100  // Committing implements storage.TxnTAEStorage
   101  func (s *taeStorage) Committing(ctx context.Context, txnMeta txn.TxnMeta) error {
   102  	return s.taeHandler.HandleCommitting(ctx, txnMeta)
   103  }
   104  
   105  // Destroy implements storage.TxnTAEStorage
   106  func (s *taeStorage) Destroy(ctx context.Context) error {
   107  	return s.taeHandler.HandleDestroy(ctx)
   108  }
   109  
   110  // Prepare implements storage.TxnTAEStorage
   111  func (s *taeStorage) Prepare(ctx context.Context, txnMeta txn.TxnMeta) (timestamp.Timestamp, error) {
   112  	return s.taeHandler.HandlePrepare(ctx, txnMeta)
   113  }
   114  
   115  // Rollback implements storage.TxnTAEStorage
   116  func (s *taeStorage) Rollback(ctx context.Context, txnMeta txn.TxnMeta) error {
   117  	return s.taeHandler.HandleRollback(ctx, txnMeta)
   118  }
   119  
   120  // StartRecovery implements storage.TxnTAEStorage
   121  func (s *taeStorage) StartRecovery(ctx context.Context, ch chan txn.TxnMeta) {
   122  	s.taeHandler.HandleStartRecovery(ctx, ch)
   123  }