diff --git a/internal/app/util.go b/internal/app/util.go index 1b71b020..bedda991 100644 --- a/internal/app/util.go +++ b/internal/app/util.go @@ -236,21 +236,28 @@ func isGTIDLessOrEqual(slaveGtidSet, masterGtidSet gtids.GTIDSet) bool { } func isSplitBrained(slaveGtidSet, masterGtidSet *gomysql.MysqlGTIDSet, masterUUID uuid.UUID) bool { - for _, MasterSet := range masterGtidSet.Sets { - slaveSet, ok := slaveGtidSet.Sets[MasterSet.SID.String()] + for _, masterSet := range masterGtidSet.Sets { + slaveSet, ok := slaveGtidSet.Sets[masterSet.SID.String()] if !ok { continue } - if MasterSet.Contain(slaveSet) || MasterSet == slaveSet { + if masterSet.Contain(slaveSet) || masterSet == slaveSet { continue } - if MasterSet.SID == masterUUID { + if masterSet.SID == masterUUID { continue } return true } + + for _, slaveSet := range slaveGtidSet.Sets { + if _, ok := masterGtidSet.Sets[slaveSet.SID.String()]; !ok { + return true + } + } + return false } diff --git a/internal/app/util_test.go b/internal/app/util_test.go index 8a71e8cb..015b90be 100644 --- a/internal/app/util_test.go +++ b/internal/app/util_test.go @@ -327,3 +327,52 @@ func getLogger() *log.Logger { } return l } + +func TestIsSplitBrained(t *testing.T) { + masterGTID, _ := gomysql.ParseMysqlGTIDSet("6DBC0B04-4B09-43DC-86CC-9AF852DED919:1-100," + + "09978591-5754-4710-BF67-062880ABE1B4:1-100," + + "AA6890C8-69F8-4BC4-B3A5-5D3FEA8C28CF:1-100") + masterMySQLGTID := masterGTID.(*gomysql.MysqlGTIDSet) + masterUUID := masterMySQLGTID.Sets["6dbc0b04-4b09-43dc-86cc-9af852ded919"].SID + + // equal gtids + slaveGTID, _ := gomysql.ParseMysqlGTIDSet("6DBC0B04-4B09-43DC-86CC-9AF852DED919:1-101," + + "09978591-5754-4710-BF67-062880ABE1B4:1-100," + + "AA6890C8-69F8-4BC4-B3A5-5D3FEA8C28CF:1-100") + slaveMySQLGTID := slaveGTID.(*gomysql.MysqlGTIDSet) + ok := isSplitBrained(slaveMySQLGTID, masterMySQLGTID, masterUUID) + require.False(t, ok) + + // the replica is lagging behind the master + slaveGTID, _ = gomysql.ParseMysqlGTIDSet("6DBC0B04-4B09-43DC-86CC-9AF852DED919:1-99," + + "09978591-5754-4710-BF67-062880ABE1B4:1-100," + + "AA6890C8-69F8-4BC4-B3A5-5D3FEA8C28CF:1-100") + slaveMySQLGTID = slaveGTID.(*gomysql.MysqlGTIDSet) + ok = isSplitBrained(slaveMySQLGTID, masterMySQLGTID, masterUUID) + require.False(t, ok) + + // the replica applied the transaction from the master before the master + slaveGTID, _ = gomysql.ParseMysqlGTIDSet("6DBC0B04-4B09-43DC-86CC-9AF852DED919:1-101," + + "09978591-5754-4710-BF67-062880ABE1B4:1-100," + + "AA6890C8-69F8-4BC4-B3A5-5D3FEA8C28CF:1-100") + slaveMySQLGTID = slaveGTID.(*gomysql.MysqlGTIDSet) + ok = isSplitBrained(slaveMySQLGTID, masterMySQLGTID, masterUUID) + require.False(t, ok) + + // the replica applied a transaction not from the master + slaveGTID, _ = gomysql.ParseMysqlGTIDSet("6DBC0B04-4B09-43DC-86CC-9AF852DED919:1-100," + + "09978591-5754-4710-BF67-062880ABE1B4:1-100," + + "AA6890C8-69F8-4BC4-B3A5-5D3FEA8C28CF:1-101") + slaveMySQLGTID = slaveGTID.(*gomysql.MysqlGTIDSet) + ok = isSplitBrained(slaveMySQLGTID, masterMySQLGTID, masterUUID) + require.True(t, ok) + + // the replica applied a new transaction not from the master + slaveGTID, _ = gomysql.ParseMysqlGTIDSet("6DBC0B04-4B09-43DC-86CC-9AF852DED919:1-101," + + "09978591-5754-4710-BF67-062880ABE1B4:1-100," + + "AA6890C8-69F8-4BC4-B3A5-5D3FEA8C28CF:1-100," + + "BB6890C8-69F8-4BC4-B3A5-5D3FEA8C28CF:1-100") + slaveMySQLGTID = slaveGTID.(*gomysql.MysqlGTIDSet) + ok = isSplitBrained(slaveMySQLGTID, masterMySQLGTID, masterUUID) + require.True(t, ok) +}