Skip to content

Commit ba4acae

Browse files
authored
GT-198 Cleanout calculation - picks members with the lowest number of shards (#1261)
1 parent b939de9 commit ba4acae

File tree

5 files changed

+58
-3
lines changed

5 files changed

+58
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
- (Improvement) Change Operator default ReplicasCount to 1
2020
- (Maintenance) Change MD content injection method
2121
- (Maintenance) Generate README Platforms
22+
- (Improvement) Cleanout calculation - picks members with the lowest number of shards
2223

2324
## [1.2.24](https://github.com/arangodb/kube-arangodb/tree/1.2.24) (2023-01-25)
2425
- (Bugfix) Fix deployment creation on ARM64

pkg/apis/deployment/v1/member_status.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ type MemberStatus struct {
5353
CreatedAt meta.Time `json:"created-at"`
5454
// Conditions specific to this member
5555
Conditions ConditionList `json:"conditions,omitempty"`
56-
// RecentTerminatons holds the times when this member was recently terminated.
56+
// RecentTerminations holds the times when this member was recently terminated.
5757
// First entry is the oldest. (do not add omitempty, since we want to be able to switch from a list to an empty list)
5858
RecentTerminations []meta.Time `json:"recent-terminations"`
5959
// IsInitialized is set after the very first time a pod was created for this member.

pkg/apis/deployment/v2alpha1/member_status.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ type MemberStatus struct {
5353
CreatedAt meta.Time `json:"created-at"`
5454
// Conditions specific to this member
5555
Conditions ConditionList `json:"conditions,omitempty"`
56-
// RecentTerminatons holds the times when this member was recently terminated.
56+
// RecentTerminations holds the times when this member was recently terminated.
5757
// First entry is the oldest. (do not add omitempty, since we want to be able to switch from a list to an empty list)
5858
RecentTerminations []meta.Time `json:"recent-terminations"`
5959
// IsInitialized is set after the very first time a pod was created for this member.

pkg/deployment/agency/state.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,42 @@ func (s State) CountShards() int {
132132
return count
133133
}
134134

135+
// ShardsByDbServers returns a map of DBServers and the amount of shards they have
136+
func (s State) ShardsByDbServers() map[Server]int {
137+
result := make(map[Server]int)
138+
139+
for _, collections := range s.Current.Collections {
140+
for _, shards := range collections {
141+
for _, shard := range shards {
142+
for _, server := range shard.Servers {
143+
result[server]++
144+
}
145+
}
146+
}
147+
}
148+
149+
return result
150+
}
151+
152+
// GetDBServerWithLowestShards returns the DBServer with the lowest amount of shards
153+
func (s State) GetDBServerWithLowestShards() Server {
154+
var resultServer Server = ""
155+
var resultShards int
156+
157+
for server, shards := range s.ShardsByDbServers() {
158+
// init first server as result
159+
if resultServer == "" {
160+
resultServer = server
161+
resultShards = shards
162+
} else if shards < resultShards {
163+
resultServer = server
164+
resultShards = shards
165+
}
166+
}
167+
return resultServer
168+
}
169+
170+
// PlanServers returns all servers which are part of the plan
135171
func (s State) PlanServers() Servers {
136172
q := map[Server]bool{}
137173

pkg/deployment/reconcile/plan_builder_scale.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,13 @@ func (r *Reconciler) createScalePlan(status api.DeploymentStatus, members api.Me
9393
Debug("Creating scale-up plan")
9494
} else if len(members) > count {
9595
// Note, we scale down 1 member at a time
96-
if m, err := members.SelectMemberToRemove(getCleanedServer(context), getToBeCleanedServer(context), topologyMissingMemberToRemoveSelector(status.Topology), topologyAwarenessMemberToRemoveSelector(group, status.Topology)); err != nil {
96+
97+
if m, err := members.SelectMemberToRemove(
98+
getCleanedServer(context),
99+
getToBeCleanedServer(context),
100+
topologyMissingMemberToRemoveSelector(status.Topology),
101+
topologyAwarenessMemberToRemoveSelector(group, status.Topology),
102+
getDbServerWithLowestShards(context, group)); err != nil {
97103
r.planLogger.Err(err).Str("role", group.AsRole()).Warn("Failed to select member to remove")
98104
} else {
99105
ready, message := groupReadyForRestart(context, status, m, group)
@@ -194,6 +200,18 @@ func getToBeCleanedServer(ctx reconciler.ArangoAgencyGet) api.MemberToRemoveSele
194200
}
195201
}
196202

203+
func getDbServerWithLowestShards(ctx reconciler.ArangoAgencyGet, g api.ServerGroup) api.MemberToRemoveSelector {
204+
return func(m api.MemberStatusList) (string, error) {
205+
if g != api.ServerGroupDBServers {
206+
return "", nil
207+
}
208+
if a, ok := ctx.GetAgencyCache(); ok {
209+
return string(a.GetDBServerWithLowestShards()), nil
210+
}
211+
return "", nil
212+
}
213+
}
214+
197215
func (r *Reconciler) scaleDownCandidate(ctx context.Context, apiObject k8sutil.APIObject,
198216
spec api.DeploymentSpec, status api.DeploymentStatus,
199217
context PlanBuilderContext) api.Plan {

0 commit comments

Comments
 (0)