From ccb6361df21a492d2f4bca2770581b94ddda79d0 Mon Sep 17 00:00:00 2001 From: qiffang <947321353@qq.com> Date: Tue, 3 Mar 2020 16:24:39 +0800 Subject: [PATCH 1/3] AutoScaling: adding status type --- .../v1alpha1/tidbclusterautoscaler_types.go | 39 +++++++++++++- .../pingcap/v1alpha1/zz_generated.deepcopy.go | 52 +++++++++++++++++++ pkg/autoscaler/autoscaler/calculate/cpu.go | 19 +++++++ pkg/autoscaler/autoscaler/tidb_autoscaler.go | 1 + pkg/autoscaler/autoscaler/tikv_autoscaler.go | 1 + 5 files changed, 111 insertions(+), 1 deletion(-) diff --git a/pkg/apis/pingcap/v1alpha1/tidbclusterautoscaler_types.go b/pkg/apis/pingcap/v1alpha1/tidbclusterautoscaler_types.go index a4639eabd8..6a970c37dc 100644 --- a/pkg/apis/pingcap/v1alpha1/tidbclusterautoscaler_types.go +++ b/pkg/apis/pingcap/v1alpha1/tidbclusterautoscaler_types.go @@ -149,6 +149,43 @@ type TidbMonitorRef struct { Name string `json:"name"` } -// TODO: sync status +// BasicAutoScalerSpec describes the basic status for auto-scaling +type BaseAutoScalerStatus struct { + // name is the name of the given metric + Name string `json:"name,omitempty"` + + // CurrentValue is the current value of the metric. + CurrentValue float64 `json:"value,omitempty"` + + // TargetValue is the target value of the metric. + TargetValue float64 `json:"value,omitempty"` + + // lastScaleTime is the last time the HorizontalPodAutoscaler scaled the number of pods, + // used by the autoscaler to control how often the number of pods is changed. + // +optional + LastScaleTime string `json:"lastScaleTime,omitempty" protobuf:"bytes,2,opt,name=lastScaleTime"` + + // currentReplicas is current number of replicas of pods managed by this autoscaler, + // as last seen by the autoscaler. + CurrentReplicas int32 `json:"currentReplicas" protobuf:"varint,3,opt,name=currentReplicas"` + + // desiredReplicas is the desired number of replicas of pods managed by this autoscaler, + // as last calculated by the autoscaler. + DesiredReplicas int32 `json:"desiredReplicas" protobuf:"varint,4,opt,name=desiredReplicas"` +} + +// TiKVAutoScalerStatus describes the status for tikv auto-scaling +type TiKVAutoScalerStatus struct { + BaseAutoScalerStatus `json:",inline"` +} + +// TiDBAutoScalerStatus describes the status for tidb auto-scaling +type TiDBAutoScalerStatus struct { + BaseAutoScalerStatus `json:",inline"` +} + +// TidbClusterAutoSclaerStatus describes the status for TidbClusterAutoScaler type TidbClusterAutoSclaerStatus struct { + TikvStatus TiKVAutoScalerStatus `json:"tikv,omitempty"` + TiDBStatus TiDBAutoScalerStatus `json:"tidb,omitempty"` } diff --git a/pkg/apis/pingcap/v1alpha1/zz_generated.deepcopy.go b/pkg/apis/pingcap/v1alpha1/zz_generated.deepcopy.go index 2dda9cf4cd..04948e39ac 100644 --- a/pkg/apis/pingcap/v1alpha1/zz_generated.deepcopy.go +++ b/pkg/apis/pingcap/v1alpha1/zz_generated.deepcopy.go @@ -326,6 +326,22 @@ func (in *BackupStatus) DeepCopy() *BackupStatus { return out } +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *BaseAutoScalerStatus) DeepCopyInto(out *BaseAutoScalerStatus) { + *out = *in + return +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new BaseAutoScalerStatus. +func (in *BaseAutoScalerStatus) DeepCopy() *BaseAutoScalerStatus { + if in == nil { + return nil + } + out := new(BaseAutoScalerStatus) + in.DeepCopyInto(out) + return out +} + // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *BasicAutoScalerSpec) DeepCopyInto(out *BasicAutoScalerSpec) { *out = *in @@ -2247,6 +2263,23 @@ func (in *TiDBAutoGeneratedCertificate) DeepCopy() *TiDBAutoGeneratedCertificate return out } +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *TiDBAutoScalerStatus) DeepCopyInto(out *TiDBAutoScalerStatus) { + *out = *in + out.BaseAutoScalerStatus = in.BaseAutoScalerStatus + return +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new TiDBAutoScalerStatus. +func (in *TiDBAutoScalerStatus) DeepCopy() *TiDBAutoScalerStatus { + if in == nil { + return nil + } + out := new(TiDBAutoScalerStatus) + in.DeepCopyInto(out) + return out +} + // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *TiDBConfig) DeepCopyInto(out *TiDBConfig) { *out = *in @@ -2610,6 +2643,23 @@ func (in *TiDBTLSClient) DeepCopy() *TiDBTLSClient { return out } +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *TiKVAutoScalerStatus) DeepCopyInto(out *TiKVAutoScalerStatus) { + *out = *in + out.BaseAutoScalerStatus = in.BaseAutoScalerStatus + return +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new TiKVAutoScalerStatus. +func (in *TiKVAutoScalerStatus) DeepCopy() *TiKVAutoScalerStatus { + if in == nil { + return nil + } + out := new(TiKVAutoScalerStatus) + in.DeepCopyInto(out) + return out +} + // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *TiKVBlockCacheConfig) DeepCopyInto(out *TiKVBlockCacheConfig) { *out = *in @@ -3932,6 +3982,8 @@ func (in *TidbClusterAutoScalerSpec) DeepCopy() *TidbClusterAutoScalerSpec { // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *TidbClusterAutoSclaerStatus) DeepCopyInto(out *TidbClusterAutoSclaerStatus) { *out = *in + out.TikvStatus = in.TikvStatus + out.TiDBStatus = in.TiDBStatus return } diff --git a/pkg/autoscaler/autoscaler/calculate/cpu.go b/pkg/autoscaler/autoscaler/calculate/cpu.go index dcad599ded..9040ffa39c 100644 --- a/pkg/autoscaler/autoscaler/calculate/cpu.go +++ b/pkg/autoscaler/autoscaler/calculate/cpu.go @@ -15,6 +15,7 @@ package calculate import ( "fmt" + "k8s.io/klog" "time" "github.com/pingcap/tidb-operator/pkg/apis/pingcap/v1alpha1" @@ -65,6 +66,24 @@ func CalculateRecomendedReplicasByCpuCosts(tac *v1alpha1.TidbClusterAutoScaler, if err != nil { return -1, err } + + switch memberType { + case v1alpha1.TiDBMemberType: + tac.Status.TiDBStatus.Name = string(MetricTypeCPU) + tac.Status.TiDBStatus.CurrentValue = cpuSecsTotal + tac.Status.TiDBStatus.TargetValue = expectedCpuSecsTotal + tac.Status.TiDBStatus.CurrentReplicas = int32(currentReplicas) + tac.Status.TiDBStatus.DesiredReplicas = rc + case v1alpha1.TiKVMemberType: + tac.Status.TikvStatus.Name = string(MetricTypeCPU) + tac.Status.TikvStatus.CurrentValue = cpuSecsTotal + tac.Status.TikvStatus.TargetValue = expectedCpuSecsTotal + tac.Status.TikvStatus.CurrentReplicas = int32(currentReplicas) + tac.Status.TikvStatus.DesiredReplicas = rc + default: + klog.Errorf("unsupport type - %s", memberType) + } + return rc, nil } diff --git a/pkg/autoscaler/autoscaler/tidb_autoscaler.go b/pkg/autoscaler/autoscaler/tidb_autoscaler.go index ef81ab94e3..3f4bed14c8 100644 --- a/pkg/autoscaler/autoscaler/tidb_autoscaler.go +++ b/pkg/autoscaler/autoscaler/tidb_autoscaler.go @@ -71,6 +71,7 @@ func syncTiDBAfterCalculated(tc *v1alpha1.TidbCluster, tac *v1alpha1.TidbCluster func updateTcTiDBIfScale(tc *v1alpha1.TidbCluster, tac *v1alpha1.TidbClusterAutoScaler, recommendedReplicas int32) error { tac.Annotations[label.AnnTiDBLastAutoScalingTimestamp] = fmt.Sprintf("%d", time.Now().Unix()) tc.Spec.TiDB.Replicas = recommendedReplicas + tac.Status.TikvStatus.LastScaleTime = time.Now().Format(time.RFC3339) return nil } diff --git a/pkg/autoscaler/autoscaler/tikv_autoscaler.go b/pkg/autoscaler/autoscaler/tikv_autoscaler.go index 0d5b519b54..f959a9b437 100644 --- a/pkg/autoscaler/autoscaler/tikv_autoscaler.go +++ b/pkg/autoscaler/autoscaler/tikv_autoscaler.go @@ -100,6 +100,7 @@ func updateTcTiKVIfScale(tc *v1alpha1.TidbCluster, tac *v1alpha1.TidbClusterAuto } } tc.Spec.TiKV.Replicas = recommendedReplicas + tac.Status.TikvStatus.LastScaleTime = time.Now().Format(time.RFC3339) return nil } From c7fa40e5eb808f06100bfdd38fe2e4660d9ec1bb Mon Sep 17 00:00:00 2001 From: qiffang <947321353@qq.com> Date: Tue, 3 Mar 2020 17:03:19 +0800 Subject: [PATCH 2/3] AutoScaling: adding status type --- pkg/apis/pingcap/v1alpha1/tidbclusterautoscaler_types.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pkg/apis/pingcap/v1alpha1/tidbclusterautoscaler_types.go b/pkg/apis/pingcap/v1alpha1/tidbclusterautoscaler_types.go index 6a970c37dc..8bbb9b4e9c 100644 --- a/pkg/apis/pingcap/v1alpha1/tidbclusterautoscaler_types.go +++ b/pkg/apis/pingcap/v1alpha1/tidbclusterautoscaler_types.go @@ -155,10 +155,10 @@ type BaseAutoScalerStatus struct { Name string `json:"name,omitempty"` // CurrentValue is the current value of the metric. - CurrentValue float64 `json:"value,omitempty"` + CurrentValue float64 `json:"currentValue,omitempty"` // TargetValue is the target value of the metric. - TargetValue float64 `json:"value,omitempty"` + TargetValue float64 `json:"targetValue,omitempty"` // lastScaleTime is the last time the HorizontalPodAutoscaler scaled the number of pods, // used by the autoscaler to control how often the number of pods is changed. @@ -176,12 +176,12 @@ type BaseAutoScalerStatus struct { // TiKVAutoScalerStatus describes the status for tikv auto-scaling type TiKVAutoScalerStatus struct { - BaseAutoScalerStatus `json:",inline"` + BaseAutoScalerStatus `json:"tikv,omitempty"` } // TiDBAutoScalerStatus describes the status for tidb auto-scaling type TiDBAutoScalerStatus struct { - BaseAutoScalerStatus `json:",inline"` + BaseAutoScalerStatus `json:"tidb,omitempty"` } // TidbClusterAutoSclaerStatus describes the status for TidbClusterAutoScaler From d60b29ca6f827aba3f8e494d419614dc652a1b59 Mon Sep 17 00:00:00 2001 From: qiffang <947321353@qq.com> Date: Mon, 16 Mar 2020 17:51:37 +0800 Subject: [PATCH 3/3] AutoScaling: adding status type --- .../v1alpha1/tidbclusterautoscaler_types.go | 2 +- pkg/autoscaler/autoscaler/calculate/cpu.go | 15 ++++++++------- pkg/autoscaler/autoscaler/tidb_autoscaler.go | 2 +- 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/pkg/apis/pingcap/v1alpha1/tidbclusterautoscaler_types.go b/pkg/apis/pingcap/v1alpha1/tidbclusterautoscaler_types.go index 8bbb9b4e9c..76ff5ced21 100644 --- a/pkg/apis/pingcap/v1alpha1/tidbclusterautoscaler_types.go +++ b/pkg/apis/pingcap/v1alpha1/tidbclusterautoscaler_types.go @@ -171,7 +171,7 @@ type BaseAutoScalerStatus struct { // desiredReplicas is the desired number of replicas of pods managed by this autoscaler, // as last calculated by the autoscaler. - DesiredReplicas int32 `json:"desiredReplicas" protobuf:"varint,4,opt,name=desiredReplicas"` + RecommendedReplicas int32 `json:"desiredReplicas" protobuf:"varint,4,opt,name=desiredReplicas"` } // TiKVAutoScalerStatus describes the status for tikv auto-scaling diff --git a/pkg/autoscaler/autoscaler/calculate/cpu.go b/pkg/autoscaler/autoscaler/calculate/cpu.go index 9040ffa39c..ace0c519e5 100644 --- a/pkg/autoscaler/autoscaler/calculate/cpu.go +++ b/pkg/autoscaler/autoscaler/calculate/cpu.go @@ -15,13 +15,13 @@ package calculate import ( "fmt" - "k8s.io/klog" "time" "github.com/pingcap/tidb-operator/pkg/apis/pingcap/v1alpha1" promClient "github.com/prometheus/client_golang/api" appsv1 "k8s.io/api/apps/v1" corev1 "k8s.io/api/core/v1" + "k8s.io/klog" ) const ( @@ -67,19 +67,20 @@ func CalculateRecomendedReplicasByCpuCosts(tac *v1alpha1.TidbClusterAutoScaler, return -1, err } + currentCpuRadio := cpuSecsTotal / float64(currentReplicas) / cpuRequestsRatio / durationSeconds * 100.0 switch memberType { case v1alpha1.TiDBMemberType: tac.Status.TiDBStatus.Name = string(MetricTypeCPU) - tac.Status.TiDBStatus.CurrentValue = cpuSecsTotal - tac.Status.TiDBStatus.TargetValue = expectedCpuSecsTotal + tac.Status.TiDBStatus.CurrentValue = currentCpuRadio + tac.Status.TiDBStatus.TargetValue = float64(*metric.Resource.Target.AverageUtilization) tac.Status.TiDBStatus.CurrentReplicas = int32(currentReplicas) - tac.Status.TiDBStatus.DesiredReplicas = rc + tac.Status.TiDBStatus.RecommendedReplicas = rc case v1alpha1.TiKVMemberType: tac.Status.TikvStatus.Name = string(MetricTypeCPU) - tac.Status.TikvStatus.CurrentValue = cpuSecsTotal - tac.Status.TikvStatus.TargetValue = expectedCpuSecsTotal + tac.Status.TikvStatus.CurrentValue = currentCpuRadio + tac.Status.TikvStatus.TargetValue = float64(*metric.Resource.Target.AverageUtilization) tac.Status.TikvStatus.CurrentReplicas = int32(currentReplicas) - tac.Status.TikvStatus.DesiredReplicas = rc + tac.Status.TikvStatus.RecommendedReplicas = rc default: klog.Errorf("unsupport type - %s", memberType) } diff --git a/pkg/autoscaler/autoscaler/tidb_autoscaler.go b/pkg/autoscaler/autoscaler/tidb_autoscaler.go index 3f4bed14c8..fe9127b504 100644 --- a/pkg/autoscaler/autoscaler/tidb_autoscaler.go +++ b/pkg/autoscaler/autoscaler/tidb_autoscaler.go @@ -71,7 +71,7 @@ func syncTiDBAfterCalculated(tc *v1alpha1.TidbCluster, tac *v1alpha1.TidbCluster func updateTcTiDBIfScale(tc *v1alpha1.TidbCluster, tac *v1alpha1.TidbClusterAutoScaler, recommendedReplicas int32) error { tac.Annotations[label.AnnTiDBLastAutoScalingTimestamp] = fmt.Sprintf("%d", time.Now().Unix()) tc.Spec.TiDB.Replicas = recommendedReplicas - tac.Status.TikvStatus.LastScaleTime = time.Now().Format(time.RFC3339) + tac.Status.TiDBStatus.LastScaleTime = time.Now().Format(time.RFC3339) return nil }