github.com/matrixorigin/matrixone@v1.2.0/pkg/cnservice/memory_engine.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  
    20  	"github.com/google/uuid"
    21  	"github.com/matrixorigin/matrixone/pkg/clusterservice"
    22  	"github.com/matrixorigin/matrixone/pkg/common/mpool"
    23  	"github.com/matrixorigin/matrixone/pkg/common/runtime"
    24  	"github.com/matrixorigin/matrixone/pkg/config"
    25  	"github.com/matrixorigin/matrixone/pkg/pb/metadata"
    26  	"github.com/matrixorigin/matrixone/pkg/txn/storage/memorystorage"
    27  	"github.com/matrixorigin/matrixone/pkg/vm/engine/memoryengine"
    28  )
    29  
    30  func (s *service) initMemoryEngine(
    31  	ctx context.Context,
    32  	pu *config.ParameterUnit,
    33  ) error {
    34  
    35  	// txn client
    36  	client, err := s.getTxnClient()
    37  	if err != nil {
    38  		return err
    39  	}
    40  	pu.TxnClient = client
    41  
    42  	// hakeeper
    43  	hakeeper, err := s.getHAKeeperClient()
    44  	if err != nil {
    45  		return err
    46  	}
    47  
    48  	// engine
    49  	mp, err := mpool.NewMPool("cnservice_mem_engine", 0, mpool.NoFixed)
    50  	if err != nil {
    51  		return err
    52  	}
    53  	pu.StorageEngine = memoryengine.New(
    54  		ctx,
    55  		memoryengine.NewDefaultShardPolicy(mp),
    56  		memoryengine.NewHakeeperIDGenerator(hakeeper),
    57  		s.moCluster,
    58  	)
    59  
    60  	return nil
    61  }
    62  
    63  func (s *service) initMemoryEngineNonDist(
    64  	ctx context.Context,
    65  	pu *config.ParameterUnit,
    66  ) error {
    67  	ck := runtime.ProcessLevelRuntime().Clock()
    68  	mp, err := mpool.NewMPool("cnservice_mem_engine_nondist", 0, mpool.NoFixed)
    69  	if err != nil {
    70  		return err
    71  	}
    72  
    73  	shard := metadata.TNShard{}
    74  	shard.ShardID = 2
    75  	shard.ReplicaID = 2
    76  	shards := []metadata.TNShard{
    77  		shard,
    78  	}
    79  	tnAddr := "1"
    80  	uid, _ := uuid.NewV7()
    81  	tnServices := []metadata.TNService{{
    82  		ServiceID:         uid.String(),
    83  		TxnServiceAddress: tnAddr,
    84  		Shards:            shards,
    85  	}}
    86  	cluster := clusterservice.NewMOCluster(nil, 0,
    87  		clusterservice.WithDisableRefresh(),
    88  		clusterservice.WithServices(nil, tnServices))
    89  	runtime.ProcessLevelRuntime().SetGlobalVariables(runtime.ClusterService, cluster)
    90  
    91  	storage, err := memorystorage.NewMemoryStorage(
    92  		mp,
    93  		ck,
    94  		memoryengine.RandomIDGenerator,
    95  	)
    96  	if err != nil {
    97  		return err
    98  	}
    99  
   100  	txnClient := memorystorage.NewStorageTxnClient(
   101  		ck,
   102  		map[string]*memorystorage.Storage{
   103  			tnAddr: storage,
   104  		},
   105  	)
   106  	pu.TxnClient = txnClient
   107  
   108  	s.storeEngine = memoryengine.New(
   109  		ctx,
   110  		memoryengine.NewDefaultShardPolicy(mp),
   111  		memoryengine.RandomIDGenerator,
   112  		cluster,
   113  	)
   114  	pu.StorageEngine = s.storeEngine
   115  	s.initInternalSQlExecutor(mp)
   116  	return nil
   117  }