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

Add more events for tidbcluster and autoscaler #2150

Merged
merged 14 commits into from
Apr 14, 2020
15 changes: 13 additions & 2 deletions pkg/autoscaler/autoscaler/autoscaler_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@ package autoscaler

import (
"fmt"
"strings"

"github.com/pingcap/tidb-operator/pkg/apis/pingcap/v1alpha1"
"github.com/pingcap/tidb-operator/pkg/client/clientset/versioned"
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"
corev1 "k8s.io/api/core/v1"
"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 @@ -80,7 +82,7 @@ func (am *autoScalerManager) Sync(tac *v1alpha1.TidbClusterAutoScaler) error {
if err := am.syncAutoScaling(tc, tac); err != nil {
return err
}
if err := am.syncTidbClusterReplicas(tc, oldTc); err != nil {
if err := am.syncTidbClusterReplicas(tac, tc, oldTc); err != nil {
return err
}
return am.syncAutoScalingStatus(tc, oldTc, tac)
Expand All @@ -102,7 +104,7 @@ func (am *autoScalerManager) syncAutoScaling(tc *v1alpha1.TidbCluster, tac *v1al
return nil
}

func (am *autoScalerManager) syncTidbClusterReplicas(tc *v1alpha1.TidbCluster, oldTc *v1alpha1.TidbCluster) error {
func (am *autoScalerManager) syncTidbClusterReplicas(tac *v1alpha1.TidbClusterAutoScaler, tc *v1alpha1.TidbCluster, oldTc *v1alpha1.TidbCluster) error {
if tc.Spec.TiDB.Replicas == oldTc.Spec.TiDB.Replicas && tc.Spec.TiKV.Replicas == oldTc.Spec.TiKV.Replicas {
return nil
}
Expand All @@ -111,6 +113,15 @@ func (am *autoScalerManager) syncTidbClusterReplicas(tc *v1alpha1.TidbCluster, o
if err != nil {
return err
}
reason := fmt.Sprintf("Successful %s", strings.Title("auto-scaling"))
msg := ""
if tc.Spec.TiDB.Replicas != oldTc.Spec.TiDB.Replicas {
msg = fmt.Sprintf("%s auto-scaling tidb from %d to %d", msg, oldTc.Spec.TiDB.Replicas, tc.Spec.TiDB.Replicas)
}
if tc.Spec.TiKV.Replicas != oldTc.Spec.TiKV.Replicas {
msg = fmt.Sprintf("%s auto-scaling tikv from %d to %d", msg, oldTc.Spec.TiKV.Replicas, tc.Spec.TiKV.Replicas)
}
am.recorder.Event(tac, corev1.EventTypeNormal, reason, msg)
return nil
}

Expand Down
7 changes: 5 additions & 2 deletions pkg/controller/tidbcluster/tidb_cluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ func NewController(
pdScaler := mm.NewPDScaler(pdControl, pvcInformer.Lister(), pvcControl)
tikvScaler := mm.NewTiKVScaler(pdControl, pvcInformer.Lister(), pvcControl, podInformer.Lister())
pdFailover := mm.NewPDFailover(cli, pdControl, pdFailoverPeriod, podInformer.Lister(), podControl, pvcInformer.Lister(), pvcControl, pvInformer.Lister(), recorder)
tikvFailover := mm.NewTiKVFailover(tikvFailoverPeriod)
tidbFailover := mm.NewTiDBFailover(tidbFailoverPeriod)
tikvFailover := mm.NewTiKVFailover(tikvFailoverPeriod, recorder)
tidbFailover := mm.NewTiDBFailover(tidbFailoverPeriod, recorder)
pdUpgrader := mm.NewPDUpgrader(pdControl, podControl, podInformer.Lister())
tikvUpgrader := mm.NewTiKVUpgrader(pdControl, podControl, podInformer.Lister())
tidbUpgrader := mm.NewTiDBUpgrader(tidbControl, podInformer.Lister())
Expand All @@ -135,6 +135,7 @@ func NewController(
pdUpgrader,
autoFailover,
pdFailover,
recorder,
Yisaer marked this conversation as resolved.
Show resolved Hide resolved
),
mm.NewTiKVMemberManager(
pdControl,
Expand All @@ -150,6 +151,7 @@ func NewController(
tikvFailover,
tikvScaler,
tikvUpgrader,
recorder,
),
mm.NewTiDBMemberManager(
setControl,
Expand All @@ -163,6 +165,7 @@ func NewController(
tidbUpgrader,
autoFailover,
tidbFailover,
recorder,
),
meta.NewReclaimPolicyManager(
pvcInformer.Lister(),
Expand Down
5 changes: 5 additions & 0 deletions pkg/manager/member/failover.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ package member

import "github.com/pingcap/tidb-operator/pkg/apis/pingcap/v1alpha1"

const (
unHealthEventReason = "Unhealthy"
unHealthEventMsgPattern = "%s pod[%s] is unhealthy, msg:%s"
)

// Failover implements the logic for pd/tikv/tidb's failover and recovery.
type Failover interface {
Failover(*v1alpha1.TidbCluster) error
Expand Down
4 changes: 2 additions & 2 deletions pkg/manager/member/pd_failover.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,8 @@ func (pf *pdFailover) tryToMarkAPeerAsFailure(tc *v1alpha1.TidbCluster) error {
return err
}

pf.recorder.Eventf(tc, apiv1.EventTypeWarning, "PDMemberMarkedAsFailure",
"%s(%s) marked as a failure member", podName, pdMember.ID)
msg := fmt.Sprintf("pd member[%s] is unhealthy", pdMember.ID)
pf.recorder.Event(tc, apiv1.EventTypeWarning, unHealthEventReason, fmt.Sprintf(unHealthEventMsgPattern, "pd", podName, msg))

tc.Status.PD.FailureMembers[podName] = v1alpha1.PDFailureMember{
PodName: podName,
Expand Down
2 changes: 1 addition & 1 deletion pkg/manager/member/pd_failover_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ func TestPDFailoverFailover(t *testing.T) {
events := collectEvents(recorder.Events)
g.Expect(events).To(HaveLen(2))
g.Expect(events[0]).To(ContainSubstring("test-pd-1(12891273174085095651) is unhealthy"))
g.Expect(events[1]).To(ContainSubstring("test-pd-1(12891273174085095651) marked as a failure member"))
g.Expect(events[1]).To(ContainSubstring("Unhealthy pd pod[test-pd-1] is unhealthy, msg:pd member[12891273174085095651] is unhealthy"))
},
},
{
Expand Down
8 changes: 6 additions & 2 deletions pkg/manager/member/pd_member_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
"k8s.io/apimachinery/pkg/util/uuid"
v1 "k8s.io/client-go/listers/apps/v1"
corelisters "k8s.io/client-go/listers/core/v1"
"k8s.io/client-go/tools/record"
"k8s.io/klog"
)

Expand All @@ -58,6 +59,7 @@ type pdMemberManager struct {
pdUpgrader Upgrader
autoFailover bool
pdFailover Failover
recorder record.EventRecorder
}

// NewPDMemberManager returns a *pdMemberManager
Expand All @@ -75,7 +77,8 @@ func NewPDMemberManager(pdControl pdapi.PDControlInterface,
pdScaler Scaler,
pdUpgrader Upgrader,
autoFailover bool,
pdFailover Failover) manager.Manager {
pdFailover Failover,
recorder record.EventRecorder) manager.Manager {
return &pdMemberManager{
pdControl,
setControl,
Expand All @@ -91,7 +94,8 @@ func NewPDMemberManager(pdControl pdapi.PDControlInterface,
pdScaler,
pdUpgrader,
autoFailover,
pdFailover}
pdFailover,
recorder}
}

func (pmm *pdMemberManager) Sync(tc *v1alpha1.TidbCluster) error {
Expand Down
4 changes: 3 additions & 1 deletion pkg/manager/member/pd_member_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import (
kubeinformers "k8s.io/client-go/informers"
kubefake "k8s.io/client-go/kubernetes/fake"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/tools/record"
"k8s.io/utils/pointer"
)

Expand Down Expand Up @@ -758,7 +759,7 @@ func newFakePDMemberManager() (*pdMemberManager, *controller.FakeStatefulSetCont
pdFailover := NewFakePDFailover()
pdUpgrader := NewFakePDUpgrader()
genericControll := controller.NewFakeGenericControl()

recorder := record.NewFakeRecorder(100)
return &pdMemberManager{
pdControl,
setControl,
Expand All @@ -775,6 +776,7 @@ func newFakePDMemberManager() (*pdMemberManager, *controller.FakeStatefulSetCont
pdUpgrader,
autoFailover,
pdFailover,
recorder,
}, setControl, svcControl, pdControl, podInformer.Informer().GetIndexer(), pvcInformer.Informer().GetIndexer(), podControl
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/manager/member/pd_scaler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ package member

import (
"fmt"
"github.com/pingcap/kvproto/pkg/pdpb"
"testing"
"time"

. "github.com/onsi/gomega"
"github.com/pingcap/kvproto/pkg/pdpb"
"github.com/pingcap/tidb-operator/pkg/apis/pingcap/v1alpha1"
"github.com/pingcap/tidb-operator/pkg/controller"
"github.com/pingcap/tidb-operator/pkg/label"
Expand Down
12 changes: 10 additions & 2 deletions pkg/manager/member/tidb_failover.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,26 @@
package member

import (
"fmt"
"time"

"github.com/pingcap/tidb-operator/pkg/apis/pingcap/v1alpha1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/tools/record"
"k8s.io/klog"
)

type tidbFailover struct {
tidbFailoverPeriod time.Duration
recorder record.EventRecorder
}

// NewTiDBFailover returns a tidbFailover instance
func NewTiDBFailover(failoverPeriod time.Duration) Failover {
func NewTiDBFailover(failoverPeriod time.Duration, recorder record.EventRecorder) Failover {
return &tidbFailover{
tidbFailoverPeriod: failoverPeriod,
recorder: recorder,
}
}

Expand Down Expand Up @@ -59,6 +64,8 @@ func (tf *tidbFailover) Failover(tc *v1alpha1.TidbCluster) error {
PodName: tidbMember.Name,
CreatedAt: metav1.Now(),
}
msg := fmt.Sprintf("tidb[%s] is unhealthy", tidbMember.Name)
tf.recorder.Event(tc, corev1.EventTypeWarning, unHealthEventReason, fmt.Sprintf(unHealthEventMsgPattern, "tidb", tidbMember.Name, msg))
break
}
}
Expand All @@ -71,7 +78,8 @@ func (tf *tidbFailover) Recover(tc *v1alpha1.TidbCluster) {
tc.Status.TiDB.FailureMembers = nil
}

type fakeTiDBFailover struct{}
type fakeTiDBFailover struct {
}

// NewFakeTiDBFailover returns a fake Failover
func NewFakeTiDBFailover() Failover {
Expand Down
4 changes: 3 additions & 1 deletion pkg/manager/member/tidb_failover_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (

. "github.com/onsi/gomega"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/tools/record"
"k8s.io/utils/pointer"

"github.com/pingcap/tidb-operator/pkg/apis/pingcap/v1alpha1"
Expand Down Expand Up @@ -382,7 +383,8 @@ func TestFakeTiDBFailoverRecover(t *testing.T) {
}

func newTiDBFailover() Failover {
return &tidbFailover{tidbFailoverPeriod: time.Duration(5 * time.Minute)}
recorder := record.NewFakeRecorder(100)
return &tidbFailover{tidbFailoverPeriod: time.Duration(5 * time.Minute), recorder: recorder}
}

func newTidbClusterForTiDBFailover() *v1alpha1.TidbCluster {
Expand Down
6 changes: 5 additions & 1 deletion pkg/manager/member/tidb_member_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
"k8s.io/apimachinery/pkg/util/uuid"
v1 "k8s.io/client-go/listers/apps/v1"
corelisters "k8s.io/client-go/listers/core/v1"
"k8s.io/client-go/tools/record"
"k8s.io/klog"
"k8s.io/utils/pointer"
)
Expand Down Expand Up @@ -68,6 +69,7 @@ type tidbMemberManager struct {
autoFailover bool
tidbFailover Failover
tidbStatefulSetIsUpgradingFn func(corelisters.PodLister, *apps.StatefulSet, *v1alpha1.TidbCluster) (bool, error)
recorder record.EventRecorder
}

// NewTiDBMemberManager returns a *tidbMemberManager
Expand All @@ -81,7 +83,8 @@ func NewTiDBMemberManager(setControl controller.StatefulSetControlInterface,
podLister corelisters.PodLister,
tidbUpgrader Upgrader,
autoFailover bool,
tidbFailover Failover) manager.Manager {
tidbFailover Failover,
recorder record.EventRecorder) manager.Manager {
return &tidbMemberManager{
setControl: setControl,
svcControl: svcControl,
Expand All @@ -95,6 +98,7 @@ func NewTiDBMemberManager(setControl controller.StatefulSetControlInterface,
autoFailover: autoFailover,
tidbFailover: tidbFailover,
tidbStatefulSetIsUpgradingFn: tidbStatefulSetIsUpgrading,
recorder: recorder,
}
}

Expand Down
3 changes: 3 additions & 0 deletions pkg/manager/member/tidb_member_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import (
kubefake "k8s.io/client-go/kubernetes/fake"
corelisters "k8s.io/client-go/listers/core/v1"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/tools/record"
"k8s.io/utils/pointer"
)

Expand Down Expand Up @@ -778,6 +779,7 @@ func newFakeTiDBMemberManager() (*tidbMemberManager, *controller.FakeStatefulSet
tidbUpgrader := NewFakeTiDBUpgrader()
tidbFailover := NewFakeTiDBFailover()
tidbControl := controller.NewFakeTiDBControl()
recorder := record.NewFakeRecorder(100)

tmm := &tidbMemberManager{
setControl,
Expand All @@ -793,6 +795,7 @@ func newFakeTiDBMemberManager() (*tidbMemberManager, *controller.FakeStatefulSet
true,
tidbFailover,
tidbStatefulSetIsUpgrading,
recorder,
}
indexers := &fakeIndexers{
pod: podInformer.Informer().GetIndexer(),
Expand Down
10 changes: 8 additions & 2 deletions pkg/manager/member/tikv_failover.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,24 @@
package member

import (
"fmt"
"time"

"github.com/pingcap/tidb-operator/pkg/apis/pingcap/v1alpha1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/tools/record"
"k8s.io/klog"
)

type tikvFailover struct {
tikvFailoverPeriod time.Duration
recorder record.EventRecorder
}

// NewTiKVFailover returns a tikv Failover
func NewTiKVFailover(tikvFailoverPeriod time.Duration) Failover {
return &tikvFailover{tikvFailoverPeriod}
func NewTiKVFailover(tikvFailoverPeriod time.Duration, recorder record.EventRecorder) Failover {
return &tikvFailover{tikvFailoverPeriod, recorder}
}

func (tf *tikvFailover) Failover(tc *v1alpha1.TidbCluster) error {
Expand Down Expand Up @@ -62,6 +66,8 @@ func (tf *tikvFailover) Failover(tc *v1alpha1.TidbCluster) error {
StoreID: store.ID,
CreatedAt: metav1.Now(),
}
msg := fmt.Sprintf("store[%s] is Down", store.ID)
tf.recorder.Event(tc, corev1.EventTypeWarning, unHealthEventReason, fmt.Sprintf(unHealthEventMsgPattern, "tikv", podName, msg))
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion pkg/manager/member/tikv_failover_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
. "github.com/onsi/gomega"
"github.com/pingcap/tidb-operator/pkg/apis/pingcap/v1alpha1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/tools/record"
"k8s.io/utils/pointer"
)

Expand Down Expand Up @@ -304,5 +305,6 @@ func TestTiKVFailoverFailover(t *testing.T) {
}

func newFakeTiKVFailover() *tikvFailover {
return &tikvFailover{1 * time.Hour}
recorder := record.NewFakeRecorder(100)
return &tikvFailover{1 * time.Hour, recorder}
}
6 changes: 5 additions & 1 deletion pkg/manager/member/tikv_member_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (
"k8s.io/apimachinery/pkg/util/uuid"
v1 "k8s.io/client-go/listers/apps/v1"
corelisters "k8s.io/client-go/listers/core/v1"
"k8s.io/client-go/tools/record"
"k8s.io/klog"
)

Expand Down Expand Up @@ -62,6 +63,7 @@ type tikvMemberManager struct {
tikvScaler Scaler
tikvUpgrader Upgrader
tikvStatefulSetIsUpgradingFn func(corelisters.PodLister, pdapi.PDControlInterface, *apps.StatefulSet, *v1alpha1.TidbCluster) (bool, error)
recorder record.EventRecorder
}

// NewTiKVMemberManager returns a *tikvMemberManager
Expand All @@ -78,7 +80,8 @@ func NewTiKVMemberManager(
autoFailover bool,
tikvFailover Failover,
tikvScaler Scaler,
tikvUpgrader Upgrader) manager.Manager {
tikvUpgrader Upgrader,
recorder record.EventRecorder) manager.Manager {
kvmm := tikvMemberManager{
pdControl: pdControl,
podLister: podLister,
Expand All @@ -93,6 +96,7 @@ func NewTiKVMemberManager(
tikvFailover: tikvFailover,
tikvScaler: tikvScaler,
tikvUpgrader: tikvUpgrader,
recorder: recorder,
}
kvmm.tikvStatefulSetIsUpgradingFn = tikvStatefulSetIsUpgrading
return &kvmm
Expand Down
Loading