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

[fix](dcr)fix dcr electionnumber nil #285

Merged
merged 3 commits into from
Nov 8, 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
9 changes: 9 additions & 0 deletions api/doris/v1/doriscluster_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
}
2 changes: 1 addition & 1 deletion api/doris/v1/doriscluster_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
}

Expand Down
18 changes: 4 additions & 14 deletions pkg/controller/sub_controller/fe/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"
Expand All @@ -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
}
13 changes: 6 additions & 7 deletions pkg/controller/sub_controller/fe/prepare_modify.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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 {
Expand Down
Loading