github.com/matrixorigin/matrixone@v1.2.0/pkg/logservice/testclient.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 logservice
    16  
    17  import (
    18  	"github.com/google/uuid"
    19  	"github.com/lni/vfs"
    20  	"github.com/matrixorigin/matrixone/pkg/common/morpc"
    21  	"github.com/matrixorigin/matrixone/pkg/defines"
    22  	"github.com/matrixorigin/matrixone/pkg/fileservice"
    23  	pb "github.com/matrixorigin/matrixone/pkg/pb/logservice"
    24  )
    25  
    26  func NewTestService(fs vfs.FS) (*Service, ClientConfig, error) {
    27  	addr := []string{"localhost:9000"}
    28  	cfg := DefaultConfig()
    29  	cfg.UUID = uuid.New().String()
    30  	cfg.RTTMillisecond = 10
    31  	cfg.GossipSeedAddresses = []string{DefaultGossipServiceAddress}
    32  	cfg.DeploymentID = 1
    33  	cfg.FS = fs
    34  	cfg.LogServicePort = 9000
    35  	cfg.DisableWorkers = true
    36  	service, err := NewService(cfg,
    37  		newFS(),
    38  		nil,
    39  		WithBackendFilter(func(msg morpc.Message, backendAddr string) bool {
    40  			return true
    41  		}),
    42  	)
    43  	if err != nil {
    44  		return nil, ClientConfig{}, err
    45  	}
    46  
    47  	cmd := pb.ScheduleCommand{
    48  		ConfigChange: &pb.ConfigChange{
    49  			ChangeType: pb.StartReplica,
    50  			Replica: pb.Replica{
    51  				ShardID:   1,
    52  				ReplicaID: 1,
    53  			},
    54  			InitialMembers: map[uint64]string{1: service.ID()},
    55  		},
    56  	}
    57  	service.handleCommands([]pb.ScheduleCommand{cmd})
    58  
    59  	ccfg := ClientConfig{
    60  		LogShardID:       1,
    61  		TNReplicaID:      10,
    62  		ServiceAddresses: addr,
    63  	}
    64  	return service, ccfg, nil
    65  }
    66  
    67  func newFS() *fileservice.FileServices {
    68  	local, err := fileservice.NewMemoryFS(defines.LocalFileServiceName, fileservice.DisabledCacheConfig, nil)
    69  	if err != nil {
    70  		panic(err)
    71  	}
    72  	s3, err := fileservice.NewMemoryFS(defines.SharedFileServiceName, fileservice.DisabledCacheConfig, nil)
    73  	if err != nil {
    74  		panic(err)
    75  	}
    76  	etl, err := fileservice.NewMemoryFS(defines.ETLFileServiceName, fileservice.DisabledCacheConfig, nil)
    77  	if err != nil {
    78  		panic(err)
    79  	}
    80  	fs, err := fileservice.NewFileServices(
    81  		"",
    82  		local,
    83  		s3,
    84  		etl,
    85  	)
    86  	if err != nil {
    87  		panic(err)
    88  	}
    89  	return fs
    90  }