github.com/matrixorigin/matrixone@v1.2.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  	"errors"
    20  
    21  	"github.com/matrixorigin/matrixone/pkg/common/runtime"
    22  	"github.com/matrixorigin/matrixone/pkg/pb/metadata"
    23  	"github.com/matrixorigin/matrixone/pkg/pb/timestamp"
    24  	"github.com/matrixorigin/matrixone/pkg/pb/txn"
    25  	"github.com/matrixorigin/matrixone/pkg/txn/storage"
    26  	"github.com/matrixorigin/matrixone/pkg/util/status"
    27  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/iface/rpchandle"
    28  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/logtail"
    29  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/logtail/service"
    30  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/options"
    31  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/rpc"
    32  )
    33  
    34  type taeStorage struct {
    35  	shard         metadata.TNShard
    36  	taeHandler    rpchandle.Handler
    37  	logtailServer *service.LogtailServer
    38  }
    39  
    40  var _ storage.TxnStorage = (*taeStorage)(nil)
    41  
    42  func NewTAEStorage(
    43  	ctx context.Context,
    44  	dataDir string,
    45  	opt *options.Options,
    46  	shard metadata.TNShard,
    47  	rt runtime.Runtime,
    48  	logtailServerAddr string,
    49  	logtailServerCfg *options.LogtailServerCfg,
    50  ) (storage.TxnStorage, error) {
    51  
    52  	taeHandler := rpc.NewTAEHandle(ctx, dataDir, opt)
    53  	tae := taeHandler.GetDB()
    54  	logtailer := logtail.NewLogtailer(ctx, tae.BGCheckpointRunner, tae.LogtailMgr, tae.Catalog)
    55  	server, err := service.NewLogtailServer(logtailServerAddr, logtailServerCfg, logtailer, rt)
    56  	if err != nil {
    57  		return nil, err
    58  	}
    59  
    60  	ss, ok := runtime.ProcessLevelRuntime().GetGlobalVariables(runtime.StatusServer)
    61  	if ok {
    62  		ss.(*status.Server).SetLogtailServer(server)
    63  	}
    64  
    65  	return &taeStorage{
    66  		shard:         shard,
    67  		taeHandler:    taeHandler,
    68  		logtailServer: server,
    69  	}, nil
    70  }
    71  
    72  // Start starts logtail push service.
    73  func (s *taeStorage) Start() error {
    74  	return s.logtailServer.Start()
    75  }
    76  
    77  // Close implements storage.TxnTAEStorage
    78  func (s *taeStorage) Close(ctx context.Context) error {
    79  	return errors.Join(s.logtailServer.Close(), s.taeHandler.HandleClose(ctx))
    80  }
    81  
    82  // Commit implements storage.TxnTAEStorage
    83  func (s *taeStorage) Commit(ctx context.Context, txnMeta txn.TxnMeta) (timestamp.Timestamp, error) {
    84  	return s.taeHandler.HandleCommit(ctx, txnMeta)
    85  }
    86  
    87  // Committing implements storage.TxnTAEStorage
    88  func (s *taeStorage) Committing(ctx context.Context, txnMeta txn.TxnMeta) error {
    89  	return s.taeHandler.HandleCommitting(ctx, txnMeta)
    90  }
    91  
    92  // Destroy implements storage.TxnTAEStorage
    93  func (s *taeStorage) Destroy(ctx context.Context) error {
    94  	return s.taeHandler.HandleDestroy(ctx)
    95  }
    96  
    97  // Prepare implements storage.TxnTAEStorage
    98  func (s *taeStorage) Prepare(ctx context.Context, txnMeta txn.TxnMeta) (timestamp.Timestamp, error) {
    99  	return s.taeHandler.HandlePrepare(ctx, txnMeta)
   100  }
   101  
   102  // Rollback implements storage.TxnTAEStorage
   103  func (s *taeStorage) Rollback(ctx context.Context, txnMeta txn.TxnMeta) error {
   104  	return s.taeHandler.HandleRollback(ctx, txnMeta)
   105  }
   106  
   107  // StartRecovery implements storage.TxnTAEStorage
   108  func (s *taeStorage) StartRecovery(ctx context.Context, ch chan txn.TxnMeta) {
   109  	s.taeHandler.HandleStartRecovery(ctx, ch)
   110  }