You are an expert in writing code in many different languages. Your goal is to perform code completion for the following code, keeping in mind the rest of the code and the file meta data.
    Metadata details:
    filename: dm_master_failover.go
    path of file: ./repos/tidb-operator/pkg/manager/member
    
    the code of the file until where you have to start completion: // Copyright 2020 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may no
    
func (f *masterFailover) Failover(dc *v1alpha1.DMCluster) error {
	ns := dc.GetNamespace()
	dcName := dc.GetName()

	if !dc.Status.Master.Synced {
		return fmt.Errorf("DMCluster: %s/%s's dm-master status sync failed, can't failover", ns, dcName)
	}
	if dc.Status.Master.FailureMembers == nil {
		dc.Status.Master.FailureMembers = map[string]v1alpha1.MasterFailureMember{}
	}

	healthCount := 0
	for podName, masterMember := range dc.Status.Master.Members {
		if masterMember.Health {
			healthCount++
		} else {
			f.deps.Recorder.Eventf(dc, apiv1.EventTypeWarning, "MasterMemberUnhealthy",
				"%s(%s) is unhealthy", podName, masterMember.ID)
		}
	}
	inQuorum := healthCount > len(dc.Status.Master.Members)/2
	if !inQuorum {
		return fmt.Errorf("DMCluster: %s/%s's dm-master cluster is not healthy: %d/%d, "+
			"replicas: %d, failureCount: %d, can't failover",
			ns, dcName, healthCount, dc.MasterStsDesiredReplicas(), dc.Spec.Master.Replicas, len(dc.Status.Master.FailureMembers))
	}

	failureReplicas := getDMMasterFailureReplicas(dc)
	if failureReplicas >= int(*dc.Spec.Master.MaxFailoverCount) {
		klog.Errorf("dm-master failover replicas (%d) reaches the limit (%d), skip failover", failureReplicas, *dc.Spec.Master.MaxFailoverCount)
		return nil
	}

	notDeletedCount := 0
	for _, masterMember := range dc.Status.Master.FailureMembers {
		if !masterMember.MemberDeleted {
			notDeletedCount++
		}
	}
	// we can only failover one at a time
	if notDeletedCount == 0 {
		return f.tryToMarkAPeerAsFailure(dc)
	}

	return f.tryToDeleteAFailureMember(dc)
}