github.com/matrixorigin/matrixone@v0.7.0/pkg/tests/txn/cluster_mo_ctl_test.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 txn 16 17 import ( 18 "database/sql" 19 "fmt" 20 "testing" 21 22 "github.com/lni/goutils/leaktest" 23 pb "github.com/matrixorigin/matrixone/pkg/pb/ctl" 24 "github.com/matrixorigin/matrixone/pkg/pb/timestamp" 25 "github.com/matrixorigin/matrixone/pkg/util/json" 26 "github.com/stretchr/testify/require" 27 ) 28 29 func TestMoCtlGetSnapshot(t *testing.T) { 30 defer leaktest.AfterTest(t)() 31 if testing.Short() { 32 t.Skip("skipping in short mode.") 33 return 34 } 35 36 // this case will start a mo cluster with 1 CNService, 1 DNService and 3 LogService. 37 // A Txn read and write will success. 38 for name, options := range testOptionsSet { 39 t.Run(name, func(t *testing.T) { 40 c, err := NewCluster(t, 41 getBasicClusterOptions(options...)) 42 require.NoError(t, err) 43 defer c.Stop() 44 c.Start() 45 46 cli := c.NewClient() 47 v := mustGetSnapshot(t, cli) 48 ts, err := timestamp.ParseTimestamp(v) 49 require.NoError(t, err) 50 require.NotEqual(t, timestamp.Timestamp{}, ts) 51 }) 52 } 53 } 54 55 func TestMoCtlUseSnapshot(t *testing.T) { 56 defer leaktest.AfterTest(t)() 57 if testing.Short() { 58 t.Skip("skipping in short mode.") 59 return 60 } 61 62 // this case will start a mo cluster with 1 CNService, 1 DNService and 3 LogService. 63 // A Txn read and write will success. 64 for name, options := range testOptionsSet { 65 t.Run(name, func(t *testing.T) { 66 c, err := NewCluster(t, 67 getBasicClusterOptions(options...)) 68 require.NoError(t, err) 69 defer c.Stop() 70 c.Start() 71 72 cli := c.NewClient() 73 74 k1 := "k1" 75 checkWrite(t, mustNewTxn(t, cli), k1, "1", nil, true) 76 checkRead(t, mustNewTxn(t, cli), k1, "1", nil, true) 77 78 v := mustGetSnapshot(t, cli) 79 80 checkWrite(t, mustNewTxn(t, cli), k1, "2", nil, true) 81 checkRead(t, mustNewTxn(t, cli), k1, "2", nil, true) 82 83 mustUseSnapshot(t, cli, v) 84 checkRead(t, mustNewTxn(t, cli), k1, "1", nil, true) 85 }) 86 } 87 } 88 89 func mustCloseRows(t *testing.T, rows *sql.Rows) { 90 require.NoError(t, rows.Close()) 91 } 92 93 func mustGetSnapshot(t *testing.T, cli Client) string { 94 txn, err := cli.NewTxn() 95 require.NoError(t, err) 96 sqlTxn := txn.(SQLBasedTxn) 97 defer func() { 98 require.NoError(t, sqlTxn.Commit()) 99 }() 100 101 rows, err := sqlTxn.ExecSQLQuery("select mo_ctl('cn', 'GetSnapshot', '')") 102 require.NoError(t, err) 103 104 defer mustCloseRows(t, rows) 105 require.True(t, rows.Next()) 106 107 var result pb.CtlResult 108 value := "" 109 require.NoError(t, rows.Scan(&value)) 110 json.MustUnmarshal([]byte(value), &result) 111 return result.Data.(string) 112 } 113 114 func mustUseSnapshot(t *testing.T, cli Client, ts string) { 115 txn, err := cli.NewTxn() 116 require.NoError(t, err) 117 sqlTxn := txn.(SQLBasedTxn) 118 defer func() { 119 require.NoError(t, sqlTxn.Commit()) 120 }() 121 122 rows, err := sqlTxn.ExecSQLQuery(fmt.Sprintf("select mo_ctl('cn', 'UseSnapshot', '%s')", ts)) 123 require.NoError(t, err) 124 mustCloseRows(t, rows) 125 }