github.com/matrixorigin/matrixone@v0.7.0/pkg/cnservice/tae.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 cnservice
    16  
    17  import (
    18  	"context"
    19  	"os"
    20  	"syscall"
    21  	"time"
    22  
    23  	"github.com/matrixorigin/matrixone/pkg/common/moerr"
    24  	"github.com/matrixorigin/matrixone/pkg/config"
    25  	"github.com/matrixorigin/matrixone/pkg/logservice"
    26  	"github.com/matrixorigin/matrixone/pkg/logutil"
    27  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/db"
    28  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/moengine"
    29  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/options"
    30  )
    31  
    32  func initTAE(
    33  	ctx context.Context,
    34  	pu *config.ParameterUnit,
    35  	cfg *Config,
    36  ) error {
    37  
    38  	targetDir := pu.SV.StorePath
    39  
    40  	mask := syscall.Umask(0)
    41  	if err := os.MkdirAll(targetDir, os.FileMode(0755)); err != nil {
    42  		syscall.Umask(mask)
    43  		logutil.Infof("Recreate dir error:%v\n", err)
    44  		return err
    45  	}
    46  	syscall.Umask(mask)
    47  
    48  	opts := &options.Options{}
    49  	switch cfg.Engine.Logstore {
    50  	case options.LogstoreLogservice:
    51  		lc := func() (logservice.Client, error) {
    52  			ctx, cancel := context.WithTimeout(ctx, time.Minute)
    53  			lc, err := logservice.NewClient(ctx, logservice.ClientConfig{
    54  				Tag:              targetDir,
    55  				ReadOnly:         false,
    56  				LogShardID:       pu.SV.LogShardID,
    57  				DNReplicaID:      pu.SV.DNReplicaID,
    58  				ServiceAddresses: cfg.HAKeeper.ClientConfig.ServiceAddresses,
    59  				MaxMessageSize:   int(cfg.RPC.MaxMessageSize),
    60  			})
    61  			cancel()
    62  			return lc, err
    63  		}
    64  		opts.Lc = lc
    65  		opts.LogStoreT = options.LogstoreLogservice
    66  	case options.LogstoreBatchStore, "":
    67  		opts.LogStoreT = options.LogstoreBatchStore
    68  	default:
    69  		return moerr.NewInternalError(ctx, "invalid logstore type: %v", cfg.Engine.Logstore)
    70  	}
    71  	opts.CheckpointCfg = &options.CheckpointCfg{}
    72  	opts.CheckpointCfg.FlushInterval = cfg.Engine.FlushInterval.Duration
    73  	opts.CheckpointCfg.ScanInterval = cfg.Engine.ScanInterval.Duration
    74  	opts.CheckpointCfg.MinCount = cfg.Engine.MinCount
    75  	opts.CheckpointCfg.IncrementalInterval = cfg.Engine.IncrementalInterval.Duration
    76  	opts.CheckpointCfg.GlobalMinCount = cfg.Engine.GlobalMinCount
    77  
    78  	tae, err := db.Open(targetDir+"/tae", opts)
    79  	if err != nil {
    80  		logutil.Infof("Open tae failed. error:%v", err)
    81  		return err
    82  	}
    83  
    84  	eng := moengine.NewEngine(tae)
    85  	pu.StorageEngine = eng
    86  	pu.TxnClient = moengine.EngineToTxnClient(eng)
    87  	logutil.Info("Initialize the engine Done")
    88  
    89  	return nil
    90  }