Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[operator] run leader balance multiple times until balanced. #532

Merged
merged 1 commit into from
Nov 9, 2024
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
12 changes: 11 additions & 1 deletion pkg/controller/component/storaged_updater.go
Original file line number Diff line number Diff line change
Expand Up @@ -467,10 +467,20 @@ func (s *storagedUpdater) balanceLeader(mc nebula.MetaInterface, nc *v1alpha1.Ne
if time.Now().Before(lastBalancedTime.Add(BalanceLeaderInterval * time.Second)) {
return utilerrors.ReconcileErrorf("partition leader is balancing")
}

balanced, err := mc.IsLeaderBalanced(space.Name)
if err != nil {
return utilerrors.ReconcileErrorf("failed to check if the leader is balanced for space %s: %v", space.Name, err)
}

if balanced {
nc.Status.Storaged.BalancedSpaces = append(nc.Status.Storaged.BalancedSpaces, *space.Id.SpaceID)
continue
}

if err := mc.BalanceLeader(*space.Id.SpaceID); err != nil {
return err
}
nc.Status.Storaged.BalancedSpaces = append(nc.Status.Storaged.BalancedSpaces, *space.Id.SpaceID)
nc.Status.Storaged.LastBalancedTime = &metav1.Time{Time: time.Now()}
return utilerrors.ReconcileErrorf("space %d need to be synced", *space.Id.SpaceID)
}
Expand Down
19 changes: 19 additions & 0 deletions pkg/nebula/meta_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ type (
GetSpaceLeaderHosts(space []byte) ([]string, error)
GetLeaderCount(leaderHost string) (int, error)
BalanceStatus(jobID int32, spaceID nebula.GraphSpaceID) error
IsLeaderBalanced(spaceName []byte) (bool, error)
BalanceLeader(spaceID nebula.GraphSpaceID) error
BalanceData(spaceID nebula.GraphSpaceID) (int32, error)
BalanceDataInZone(spaceID nebula.GraphSpaceID) (int32, error)
Expand Down Expand Up @@ -356,6 +357,24 @@ func (m *metaClient) BalanceLeader(spaceID nebula.GraphSpaceID) error {
return nil
}

func (m *metaClient) IsLeaderBalanced(spaceName []byte) (bool, error) {
hosts, err := m.ListHosts(meta.ListHostType_ALLOC)
if err != nil {
return false, err
}

for _, host := range hosts {
if host.Status != meta.HostStatus_ONLINE {
continue
}
if host.LeaderParts[(string)(spaceName)] == nil {
return false, nil
}
}

return true, nil
}

func (m *metaClient) runAdminJob(req *meta.AdminJobReq) (int32, error) {
resp, err := m.retryOnError(req, func(req interface{}) (interface{}, error) {
resp, err := m.client.RunAdminJob(req.(*meta.AdminJobReq))
Expand Down