github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/dm/pkg/checker/conn_checker_test.go (about) 1 // Copyright 2022 PingCAP, Inc. 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 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 package checker 15 16 import ( 17 "testing" 18 19 "github.com/DATA-DOG/go-sqlmock" 20 "github.com/pingcap/tidb/dumpling/context" 21 "github.com/pingcap/tiflow/dm/config" 22 "github.com/pingcap/tiflow/dm/pkg/conn" 23 "github.com/stretchr/testify/require" 24 ) 25 26 func TestConnNumberChecker(t *testing.T) { 27 db, dbMock, err := sqlmock.New() 28 require.NoError(t, err) 29 stCfgs := []*config.SubTaskConfig{ 30 { 31 SyncerConfig: config.SyncerConfig{ 32 WorkerCount: 10, 33 }, 34 LoaderConfig: config.LoaderConfig{ 35 PoolSize: 16, 36 }, 37 }, 38 } 39 baseDB := conn.NewBaseDBForTest(db, func() {}) 40 // test lightning: warning 41 dbMock.ExpectQuery("SHOW GLOBAL VARIABLES LIKE 'max_connections'").WillReturnRows(sqlmock.NewRows([]string{"Variable_name", "Value"}). 42 AddRow("max_connections", 16)) 43 dbMock.ExpectQuery("SHOW GRANTS").WillReturnRows(sqlmock.NewRows([]string{"Grants for User"}). 44 AddRow("GRANT ALL PRIVILEGES ON *.* TO 'test'@'%'")) 45 dbMock.ExpectQuery("SHOW PROCESSLIST").WillReturnRows(sqlmock.NewRows( 46 []string{"Id", "User", "Host", "db", "Command", "Time", "State", "Info"}). 47 AddRow(1, "root", "localhost", "test", "Query", 0, "init", ""), 48 ) 49 loaderChecker := NewLoaderConnNumberChecker(baseDB, stCfgs) 50 result := loaderChecker.Check(context.Background()) 51 require.Equal(t, StateWarning, result.State) 52 require.Equal(t, 2, len(result.Errors)) 53 require.Contains(t, result.Errors[0].ShortErr, "is less than the number loader") 54 require.Contains(t, result.Errors[1].ShortErr, "task precheck cannot accurately check the number of connection needed for Lightning") 55 56 // test lightning: success 57 db, dbMock, err = sqlmock.New() 58 require.NoError(t, err) 59 baseDB = conn.NewBaseDBForTest(db, func() {}) 60 dbMock.ExpectQuery("SHOW GLOBAL VARIABLES LIKE 'max_connections'").WillReturnRows(sqlmock.NewRows([]string{"Variable_name", "Value"}). 61 AddRow("max_connections", 17)) 62 dbMock.ExpectQuery("SHOW GRANTS").WillReturnRows(sqlmock.NewRows([]string{"Grants for User"}). 63 AddRow("GRANT ALL PRIVILEGES ON *.* TO 'test'@'%'")) 64 dbMock.ExpectQuery("SHOW PROCESSLIST").WillReturnRows(sqlmock.NewRows( 65 []string{"Id", "User", "Host", "db", "Command", "Time", "State", "Info"}). 66 AddRow(1, "root", "localhost", "test", "Query", 0, "init", ""), 67 ) 68 loaderChecker = NewLoaderConnNumberChecker(baseDB, stCfgs) 69 result = loaderChecker.Check(context.Background()) 70 require.Equal(t, 0, len(result.Errors)) 71 require.Equal(t, StateSuccess, result.State) 72 73 // test lightning maxConn - usedConn < neededConn: warn 74 db, dbMock, err = sqlmock.New() 75 require.NoError(t, err) 76 baseDB = conn.NewBaseDBForTest(db, func() {}) 77 dbMock.ExpectQuery("SHOW GLOBAL VARIABLES LIKE 'max_connections'").WillReturnRows(sqlmock.NewRows([]string{"Variable_name", "Value"}). 78 AddRow("max_connections", 17)) 79 dbMock.ExpectQuery("SHOW GRANTS").WillReturnRows(sqlmock.NewRows([]string{"Grants for User"}). 80 AddRow("GRANT ALL PRIVILEGES ON *.* TO 'test'@'%'")) 81 dbMock.ExpectQuery("SHOW PROCESSLIST").WillReturnRows(sqlmock.NewRows( 82 []string{"Id", "User", "Host", "db", "Command", "Time", "State", "Info"}). 83 AddRow(1, "root", "localhost", "test", "Query", 0, "init", ""). 84 AddRow(2, "root", "localhost", "test", "Query", 0, "init", ""), 85 ) 86 loaderChecker = NewLoaderConnNumberChecker(baseDB, stCfgs) 87 result = loaderChecker.Check(context.Background()) 88 require.Equal(t, 1, len(result.Errors)) 89 require.Equal(t, StateWarning, result.State) 90 require.Regexp(t, "(.|\n)*is less than loader needs(.|\n)*", result.Errors[0].ShortErr) 91 92 // test lightning no enough privilege: warn 93 db, dbMock, err = sqlmock.New() 94 require.NoError(t, err) 95 baseDB = conn.NewBaseDBForTest(db, func() {}) 96 dbMock.ExpectQuery("SHOW GLOBAL VARIABLES LIKE 'max_connections'").WillReturnRows(sqlmock.NewRows([]string{"Variable_name", "Value"}). 97 AddRow("max_connections", 17)) 98 dbMock.ExpectQuery("SHOW GRANTS").WillReturnRows(sqlmock.NewRows([]string{"Grants for User"}). 99 AddRow("GRANT INDEX ON *.* TO 'test'@'%'")) 100 dbMock.ExpectQuery("SHOW PROCESSLIST").WillReturnRows(sqlmock.NewRows( 101 []string{"Id", "User", "Host", "db", "Command", "Time", "State", "Info"}). 102 AddRow(1, "root", "localhost", "test", "Query", 0, "init", ""), 103 ) 104 loaderChecker = NewLoaderConnNumberChecker(baseDB, stCfgs) 105 result = loaderChecker.Check(context.Background()) 106 require.Equal(t, 1, len(result.Errors)) 107 require.Equal(t, StateWarning, result.State) 108 require.Regexp(t, "(.|\n)*lack of Super global(.|\n)*", result.Errors[0].ShortErr) 109 }