|
| 1 | +package inst |
| 2 | + |
| 3 | +import ( |
| 4 | + "database/sql" |
| 5 | + "testing" |
| 6 | + |
| 7 | + test "github.com/proxysql/golib/tests" |
| 8 | +) |
| 9 | + |
| 10 | +func TestSelectCanonicalChannelIndexEmpty(t *testing.T) { |
| 11 | + channels := []ChannelStatus{} |
| 12 | + idx := selectCanonicalChannelIndex(channels) |
| 13 | + test.S(t).ExpectEquals(idx, -1) |
| 14 | +} |
| 15 | + |
| 16 | +func TestSelectCanonicalChannelIndexSingleDefault(t *testing.T) { |
| 17 | + channels := []ChannelStatus{ |
| 18 | + {ChannelName: "", MasterKey: InstanceKey{Hostname: "master1", Port: 3306}}, |
| 19 | + } |
| 20 | + idx := selectCanonicalChannelIndex(channels) |
| 21 | + test.S(t).ExpectEquals(idx, 0) |
| 22 | +} |
| 23 | + |
| 24 | +func TestSelectCanonicalChannelIndexPrefersDefault(t *testing.T) { |
| 25 | + channels := []ChannelStatus{ |
| 26 | + {ChannelName: "channel_a", MasterKey: InstanceKey{Hostname: "master1", Port: 3306}}, |
| 27 | + {ChannelName: "", MasterKey: InstanceKey{Hostname: "master2", Port: 3306}}, |
| 28 | + {ChannelName: "channel_b", MasterKey: InstanceKey{Hostname: "master3", Port: 3306}}, |
| 29 | + } |
| 30 | + idx := selectCanonicalChannelIndex(channels) |
| 31 | + test.S(t).ExpectEquals(idx, 1) |
| 32 | +} |
| 33 | + |
| 34 | +func TestSelectCanonicalChannelIndexSkipsGRChannels(t *testing.T) { |
| 35 | + channels := []ChannelStatus{ |
| 36 | + {ChannelName: "group_replication_applier", MasterKey: InstanceKey{Hostname: "master1", Port: 3306}}, |
| 37 | + {ChannelName: "group_replication_recovery", MasterKey: InstanceKey{Hostname: "master1", Port: 3306}}, |
| 38 | + {ChannelName: "my_channel", MasterKey: InstanceKey{Hostname: "master2", Port: 3306}}, |
| 39 | + } |
| 40 | + idx := selectCanonicalChannelIndex(channels) |
| 41 | + test.S(t).ExpectEquals(idx, 2) |
| 42 | +} |
| 43 | + |
| 44 | +func TestSelectCanonicalChannelIndexAllGR(t *testing.T) { |
| 45 | + channels := []ChannelStatus{ |
| 46 | + {ChannelName: "group_replication_applier", MasterKey: InstanceKey{Hostname: "master1", Port: 3306}}, |
| 47 | + {ChannelName: "group_replication_recovery", MasterKey: InstanceKey{Hostname: "master1", Port: 3306}}, |
| 48 | + } |
| 49 | + // When only GR channels exist, falls back to index 0 |
| 50 | + idx := selectCanonicalChannelIndex(channels) |
| 51 | + test.S(t).ExpectEquals(idx, 0) |
| 52 | +} |
| 53 | + |
| 54 | +func TestSelectCanonicalChannelIndexMultipleNamed(t *testing.T) { |
| 55 | + channels := []ChannelStatus{ |
| 56 | + {ChannelName: "channel_a", MasterKey: InstanceKey{Hostname: "master1", Port: 3306}}, |
| 57 | + {ChannelName: "channel_b", MasterKey: InstanceKey{Hostname: "master2", Port: 3306}}, |
| 58 | + } |
| 59 | + // No default channel, no GR channels: picks the first one |
| 60 | + idx := selectCanonicalChannelIndex(channels) |
| 61 | + test.S(t).ExpectEquals(idx, 0) |
| 62 | +} |
| 63 | + |
| 64 | +func TestIsGRInternalChannel(t *testing.T) { |
| 65 | + cs := ChannelStatus{ChannelName: "group_replication_applier"} |
| 66 | + test.S(t).ExpectTrue(cs.IsGRInternalChannel()) |
| 67 | + |
| 68 | + cs2 := ChannelStatus{ChannelName: "group_replication_recovery"} |
| 69 | + test.S(t).ExpectTrue(cs2.IsGRInternalChannel()) |
| 70 | + |
| 71 | + cs3 := ChannelStatus{ChannelName: "my_channel"} |
| 72 | + test.S(t).ExpectFalse(cs3.IsGRInternalChannel()) |
| 73 | + |
| 74 | + cs4 := ChannelStatus{ChannelName: ""} |
| 75 | + test.S(t).ExpectFalse(cs4.IsGRInternalChannel()) |
| 76 | +} |
| 77 | + |
| 78 | +func TestForChannelClause(t *testing.T) { |
| 79 | + test.S(t).ExpectEquals(forChannelClause(""), "") |
| 80 | + test.S(t).ExpectEquals(forChannelClause("my_channel"), " FOR CHANNEL 'my_channel'") |
| 81 | + test.S(t).ExpectEquals(forChannelClause("group_replication_applier"), " FOR CHANNEL 'group_replication_applier'") |
| 82 | +} |
| 83 | + |
| 84 | +func TestSingleSourceBehaviorUnchanged(t *testing.T) { |
| 85 | + // Verify that an instance with no replication channels behaves identically |
| 86 | + // to pre-channel-support behavior |
| 87 | + instance := NewInstance() |
| 88 | + instance.Key = InstanceKey{Hostname: "replica1", Port: 3306} |
| 89 | + instance.MasterKey = InstanceKey{Hostname: "master1", Port: 3306} |
| 90 | + instance.ReadBinlogCoordinates = BinlogCoordinates{LogFile: "mysql-bin.000001", LogPos: 100} |
| 91 | + instance.Version = "5.7.35" |
| 92 | + |
| 93 | + // No channels set |
| 94 | + test.S(t).ExpectEquals(len(instance.ReplicationChannels), 0) |
| 95 | + test.S(t).ExpectEquals(instance.ManagedChannelName, "") |
| 96 | + test.S(t).ExpectTrue(instance.IsReplica()) |
| 97 | +} |
| 98 | + |
| 99 | +func TestMultiSourceInstanceManagedChannel(t *testing.T) { |
| 100 | + // Verify that when ReplicationChannels has multiple entries and we pick canonical, |
| 101 | + // the ManagedChannelName reflects the chosen channel |
| 102 | + channels := []ChannelStatus{ |
| 103 | + { |
| 104 | + ChannelName: "channel_a", |
| 105 | + MasterKey: InstanceKey{Hostname: "master1", Port: 3306}, |
| 106 | + ReplicationIOThreadRunning: true, |
| 107 | + ReplicationSQLThreadRunning: true, |
| 108 | + SecondsBehindMaster: sql.NullInt64{Int64: 0, Valid: true}, |
| 109 | + }, |
| 110 | + { |
| 111 | + ChannelName: "channel_b", |
| 112 | + MasterKey: InstanceKey{Hostname: "master2", Port: 3306}, |
| 113 | + ReplicationIOThreadRunning: true, |
| 114 | + ReplicationSQLThreadRunning: true, |
| 115 | + SecondsBehindMaster: sql.NullInt64{Int64: 5, Valid: true}, |
| 116 | + }, |
| 117 | + } |
| 118 | + |
| 119 | + idx := selectCanonicalChannelIndex(channels) |
| 120 | + test.S(t).ExpectEquals(idx, 0) |
| 121 | + |
| 122 | + // The canonical channel should be channel_a (first non-GR channel) |
| 123 | + ch := channels[idx] |
| 124 | + test.S(t).ExpectEquals(ch.ChannelName, "channel_a") |
| 125 | + test.S(t).ExpectEquals(ch.MasterKey.Hostname, "master1") |
| 126 | +} |
| 127 | + |
| 128 | +func TestChannelAwareSQLGeneration(t *testing.T) { |
| 129 | + qsp := GetQueryStringProvider("5.7.35") |
| 130 | + |
| 131 | + // Without channel name -- no FOR CHANNEL clause |
| 132 | + stopSQL := qsp.StopReplicaForChannel("") |
| 133 | + test.S(t).ExpectTrue(len(stopSQL) > 0) |
| 134 | + test.S(t).ExpectEquals(stopSQL, qsp.stop_slave()) |
| 135 | + |
| 136 | + startSQL := qsp.StartReplicaForChannel("") |
| 137 | + test.S(t).ExpectEquals(startSQL, qsp.start_slave()) |
| 138 | + |
| 139 | + // With channel name -- should include FOR CHANNEL clause |
| 140 | + stopSQLCh := qsp.StopReplicaForChannel("my_channel") |
| 141 | + test.S(t).ExpectTrue(len(stopSQLCh) > len(stopSQL)) |
| 142 | + expectedStop := qsp.stop_slave() + " FOR CHANNEL 'my_channel'" |
| 143 | + test.S(t).ExpectEquals(stopSQLCh, expectedStop) |
| 144 | + |
| 145 | + startSQLCh := qsp.StartReplicaForChannel("my_channel") |
| 146 | + expectedStart := qsp.start_slave() + " FOR CHANNEL 'my_channel'" |
| 147 | + test.S(t).ExpectEquals(startSQLCh, expectedStart) |
| 148 | + |
| 149 | + resetSQLCh := qsp.ResetReplicaForChannel("my_channel") |
| 150 | + expectedReset := qsp.reset_slave() + " FOR CHANNEL 'my_channel'" |
| 151 | + test.S(t).ExpectEquals(resetSQLCh, expectedReset) |
| 152 | +} |
| 153 | + |
| 154 | +func TestChannelAwareSQLGeneration84(t *testing.T) { |
| 155 | + // Test with MySQL 8.4+ which uses "stop replica" / "start replica" syntax |
| 156 | + qsp := GetQueryStringProvider("8.4.0") |
| 157 | + |
| 158 | + stopSQL := qsp.StopReplicaForChannel("my_channel") |
| 159 | + test.S(t).ExpectEquals(stopSQL, "stop replica FOR CHANNEL 'my_channel'") |
| 160 | + |
| 161 | + startSQL := qsp.StartReplicaForChannel("my_channel") |
| 162 | + test.S(t).ExpectEquals(startSQL, "start replica FOR CHANNEL 'my_channel'") |
| 163 | + |
| 164 | + resetSQL := qsp.ResetReplicaForChannel("my_channel") |
| 165 | + test.S(t).ExpectEquals(resetSQL, "reset replica FOR CHANNEL 'my_channel'") |
| 166 | +} |
| 167 | + |
| 168 | +func TestChannelAwareIOSQLThreadOperations(t *testing.T) { |
| 169 | + qsp := GetQueryStringProvider("5.7.35") |
| 170 | + |
| 171 | + // IO thread operations |
| 172 | + stopIO := qsp.StopReplicaIOThreadForChannel("ch1") |
| 173 | + test.S(t).ExpectEquals(stopIO, "stop slave io_thread FOR CHANNEL 'ch1'") |
| 174 | + |
| 175 | + startIO := qsp.StartReplicaIOThreadForChannel("ch1") |
| 176 | + test.S(t).ExpectEquals(startIO, "start slave io_thread FOR CHANNEL 'ch1'") |
| 177 | + |
| 178 | + // SQL thread operations |
| 179 | + stopSQLThread := qsp.StopReplicaSQLThreadForChannel("ch1") |
| 180 | + test.S(t).ExpectEquals(stopSQLThread, "stop slave sql_thread FOR CHANNEL 'ch1'") |
| 181 | + |
| 182 | + startSQLThread := qsp.StartReplicaSQLThreadForChannel("ch1") |
| 183 | + test.S(t).ExpectEquals(startSQLThread, "start slave sql_thread FOR CHANNEL 'ch1'") |
| 184 | + |
| 185 | + // Without channel name -- no FOR CHANNEL |
| 186 | + stopIODefault := qsp.StopReplicaIOThreadForChannel("") |
| 187 | + test.S(t).ExpectEquals(stopIODefault, "stop slave io_thread") |
| 188 | +} |
| 189 | + |
| 190 | +func TestSelectCanonicalChannelWithDefaultAndGR(t *testing.T) { |
| 191 | + // When default channel exists alongside GR channels, prefer default |
| 192 | + channels := []ChannelStatus{ |
| 193 | + {ChannelName: "group_replication_applier"}, |
| 194 | + {ChannelName: ""}, |
| 195 | + {ChannelName: "group_replication_recovery"}, |
| 196 | + } |
| 197 | + idx := selectCanonicalChannelIndex(channels) |
| 198 | + test.S(t).ExpectEquals(idx, 1) |
| 199 | +} |
| 200 | + |
| 201 | +func TestSelectCanonicalChannelWithNamedAndGR(t *testing.T) { |
| 202 | + // When no default channel but named + GR channels exist, prefer the named one |
| 203 | + channels := []ChannelStatus{ |
| 204 | + {ChannelName: "group_replication_applier"}, |
| 205 | + {ChannelName: "group_replication_recovery"}, |
| 206 | + {ChannelName: "custom_repl"}, |
| 207 | + {ChannelName: "another_repl"}, |
| 208 | + } |
| 209 | + idx := selectCanonicalChannelIndex(channels) |
| 210 | + test.S(t).ExpectEquals(idx, 2) |
| 211 | +} |
0 commit comments