From a47555008b0d45e3b3494d7d1b025ed93d927dbd Mon Sep 17 00:00:00 2001 From: Masayuki Ishii Date: Thu, 19 Oct 2023 03:14:59 +0000 Subject: [PATCH] Remove copy code of FindTopRunner Signed-off-by: Masayuki Ishii --- clustering/mock_test.go | 59 ---------------------------------------- clustering/operations.go | 2 +- pkg/dbop/gtid.go | 5 +++- pkg/dbop/gtid_test.go | 10 +++---- pkg/dbop/nop.go | 4 --- pkg/dbop/operator.go | 5 ---- 6 files changed, 10 insertions(+), 75 deletions(-) diff --git a/clustering/mock_test.go b/clustering/mock_test.go index 286b51389..215c85d03 100644 --- a/clustering/mock_test.go +++ b/clustering/mock_test.go @@ -217,65 +217,6 @@ func (o *mockOperator) IsSubsetGTID(ctx context.Context, set1, set2 string) (boo return len(map1) == 0, nil } -// FindTopRunner returns the index of the slice whose `GlobalVariables.ExecutedGtidSet` -// is most advanced. This may return ErrErrantTransactions for errant transactions -// or ErrNoTopRunner if there is no such instance. -func (o *mockOperator) FindTopRunner(ctx context.Context, status []*dbop.MySQLInstanceStatus) (int, error) { - if o.failing { - return -1, errors.New("mysqld is down") - } - - latest := -1 - var latestGTID string - - for i := 0; i < len(status); i++ { - if status[i] == nil { - continue - } - repl := status[i].ReplicaStatus - if repl == nil { - continue - } - - gtid := repl.RetrievedGtidSet - if len(gtid) == 0 { - continue - } - - if len(latestGTID) == 0 { - latest = i - latestGTID = gtid - continue - } - - isSubset, err := o.IsSubsetGTID(ctx, gtid, latestGTID) - if err != nil { - return -1, err - } - if isSubset { - continue - } - - isSubset, err = o.IsSubsetGTID(ctx, latestGTID, gtid) - if err != nil { - return -1, err - } - if isSubset { - latest = i - latestGTID = gtid - continue - } - - return -1, fmt.Errorf("%w: set1=%s, set2=%s", dbop.ErrErrantTransactions, gtid, latestGTID) - } - - if latest == -1 { - return -1, dbop.ErrNoTopRunner - } - - return latest, nil -} - // ConfigureReplica configures client-side replication. // If `symisync` is true, it enables client-side semi-synchronous replication. // In either case, it disables server-side semi-synchronous replication. diff --git a/clustering/operations.go b/clustering/operations.go index c5975e91c..611487fd9 100644 --- a/clustering/operations.go +++ b/clustering/operations.go @@ -218,7 +218,7 @@ func (p *managerProcess) failover(ctx context.Context, ss *StatusSet) error { candidates[i] = newStatus } - candidate, err := op.FindTopRunner(ctx, candidates) + candidate, err := dbop.FindTopRunner(ctx, op, candidates) if err != nil { return fmt.Errorf("failed to choose the next primary: %w", err) } diff --git a/pkg/dbop/gtid.go b/pkg/dbop/gtid.go index 15b7d8390..e9eb91ed7 100644 --- a/pkg/dbop/gtid.go +++ b/pkg/dbop/gtid.go @@ -5,7 +5,10 @@ import ( "fmt" ) -func (o *operator) FindTopRunner(ctx context.Context, status []*MySQLInstanceStatus) (int, error) { +// FindTopRunner returns the index of the slice whose `GlobalVariables.ExecutedGtidSet` +// is most advanced. This may return ErrErrantTransactions for errant transactions +// or ErrNoTopRunner if there is no such instance. +func FindTopRunner(ctx context.Context, o Operator, status []*MySQLInstanceStatus) (int, error) { latest := -1 var latestGTIDs string diff --git a/pkg/dbop/gtid_test.go b/pkg/dbop/gtid_test.go index fa91bba70..fb040136b 100644 --- a/pkg/dbop/gtid_test.go +++ b/pkg/dbop/gtid_test.go @@ -24,7 +24,7 @@ var _ = Describe("FindTopRunner", func() { defer op.Close() statuses := make([]*MySQLInstanceStatus, 3) - _, err = op.FindTopRunner(context.Background(), statuses) + _, err = FindTopRunner(context.Background(), op, statuses) Expect(err).To(MatchError(ErrNoTopRunner)) set0 := `8e349184-bc14-11e3-8d4c-0800272864ba:1-29` @@ -33,21 +33,21 @@ var _ = Describe("FindTopRunner", func() { statuses[0] = &MySQLInstanceStatus{ReplicaStatus: &ReplicaStatus{RetrievedGtidSet: set0}} statuses[1] = &MySQLInstanceStatus{ReplicaStatus: &ReplicaStatus{RetrievedGtidSet: set1}} statuses[2] = &MySQLInstanceStatus{ReplicaStatus: &ReplicaStatus{RetrievedGtidSet: set2}} - top, err := op.FindTopRunner(context.Background(), statuses) + top, err := FindTopRunner(context.Background(), op, statuses) Expect(err).NotTo(HaveOccurred()) Expect(top).To(Equal(2)) statuses[0] = &MySQLInstanceStatus{ReplicaStatus: &ReplicaStatus{RetrievedGtidSet: set2}} statuses[1] = &MySQLInstanceStatus{ReplicaStatus: &ReplicaStatus{RetrievedGtidSet: set0}} statuses[2] = &MySQLInstanceStatus{ReplicaStatus: &ReplicaStatus{RetrievedGtidSet: set1}} - top, err = op.FindTopRunner(context.Background(), statuses) + top, err = FindTopRunner(context.Background(), op, statuses) Expect(err).NotTo(HaveOccurred()) Expect(top).To(Equal(0)) statuses[0] = &MySQLInstanceStatus{ReplicaStatus: &ReplicaStatus{RetrievedGtidSet: set1}} statuses[1] = nil statuses[2] = &MySQLInstanceStatus{ReplicaStatus: &ReplicaStatus{RetrievedGtidSet: set2}} - top, err = op.FindTopRunner(context.Background(), statuses) + top, err = FindTopRunner(context.Background(), op, statuses) Expect(err).NotTo(HaveOccurred()) Expect(top).To(Equal(2)) @@ -59,7 +59,7 @@ var _ = Describe("FindTopRunner", func() { statuses[0] = &MySQLInstanceStatus{ReplicaStatus: &ReplicaStatus{RetrievedGtidSet: set0}} statuses[1] = &MySQLInstanceStatus{ReplicaStatus: &ReplicaStatus{RetrievedGtidSet: set1}} statuses[2] = nil - _, err = op.FindTopRunner(context.Background(), statuses) + _, err = FindTopRunner(context.Background(), op, statuses) Expect(err).To(MatchError(ErrErrantTransactions)) }) }) diff --git a/pkg/dbop/nop.go b/pkg/dbop/nop.go index 15ead15da..a07191cf7 100644 --- a/pkg/dbop/nop.go +++ b/pkg/dbop/nop.go @@ -35,10 +35,6 @@ func (o NopOperator) IsSubsetGTID(ctx context.Context, set1, set2 string) (bool, return false, ErrNop } -func (o NopOperator) FindTopRunner(context.Context, []*MySQLInstanceStatus) (int, error) { - return 0, ErrNop -} - func (o NopOperator) ConfigureReplica(ctx context.Context, source AccessInfo, semisync bool) error { return ErrNop } diff --git a/pkg/dbop/operator.go b/pkg/dbop/operator.go index 510d21d4e..3f2262b2f 100644 --- a/pkg/dbop/operator.go +++ b/pkg/dbop/operator.go @@ -37,11 +37,6 @@ type Operator interface { // IsSubsetGTID returns true if set1 is a subset of set2. IsSubsetGTID(ctx context.Context, set1, set2 string) (bool, error) - // FindTopRunner returns the index of the slice whose `GlobalVariables.ExecutedGtidSet` - // is most advanced. This may return ErrErrantTransactions for errant transactions - // or ErrNoTopRunner if there is no such instance. - FindTopRunner(context.Context, []*MySQLInstanceStatus) (int, error) - // ConfigureReplica configures client-side replication. // If `symisync` is true, it enables client-side semi-synchronous replication. // In either case, it disables server-side semi-synchronous replication.