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

Make TidbMonitor intergrated in AutoScaler (#1747) #1759

Merged
merged 3 commits into from
Feb 24, 2020
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
13 changes: 13 additions & 0 deletions manifests/crd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7043,6 +7043,19 @@ spec:
the pd could provide it. MetricsUrl represents the url to fetch the
metrics info
type: string
monitor:
description: TidbMonitorRef reference to a TidbMonitor
properties:
name:
description: Name is the name of TidbMonitor object
type: string
namespace:
description: Namespace is the namespace that TidbMonitor object
locates, default to the same namespace with TidbClusterAutoScaler
type: string
required:
- name
type: object
tidb:
description: TidbAutoScalerSpec describes the spec for tidb auto-scaling
properties:
Expand Down
37 changes: 36 additions & 1 deletion pkg/apis/pingcap/v1alpha1/openapi_generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions pkg/apis/pingcap/v1alpha1/tidbclusterautoscaler_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ type TidbClusterAutoScalerSpec struct {
// +optional
MetricsUrl *string `json:"metricsUrl,omitempty"`

// TidbMonitorRef describe the target TidbMonitor, when MetricsUrl and Monitor are both set,
// Operator will use MetricsUrl
// +optional
Monitor *TidbMonitorRef `json:"monitor,omitempty"`

// TiKV represents the auto-scaling spec for tikv
// +optional
TiKV *TikvAutoScalerSpec `json:"tikv,omitempty"`
Expand Down Expand Up @@ -132,6 +137,18 @@ type BasicAutoScalerSpec struct {
ScaleInThreshold *int32 `json:"scaleInThreshold,omitempty"`
}

// +k8s:openapi-gen=true
// TidbMonitorRef reference to a TidbMonitor
type TidbMonitorRef struct {
// Namespace is the namespace that TidbMonitor object locates,
// default to the same namespace with TidbClusterAutoScaler
// +optional
Namespace string `json:"namespace,omitempty"`

// Name is the name of TidbMonitor object
Name string `json:"name"`
}

// TODO: sync status
type TidbClusterAutoSclaerStatus struct {
}
21 changes: 21 additions & 0 deletions pkg/apis/pingcap/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 2 additions & 10 deletions pkg/autoscaler/autoscaler/autoscaler_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
informers "github.com/pingcap/tidb-operator/pkg/client/informers/externalversions"
v1alpha1listers "github.com/pingcap/tidb-operator/pkg/client/listers/pingcap/v1alpha1"
"github.com/pingcap/tidb-operator/pkg/controller"
promClient "github.com/prometheus/client_golang/api"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
Expand Down Expand Up @@ -88,21 +87,14 @@ func (am *autoScalerManager) Sync(tac *v1alpha1.TidbClusterAutoScaler) error {
}

func (am *autoScalerManager) syncAutoScaling(tc *v1alpha1.TidbCluster, tac *v1alpha1.TidbClusterAutoScaler) error {
if tac.Spec.MetricsUrl == nil {
return fmt.Errorf("tidbclusterAutoScaler[%s/%s]' metrics url should be defined explicitly", tac.Namespace, tac.Name)
}
c, err := promClient.NewClient(promClient.Config{Address: *tac.Spec.MetricsUrl})
if err != nil {
return err
}
defaultTAC(tac)
oldTikvReplicas := tc.Spec.TiKV.Replicas
if err := am.syncTiKV(tc, tac, c); err != nil {
if err := am.syncTiKV(tc, tac); err != nil {
tc.Spec.TiKV.Replicas = oldTikvReplicas
klog.Errorf("tac[%s/%s] tikv sync failed, continue to sync next, err:%v", tac.Namespace, tac.Name, err)
}
oldTidbReplicas := tc.Spec.TiDB.Replicas
if err := am.syncTiDB(tc, tac, c); err != nil {
if err := am.syncTiDB(tc, tac); err != nil {
tc.Spec.TiDB.Replicas = oldTidbReplicas
klog.Errorf("tac[%s/%s] tidb sync failed, continue to sync next, err:%v", tac.Namespace, tac.Name, err)
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/autoscaler/autoscaler/calculate/calculate.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ const (
)

type SingleQuery struct {
Endpoint string
Timestamp int64
Quary string
Instances []string
Expand All @@ -45,7 +46,7 @@ type SingleQuery struct {
func queryMetricsFromPrometheus(tac *v1alpha1.TidbClusterAutoScaler, client promClient.Client, sq *SingleQuery, resp *Response) error {
query := sq.Quary
timestamp := sq.Timestamp
req, err := http.NewRequest("GET", fmt.Sprintf("%s%s", *tac.Spec.MetricsUrl, queryPath), nil)
req, err := http.NewRequest("GET", fmt.Sprintf("%s%s", sq.Endpoint, queryPath), nil)
if err != nil {
return err
}
Expand Down
15 changes: 12 additions & 3 deletions pkg/autoscaler/autoscaler/tidb_autoscaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
appsv1 "k8s.io/api/apps/v1"
)

func (am *autoScalerManager) syncTiDB(tc *v1alpha1.TidbCluster, tac *v1alpha1.TidbClusterAutoScaler, client promClient.Client) error {
func (am *autoScalerManager) syncTiDB(tc *v1alpha1.TidbCluster, tac *v1alpha1.TidbClusterAutoScaler) error {
if tac.Spec.TiDB == nil {
return nil
}
Expand All @@ -38,7 +38,7 @@ func (am *autoScalerManager) syncTiDB(tc *v1alpha1.TidbCluster, tac *v1alpha1.Ti
}
currentReplicas := tc.Spec.TiDB.Replicas
instances := filterTidbInstances(tc)
targetReplicas, err := calculateTidbMetrics(tac, sts, client, instances)
targetReplicas, err := calculateTidbMetrics(tac, sts, instances)
if err != nil {
return err
}
Expand Down Expand Up @@ -73,7 +73,15 @@ func updateTcTiDBAnnIfScale(tac *v1alpha1.TidbClusterAutoScaler) {
tac.Annotations[label.AnnTiDBLastAutoScalingTimestamp] = fmt.Sprintf("%d", time.Now().Unix())
}

func calculateTidbMetrics(tac *v1alpha1.TidbClusterAutoScaler, sts *appsv1.StatefulSet, client promClient.Client, instances []string) (int32, error) {
func calculateTidbMetrics(tac *v1alpha1.TidbClusterAutoScaler, sts *appsv1.StatefulSet, instances []string) (int32, error) {
ep, err := genMetricsEndpoint(tac)
if err != nil {
return -1, err
}
client, err := promClient.NewClient(promClient.Config{Address: ep})
if err != nil {
return -1, err
}
metric := calculate.FilterMetrics(tac.Spec.TiDB.Metrics)
mType, err := calculate.GenMetricType(tac, metric)
if err != nil {
Expand All @@ -84,6 +92,7 @@ func calculateTidbMetrics(tac *v1alpha1.TidbClusterAutoScaler, sts *appsv1.State
return -1, err
}
sq := &calculate.SingleQuery{
Endpoint: ep,
Timestamp: time.Now().Unix(),
Instances: instances,
Metric: metric,
Expand Down
16 changes: 13 additions & 3 deletions pkg/autoscaler/autoscaler/tikv_autoscaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
appsv1 "k8s.io/api/apps/v1"
)

func (am *autoScalerManager) syncTiKV(tc *v1alpha1.TidbCluster, tac *v1alpha1.TidbClusterAutoScaler, client promClient.Client) error {
func (am *autoScalerManager) syncTiKV(tc *v1alpha1.TidbCluster, tac *v1alpha1.TidbClusterAutoScaler) error {
if tac.Spec.TiKV == nil {
return nil
}
Expand All @@ -38,7 +38,7 @@ func (am *autoScalerManager) syncTiKV(tc *v1alpha1.TidbCluster, tac *v1alpha1.Ti
}
instances := filterTiKVInstances(tc)
currentReplicas := int32(len(instances))
targetReplicas, err := calculateTikvMetrics(tac, sts, client, instances)
targetReplicas, err := calculateTikvMetrics(tac, sts, instances)
if err != nil {
return err
}
Expand Down Expand Up @@ -87,7 +87,16 @@ func updateTcTiKVAnnIfScale(tac *v1alpha1.TidbClusterAutoScaler) {
tac.Annotations[label.AnnTiKVLastAutoScalingTimestamp] = fmt.Sprintf("%d", time.Now().Unix())
}

func calculateTikvMetrics(tac *v1alpha1.TidbClusterAutoScaler, sts *appsv1.StatefulSet, client promClient.Client, instances []string) (int32, error) {
func calculateTikvMetrics(tac *v1alpha1.TidbClusterAutoScaler, sts *appsv1.StatefulSet, instances []string) (int32, error) {
ep, err := genMetricsEndpoint(tac)
if err != nil {
return -1, err
}
client, err := promClient.NewClient(promClient.Config{Address: ep})
if err != nil {
return -1, err
}

metric := calculate.FilterMetrics(tac.Spec.TiKV.Metrics)
mType, err := calculate.GenMetricType(tac, metric)
if err != nil {
Expand All @@ -99,6 +108,7 @@ func calculateTikvMetrics(tac *v1alpha1.TidbClusterAutoScaler, sts *appsv1.State
return -1, err
}
sq := &calculate.SingleQuery{
Endpoint: ep,
Timestamp: time.Now().Unix(),
Instances: instances,
Metric: metric,
Expand Down
17 changes: 17 additions & 0 deletions pkg/autoscaler/autoscaler/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package autoscaler

import (
"fmt"
"strconv"
"time"

Expand Down Expand Up @@ -174,6 +175,12 @@ func defaultTAC(tac *v1alpha1.TidbClusterAutoScaler) {
tac.Spec.TiDB.ScaleInIntervalSeconds = pointer.Int32Ptr(500)
}
}

if tac.Spec.Monitor != nil {
if len(tac.Spec.Monitor.Namespace) < 1 {
tac.Spec.Monitor.Namespace = tac.Namespace
}
}
}

func resetAutoScalingAnn(tac *v1alpha1.TidbClusterAutoScaler) {
Expand All @@ -198,3 +205,13 @@ func checkAndUpdateTacAnn(tac *v1alpha1.TidbClusterAutoScaler) {
// If not satisfied, reset tac Ann
resetAutoScalingAnn(tac)
}

func genMetricsEndpoint(tac *v1alpha1.TidbClusterAutoScaler) (string, error) {
if tac.Spec.MetricsUrl == nil && tac.Spec.Monitor == nil {
return "", fmt.Errorf("tac[%s/%s] metrics url or monitor should be defined explicitly", tac.Namespace, tac.Name)
}
if tac.Spec.MetricsUrl != nil {
return *tac.Spec.MetricsUrl, nil
}
return fmt.Sprintf("http://%s-prometheus.%s.svc:9090", tac.Spec.Monitor.Name, tac.Spec.Monitor.Namespace), nil
}