github.com/matrixorigin/matrixone@v1.2.0/pkg/proxy/conn_migration_test.go (about) 1 // Copyright 2021 - 2024 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 proxy 16 17 import ( 18 "context" 19 "fmt" 20 "os" 21 "testing" 22 "time" 23 24 "github.com/lni/goutils/leaktest" 25 "github.com/matrixorigin/matrixone/pkg/clusterservice" 26 "github.com/matrixorigin/matrixone/pkg/common/moerr" 27 "github.com/matrixorigin/matrixone/pkg/common/morpc" 28 "github.com/matrixorigin/matrixone/pkg/common/runtime" 29 "github.com/matrixorigin/matrixone/pkg/defines" 30 "github.com/matrixorigin/matrixone/pkg/pb/metadata" 31 pb "github.com/matrixorigin/matrixone/pkg/pb/query" 32 "github.com/matrixorigin/matrixone/pkg/queryservice" 33 qclient "github.com/matrixorigin/matrixone/pkg/queryservice/client" 34 "github.com/stretchr/testify/assert" 35 ) 36 37 func runTestWithQueryService(t *testing.T, cn metadata.CNService, fn func(qc qclient.QueryClient, addr string)) { 38 defer leaktest.AfterTest(t)() 39 runtime.SetupProcessLevelRuntime(runtime.DefaultRuntime()) 40 runtime.ProcessLevelRuntime().SetGlobalVariables(runtime.MOProtocolVersion, defines.MORPCLatestVersion) 41 address := fmt.Sprintf("unix:///tmp/cn-%d-%s.sock", 42 time.Now().Nanosecond(), cn.ServiceID) 43 44 if err := os.RemoveAll(address[7:]); err != nil { 45 panic(err) 46 } 47 cluster := clusterservice.NewMOCluster( 48 nil, 49 0, 50 clusterservice.WithDisableRefresh(), 51 clusterservice.WithServices([]metadata.CNService{{ 52 ServiceID: cn.ServiceID, 53 QueryAddress: address, 54 }}, nil)) 55 runtime.ProcessLevelRuntime().SetGlobalVariables(runtime.ClusterService, cluster) 56 57 qs, err := queryservice.NewQueryService(cn.ServiceID, address, morpc.Config{}) 58 assert.NoError(t, err) 59 60 qt, err := qclient.NewQueryClient(cn.ServiceID, morpc.Config{}) 61 assert.NoError(t, err) 62 63 qs.AddHandleFunc(pb.CmdMethod_MigrateConnFrom, func(ctx context.Context, req *pb.Request, resp *pb.Response) error { 64 if req.MigrateConnFromRequest == nil { 65 return moerr.NewInternalError(ctx, "bad request") 66 } 67 resp.MigrateConnFromResponse = &pb.MigrateConnFromResponse{ 68 DB: "d1", 69 } 70 return nil 71 }, false) 72 qs.AddHandleFunc(pb.CmdMethod_MigrateConnTo, func(ctx context.Context, req *pb.Request, resp *pb.Response) error { 73 if req.MigrateConnToRequest == nil { 74 return moerr.NewInternalError(ctx, "bad request") 75 } 76 resp.MigrateConnToResponse = &pb.MigrateConnToResponse{ 77 Success: true, 78 } 79 return nil 80 }, false) 81 err = qs.Start() 82 assert.NoError(t, err) 83 84 fn(qt, address) 85 86 err = qs.Close() 87 assert.NoError(t, err) 88 err = qt.Close() 89 assert.NoError(t, err) 90 } 91 92 func TestQueryServiceMigrateConn(t *testing.T) { 93 cn := metadata.CNService{ServiceID: "s1"} 94 runTestWithQueryService(t, cn, func(qc qclient.QueryClient, addr string) { 95 ctx, cancel := context.WithTimeout(context.Background(), time.Second*10) 96 defer cancel() 97 req := qc.NewRequest(pb.CmdMethod_MigrateConnFrom) 98 req.MigrateConnFromRequest = &pb.MigrateConnFromRequest{ 99 ConnID: 1, 100 } 101 resp, err := qc.SendMessage(ctx, addr, req) 102 assert.NoError(t, err) 103 defer qc.Release(resp) 104 assert.NotNil(t, resp.MigrateConnFromResponse) 105 assert.Equal(t, "d1", resp.MigrateConnFromResponse.DB) 106 }) 107 } 108 109 func TestQueryServiceMigrateFrom(t *testing.T) { 110 cn := metadata.CNService{ServiceID: "s1"} 111 runTestWithQueryService(t, cn, func(qc qclient.QueryClient, addr string) { 112 ctx, cancel := context.WithTimeout(context.Background(), time.Second*10) 113 defer cancel() 114 req := qc.NewRequest(pb.CmdMethod_MigrateConnTo) 115 req.MigrateConnToRequest = &pb.MigrateConnToRequest{ 116 ConnID: 1, 117 } 118 resp, err := qc.SendMessage(ctx, addr, req) 119 assert.NoError(t, err) 120 defer qc.Release(resp) 121 assert.NotNil(t, resp.MigrateConnToResponse) 122 assert.Equal(t, true, resp.MigrateConnToResponse.Success) 123 }) 124 }