diff --git a/api/doris/v1/doriscluster_util.go b/api/doris/v1/doriscluster_util.go index bbfc98f..3ecfc74 100644 --- a/api/doris/v1/doriscluster_util.go +++ b/api/doris/v1/doriscluster_util.go @@ -61,6 +61,8 @@ const ( Component_Broker ComponentType = "broker" ) +var DefaultFeElectionNumber int32 = 3 + func GenerateExternalServiceName(dcr *DorisCluster, componentType ComponentType) string { switch componentType { case Component_FE: @@ -366,3 +368,10 @@ func IsReconcilingStatusPhase(c *ComponentStatus) bool { c.ComponentCondition.Phase == Restarting || c.ComponentCondition.Phase == Reconciling } + +func (dcr *DorisCluster) GetElectionNumber() int32 { + if dcr.Spec.FeSpec.ElectionNumber != nil { + return *dcr.Spec.FeSpec.ElectionNumber + } + return DefaultFeElectionNumber +} diff --git a/api/doris/v1/doriscluster_webhook.go b/api/doris/v1/doriscluster_webhook.go index 0940822..e69f9fe 100644 --- a/api/doris/v1/doriscluster_webhook.go +++ b/api/doris/v1/doriscluster_webhook.go @@ -77,7 +77,7 @@ func (r *DorisCluster) ValidateUpdate(old runtime.Object) error { klog.Info("validate update", "name", r.Name) var errors []error // fe FeSpec.Replicas must greater than or equal to FeSpec.ElectionNumber - if *r.Spec.FeSpec.Replicas < *r.Spec.FeSpec.ElectionNumber { + if *r.Spec.FeSpec.Replicas < r.GetElectionNumber() { errors = append(errors, fmt.Errorf("'FeSpec.Replicas' error: the number of FeSpec.Replicas should greater than or equal to FeSpec.ElectionNumber")) } diff --git a/pkg/controller/sub_controller/fe/pod.go b/pkg/controller/sub_controller/fe/pod.go index 0073214..e1c0af5 100644 --- a/pkg/controller/sub_controller/fe/pod.go +++ b/pkg/controller/sub_controller/fe/pod.go @@ -26,10 +26,6 @@ import ( corev1 "k8s.io/api/core/v1" ) -var ( - Default_Election_Number int32 = 3 -) - func (fc *Controller) buildFEPodTemplateSpec(dcr *v1.DorisCluster) corev1.PodTemplateSpec { podTemplateSpec := resource.NewPodTemplateSpec(dcr, v1.Component_FE) var containers []corev1.Container @@ -54,9 +50,7 @@ func (fc *Controller) feContainer(dcr *v1.DorisCluster, config map[string]interf queryPort = strconv.FormatInt(int64(port), 10) } - if dcr.Spec.FeSpec.ElectionNumber == nil { - dcr.Spec.FeSpec.ElectionNumber = resource.GetInt32Pointer(Default_Election_Number) - } + ele := dcr.GetElectionNumber() ports := resource.GetContainerPorts(config, v1.Component_FE) c.Name = "fe" @@ -67,14 +61,10 @@ func (fc *Controller) feContainer(dcr *v1.DorisCluster, config map[string]interf }, corev1.EnvVar{ Name: resource.ENV_FE_PORT, Value: queryPort, + }, corev1.EnvVar{ + Name: resource.ENV_FE_ELECT_NUMBER, + Value: strconv.FormatInt(int64(ele), 10), }) - if dcr.Spec.FeSpec.ElectionNumber != nil { - c.Env = append(c.Env, corev1.EnvVar{ - Name: resource.ENV_FE_ELECT_NUMBER, - Value: strconv.FormatInt(int64(*dcr.Spec.FeSpec.ElectionNumber), 10), - }) - } - return c } diff --git a/pkg/controller/sub_controller/fe/prepare_modify.go b/pkg/controller/sub_controller/fe/prepare_modify.go index 938ac27..c484c5d 100644 --- a/pkg/controller/sub_controller/fe/prepare_modify.go +++ b/pkg/controller/sub_controller/fe/prepare_modify.go @@ -44,10 +44,11 @@ func (fc *Controller) prepareStatefulsetApply(ctx context.Context, cluster *v1.D cluster.Spec.FeSpec.Replicas = resource.GetInt32Pointer(0) } - if *(cluster.Spec.FeSpec.Replicas) < *(cluster.Spec.FeSpec.ElectionNumber) { + ele := cluster.GetElectionNumber() + + if *(cluster.Spec.FeSpec.Replicas) < ele { fc.K8srecorder.Event(cluster, string(sc.EventWarning), string(sc.FESpecSetError), "The number of fe ElectionNumber is large than Replicas, Replicas has been corrected to the correct minimum value") - klog.Errorf("prepareStatefulsetApply namespace=%s,name=%s ,The number of fe ElectionNumber(%d) is large than Replicas(%d)", cluster.Namespace, cluster.Name, *(cluster.Spec.FeSpec.ElectionNumber), *(cluster.Spec.FeSpec.Replicas)) - ele := *(cluster.Spec.FeSpec.ElectionNumber) + klog.Errorf("prepareStatefulsetApply namespace=%s,name=%s ,The number of fe ElectionNumber(%d) is large than Replicas(%d)", cluster.Namespace, cluster.Name, ele, *(cluster.Spec.FeSpec.Replicas)) cluster.Spec.FeSpec.Replicas = &ele } @@ -104,10 +105,8 @@ func (fc *Controller) dropObserverBySqlClient(ctx context.Context, k8sclient cli } // make sure needRemovedAmount, this may involve retrying tasks and scaling down followers. - electionNumber := Default_Election_Number - if targetDCR.Spec.FeSpec.ElectionNumber != nil { - electionNumber = *(targetDCR.Spec.FeSpec.ElectionNumber) - } + electionNumber := targetDCR.GetElectionNumber() + // means: needRemovedAmount = allobservers - (replicas - election) needRemovedAmount := int32(len(allObserves)) - *(targetDCR.Spec.FeSpec.Replicas) + electionNumber if needRemovedAmount <= 0 {