-
Notifications
You must be signed in to change notification settings - Fork 277
Expand file tree
/
Copy pathhorizontal_scaling.go
More file actions
778 lines (743 loc) · 32.3 KB
/
Copy pathhorizontal_scaling.go
File metadata and controls
778 lines (743 loc) · 32.3 KB
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
/*
Copyright (C) 2022-2026 ApeCloud Co., Ltd
This file is part of KubeBlocks project
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package operations
import (
"fmt"
"slices"
"strconv"
"strings"
"time"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/utils/pointer"
"sigs.k8s.io/controller-runtime/pkg/client"
appsv1 "github.com/apecloud/kubeblocks/apis/apps/v1"
dpv1alpha1 "github.com/apecloud/kubeblocks/apis/dataprotection/v1alpha1"
opsv1alpha1 "github.com/apecloud/kubeblocks/apis/operations/v1alpha1"
"github.com/apecloud/kubeblocks/pkg/constant"
intctrlcomp "github.com/apecloud/kubeblocks/pkg/controller/component"
"github.com/apecloud/kubeblocks/pkg/controller/model"
"github.com/apecloud/kubeblocks/pkg/controller/plan"
intctrlutil "github.com/apecloud/kubeblocks/pkg/controllerutil"
)
type horizontalScalingOpsHandler struct{}
var _ OpsHandler = horizontalScalingOpsHandler{}
func init() {
hsHandler := horizontalScalingOpsHandler{}
horizontalScalingBehaviour := OpsBehaviour{
// if cluster is Abnormal or Failed, new opsRequest may repair it.
FromClusterPhases: appsv1.GetClusterUpRunningPhases(),
ToClusterPhase: appsv1.UpdatingClusterPhase,
QueueByCluster: true,
OpsHandler: hsHandler,
CancelFunc: hsHandler.Cancel,
}
opsMgr := GetOpsManager()
opsMgr.RegisterOps(opsv1alpha1.HorizontalScalingType, horizontalScalingBehaviour)
}
// ActionStartedCondition the started condition when handling the horizontal scaling request.
func (hs horizontalScalingOpsHandler) ActionStartedCondition(reqCtx intctrlutil.RequestCtx, cli client.Client, opsRes *OpsResource) (*metav1.Condition, error) {
return opsv1alpha1.NewHorizontalScalingCondition(opsRes.OpsRequest), nil
}
// Action modifies Cluster.spec.components[*].replicas from the opsRequest
func (hs horizontalScalingOpsHandler) Action(reqCtx intctrlutil.RequestCtx, cli client.Client, opsRes *OpsResource) error {
if slices.Contains([]appsv1.ClusterPhase{appsv1.StoppedClusterPhase,
appsv1.StoppingClusterPhase}, opsRes.Cluster.Status.Phase) {
return intctrlutil.NewFatalError("please start the cluster before scaling the cluster horizontally")
}
compOpsSet := newComponentOpsHelper(opsRes.OpsRequest.Spec.HorizontalScalingList)
// abort earlier running horizontal scaling opsRequest.
if err := abortEarlierOpsRequestWithSameKind(reqCtx, cli, opsRes, []opsv1alpha1.OpsType{opsv1alpha1.HorizontalScalingType, opsv1alpha1.StartType},
func(earlierOps *opsv1alpha1.OpsRequest) (bool, error) {
if slices.Contains([]opsv1alpha1.OpsType{opsv1alpha1.StartType, opsv1alpha1.StopType}, earlierOps.Spec.Type) {
return true, nil
}
for _, v := range earlierOps.Spec.HorizontalScalingList {
compOps, ok := compOpsSet.componentOpsSet[v.ComponentName]
if !ok {
return false, nil
}
currHorizontalScaling := compOps.(opsv1alpha1.HorizontalScaling)
// if the earlier opsRequest is pending return false.
if earlierOps.Status.Phase == opsv1alpha1.OpsPendingPhase {
return false, nil
}
if v.Shards != nil && currHorizontalScaling.Shards != nil {
return true, nil
}
// check if the instance to be taken offline was created by another opsRequest.
if err := hs.checkIntersectionWithEarlierOps(opsRes, earlierOps, currHorizontalScaling, v); err != nil {
return false, err
}
}
return false, nil
}); err != nil {
return err
}
// update shard count
for i := range opsRes.Cluster.Spec.Shardings {
sharding := &opsRes.Cluster.Spec.Shardings[i]
if compOps, ok := compOpsSet.componentOpsSet[sharding.Name]; ok {
horizontalScaling := compOps.(opsv1alpha1.HorizontalScaling)
if horizontalScaling.Shards != nil {
sharding.Shards = *horizontalScaling.Shards
}
}
}
if err := compOpsSet.updateClusterComponentsAndShardings(opsRes.Cluster, func(compSpec *appsv1.ClusterComponentSpec, obj ComponentOpsInterface) error {
horizontalScaling := obj.(opsv1alpha1.HorizontalScaling)
if horizontalScaling.Shards != nil {
return nil
}
lastCompConfiguration := opsRes.OpsRequest.Status.LastConfiguration.Components[obj.GetComponentName()]
if err := hs.validateHorizontalScaling(opsRes, lastCompConfiguration, horizontalScaling); err != nil {
return err
}
replicas, instances, offlineInstances, err := hs.getExpectedCompValues(opsRes, compSpec.DeepCopy(),
lastCompConfiguration, horizontalScaling)
if err != nil {
return err
}
var insReplicas int32
for _, v := range instances {
insReplicas += v.GetReplicas()
}
if insReplicas > replicas {
errMsg := fmt.Sprintf(`the total number of replicas for the instance template cannot be greater than the number of replicas for component "%s" after horizontally scaling`,
horizontalScaling.ComponentName)
return intctrlutil.NewFatalError(errMsg)
}
if horizontalScaling.ScaleOut != nil && horizontalScaling.ScaleOut.FromBackup != nil {
// Wait for the persistent volume to be restored from backup before proceeding.
return nil
}
compSpec.Replicas = replicas
compSpec.Instances = instances
compSpec.OfflineInstances = offlineInstances
return nil
}); err != nil {
return err
}
return cli.Update(reqCtx.Ctx, opsRes.Cluster)
}
// ReconcileAction will be performed when action is done and loops till OpsRequest.status.phase is Succeed/Failed.
// the Reconcile function for horizontal scaling opsRequest.
func (hs horizontalScalingOpsHandler) ReconcileAction(reqCtx intctrlutil.RequestCtx, cli client.Client, opsRes *OpsResource) (opsv1alpha1.OpsPhase, time.Duration, error) {
handleComponentProgress := func(
reqCtx intctrlutil.RequestCtx,
cli client.Client,
opsRes *OpsResource,
pgRes *progressResource,
compStatus *opsv1alpha1.OpsRequestComponentStatus) (int32, int32, error) {
horizontalScaling := pgRes.compOps.(opsv1alpha1.HorizontalScaling)
pgRes.noWaitComponentCompleted = true
if horizontalScaling.Shards != nil {
// horizontal scaling for shard count.
return handleComponentProgressForScalingShards(reqCtx, cli, opsRes, pgRes, compStatus)
}
var err error
lastCompConfiguration := opsRes.OpsRequest.Status.LastConfiguration.Components[pgRes.compOps.GetComponentName()]
clusterComponentSpec := pgRes.clusterComponent.DeepCopy()
if horizontalScaling.ScaleOut != nil && horizontalScaling.ScaleOut.FromBackup != nil {
if err := hs.restoreDataFromBackup(reqCtx, cli, opsRes, pgRes, clusterComponentSpec, horizontalScaling, lastCompConfiguration, compStatus); err != nil {
return 0, 0, err
}
}
pgRes.createdPodSet, pgRes.deletedPodSet, err = hs.getCreateAndDeletePodSet(opsRes, lastCompConfiguration, *clusterComponentSpec, horizontalScaling, pgRes.fullComponentName)
if err != nil {
return 0, 0, err
}
return handleComponentProgressForScalingReplicas(reqCtx, cli, opsRes, pgRes, compStatus)
}
compOpsHelper := newComponentOpsHelper(opsRes.OpsRequest.Spec.HorizontalScalingList)
return compOpsHelper.reconcileActionWithComponentOps(reqCtx, cli, opsRes, "", handleComponentProgress)
}
func (hs horizontalScalingOpsHandler) getBackupObj(reqCtx intctrlutil.RequestCtx,
cli client.Client,
opsRes *OpsResource,
fromBackup opsv1alpha1.FromBackup) (*dpv1alpha1.Backup, error) {
backupNamespace := opsRes.Cluster.Namespace
if fromBackup.Namespace != "" {
backupNamespace = fromBackup.Namespace
}
backupObj := &dpv1alpha1.Backup{}
if err := cli.Get(reqCtx.Ctx, client.ObjectKey{Namespace: backupNamespace, Name: fromBackup.Name}, backupObj); err != nil {
if apierrors.IsNotFound(err) {
return nil, intctrlutil.NewFatalError(fmt.Sprintf("backup %s not found", fromBackup.Name))
}
return nil, err
}
if backupObj.Status.Phase != dpv1alpha1.BackupPhaseCompleted {
return nil, intctrlutil.NewFatalError(fmt.Sprintf("backup %s phase is not completed", fromBackup.Name))
}
return backupObj, nil
}
func (hs horizontalScalingOpsHandler) createRestore(reqCtx intctrlutil.RequestCtx,
cli client.Client,
opsRes *OpsResource,
synthesizedComponent *intctrlcomp.SynthesizedComponent,
restoreMGR *plan.RestoreManager,
compSpecDeepyCopy *appsv1.ClusterComponentSpec,
backupObj *dpv1alpha1.Backup,
templateName string) error {
getTemplate := func(templateName string) *appsv1.InstanceTemplate {
if templateName == "" {
return nil
}
for _, template := range compSpecDeepyCopy.Instances {
if template.Name == templateName {
return &template
}
}
return nil
}
// create restore
restore, err := restoreMGR.BuildPrepareDataRestore(synthesizedComponent, backupObj, getTemplate(templateName))
if err != nil {
if intctrlutil.IsTargetError(err, intctrlutil.ErrorTypeRestoreFailed) {
return intctrlutil.NewFatalError(err.Error())
}
return err
}
if restore == nil {
return intctrlutil.NewFatalError(fmt.Sprintf("scale-out from backup %s/%s is not supported because backup method %q has no target volumes matching component %q",
backupObj.Namespace, backupObj.Name, backupObj.Status.BackupMethod.Name, synthesizedComponent.Name))
}
scheme, _ := opsv1alpha1.SchemeBuilder.Build()
if err := intctrlutil.SetOwnership(opsRes.OpsRequest, restore, scheme, ""); err != nil {
return err
}
if err := cli.Create(reqCtx.Ctx, restore); err != nil {
return err
}
reqCtx.Recorder.Eventf(opsRes.OpsRequest, corev1.EventTypeNormal, "RestoreCreated", "Restore %s created", restore.Name)
return nil
}
func (hs horizontalScalingOpsHandler) restoreDataFromBackup(reqCtx intctrlutil.RequestCtx,
cli client.Client,
opsRes *OpsResource,
pgRes *progressResource,
compSpecDeepyCopy *appsv1.ClusterComponentSpec,
horizontalScaling opsv1alpha1.HorizontalScaling,
lastCompConfiguration opsv1alpha1.LastComponentConfiguration,
compStatus *opsv1alpha1.OpsRequestComponentStatus) error {
restoreCompletedMsg := "Restore Data Completed"
// check if restore completed
if compStatus.Message == restoreCompletedMsg {
return nil
}
// get and check backup
fromBackup := horizontalScaling.ScaleOut.FromBackup
backupObj, err := hs.getBackupObj(reqCtx, cli, opsRes, *fromBackup)
if err != nil {
return err
}
replicas, instances, offlineInstances, err := hs.getExpectedCompValues(opsRes, compSpecDeepyCopy,
lastCompConfiguration, horizontalScaling)
if err != nil {
return err
}
compSpecDeepyCopy.Replicas = replicas
compSpecDeepyCopy.Instances = instances
compSpecDeepyCopy.OfflineInstances = offlineInstances
createdPodSet, _, err := hs.getCreateAndDeletePodSet(opsRes, lastCompConfiguration, *compSpecDeepyCopy, horizontalScaling, pgRes.fullComponentName)
if err != nil {
return err
}
comp, compDef, err := intctrlcomp.GetCompNCompDefByName(reqCtx.Ctx, cli, opsRes.Cluster.Namespace, constant.GenerateClusterComponentName(opsRes.Cluster.Name, pgRes.fullComponentName))
if err != nil {
return err
}
synthesizedComponent, err := intctrlcomp.BuildSynthesizedComponent(reqCtx.Ctx, cli, compDef, comp)
if err != nil {
return err
}
allRestoreCompleted := true
for podName, templateName := range createdPodSet {
idx := strings.LastIndex(podName, "-")
podIndex := podName[idx+1:]
podIndexInt, _ := strconv.ParseInt(podIndex, 10, 32)
restoreMGR := plan.NewRestoreManager(reqCtx.Ctx, cli, opsRes.Cluster, model.GetScheme(), map[string]string{
constant.OpsRequestNameLabelKey: opsRes.OpsRequest.Name,
constant.AppInstanceLabelKey: opsRes.Cluster.Name,
constant.KBAppComponentLabelKey: pgRes.compOps.GetComponentName(),
}, 1, int32(podIndexInt))
restoreMGR.RestoreTime = fromBackup.RestorePointInTime
restoreMGR.SetRestoreEnv(fromBackup.RestoreEnv)
restoreMGR.RestoreNamePrefix = string(opsRes.OpsRequest.UID[:8])
restoreMGR.SourceTargetName = fromBackup.SourceTargetName
// check restore status
restoreMeta := restoreMGR.GetRestoreObjectMeta(synthesizedComponent, dpv1alpha1.PrepareData, templateName)
restore := &dpv1alpha1.Restore{}
if err := cli.Get(reqCtx.Ctx, types.NamespacedName{Namespace: opsRes.Cluster.Namespace, Name: restoreMeta.Name}, restore); err != nil {
if apierrors.IsNotFound(err) {
allRestoreCompleted = false
if err = hs.createRestore(reqCtx, cli, opsRes, synthesizedComponent, restoreMGR, compSpecDeepyCopy, backupObj, templateName); err != nil {
return err
}
continue
}
return err
}
if restore.Status.Phase == dpv1alpha1.RestorePhaseFailed {
return intctrlutil.NewFatalError(fmt.Sprintf("restore for horizontalScaling failed: you can describe the restore resource \"%s\"", restore.Name))
}
if restore.Status.Phase != dpv1alpha1.RestorePhaseCompleted {
allRestoreCompleted = false
}
}
compStatus.Message = "Restore Data In Progress"
if allRestoreCompleted {
if err := hs.scaleOutComponentAfterRestoreData(reqCtx, cli, opsRes, compSpecDeepyCopy); err != nil {
return err
}
// set restore completed message
compStatus.Message = restoreCompletedMsg
}
return nil
}
func (hs horizontalScalingOpsHandler) scaleOutComponentAfterRestoreData(reqCtx intctrlutil.RequestCtx,
cli client.Client,
opsRes *OpsResource,
compSpecDeepyCopy *appsv1.ClusterComponentSpec) error {
for i := range opsRes.Cluster.Spec.ComponentSpecs {
compSpec := &opsRes.Cluster.Spec.ComponentSpecs[i]
if compSpec.Name == compSpecDeepyCopy.Name {
compSpec.Replicas = compSpecDeepyCopy.Replicas
compSpec.OfflineInstances = compSpecDeepyCopy.OfflineInstances
compSpec.Instances = compSpecDeepyCopy.Instances
}
}
return cli.Update(reqCtx.Ctx, opsRes.Cluster)
}
// SaveLastConfiguration records last configuration to the OpsRequest.status.lastConfiguration
func (hs horizontalScalingOpsHandler) SaveLastConfiguration(reqCtx intctrlutil.RequestCtx, cli client.Client, opsRes *OpsResource) error {
shardsMap := make(map[string]int32, len(opsRes.Cluster.Spec.Shardings))
for _, v := range opsRes.Cluster.Spec.Shardings {
shardsMap[v.Name] = v.Shards
}
getLastComponentInfo := func(compSpec appsv1.ClusterComponentSpec, comOps ComponentOpsInterface) opsv1alpha1.LastComponentConfiguration {
horizontalScaling := comOps.(opsv1alpha1.HorizontalScaling)
if horizontalScaling.Shards != nil {
var lastCompConfiguration opsv1alpha1.LastComponentConfiguration
if shards, ok := shardsMap[comOps.GetComponentName()]; ok {
lastCompConfiguration.Shards = pointer.Int32(shards)
}
return lastCompConfiguration
}
return opsv1alpha1.LastComponentConfiguration{
Replicas: pointer.Int32(compSpec.Replicas),
Instances: compSpec.Instances,
OfflineInstances: compSpec.OfflineInstances,
}
}
compOpsHelper := newComponentOpsHelper(opsRes.OpsRequest.Spec.HorizontalScalingList)
compOpsHelper.saveLastConfigurations(opsRes, getLastComponentInfo)
return nil
}
// getCreateAndDeletePodSet gets the pod set that are created and deleted in this opsRequest.
func (hs horizontalScalingOpsHandler) getCreateAndDeletePodSet(opsRes *OpsResource,
lastCompConfiguration opsv1alpha1.LastComponentConfiguration,
currCompSpec appsv1.ClusterComponentSpec,
horizontalScaling opsv1alpha1.HorizontalScaling,
fullCompName string) (map[string]string, map[string]string, error) {
clusterName := opsRes.Cluster.Name
runtime, err := opsRes.GetRuntime(horizontalScaling.ComponentName)
if err != nil {
return nil, nil, err
}
lastPodSet, err := runtime.GenerateInstanceNameSet(clusterName, fullCompName,
*lastCompConfiguration.Replicas, lastCompConfiguration.Instances, lastCompConfiguration.OfflineInstances)
if err != nil {
return nil, nil, err
}
expectReplicas, expectInstanceTpls, expectOfflineInstances, err := hs.getExpectedCompValues(opsRes, &currCompSpec, lastCompConfiguration, horizontalScaling)
if err != nil {
return nil, nil, err
}
currPodSet, err := runtime.GenerateInstanceNameSet(clusterName, fullCompName,
expectReplicas, expectInstanceTpls, expectOfflineInstances)
if err != nil {
return nil, nil, err
}
createPodSet := map[string]string{}
deletePodSet := map[string]string{}
for k := range currPodSet {
if _, ok := lastPodSet[k]; !ok {
createPodSet[k] = appsv1.GetInstanceTemplateName(clusterName, fullCompName, k)
}
}
for k := range lastPodSet {
if _, ok := currPodSet[k]; !ok {
deletePodSet[k] = appsv1.GetInstanceTemplateName(clusterName, fullCompName, k)
}
}
if opsRes.OpsRequest.Status.Phase == opsv1alpha1.OpsCancellingPhase {
// when cancelling this opsRequest, revert the changes.
return deletePodSet, createPodSet, nil
}
return createPodSet, deletePodSet, nil
}
// Cancel this function defines the cancel horizontalScaling action.
func (hs horizontalScalingOpsHandler) Cancel(reqCtx intctrlutil.RequestCtx, cli client.Client, opsRes *OpsResource) error {
for _, v := range opsRes.OpsRequest.Spec.HorizontalScalingList {
if v.Shards != nil {
// This operation requires intervention by operations personnel.
return intctrlutil.NewErrorf(intctrlutil.ErrorIgnoreCancel, "does not support cancellation of shard count changes during horizontal scaling.")
}
}
compOpsHelper := newComponentOpsHelper(opsRes.OpsRequest.Spec.HorizontalScalingList)
return compOpsHelper.cancelComponentOps(reqCtx.Ctx, cli, opsRes, func(lastConfig *opsv1alpha1.LastComponentConfiguration, comp *appsv1.ClusterComponentSpec) {
comp.Replicas = *lastConfig.Replicas
comp.Instances = lastConfig.Instances
comp.OfflineInstances = lastConfig.OfflineInstances
})
}
// checkIntersectionWithEarlierOps checks if the pod deleted by the current ops is a pod created by another ops
func (hs horizontalScalingOpsHandler) checkIntersectionWithEarlierOps(opsRes *OpsResource, earlierOps *opsv1alpha1.OpsRequest,
currOpsHScaling, earlierOpsHScaling opsv1alpha1.HorizontalScaling) error {
getCreatedOrDeletedPodSet := func(ops *opsv1alpha1.OpsRequest, hScaling opsv1alpha1.HorizontalScaling) (map[string]string, map[string]string, error) {
lastCompSnapshot := ops.Status.LastConfiguration.Components[earlierOpsHScaling.ComponentName]
compSpec := getComponentSpecOrShardingTemplate(opsRes.Cluster, earlierOpsHScaling.ComponentName).DeepCopy()
var err error
compSpec.Replicas, compSpec.Instances, compSpec.OfflineInstances, err = hs.getExpectedCompValues(opsRes, compSpec, lastCompSnapshot, hScaling)
if err != nil {
return nil, nil, err
}
return hs.getCreateAndDeletePodSet(opsRes, lastCompSnapshot, *compSpec, hScaling, hScaling.ComponentName)
}
createdPodSetForEarlier, _, err := getCreatedOrDeletedPodSet(earlierOps, earlierOpsHScaling)
if err != nil {
return err
}
_, deletedPodSetForCurrent, err := getCreatedOrDeletedPodSet(opsRes.OpsRequest, currOpsHScaling)
if err != nil {
return err
}
for deletedPod := range deletedPodSetForCurrent {
if _, ok := createdPodSetForEarlier[deletedPod]; ok {
errMsg := fmt.Sprintf(`instance "%s" cannot be taken offline as it has been created by another running opsRequest "%s"`,
deletedPod, earlierOps.Name)
return intctrlutil.NewFatalError(errMsg)
}
}
return nil
}
// getExpectedCompValues gets the expected replicas, instances, offlineInstances.
func (hs horizontalScalingOpsHandler) getExpectedCompValues(
opsRes *OpsResource,
compSpec *appsv1.ClusterComponentSpec,
lastCompConfiguration opsv1alpha1.LastComponentConfiguration,
horizontalScaling opsv1alpha1.HorizontalScaling) (int32, []appsv1.InstanceTemplate, []string, error) {
compReplicas := *lastCompConfiguration.Replicas
compInstanceTpls := slices.Clone(lastCompConfiguration.Instances)
compOfflineInstances := lastCompConfiguration.OfflineInstances
filteredHorizontal, err := filterHorizontalScalingSpec(opsRes, compReplicas, compInstanceTpls, compOfflineInstances, horizontalScaling.DeepCopy())
if err != nil {
return 0, nil, nil, err
}
expectOfflineInstances := hs.getCompExpectedOfflineInstances(compOfflineInstances, *filteredHorizontal)
err = hs.autoSyncReplicaChanges(opsRes, *filteredHorizontal, compReplicas, compInstanceTpls, expectOfflineInstances)
if err != nil {
return 0, nil, nil, err
}
return hs.getCompExpectReplicas(*filteredHorizontal, compReplicas),
hs.getCompExpectedInstances(compInstanceTpls, *filteredHorizontal),
expectOfflineInstances, nil
}
// only offlined instances could be taken online.
// and only onlined instances could be taken offline.
func filterHorizontalScalingSpec(
opsRes *OpsResource,
compReplicas int32,
compInstanceTpls []appsv1.InstanceTemplate,
compOfflineInstances []string,
horizontalScaling *opsv1alpha1.HorizontalScaling) (*opsv1alpha1.HorizontalScaling, error) {
offlineInstances := sets.New(compOfflineInstances...)
runtime, err := opsRes.GetRuntime(horizontalScaling.ComponentName)
if err != nil {
return nil, err
}
podSet, err := runtime.GenerateInstanceNameSet(opsRes.Cluster.Name, horizontalScaling.ComponentName,
compReplicas, compInstanceTpls, compOfflineInstances)
if err != nil {
return nil, err
}
if horizontalScaling.ScaleIn != nil && len(horizontalScaling.ScaleIn.OnlineInstancesToOffline) > 0 {
onlinedInstanceFromOps := sets.Set[string]{}
for _, insName := range horizontalScaling.ScaleIn.OnlineInstancesToOffline {
if _, ok := podSet[insName]; ok {
onlinedInstanceFromOps.Insert(insName)
}
}
horizontalScaling.ScaleIn.OnlineInstancesToOffline = sets.List(onlinedInstanceFromOps)
}
if horizontalScaling.ScaleOut != nil && len(horizontalScaling.ScaleOut.OfflineInstancesToOnline) > 0 {
offlinedInstanceFromOps := sets.Set[string]{}
for _, insName := range horizontalScaling.ScaleOut.OfflineInstancesToOnline {
if _, ok := offlineInstances[insName]; ok {
offlinedInstanceFromOps.Insert(insName)
}
}
horizontalScaling.ScaleOut.OfflineInstancesToOnline = sets.List(offlinedInstanceFromOps)
}
return horizontalScaling, nil
}
// autoSyncReplicaChanges auto-sync the replicaChanges of the component and instance templates.
func (hs horizontalScalingOpsHandler) autoSyncReplicaChanges(
opsRes *OpsResource,
horizontalScaling opsv1alpha1.HorizontalScaling,
compReplicas int32,
compInstanceTpls []appsv1.InstanceTemplate,
compExpectOfflineInstances []string) error {
// sync the replicaChanges for component and instance template.
getSyncedInstancesAndReplicaChanges := func(offlineOrOnlineInsCountMap map[string]int32,
replicaChanger opsv1alpha1.ReplicaChanger,
newInstances []appsv1.InstanceTemplate) ([]opsv1alpha1.InstanceReplicasTemplate, *int32) {
allReplicaChanges := int32(0)
insTplMap := map[string]sets.Empty{}
for _, v := range replicaChanger.Instances {
insTplMap[v.Name] = sets.Empty{}
allReplicaChanges += v.ReplicaChanges
}
for k, v := range offlineOrOnlineInsCountMap {
if k == "" {
allReplicaChanges += v
continue
}
if _, ok := insTplMap[k]; !ok {
// auto sync the replicaChanges for the instance template if the replicaChanges is not specified.
replicaChanger.Instances = append(replicaChanger.Instances, opsv1alpha1.InstanceReplicasTemplate{Name: k, ReplicaChanges: v})
allReplicaChanges += v
}
}
for _, v := range newInstances {
allReplicaChanges += v.GetReplicas()
}
if replicaChanger.ReplicaChanges != nil {
allReplicaChanges = *replicaChanger.ReplicaChanges
}
return replicaChanger.Instances, &allReplicaChanges
}
// auto sync the replicaChanges.
scaleIn := horizontalScaling.ScaleIn
if scaleIn != nil {
offlineInsCountMap := opsRes.OpsRequest.CountOfflineOrOnlineInstances(opsRes.Cluster.Name, horizontalScaling.ComponentName, scaleIn.OnlineInstancesToOffline)
scaleIn.Instances, scaleIn.ReplicaChanges = getSyncedInstancesAndReplicaChanges(offlineInsCountMap, scaleIn.ReplicaChanger, nil)
}
scaleOut := horizontalScaling.ScaleOut
if scaleOut != nil {
onlineInsCountMap, err := hs.getToOnlineInsCountMap(opsRes, horizontalScaling, compReplicas, compInstanceTpls, compExpectOfflineInstances)
if err != nil {
return err
}
scaleOut.Instances, scaleOut.ReplicaChanges = getSyncedInstancesAndReplicaChanges(onlineInsCountMap, scaleOut.ReplicaChanger, scaleOut.NewInstances)
}
return nil
}
func (hs horizontalScalingOpsHandler) getToOnlineInsCountMap(
opsRes *OpsResource,
horizontalScaling opsv1alpha1.HorizontalScaling,
compReplicas int32,
compInstanceTpls []appsv1.InstanceTemplate,
compExpectOfflineInstances []string) (map[string]int32, error) {
if horizontalScaling.ScaleOut.ReplicaChanges != nil || len(horizontalScaling.ScaleOut.OfflineInstancesToOnline) == 0 {
return nil, nil
}
compInstanceTplsClone := slices.Clone(compInstanceTpls)
slices.Sort(horizontalScaling.ScaleOut.OfflineInstancesToOnline)
// 1. Automatically synchronize replicaChanges based on the specified OfflineInstancesToOnline
offlineInsMap := map[string][]string{}
instanceTplChangesMap := map[string]int32{}
for _, tplChange := range horizontalScaling.ScaleOut.ReplicaChanger.Instances {
instanceTplChangesMap[tplChange.Name] = tplChange.ReplicaChanges
}
for _, insName := range horizontalScaling.ScaleOut.OfflineInstancesToOnline {
insTplName := appsv1.GetInstanceTemplateName(opsRes.Cluster.Name, horizontalScaling.ComponentName, insName)
if _, ok := instanceTplChangesMap[insTplName]; ok {
// Ignore the template instance that has already been specified in the replica changes.
continue
}
offlineInsMap[insTplName] = append(offlineInsMap[insTplName], insName)
compReplicas += 1
}
for i := range compInstanceTplsClone {
tplName := compInstanceTplsClone[i].Name
if insNames, ok := offlineInsMap[tplName]; ok {
compInstanceTplsClone[i].Replicas = pointer.Int32(compInstanceTplsClone[i].GetReplicas() + int32(len(insNames)))
}
}
// 2. obtain the updated Pod set after synchronization replicas.
runtime, err := opsRes.GetRuntime(horizontalScaling.ComponentName)
if err != nil {
return nil, err
}
podSet, err := runtime.GenerateInstanceNameSet(opsRes.Cluster.Name, horizontalScaling.ComponentName,
compReplicas, compInstanceTplsClone, compExpectOfflineInstances)
if err != nil {
return nil, err
}
// 3. count the number of online instances for each instance template.
onlineInsCountMap := map[string]int32{}
for insTplName, insNames := range offlineInsMap {
for _, insName := range insNames {
// Once replica synchronization is complete, sequentially process each instance in the toOnline list to confirm its successful creation.
// If any instance fails to come online, break it.
if _, ok := podSet[insName]; !ok {
break
}
onlineInsCountMap[insTplName]++
}
}
return onlineInsCountMap, nil
}
// getCompExpectReplicas gets the expected replicas for the component.
func (hs horizontalScalingOpsHandler) getCompExpectReplicas(horizontalScaling opsv1alpha1.HorizontalScaling,
compReplicas int32) int32 {
if horizontalScaling.ScaleOut != nil && horizontalScaling.ScaleOut.ReplicaChanges != nil {
compReplicas += *horizontalScaling.ScaleOut.ReplicaChanges
}
if horizontalScaling.ScaleIn != nil && horizontalScaling.ScaleIn.ReplicaChanges != nil {
compReplicas -= *horizontalScaling.ScaleIn.ReplicaChanges
}
return compReplicas
}
// getCompExpectedOfflineInstances gets the expected instance templates of the component.
func (hs horizontalScalingOpsHandler) getCompExpectedInstances(
compInstanceTpls []appsv1.InstanceTemplate,
horizontalScaling opsv1alpha1.HorizontalScaling,
) []appsv1.InstanceTemplate {
compInsTplSet := map[string]int{}
for i := range compInstanceTpls {
compInsTplSet[compInstanceTpls[i].Name] = i
}
handleInstanceTplReplicaChanges := func(instances []opsv1alpha1.InstanceReplicasTemplate, isScaleIn bool) {
for _, v := range instances {
compInsIndex, ok := compInsTplSet[v.Name]
if !ok {
continue
}
if isScaleIn {
compInstanceTpls[compInsIndex].Replicas = pointer.Int32(compInstanceTpls[compInsIndex].GetReplicas() - v.ReplicaChanges)
} else {
compInstanceTpls[compInsIndex].Replicas = pointer.Int32(compInstanceTpls[compInsIndex].GetReplicas() + v.ReplicaChanges)
}
}
}
if horizontalScaling.ScaleOut != nil {
compInstanceTpls = append(compInstanceTpls, horizontalScaling.ScaleOut.NewInstances...)
handleInstanceTplReplicaChanges(horizontalScaling.ScaleOut.Instances, false)
}
if horizontalScaling.ScaleIn != nil {
handleInstanceTplReplicaChanges(horizontalScaling.ScaleIn.Instances, true)
}
return compInstanceTpls
}
// getCompExpectedOfflineInstances gets the expected offlineInstances of the component.
func (hs horizontalScalingOpsHandler) getCompExpectedOfflineInstances(
compOfflineInstances []string,
horizontalScaling opsv1alpha1.HorizontalScaling,
) []string {
handleOfflineInstances := func(baseInstanceNames, comparedInstanceNames, newOfflineInstances []string) []string {
instanceNameSet := sets.New(comparedInstanceNames...)
for _, instanceName := range baseInstanceNames {
if _, ok := instanceNameSet[instanceName]; !ok {
newOfflineInstances = append(newOfflineInstances, instanceName)
}
}
return newOfflineInstances
}
if horizontalScaling.ScaleIn != nil && len(horizontalScaling.ScaleIn.OnlineInstancesToOffline) > 0 {
compOfflineInstances = handleOfflineInstances(horizontalScaling.ScaleIn.OnlineInstancesToOffline, compOfflineInstances, compOfflineInstances)
}
if horizontalScaling.ScaleOut != nil && len(horizontalScaling.ScaleOut.OfflineInstancesToOnline) > 0 {
compOfflineInstances = handleOfflineInstances(compOfflineInstances, horizontalScaling.ScaleOut.OfflineInstancesToOnline, make([]string, 0))
}
return compOfflineInstances
}
// validateHorizontalScaling validates the horizontal scaling if they are already offlined or onlined.
// if ignoreHscaleValidateAnnoKey is 'true', it would ignore and skip this validation.
func (hs horizontalScalingOpsHandler) validateHorizontalScaling(
opsRes *OpsResource,
lastCompConfiguration opsv1alpha1.LastComponentConfiguration,
horizontalScaling opsv1alpha1.HorizontalScaling,
) error {
if opsRes.OpsRequest.Annotations[constant.IgnoreHscaleValidateAnnoKey] == "true" {
return nil
}
if horizontalScaling.ScaleIn != nil {
if err := hs.validateOnlineInstancesToOffline(lastCompConfiguration,
horizontalScaling.ScaleIn.OnlineInstancesToOffline, opsRes, horizontalScaling.ComponentName); err != nil {
return err
}
}
if horizontalScaling.ScaleOut != nil {
if err := hs.validateOfflineInstancesToOnline(lastCompConfiguration,
horizontalScaling.ScaleOut.OfflineInstancesToOnline, horizontalScaling.ComponentName); err != nil {
return err
}
}
return nil
}
func (hs horizontalScalingOpsHandler) validateOnlineInstancesToOffline(
lastCompConfiguration opsv1alpha1.LastComponentConfiguration,
onlineInstancesToOffline []string,
opsRes *OpsResource,
componentName string) error {
if len(onlineInstancesToOffline) == 0 {
return nil
}
toOfflineSet := sets.New(onlineInstancesToOffline...)
if len(toOfflineSet) < len(onlineInstancesToOffline) {
return intctrlutil.NewFatalError("instances specified in onlineInstancesToOffline has duplicates")
}
runtime, err := opsRes.GetRuntime(componentName)
if err != nil {
return err
}
currPodSet, err := runtime.GenerateInstanceNameSet(opsRes.Cluster.Name, componentName,
*lastCompConfiguration.Replicas, lastCompConfiguration.Instances, lastCompConfiguration.OfflineInstances)
if err != nil {
return err
}
for _, onlineIns := range onlineInstancesToOffline {
if _, ok := currPodSet[onlineIns]; !ok {
return intctrlutil.NewFatalError(fmt.Sprintf(`instance "%s" specified in onlineInstancesToOffline is not online`, onlineIns))
}
}
return nil
}
func (hs horizontalScalingOpsHandler) validateOfflineInstancesToOnline(
lastCompConfiguration opsv1alpha1.LastComponentConfiguration,
offlineInstancesToOnline []string,
componentName string) error {
if len(offlineInstancesToOnline) == 0 {
return nil
}
toOnlineSet := sets.New(offlineInstancesToOnline...)
if len(toOnlineSet) < len(offlineInstancesToOnline) {
return intctrlutil.NewFatalError("instances specified in offlineInstancesToOnline has duplicates")
}
offlineInstanceSet := sets.New(lastCompConfiguration.OfflineInstances...)
for _, offlineIns := range offlineInstancesToOnline {
if _, ok := offlineInstanceSet[offlineIns]; !ok {
return intctrlutil.NewFatalError(fmt.Sprintf(`cannot find the offline instance "%s" in component "%s" for scaleOut operation`, offlineIns, componentName))
}
}
return nil
}