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