github.com/matrixorigin/matrixone@v1.2.0/pkg/tnservice/factory.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 tnservice
    16  
    17  import (
    18  	"context"
    19  	"math"
    20  
    21  	"github.com/matrixorigin/matrixone/pkg/common/moerr"
    22  	"github.com/matrixorigin/matrixone/pkg/common/mpool"
    23  	"github.com/matrixorigin/matrixone/pkg/defines"
    24  	"github.com/matrixorigin/matrixone/pkg/fileservice"
    25  	"github.com/matrixorigin/matrixone/pkg/logservice"
    26  	"github.com/matrixorigin/matrixone/pkg/pb/metadata"
    27  	"github.com/matrixorigin/matrixone/pkg/txn/storage"
    28  	"github.com/matrixorigin/matrixone/pkg/txn/storage/mem"
    29  	"github.com/matrixorigin/matrixone/pkg/txn/storage/memorystorage"
    30  	taestorage "github.com/matrixorigin/matrixone/pkg/txn/storage/tae"
    31  	"github.com/matrixorigin/matrixone/pkg/vm/engine/memoryengine"
    32  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/logstore/driver/logservicedriver"
    33  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/options"
    34  	"go.uber.org/zap"
    35  )
    36  
    37  var (
    38  	supportTxnStorageBackends = map[StorageType]struct{}{
    39  		StorageMEMKV: {},
    40  		StorageMEM:   {},
    41  		StorageTAE:   {},
    42  	}
    43  )
    44  
    45  func (s *store) createTxnStorage(ctx context.Context, shard metadata.TNShard) (storage.TxnStorage, error) {
    46  	factory := s.createLogServiceClientFactroy(shard)
    47  	closeLogClientFn := func(logClient logservice.Client) {
    48  		if err := logClient.Close(); err != nil {
    49  			s.rt.Logger().Error("close log client failed",
    50  				zap.Error(err))
    51  		}
    52  	}
    53  
    54  	switch s.cfg.Txn.Storage.Backend {
    55  	case StorageMEM:
    56  		logClient, err := factory()
    57  		if err != nil {
    58  			return nil, err
    59  		}
    60  		ts, err := s.newMemTxnStorage(shard, logClient, s.hakeeperClient)
    61  		if err != nil {
    62  			closeLogClientFn(logClient)
    63  			return nil, err
    64  		}
    65  		return ts, nil
    66  
    67  	case StorageMEMKV:
    68  		logClient, err := factory()
    69  		if err != nil {
    70  			return nil, err
    71  		}
    72  		return s.newMemKVStorage(shard, logClient)
    73  
    74  	case StorageTAE:
    75  		ts, err := s.newTAEStorage(ctx, shard, factory)
    76  		if err != nil {
    77  			return nil, err
    78  		}
    79  		return ts, nil
    80  	default:
    81  		return nil, moerr.NewInternalError(ctx, "not implment for %s", s.cfg.Txn.Storage.Backend)
    82  	}
    83  }
    84  
    85  func (s *store) createLogServiceClient(shard metadata.TNShard) (logservice.Client, error) {
    86  	if s.options.logServiceClientFactory != nil {
    87  		return s.options.logServiceClientFactory(shard)
    88  	}
    89  	return s.newLogServiceClient(shard)
    90  }
    91  
    92  func (s *store) createLogServiceClientFactroy(shard metadata.TNShard) logservice.ClientFactory {
    93  	return func() (logservice.Client, error) {
    94  		return s.createLogServiceClient(shard)
    95  	}
    96  }
    97  
    98  func (s *store) newLogServiceClient(shard metadata.TNShard) (logservice.Client, error) {
    99  	ctx, cancel := context.WithTimeout(context.Background(), s.cfg.LogService.ConnectTimeout.Duration)
   100  	defer cancel()
   101  	return logservice.NewClient(ctx, logservice.ClientConfig{
   102  		ReadOnly:         false,
   103  		LogShardID:       shard.LogShardID,
   104  		TNReplicaID:      shard.ReplicaID,
   105  		ServiceAddresses: s.cfg.HAKeeper.ClientConfig.ServiceAddresses,
   106  		MaxMessageSize:   int(s.cfg.RPC.MaxMessageSize),
   107  	})
   108  }
   109  
   110  func (s *store) newMemTxnStorage(
   111  	shard metadata.TNShard,
   112  	logClient logservice.Client,
   113  	hakeeper logservice.TNHAKeeperClient,
   114  ) (storage.TxnStorage, error) {
   115  	// should it be no fixed or a certain size?
   116  	mp, err := mpool.NewMPool("mem_txn_storge", 0, mpool.NoFixed)
   117  	if err != nil {
   118  		return nil, err
   119  	}
   120  	return memorystorage.NewMemoryStorage(
   121  		mp,
   122  		s.rt.Clock(),
   123  		memoryengine.NewHakeeperIDGenerator(hakeeper),
   124  	)
   125  }
   126  
   127  func (s *store) newMemKVStorage(shard metadata.TNShard, logClient logservice.Client) (storage.TxnStorage, error) {
   128  	return mem.NewKVTxnStorage(0, logClient, s.rt.Clock()), nil
   129  }
   130  
   131  func (s *store) newTAEStorage(ctx context.Context, shard metadata.TNShard, factory logservice.ClientFactory) (storage.TxnStorage, error) {
   132  	// use s3 as main fs
   133  	fs, err := fileservice.Get[fileservice.FileService](s.fileService, defines.SharedFileServiceName)
   134  	if err != nil {
   135  		return nil, err
   136  	}
   137  
   138  	ckpcfg := &options.CheckpointCfg{
   139  		MinCount:               s.cfg.Ckp.MinCount,
   140  		ScanInterval:           s.cfg.Ckp.ScanInterval.Duration,
   141  		FlushInterval:          s.cfg.Ckp.FlushInterval.Duration,
   142  		IncrementalInterval:    s.cfg.Ckp.IncrementalInterval.Duration,
   143  		GlobalMinCount:         s.cfg.Ckp.GlobalMinCount,
   144  		ReservedWALEntryCount:  s.cfg.Ckp.ReservedWALEntryCount,
   145  		OverallFlushMemControl: s.cfg.Ckp.OverallFlushMemControl,
   146  	}
   147  
   148  	gcCfg := &options.GCCfg{
   149  		GCTTL:          s.cfg.GCCfg.GCTTL.Duration,
   150  		ScanGCInterval: s.cfg.GCCfg.ScanGCInterval.Duration,
   151  		DisableGC:      s.cfg.GCCfg.DisableGC,
   152  	}
   153  
   154  	mergeCfg := &options.MergeConfig{
   155  		CNMergeMemControlHint: uint64(s.cfg.Merge.CNMergeMemHint),
   156  		CNTakeOverAll:         s.cfg.Merge.CNTakeOverAll,
   157  		CNTakeOverExceed:      uint64(s.cfg.Merge.CNTakeOverExceed),
   158  		CNStandaloneTake:      s.cfg.Merge.CNStandaloneTake,
   159  	}
   160  
   161  	logtailServerAddr := s.logtailServiceListenAddr()
   162  	logtailServerCfg := &options.LogtailServerCfg{
   163  		RpcMaxMessageSize:      int64(s.cfg.LogtailServer.RpcMaxMessageSize),
   164  		RpcEnableChecksum:      s.cfg.LogtailServer.RpcEnableChecksum,
   165  		RPCStreamPoisonTime:    s.cfg.LogtailServer.LogtailRPCStreamPoisonTime.Duration,
   166  		LogtailCollectInterval: s.cfg.LogtailServer.LogtailCollectInterval.Duration,
   167  		ResponseSendTimeout:    s.cfg.LogtailServer.LogtailResponseSendTimeout.Duration,
   168  	}
   169  
   170  	// the previous values
   171  	//max2LogServiceMsgSizeLimit := s.cfg.RPC.MaxMessageSize
   172  	// unlimited, divided by 2 to avoid overflow
   173  	max2LogServiceMsgSizeLimit := uint64(math.MaxUint64 / 2)
   174  
   175  	opt := &options.Options{
   176  		Clock:             s.rt.Clock(),
   177  		Fs:                fs,
   178  		Lc:                logservicedriver.LogServiceClientFactory(factory),
   179  		Shard:             shard,
   180  		CheckpointCfg:     ckpcfg,
   181  		GCCfg:             gcCfg,
   182  		MergeCfg:          mergeCfg,
   183  		LogStoreT:         options.LogstoreLogservice,
   184  		IncrementalDedup:  s.cfg.Txn.IncrementalDedup == "true",
   185  		IsStandalone:      s.cfg.InStandalone,
   186  		Ctx:               ctx,
   187  		MaxMessageSize:    max2LogServiceMsgSizeLimit,
   188  		TaskServiceGetter: s.GetTaskService,
   189  	}
   190  
   191  	return taestorage.NewTAEStorage(
   192  		ctx,
   193  		s.cfg.Txn.Storage.dataDir,
   194  		opt,
   195  		shard,
   196  		s.rt,
   197  		logtailServerAddr,
   198  		logtailServerCfg,
   199  	)
   200  }