Skip to content

Commit

Permalink
feat(tikvgroup): refactor to use task v3 and add more uts (#6016)
Browse files Browse the repository at this point in the history
Signed-off-by: liubo02 <[email protected]>
  • Loading branch information
liubog2008 authored Dec 30, 2024
1 parent 90a3a2a commit 3e7945d
Show file tree
Hide file tree
Showing 48 changed files with 2,746 additions and 899 deletions.
11 changes: 11 additions & 0 deletions apis/core/v1alpha1/common_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,17 @@ import (
"k8s.io/apimachinery/pkg/runtime/schema"
)

const (
// CondHealth is a condition to display whether the instance is health
CondHealth = "Health"

// CondSuspended is a condition to display whether the group or instance is suspended
CondSuspended = "Suspended"
ReasonSuspended = "Suspended"
ReasonSuspending = "Suspending"
ReasonUnsuspended = "Unsuspended"
)

const (
// Finalizer is the finalizer used by all resources managed by TiDB Operator.
Finalizer = "core.pingcap.com/finalizer"
Expand Down
6 changes: 3 additions & 3 deletions apis/core/v1alpha1/tikv_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,15 @@ const (
)

const (
TiKVCondHealth = "Health"
TiKVCondHealth = CondHealth
TiKVHealthReason = "TiKVHealth"

TiKVCondLeadersEvicted = "LeadersEvicted"

TiKVCondSuspended = "Suspended"
TiKVCondSuspended = CondSuspended
TiKVSuspendReason = "TiKVSuspend"

TiKVGroupCondSuspended = "Suspended"
TiKVGroupCondSuspended = CondSuspended
TiKVGroupSuspendReason = "TiKVGroupSuspend"
)

Expand Down
12 changes: 12 additions & 0 deletions pkg/controllers/common/cond.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,15 @@ func CondPDGroupIsDeleting(ctx PDGroupState) task.Condition {
return !ctx.PDGroup().GetDeletionTimestamp().IsZero()
})
}

func CondTiKVGroupHasBeenDeleted(ctx TiKVGroupState) task.Condition {
return task.CondFunc(func() bool {
return ctx.TiKVGroup() == nil
})
}

func CondTiKVGroupIsDeleting(ctx TiKVGroupState) task.Condition {
return task.CondFunc(func() bool {
return !ctx.TiKVGroup().GetDeletionTimestamp().IsZero()
})
}
155 changes: 123 additions & 32 deletions pkg/controllers/common/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (

"github.com/pingcap/tidb-operator/apis/core/v1alpha1"
"github.com/pingcap/tidb-operator/pkg/client"
"github.com/pingcap/tidb-operator/pkg/runtime"
)

type Object[T any] interface {
Expand All @@ -32,51 +33,141 @@ type ObjectList[T any] interface {
}

type (
PDInitializer = ResourceInitializer[v1alpha1.PD]

ClusterInitializer = ResourceInitializer[v1alpha1.Cluster]

PodInitializer = ResourceInitializer[corev1.Pod]
PDSliceInitializer = ResourceSliceInitializer[v1alpha1.PD]
PDGroupInitializer = ResourceInitializer[v1alpha1.PDGroup]
)
PDInitializer = ResourceInitializer[v1alpha1.PD]
PDSliceInitializer = ResourceSliceInitializer[v1alpha1.PD]

type PDStateInitializer interface {
PDInitializer() PDInitializer
}
TiKVGroupInitializer = ResourceInitializer[v1alpha1.TiKVGroup]
TiKVInitializer = ResourceInitializer[v1alpha1.TiKV]
TiKVSliceInitializer = ResourceSliceInitializer[v1alpha1.TiKV]

type PDState interface {
PD() *v1alpha1.PD
}
TiDBGroupInitializer = ResourceInitializer[v1alpha1.TiDBGroup]
TiDBInitializer = ResourceInitializer[v1alpha1.TiDB]
TiDBSliceInitializer = ResourceSliceInitializer[v1alpha1.TiDB]

type ClusterStateInitializer interface {
ClusterInitializer() ClusterInitializer
}
TiFlashGroupInitializer = ResourceInitializer[v1alpha1.TiFlashGroup]
TiFlashInitializer = ResourceInitializer[v1alpha1.TiFlash]
TiFlashSliceInitializer = ResourceSliceInitializer[v1alpha1.TiFlash]

type ClusterState interface {
Cluster() *v1alpha1.Cluster
}
PodInitializer = ResourceInitializer[corev1.Pod]
)

type PodStateInitializer interface {
PodInitializer() PodInitializer
}
type (
ClusterStateInitializer interface {
ClusterInitializer() ClusterInitializer
}
ClusterState interface {
Cluster() *v1alpha1.Cluster
}
)

type PodState interface {
Pod() *corev1.Pod
type GroupState[G runtime.Group] interface {
Group() G
}

type PDGroupStateInitializer interface {
PDGroupInitializer() PDGroupInitializer
type InstanceSliceState[I runtime.Instance] interface {
Slice() []I
}

type PDGroupState interface {
PDGroup() *v1alpha1.PDGroup
type GroupAndInstanceSliceState[
G runtime.Group,
I runtime.Instance,
] interface {
GroupState[G]
InstanceSliceState[I]
}

type PDSliceStateInitializer interface {
PDSliceInitializer() PDSliceInitializer
}
type (
PDGroupStateInitializer interface {
PDGroupInitializer() PDGroupInitializer
}
PDGroupState interface {
PDGroup() *v1alpha1.PDGroup
}
PDStateInitializer interface {
PDInitializer() PDInitializer
}
PDState interface {
PD() *v1alpha1.PD
}
PDSliceStateInitializer interface {
PDSliceInitializer() PDSliceInitializer
}
PDSliceState interface {
PDSlice() []*v1alpha1.PD
}
)

type PDSliceState interface {
PDSlice() []*v1alpha1.PD
}
type (
TiKVGroupStateInitializer interface {
TiKVGroupInitializer() TiKVGroupInitializer
}
TiKVGroupState interface {
TiKVGroup() *v1alpha1.TiKVGroup
}
TiKVStateInitializer interface {
TiKVInitializer() TiKVInitializer
}
TiKVState interface {
TiKV() *v1alpha1.TiKV
}
TiKVSliceStateInitializer interface {
TiKVSliceInitializer() TiKVSliceInitializer
}
TiKVSliceState interface {
TiKVSlice() []*v1alpha1.TiKV
}
)

type (
TiDBGroupStateInitializer interface {
TiDBGroupInitializer() TiDBGroupInitializer
}
TiDBGroupState interface {
TiDBGroup() *v1alpha1.TiDBGroup
}
TiDBStateInitializer interface {
TiDBInitializer() TiDBInitializer
}
TiDBState interface {
TiDB() *v1alpha1.TiDB
}
TiDBSliceStateInitializer interface {
TiDBSliceInitializer() TiDBSliceInitializer
}
TiDBSliceState interface {
TiDBSlice() []*v1alpha1.TiDB
}
)

type (
TiFlashGroupStateInitializer interface {
TiFlashGroupInitializer() TiFlashGroupInitializer
}
TiFlashGroupState interface {
TiFlashGroup() *v1alpha1.TiFlashGroup
}
TiFlashStateInitializer interface {
TiFlashInitializer() TiFlashGroupInitializer
}
TiFlashState interface {
TiFlash() *v1alpha1.TiFlash
}
TiFlashSliceStateInitializer interface {
TiFlashSliceInitializer() TiFlashSliceInitializer
}
TiFlashSliceState interface {
TiFlashSlice() []*v1alpha1.TiFlash
}
)

type (
PodStateInitializer interface {
PodInitializer() PodInitializer
}
PodState interface {
Pod() *corev1.Pod
}
)
47 changes: 47 additions & 0 deletions pkg/controllers/common/interfaces_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
corev1 "k8s.io/api/core/v1"

"github.com/pingcap/tidb-operator/apis/core/v1alpha1"
"github.com/pingcap/tidb-operator/pkg/runtime"
)

type fakeState[T any] struct {
Expand Down Expand Up @@ -101,3 +102,49 @@ func (f *fakePDSliceState) PDSlice() []*v1alpha1.PD {
func (f *fakePDSliceState) PDSliceInitializer() PDSliceInitializer {
return f.s.Initializer()
}

type fakeGroupState[RG runtime.Group] struct {
g RG
}

func (f *fakeGroupState[RG]) Group() RG {
return f.g
}

func FakeGroupState[RG runtime.Group](g RG) GroupState[RG] {
return &fakeGroupState[RG]{
g: g,
}
}

type fakeInstanceSliceState[RI runtime.Instance] struct {
slice []RI
}

func (f *fakeInstanceSliceState[RI]) Slice() []RI {
return f.slice
}

func FakeInstanceSliceState[RI runtime.Instance](in []RI) InstanceSliceState[RI] {
return &fakeInstanceSliceState[RI]{
slice: in,
}
}

type fakeGroupAndInstanceSliceState[
RG runtime.Group,
RI runtime.Instance,
] struct {
GroupState[RG]
InstanceSliceState[RI]
}

func FakeGroupAndInstanceSliceState[
RG runtime.Group,
RI runtime.Instance,
](g RG, s ...RI) GroupAndInstanceSliceState[RG, RI] {
return &fakeGroupAndInstanceSliceState[RG, RI]{
GroupState: FakeGroupState[RG](g),
InstanceSliceState: FakeInstanceSliceState[RI](s),
}
}
Loading

0 comments on commit 3e7945d

Please sign in to comment.