Skip to content

Commit

Permalink
feat: tests; add: new condition
Browse files Browse the repository at this point in the history
  • Loading branch information
Fizic committed Mar 19, 2024
1 parent 185eaaf commit 98bfdbf
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 4 deletions.
15 changes: 11 additions & 4 deletions internal/app/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
49 changes: 49 additions & 0 deletions internal/app/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit 98bfdbf

Please sign in to comment.