Skip to content

Commit

Permalink
Merge branch 'master' into cdc-gracefulshutdown
Browse files Browse the repository at this point in the history
  • Loading branch information
KanShiori authored Jul 20, 2022
2 parents 23af01d + 12c5cfd commit 7bd34f0
Show file tree
Hide file tree
Showing 15 changed files with 614 additions and 466 deletions.
7 changes: 3 additions & 4 deletions docs/api-references/docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -4327,10 +4327,6 @@ FlashSecurity
</tr>
</tbody>
</table>
<h3 id="component">Component</h3>
<p>
<p>Component defines component identity of all components</p>
</p>
<h3 id="componentaccessor">ComponentAccessor</h3>
<p>
<p>ComponentAccessor is the interface to access component details, which respects the cluster-level properties
Expand Down Expand Up @@ -4797,6 +4793,9 @@ string
<p>
<p>ConfigUpdateStrategy represents the strategy to update configuration</p>
</p>
<h3 id="containername">ContainerName</h3>
<p>
</p>
<h3 id="coprocessorcache">CoprocessorCache</h3>
<p>
(<em>Appears on:</em>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const (
// ComponentAccessor is the interface to access component details, which respects the cluster-level properties
// and component-level overrides
type ComponentAccessor interface {
MemberType() MemberType
ImagePullPolicy() corev1.PullPolicy
ImagePullSecrets() []corev1.LocalObjectReference
HostNetwork() bool
Expand All @@ -53,25 +54,84 @@ type ComponentAccessor interface {
TopologySpreadConstraints() []corev1.TopologySpreadConstraint
}

// Component defines component identity of all components
type Component int
// AllComponentSpec return all component specs of tidb cluster
func (tc *TidbCluster) AllComponentSpec() []ComponentAccessor {
components := []ComponentAccessor{}
components = append(components, tc.BaseDiscoverySpec())
if tc.Spec.PD != nil {
components = append(components, tc.BasePDSpec())
}
if tc.Spec.TiDB != nil {
components = append(components, tc.BaseTiDBSpec())
}
if tc.Spec.TiKV != nil {
components = append(components, tc.BaseTiKVSpec())
}
if tc.Spec.TiFlash != nil {
components = append(components, tc.BaseTiFlashSpec())
}
if tc.Spec.TiCDC != nil {
components = append(components, tc.BaseTiCDCSpec())
}
if tc.Spec.Pump != nil {
components = append(components, tc.BasePumpSpec())
}
return components
}

const (
ComponentPD Component = iota
ComponentTiDB
ComponentTiKV
ComponentTiFlash
ComponentTiCDC
ComponentPump
ComponentDiscovery
ComponentDMDiscovery
ComponentDMMaster
ComponentDMWorker
ComponentNGMonitoring
)
// ComponentSpec return a component spec of tidb cluster, return nil if not exist
func (tc *TidbCluster) ComponentSpec(typ MemberType) ComponentAccessor {
components := tc.AllComponentSpec()
for _, component := range components {
if component.MemberType() == typ {
return component
}
}
return nil
}

// AllComponentSpec return all component specs of dm cluster
func (dc *DMCluster) AllComponentSpec() []ComponentAccessor {
components := []ComponentAccessor{}
components = append(components, dc.BaseDiscoverySpec())
components = append(components, dc.BaseMasterSpec())
if dc.Spec.Worker != nil {
components = append(components, dc.BaseWorkerSpec())
}
return components
}

// ComponentSpec return a component spec of dm cluster, return nil if not exist
func (dc *DMCluster) ComponentSpec(typ MemberType) ComponentAccessor {
components := dc.AllComponentSpec()
for _, component := range components {
if component.MemberType() == typ {
return component
}
}
return nil
}

// AllComponentSpec return all component specs of ng monitoring
func (ngm *TidbNGMonitoring) AllComponentSpec() []ComponentAccessor {
components := []ComponentAccessor{}
components = append(components, ngm.BaseNGMonitoringSpec())
return components
}

// ComponentSpec return a component spec of ng monitoring, return nil if not exist
func (ngm *TidbNGMonitoring) ComponentSpec(typ MemberType) ComponentAccessor {
components := ngm.AllComponentSpec()
for _, component := range components {
if component.MemberType() == typ {
return component
}
}
return nil
}

type componentAccessorImpl struct {
component Component
component MemberType
name string
kind string

Expand All @@ -97,6 +157,10 @@ type componentAccessorImpl struct {
ComponentSpec *ComponentSpec
}

func (a *componentAccessorImpl) MemberType() MemberType {
return a.component
}

func (a *componentAccessorImpl) StatefulSetUpdateStrategy() apps.StatefulSetUpdateStrategyType {
if a.ComponentSpec == nil || len(a.ComponentSpec.StatefulSetUpdateStrategy) == 0 {
if len(a.statefulSetUpdateStrategy) == 0 {
Expand Down Expand Up @@ -366,33 +430,35 @@ func (a *componentAccessorImpl) TopologySpreadConstraints() []corev1.TopologySpr
return ptscs
}

func getComponentLabelValue(c Component) string {
func getComponentLabelValue(c MemberType) string {
switch c {
case ComponentPD:
case PDMemberType:
return label.PDLabelVal
case ComponentTiDB:
case TiDBMemberType:
return label.TiDBLabelVal
case ComponentTiKV:
case TiKVMemberType:
return label.TiKVLabelVal
case ComponentTiFlash:
case TiFlashMemberType:
return label.TiFlashLabelVal
case ComponentTiCDC:
case TiCDCMemberType:
return label.TiCDCLabelVal
case ComponentPump:
case PumpMemberType:
return label.PumpLabelVal
case ComponentDiscovery:
case DiscoveryMemberType:
return label.DiscoveryLabelVal
case ComponentDMDiscovery:
case DMDiscoveryMemberType:
return label.DiscoveryLabelVal
case ComponentDMMaster:
case DMMasterMemberType:
return label.DMMasterLabelVal
case ComponentDMWorker:
case DMWorkerMemberType:
return label.DMWorkerLabelVal
case NGMonitoringMemberType:
return label.NGMonitorLabelVal
}
return ""
}

func buildTidbClusterComponentAccessor(c Component, tc *TidbCluster, componentSpec *ComponentSpec) ComponentAccessor {
func buildTidbClusterComponentAccessor(c MemberType, tc *TidbCluster, componentSpec *ComponentSpec) ComponentAccessor {
spec := &tc.Spec
return &componentAccessorImpl{
name: tc.Name,
Expand Down Expand Up @@ -420,7 +486,7 @@ func buildTidbClusterComponentAccessor(c Component, tc *TidbCluster, componentSp
}
}

func buildDMClusterComponentAccessor(c Component, dc *DMCluster, componentSpec *ComponentSpec) ComponentAccessor {
func buildDMClusterComponentAccessor(c MemberType, dc *DMCluster, componentSpec *ComponentSpec) ComponentAccessor {
spec := &dc.Spec
return &componentAccessorImpl{
name: dc.Name,
Expand Down Expand Up @@ -448,9 +514,40 @@ func buildDMClusterComponentAccessor(c Component, dc *DMCluster, componentSpec *
}
}

func buildTiDBNGMonitoringComponentAccessor(c MemberType, tngm *TidbNGMonitoring, componentSpec *ComponentSpec) ComponentAccessor {
commonSpec := tngm.Spec.ComponentSpec
impl := &componentAccessorImpl{
name: tngm.GetName(),
kind: TiDBNGMonitoringKind,
component: c,
imagePullSecrets: commonSpec.ImagePullSecrets,
hostNetwork: commonSpec.HostNetwork,
affinity: commonSpec.Affinity,
priorityClassName: commonSpec.PriorityClassName,
clusterNodeSelector: commonSpec.NodeSelector,
clusterLabels: commonSpec.Labels,
clusterAnnotations: commonSpec.Annotations,
tolerations: commonSpec.Tolerations,
configUpdateStrategy: ConfigUpdateStrategyRollingUpdate,
podSecurityContext: commonSpec.PodSecurityContext,
topologySpreadConstraints: commonSpec.TopologySpreadConstraints,
dnsConfig: commonSpec.DNSConfig,
dnsPolicy: commonSpec.DNSPolicy,

ComponentSpec: componentSpec,
}
if commonSpec.ImagePullPolicy != nil {
impl.imagePullPolicy = *commonSpec.ImagePullPolicy
}
if commonSpec.SchedulerName != nil {
impl.schedulerName = *commonSpec.SchedulerName
}
return impl
}

// BaseDiscoverySpec returns the base spec of discovery component
func (tc *TidbCluster) BaseDiscoverySpec() ComponentAccessor {
return buildTidbClusterComponentAccessor(ComponentDiscovery, tc, tc.Spec.Discovery.ComponentSpec)
return buildTidbClusterComponentAccessor(DiscoveryMemberType, tc, tc.Spec.Discovery.ComponentSpec)
}

// BaseTiDBSpec returns the base spec of TiDB servers
Expand All @@ -460,7 +557,7 @@ func (tc *TidbCluster) BaseTiDBSpec() ComponentAccessor {
spec = &tc.Spec.TiDB.ComponentSpec
}

return buildTidbClusterComponentAccessor(ComponentTiDB, tc, spec)
return buildTidbClusterComponentAccessor(TiDBMemberType, tc, spec)
}

// BaseTiKVSpec returns the base spec of TiKV servers
Expand All @@ -470,7 +567,7 @@ func (tc *TidbCluster) BaseTiKVSpec() ComponentAccessor {
spec = &tc.Spec.TiKV.ComponentSpec
}

return buildTidbClusterComponentAccessor(ComponentTiKV, tc, spec)
return buildTidbClusterComponentAccessor(TiKVMemberType, tc, spec)
}

// BaseTiFlashSpec returns the base spec of TiFlash servers
Expand All @@ -480,7 +577,7 @@ func (tc *TidbCluster) BaseTiFlashSpec() ComponentAccessor {
spec = &tc.Spec.TiFlash.ComponentSpec
}

return buildTidbClusterComponentAccessor(ComponentTiFlash, tc, spec)
return buildTidbClusterComponentAccessor(TiFlashMemberType, tc, spec)
}

// BaseTiCDCSpec returns the base spec of TiCDC servers
Expand All @@ -490,7 +587,7 @@ func (tc *TidbCluster) BaseTiCDCSpec() ComponentAccessor {
spec = &tc.Spec.TiCDC.ComponentSpec
}

return buildTidbClusterComponentAccessor(ComponentTiCDC, tc, spec)
return buildTidbClusterComponentAccessor(TiCDCMemberType, tc, spec)
}

// BasePDSpec returns the base spec of PD servers
Expand All @@ -500,7 +597,7 @@ func (tc *TidbCluster) BasePDSpec() ComponentAccessor {
spec = &tc.Spec.PD.ComponentSpec
}

return buildTidbClusterComponentAccessor(ComponentPD, tc, spec)
return buildTidbClusterComponentAccessor(PDMemberType, tc, spec)
}

// BasePumpSpec returns the base spec of Pump:
Expand All @@ -510,15 +607,15 @@ func (tc *TidbCluster) BasePumpSpec() ComponentAccessor {
spec = &tc.Spec.Pump.ComponentSpec
}

return buildTidbClusterComponentAccessor(ComponentPump, tc, spec)
return buildTidbClusterComponentAccessor(PumpMemberType, tc, spec)
}

func (dc *DMCluster) BaseDiscoverySpec() ComponentAccessor {
return buildDMClusterComponentAccessor(ComponentDMDiscovery, dc, dc.Spec.Discovery.ComponentSpec)
return buildDMClusterComponentAccessor(DMDiscoveryMemberType, dc, dc.Spec.Discovery.ComponentSpec)
}

func (dc *DMCluster) BaseMasterSpec() ComponentAccessor {
return buildDMClusterComponentAccessor(ComponentDMMaster, dc, &dc.Spec.Master.ComponentSpec)
return buildDMClusterComponentAccessor(DMMasterMemberType, dc, &dc.Spec.Master.ComponentSpec)
}

func (dc *DMCluster) BaseWorkerSpec() ComponentAccessor {
Expand All @@ -527,5 +624,9 @@ func (dc *DMCluster) BaseWorkerSpec() ComponentAccessor {
spec = &dc.Spec.Worker.ComponentSpec
}

return buildDMClusterComponentAccessor(ComponentDMWorker, dc, spec)
return buildDMClusterComponentAccessor(DMWorkerMemberType, dc, spec)
}

func (tngm *TidbNGMonitoring) BaseNGMonitoringSpec() ComponentAccessor {
return buildTiDBNGMonitoringComponentAccessor(NGMonitoringMemberType, tngm, &tngm.Spec.NGMonitoring.ComponentSpec)
}
Loading

0 comments on commit 7bd34f0

Please sign in to comment.