|
| 1 | +/* |
| 2 | +Copyright 2024 The Vitess Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package semisync |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "testing" |
| 22 | + |
| 23 | + "github.com/stretchr/testify/require" |
| 24 | + |
| 25 | + "vitess.io/vitess/go/mysql" |
| 26 | + "vitess.io/vitess/go/test/endtoend/cluster" |
| 27 | + "vitess.io/vitess/go/test/endtoend/reparent/utils" |
| 28 | +) |
| 29 | + |
| 30 | +func TestSemiSyncUpgradeDowngrade(t *testing.T) { |
| 31 | + ver, err := cluster.GetMajorVersion("vtgate") |
| 32 | + require.NoError(t, err) |
| 33 | + if ver != 21 { |
| 34 | + t.Skip("We only want to run this test for v21 release") |
| 35 | + } |
| 36 | + defer cluster.PanicHandler(t) |
| 37 | + clusterInstance := utils.SetupReparentCluster(t, "semi_sync") |
| 38 | + defer utils.TeardownCluster(clusterInstance) |
| 39 | + tablets := clusterInstance.Keyspaces[0].Shards[0].Vttablets |
| 40 | + |
| 41 | + // Verify that replication is running as intended. |
| 42 | + utils.ConfirmReplication(t, tablets[0], []*cluster.Vttablet{tablets[1], tablets[2], tablets[3]}) |
| 43 | + |
| 44 | + replica := tablets[1] |
| 45 | + // Verify we are using the correct vttablet version. |
| 46 | + verifyVttabletVersion(t, replica, 21) |
| 47 | + // Check the plugin loaded in vttablet. |
| 48 | + require.EqualValues(t, mysql.SemiSyncTypeSource, semiSyncExtensionLoaded(t, replica)) |
| 49 | + |
| 50 | + t.Run("Downgrade to previous release", func(t *testing.T) { |
| 51 | + // change vttablet binary and downgrade it. |
| 52 | + changeVttabletBinary(t, replica, "vttabletold") |
| 53 | + // Verify we are using the older vttablet version. |
| 54 | + verifyVttabletVersion(t, replica, 20) |
| 55 | + // Verify that replication is running as intended. |
| 56 | + utils.ConfirmReplication(t, tablets[0], []*cluster.Vttablet{tablets[1], tablets[2], tablets[3]}) |
| 57 | + // Check the plugin loaded in vttablet. |
| 58 | + require.EqualValues(t, mysql.SemiSyncTypeSource, semiSyncExtensionLoaded(t, replica)) |
| 59 | + }) |
| 60 | + |
| 61 | + t.Run("Upgrade to current release", func(t *testing.T) { |
| 62 | + // change vttablet binary and downgrade it. |
| 63 | + changeVttabletBinary(t, replica, "vttablet") |
| 64 | + // Verify we are using the older vttablet version. |
| 65 | + verifyVttabletVersion(t, replica, 21) |
| 66 | + // Verify that replication is running as intended. |
| 67 | + utils.ConfirmReplication(t, tablets[0], []*cluster.Vttablet{tablets[1], tablets[2], tablets[3]}) |
| 68 | + // Check the plugin loaded in vttablet. |
| 69 | + require.EqualValues(t, mysql.SemiSyncTypeSource, semiSyncExtensionLoaded(t, replica)) |
| 70 | + }) |
| 71 | +} |
| 72 | + |
| 73 | +// semiSyncExtensionLoaded checks if the semisync extension has been loaded. |
| 74 | +// It should work for both MariaDB and MySQL. |
| 75 | +func semiSyncExtensionLoaded(t *testing.T, replica *cluster.Vttablet) mysql.SemiSyncType { |
| 76 | + qr := utils.RunSQL(context.Background(), t, `SHOW VARIABLES LIKE 'rpl_semi_sync_%_enabled'`, replica) |
| 77 | + for _, row := range qr.Rows { |
| 78 | + if row[0].ToString() == "rpl_semi_sync_source_enabled" { |
| 79 | + return mysql.SemiSyncTypeSource |
| 80 | + } |
| 81 | + if row[0].ToString() == "rpl_semi_sync_master_enabled" { |
| 82 | + return mysql.SemiSyncTypeMaster |
| 83 | + } |
| 84 | + } |
| 85 | + return mysql.SemiSyncTypeOff |
| 86 | +} |
| 87 | + |
| 88 | +func changeVttabletBinary(t *testing.T, replica *cluster.Vttablet, binary string) { |
| 89 | + t.Helper() |
| 90 | + err := replica.VttabletProcess.TearDown() |
| 91 | + require.NoError(t, err) |
| 92 | + replica.VttabletProcess.Binary = binary |
| 93 | + err = replica.VttabletProcess.Setup() |
| 94 | + require.NoError(t, err) |
| 95 | +} |
| 96 | + |
| 97 | +func verifyVttabletVersion(t *testing.T, replica *cluster.Vttablet, version int) { |
| 98 | + t.Helper() |
| 99 | + verGot, err := cluster.GetMajorVersion(replica.VttabletProcess.Binary) |
| 100 | + require.NoError(t, err) |
| 101 | + require.EqualValues(t, version, verGot) |
| 102 | +} |
0 commit comments