Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 56 additions & 25 deletions go/inst/instance_dao.go
Original file line number Diff line number Diff line change
Expand Up @@ -1191,32 +1191,9 @@ Cleanup:
}()

if instanceFound {
if instance.IsCoMaster {
// Take co-master into account, and avoid infinite loop
instance.AncestryUUID = fmt.Sprintf("%s,%s", instance.MasterUUID, instance.ServerUUID)
} else {
instance.AncestryUUID = fmt.Sprintf("%s,%s", instance.AncestryUUID, instance.ServerUUID)
}
// Add replication group ancestry UUID as well. Otherwise, Orchestrator thinks there are errant GTIDs in group
// members and its slaves, even though they are not.
instance.AncestryUUID = fmt.Sprintf("%s,%s", instance.AncestryUUID, instance.ReplicationGroupName)
instance.AncestryUUID = strings.Trim(instance.AncestryUUID, ",")
instance.AncestryUUID = computeAncestryUUID(instance.AncestryUUID, instance.MasterUUID, instance.ServerUUID, instance.ReplicationGroupName, instance.IsCoMaster)
if instance.ExecutedGtidSet != "" && instance.masterExecutedGtidSet != "" {
// Compare master & replica GTID sets, but ignore the sets that present the master's UUID.
// This is because orchestrator may pool master and replica at an inconvenient timing,
// such that the replica may _seems_ to have more entries than the master, when in fact
// it's just that the master's probing is stale.
redactedExecutedGtidSet, _ := NewOracleGtidSet(instance.ExecutedGtidSet)
for _, uuid := range strings.Split(instance.AncestryUUID, ",") {
if uuid != instance.ServerUUID {
redactedExecutedGtidSet.RemoveUUID(uuid)
}
if instance.IsCoMaster && uuid == instance.ServerUUID {
// If this is a co-master, then this server is likely to show its own generated GTIDs as errant,
// because its co-master has not applied them yet
redactedExecutedGtidSet.RemoveUUID(uuid)
}
}
redactedExecutedGtidSet := redactExecutedGtidSetForErrantCheck(instance.ExecutedGtidSet, instance.AncestryUUID, instance.ServerUUID, instance.IsCoMaster)
// Avoid querying the database if there's no point:
if !redactedExecutedGtidSet.IsEmpty() {
redactedMasterExecutedGtidSet, _ := NewOracleGtidSet(instance.masterExecutedGtidSet)
Expand Down Expand Up @@ -1407,6 +1384,60 @@ func ReadInstanceClusterAttributes(instance *Instance) (err error) {
return nil
}

// computeAncestryUUID returns the comma separated list of server UUIDs in the replication chain of an instance,
// including the instance itself. masterAncestryUUID is the ancestry of the instance's master as stored in the
// backend database.
//
// The instance's own Master_UUID is always part of the chain: the master's stored ancestry may be empty or
// incomplete, e.g. when orchestrator cannot read the master (such as an AWS RDS source). Without it, every
// replica further down the chain would see the master's transactions as errant GTIDs whenever it is ahead of the
// master's last known executed_gtid_set (https://github.com/ProxySQL/orchestrator/issues/137).
func computeAncestryUUID(masterAncestryUUID, masterUUID, serverUUID, replicationGroupName string, isCoMaster bool) string {
var uuids []string
if isCoMaster {
// Take co-master into account, and avoid infinite loop
uuids = []string{masterUUID, serverUUID}
} else {
uuids = append(strings.Split(masterAncestryUUID, ","), masterUUID, serverUUID)
}
// Add replication group ancestry UUID as well. Otherwise, Orchestrator thinks there are errant GTIDs in group
// members and its slaves, even though they are not.
uuids = append(uuids, replicationGroupName)

ancestry := []string{}
seen := map[string]bool{}
for _, uuid := range uuids {
uuid = strings.TrimSpace(uuid)
if uuid == "" || seen[uuid] {
continue
}
seen[uuid] = true
ancestry = append(ancestry, uuid)
}
return strings.Join(ancestry, ",")
}

// redactExecutedGtidSetForErrantCheck returns the instance's executed GTID set, without the GTIDs
// generated by its ancestors.
// Compare master & replica GTID sets, but ignore the sets that present the master's UUID.
// This is because orchestrator may pool master and replica at an inconvenient timing,
// such that the replica may _seems_ to have more entries than the master, when in fact
// it's just that the master's probing is stale.
func redactExecutedGtidSetForErrantCheck(executedGtidSet, ancestryUUID, serverUUID string, isCoMaster bool) *OracleGtidSet {
redactedExecutedGtidSet, _ := NewOracleGtidSet(executedGtidSet)
for _, uuid := range strings.Split(ancestryUUID, ",") {
if uuid != serverUUID {
redactedExecutedGtidSet.RemoveUUID(uuid)
}
if isCoMaster && uuid == serverUUID {
// If this is a co-master, then this server is likely to show its own generated GTIDs as errant,
// because its co-master has not applied them yet
redactedExecutedGtidSet.RemoveUUID(uuid)
}
}
return redactedExecutedGtidSet
}

type byNamePort [](*InstanceKey)

func (this byNamePort) Len() int { return len(this) }
Expand Down
83 changes: 83 additions & 0 deletions go/inst/instance_dao_ancestry_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package inst

import (
"testing"

test "github.com/proxysql/golib/tests"
)

const (
ancestryTestSourceUUID = "623335cf-c858-3eb6-a90d-40122a2d21f4"
ancestryTestIntermediateUUID = "00000000-0000-0000-0000-000000000200"
ancestryTestLeafUUID = "00000000-0000-0000-0000-000000000201"
)

// Topology from https://github.com/ProxySQL/orchestrator/issues/137:
//
// source (e.g. AWS RDS, not readable by orchestrator: no backend row, no ancestry)
// └── intermediate
// └── leaf
//
// Returns the ancestry of the intermediate and the leaf as orchestrator computes them during discovery.
func ancestryTestChainWithUnreadableSource() (intermediateAncestry, leafAncestry string) {
// The source has no row in the backend, so the intermediate's master ancestry is empty.
intermediateAncestry = computeAncestryUUID("", ancestryTestSourceUUID, ancestryTestIntermediateUUID, "", false)
leafAncestry = computeAncestryUUID(intermediateAncestry, ancestryTestIntermediateUUID, ancestryTestLeafUUID, "", false)
return intermediateAncestry, leafAncestry
}

func TestComputeAncestryUUIDIncludesMasterUUID(t *testing.T) {
intermediateAncestry, leafAncestry := ancestryTestChainWithUnreadableSource()

intermediate := &Instance{ServerUUID: ancestryTestIntermediateUUID, AncestryUUID: intermediateAncestry}
leaf := &Instance{ServerUUID: ancestryTestLeafUUID, AncestryUUID: leafAncestry}
source := &Instance{ServerUUID: ancestryTestSourceUUID}

test.S(t).ExpectTrue(intermediate.IsDescendantOf(source))
test.S(t).ExpectTrue(leaf.IsDescendantOf(source))
test.S(t).ExpectTrue(leaf.IsDescendantOf(intermediate))
test.S(t).ExpectFalse(intermediate.IsDescendantOf(leaf))
}

func TestComputeAncestryUUIDNoDuplicates(t *testing.T) {
// Fully discovered chain: the master's stored ancestry already contains the master's UUID.
sourceAncestry := computeAncestryUUID("", "", ancestryTestSourceUUID, "", false)
test.S(t).ExpectEquals(sourceAncestry, ancestryTestSourceUUID)

intermediateAncestry := computeAncestryUUID(sourceAncestry, ancestryTestSourceUUID, ancestryTestIntermediateUUID, "", false)
test.S(t).ExpectEquals(intermediateAncestry, ancestryTestSourceUUID+","+ancestryTestIntermediateUUID)
}

func TestComputeAncestryUUIDCoMaster(t *testing.T) {
ancestry := computeAncestryUUID("whatever-stored", ancestryTestIntermediateUUID, ancestryTestLeafUUID, "", true)
test.S(t).ExpectEquals(ancestry, ancestryTestIntermediateUUID+","+ancestryTestLeafUUID)
}

func TestComputeAncestryUUIDReplicationGroup(t *testing.T) {
groupName := "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"
ancestry := computeAncestryUUID("", "", ancestryTestLeafUUID, groupName, false)
test.S(t).ExpectEquals(ancestry, ancestryTestLeafUUID+","+groupName)
}

// The leaf has applied more of the source's transactions than the intermediate's last stored
// executed_gtid_set shows. None of them may be considered errant (issue #137).
func TestErrantCheckIgnoresUpstreamGtidsWithUnreadableSource(t *testing.T) {
_, leafAncestry := ancestryTestChainWithUnreadableSource()

leafExecuted := ancestryTestSourceUUID + ":1-4335836," + ancestryTestIntermediateUUID + ":1-5"
redacted := redactExecutedGtidSetForErrantCheck(leafExecuted, leafAncestry, ancestryTestLeafUUID, false)
test.S(t).ExpectTrue(redacted.IsEmpty())
}

// Transactions written on the replica itself must still be reported as errant.
func TestErrantCheckKeepsLocalGtids(t *testing.T) {
intermediateAncestry, leafAncestry := ancestryTestChainWithUnreadableSource()

leafExecuted := ancestryTestSourceUUID + ":1-4335836," + ancestryTestLeafUUID + ":1-3"
redacted := redactExecutedGtidSetForErrantCheck(leafExecuted, leafAncestry, ancestryTestLeafUUID, false)
test.S(t).ExpectEquals(redacted.String(), ancestryTestLeafUUID+":1-3")

intermediateExecuted := ancestryTestSourceUUID + ":1-4335836," + ancestryTestIntermediateUUID + ":1-7"
redacted = redactExecutedGtidSetForErrantCheck(intermediateExecuted, intermediateAncestry, ancestryTestIntermediateUUID, false)
test.S(t).ExpectEquals(redacted.String(), ancestryTestIntermediateUUID+":1-7")
}
Loading