Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
Signed-off-by: husharp <ihusharp@gmail.com>
  • Loading branch information
HuSharp committed Jul 25, 2024
1 parent 156054e commit 8705617
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 8 deletions.
30 changes: 24 additions & 6 deletions pkg/cluster/operation/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,17 +131,19 @@ func Upgrade(
if instance.IgnoreMonitorAgent() {
noAgentHosts.Insert(instance.GetManageHost())
}

// Usage within the switch statement
switch component.Name() {
case spec.ComponentPD:
// defer PD leader to be upgraded after others
isLeader, err := instance.(*spec.PDInstance).IsLeader(ctx, topo, int(options.APITimeout), tlsCfg)
case spec.ComponentPD, spec.ComponentTSO, spec.ComponentScheduling:
// defer PD leader/primary to be upgraded after others
isPrimary, err := checkAndDeferPDInstance(ctx, topo, int(options.APITimeout), tlsCfg, instance)
if err != nil {
logger.Warnf("cannot found pd leader, ignore: %s", err)
logger.Warnf("cannot found pd related leader/primary, ignore: %s", err)
return err
}
if isLeader {
if isPrimary {
deferInstances = append(deferInstances, instance)
logger.Debugf("Deferred upgrading of PD leader %s", instance.ID())
logger.Debugf("Deferred upgrading of TSO primary %s", instance.ID())
continue
}
case spec.ComponentCDC:
Expand Down Expand Up @@ -218,6 +220,22 @@ func Upgrade(
return RestartMonitored(ctx, uniqueHosts.Slice(), noAgentHosts, topo.GetMonitoredOptions(), options.OptTimeout, systemdMode)
}

// checkAndDeferInstance checks the instance's status and defers its upgrade if necessary.
func checkAndDeferPDInstance(ctx context.Context, topo spec.Topology, apiTimeout int, tlsCfg *tls.Config, instance spec.Instance) (isPrimary bool, err error) {
switch instance.ComponentName() {
case spec.ComponentPD:
isPrimary, err = instance.(*spec.PDInstance).IsLeader(ctx, topo, apiTimeout, tlsCfg)
case spec.ComponentScheduling:
isPrimary, err = instance.(*spec.SchedulingInstance).IsPrimary(ctx, topo, tlsCfg)
case spec.ComponentTSO:
isPrimary, err = instance.(*spec.TSOInstance).IsPrimary(ctx, topo, tlsCfg)
}
if err != nil {
return false, err
}
return isPrimary, nil
}

func upgradeInstance(
ctx context.Context,
topo spec.Topology,
Expand Down
26 changes: 25 additions & 1 deletion pkg/cluster/spec/scheduling.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,16 @@ import (
"strings"
"time"

"github.com/pingcap/errors"
"github.com/pingcap/tiup/pkg/cluster/api"
"github.com/pingcap/tiup/pkg/cluster/ctxt"
"github.com/pingcap/tiup/pkg/cluster/template/scripts"
"github.com/pingcap/tiup/pkg/meta"
"github.com/pingcap/tiup/pkg/utils"
)

var schedulingService = "scheduling"

// SchedulingSpec represents the scheduling topology specification in topology.yaml
type SchedulingSpec struct {
Host string `yaml:"host"`
Expand Down Expand Up @@ -63,7 +66,7 @@ func (s *SchedulingSpec) Status(ctx context.Context, timeout time.Duration, tlsC
return "Down"
}

primary, err := pc.GetServicePrimary("scheduling")
primary, err := pc.GetServicePrimary(schedulingService)
if err != nil {
return "ERR"
}
Expand Down Expand Up @@ -303,6 +306,27 @@ func (i *SchedulingInstance) setTLSConfig(ctx context.Context, enableTLS bool, c
return configs, nil
}

// IsPrimary checks if the instance is primary
func (i *SchedulingInstance) IsPrimary(ctx context.Context, topo Topology, tlsCfg *tls.Config) (bool, error) {
tidbTopo, ok := topo.(*Specification)
if !ok {
panic("topo should be type of tidb topology")
}
pdClient := api.NewPDClient(ctx, tidbTopo.GetPDListWithManageHost(), time.Second*5, tlsCfg)
primary, err := pdClient.GetServicePrimary(schedulingService)
if err != nil {
return false, errors.Annotatef(err, "failed to get Scheduling primary %s", i.GetHost())
}

spec := i.InstanceSpec.(*SchedulingSpec)
enableTLS := false
if tlsCfg != nil {
enableTLS = true
}

return primary == spec.GetAdvertiseListenURL(enableTLS), nil
}

// ScaleConfig deploy temporary config on scaling
func (i *SchedulingInstance) ScaleConfig(
ctx context.Context,
Expand Down
26 changes: 25 additions & 1 deletion pkg/cluster/spec/tso.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,16 @@ import (
"strings"
"time"

"github.com/pingcap/errors"
"github.com/pingcap/tiup/pkg/cluster/api"
"github.com/pingcap/tiup/pkg/cluster/ctxt"
"github.com/pingcap/tiup/pkg/cluster/template/scripts"
"github.com/pingcap/tiup/pkg/meta"
"github.com/pingcap/tiup/pkg/utils"
)

var tsoService = "tso"

// TSOSpec represents the TSO topology specification in topology.yaml
type TSOSpec struct {
Host string `yaml:"host"`
Expand Down Expand Up @@ -63,7 +66,7 @@ func (s *TSOSpec) Status(ctx context.Context, timeout time.Duration, tlsCfg *tls
return "Down"
}

primary, err := pc.GetServicePrimary("tso")
primary, err := pc.GetServicePrimary(tsoService)
if err != nil {
return "ERR"
}
Expand Down Expand Up @@ -303,6 +306,27 @@ func (i *TSOInstance) setTLSConfig(ctx context.Context, enableTLS bool, configs
return configs, nil
}

// IsPrimary checks if the instance is primary
func (i *TSOInstance) IsPrimary(ctx context.Context, topo Topology, tlsCfg *tls.Config) (bool, error) {
tidbTopo, ok := topo.(*Specification)
if !ok {
panic("topo should be type of tidb topology")
}
pdClient := api.NewPDClient(ctx, tidbTopo.GetPDListWithManageHost(), time.Second*5, tlsCfg)
primary, err := pdClient.GetServicePrimary(tsoService)
if err != nil {
return false, errors.Annotatef(err, "failed to get TSO primary %s", i.GetHost())
}

spec := i.InstanceSpec.(*TSOSpec)
enableTLS := false
if tlsCfg != nil {
enableTLS = true
}

return primary == spec.GetAdvertiseListenURL(enableTLS), nil
}

// ScaleConfig deploy temporary config on scaling
func (i *TSOInstance) ScaleConfig(
ctx context.Context,
Expand Down

0 comments on commit 8705617

Please sign in to comment.