From a111a9c2e2ca16e35c0b581201910861163c6fa1 Mon Sep 17 00:00:00 2001 From: Andrii Dema Date: Mon, 12 Aug 2024 13:15:56 +0300 Subject: [PATCH] K8SPS-372: fix bootstrap for cluster with 2 mysql pods (#722) https://perconadev.atlassian.net/browse/K8SPS-372 Co-authored-by: Viacheslav Sarzhan --- cmd/bootstrap/async_replication.go | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/cmd/bootstrap/async_replication.go b/cmd/bootstrap/async_replication.go index 4da4426d3..c0b4fc400 100644 --- a/cmd/bootstrap/async_replication.go +++ b/cmd/bootstrap/async_replication.go @@ -37,18 +37,18 @@ func bootstrapAsyncReplication(ctx context.Context) error { } log.Printf("Peers: %v", sets.List(peers)) - primary, replicas, err := getTopology(ctx, peers) - if err != nil { - return errors.Wrap(err, "select donor") - } - log.Printf("Primary: %s Replicas: %v", primary, replicas) - fqdn, err := getFQDN(mysqlSvc) if err != nil { return errors.Wrap(err, "get FQDN") } log.Printf("FQDN: %s", fqdn) + primary, replicas, err := getTopology(ctx, fqdn, peers) + if err != nil { + return errors.Wrap(err, "select donor") + } + log.Printf("Primary: %s Replicas: %v", primary, replicas) + podHostname, err := os.Hostname() if err != nil { return errors.Wrap(err, "get hostname") @@ -189,7 +189,7 @@ func bootstrapAsyncReplication(ctx context.Context) error { return nil } -func getTopology(ctx context.Context, peers sets.Set[string]) (string, []string, error) { +func getTopology(ctx context.Context, fqdn string, peers sets.Set[string]) (string, []string, error) { replicas := sets.New[string]() primary := "" @@ -227,7 +227,15 @@ func getTopology(ctx context.Context, peers sets.Set[string]) (string, []string, if primary == "" && peers.Len() == 1 { primary = sets.List(peers)[0] } else if primary == "" { - primary = sets.List(replicas)[0] + for _, r := range sets.List(replicas) { + // We should set primary to the first replica, which is not the bootstrapped pod. + // The bootstrapped pod can't be a primary. + // Even if it was a primary before, orchestrator will promote another replica "as result of DeadMaster". + if r != fqdn { + primary = r + break + } + } } if replicas.Len() > 0 {