/
action_rebalancer_generate_v2.go
166 lines (130 loc) · 4.22 KB
/
action_rebalancer_generate_v2.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
//
// DISCLAIMER
//
// Copyright 2016-2023 ArangoDB GmbH, Cologne, Germany
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Copyright holder is ArangoDB GmbH, Cologne, Germany
//
package reconcile
import (
"context"
meta "k8s.io/apimachinery/pkg/apis/meta/v1"
"github.com/arangodb/go-driver"
api "github.com/arangodb/kube-arangodb/pkg/apis/deployment/v1"
"github.com/arangodb/kube-arangodb/pkg/deployment/client"
"github.com/arangodb/kube-arangodb/pkg/util"
"github.com/arangodb/kube-arangodb/pkg/util/globals"
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil"
)
func newRebalancerGenerateV2Action(action api.Action, actionCtx ActionContext) Action {
a := &actionRebalancerGenerateV2{}
a.actionImpl = newActionImplDefRef(action, actionCtx)
return a
}
type actionRebalancerGenerateV2 struct {
actionImpl
actionEmptyCheckProgress
}
func (r actionRebalancerGenerateV2) Start(ctx context.Context) (bool, error) {
spec := r.actionCtx.GetSpec()
if spec.Rebalancer == nil {
if err := r.actionCtx.WithStatusUpdate(ctx, func(s *api.DeploymentStatus) bool {
if s.Rebalancer == nil {
return false
}
s.Rebalancer = nil
return true
}); err != nil {
r.log.Err(err).Warn("Unable to propagate changes")
return true, nil
}
return true, nil
}
c, err := r.actionCtx.GetMembersState().State().GetDatabaseClient()
if err != nil {
r.log.Err(err).Error("Unable to get client")
return true, nil
}
nctx, cancel := globals.GetGlobalTimeouts().ArangoD().WithTimeout(ctx)
defer cancel()
resp, err := client.NewClient(c.Connection(), r.log).GenerateRebalanceMoves(nctx, &client.RebalancePlanRequest{
MaximumNumberOfMoves: util.NewType(spec.Rebalancer.GetParallelMoves()),
})
if err != nil {
r.log.Err(err).Error("Unable to generate rebalancer moves")
return true, nil
}
if len(resp.Result.Moves) > 0 {
cache, ok := r.actionCtx.GetAgencyCache()
if !ok {
r.log.Debug("AgencyCache is not ready")
return true, nil
}
actions := make(RebalanceActions, len(resp.Result.Moves))
for id, move := range resp.Result.Moves {
db, ok := cache.GetCollectionDatabaseByID(move.Collection.String())
if !ok {
r.log.Warn("Database not found for Collection %s", move.Collection)
return true, nil
}
actions[id] = RebalanceAction{
Database: db,
Collection: move.Collection.String(),
Shard: move.Shard,
From: move.From,
To: move.To,
}
}
cluster, err := c.Cluster(ctx)
if err != nil {
r.log.Err(err).Warn("Unable to get cluster")
return true, nil
}
if err := r.executeActions(ctx, spec.Rebalancer.GetParallelMoves(), c, cluster, actions); err != nil {
r.log.Err(err).Warn("Unable to execute actions")
return true, nil
}
return true, nil
}
if err := r.actionCtx.WithStatusUpdate(ctx, func(s *api.DeploymentStatus) bool {
s.Rebalancer = &api.ArangoDeploymentRebalancerStatus{
LastCheckTime: k8sutil.NewTime(meta.Now()),
}
return true
}); err != nil {
r.log.Err(err).Warn("Unable to save plan")
return true, nil
}
return true, nil
}
func (r actionRebalancerGenerateV2) executeActions(ctx context.Context, size int, client driver.Client, cluster driver.Cluster, a RebalanceActions) error {
if len(a) > size {
a = a[0:size]
}
r.actionCtx.Metrics().GetRebalancer().AddMoves(len(a))
ids, errors := runMoveJobs(ctx, client, cluster, a)
r.actionCtx.Metrics().GetRebalancer().AddFailures(len(errors))
for _, err := range errors {
r.log.Err(err).Warn("MoveShard failed")
}
if err := r.actionCtx.WithStatusUpdate(ctx, func(s *api.DeploymentStatus) bool {
s.Rebalancer = &api.ArangoDeploymentRebalancerStatus{}
s.Rebalancer.MoveJobs = append(s.Rebalancer.MoveJobs, ids...)
return true
}); err != nil {
return err
}
return nil
}