Issues with TiUP Source Code

Note:
This topic has been translated from a Chinese forum by GPT and might contain errors.

Original topic: tiup 源码的问题

| username: Raymond

Dear teachers, I have been looking at the tiup source code recently. In src/tiup-1.11.0/pkg/cluster/manager/manager.go, there is a line:

metadata = m.specManager.NewMetadata()

Here, calling m.specManager.NewMetadata() actually returns an interface (I don’t quite understand why it’s written this way).

However, when I tried to simulate this part of the code myself, I encountered an error. Could you please take a look and see where I might have gone wrong?

type Metadata interface {
    GetTopology() Topology
}

type Topology interface {
    Type() string
}

func (s *Specification) Type() string {
    TopoTypeTiDB := "tidb-cluster"
    return TopoTypeTiDB
    //TopoTypeTiDB      = "tidb-cluster"
}

type Specification struct {
    GlobalOptions GlobalOptions `yaml:"global,omitempty" validate:"global:editable"`
    //MonitoredOptions MonitoredOptions     `yaml:"monitored,omitempty" validate:"monitored:editable"`
    //ServerConfigs    ServerConfigs        `yaml:"server_configs,omitempty" validate:"server_configs:ignore"`
    //TiDBServers      []*TiDBSpec          `yaml:"tidb_servers"`
    //TiKVServers      []*TiKVSpec          `yaml:"tikv_servers"`
    //TiFlashServers   []*TiFlashSpec       `yaml:"tiflash_servers"`
    //PDServers        []*PDSpec            `yaml:"pd_servers"`
    //DashboardServers []*DashboardSpec     `yaml:"tidb_dashboard_servers,omitempty"`
    //PumpServers      []*PumpSpec          `yaml:"pump_servers,omitempty"`
    //Drainers         []*DrainerSpec       `yaml:"drainer_servers,omitempty"`
    //CDCServers       []*CDCSpec           `yaml:"cdc_servers,omitempty"`
    //TiKVCDCServers   []*TiKVCDCSpec       `yaml:"kvcdc_servers,omitempty"`
    //TiSparkMasters   []*TiSparkMasterSpec `yaml:"tispark_masters,omitempty"`
    //TiSparkWorkers   []*TiSparkWorkerSpec `yaml:"tispark_workers,omitempty"`
    //Monitors         []*PrometheusSpec    `yaml:"monitoring_servers"`
    //Grafanas         []*GrafanaSpec       `yaml:"grafana_servers,omitempty"`
    //Alertmanagers    []*AlertmanagerSpec  `yaml:"alertmanager_servers,omitempty"`
}

type GlobalOptions struct {
    User    string `yaml:"user,omitempty" default:"tidb"`
    Group   string `yaml:"group,omitempty"`
    SSHPort int    `yaml:"ssh_port,omitempty" default:"22" validate:"ssh_port:editable"`
    //SSHType         executor.SSHType     `yaml:"ssh_type,omitempty" default:"builtin"`
    TLSEnabled bool   `yaml:"enable_tls,omitempty"`
    DeployDir  string `yaml:"deploy_dir,omitempty" default:"deploy"`
    DataDir    string `yaml:"data_dir,omitempty" default:"data"`
    LogDir     string `yaml:"log_dir,omitempty"`
    //ResourceControl meta.ResourceControl `yaml:"resource_control,omitempty" validate:"resource_control:editable"`
    OS     string      `yaml:"os,omitempty" default:"linux"`
    Arch   string      `yaml:"arch,omitempty"`
    Custom interface{} `yaml:"custom,omitempty" validate:"custom:ignore"`
}

type Manager struct {
    sysName     string
    specManager *SpecManager
    //bindVersion spec.BindVersion
    //logger      *logprinter.Logger
}

type SpecManager struct {
    base    string
    newMeta func() Metadata
}

func (s *SpecManager) NewMetadata() Metadata {
    return s.newMeta()
}

func (m *Manager) meta(name string) {
    metadata := m.specManager.NewMetadata()
    fmt.Println(metadata)
}

func main() {
    var cm *Manager
    cm.meta("tidb-test")
}

The error is as follows:

| username: hey-hoho | Original post link

Your cm is nil, so a null pointer exception is reported.
You need to create a Manager instance first.

| username: Raymond | Original post link

So why does it work when executed like this?

| username: hey-hoho | Original post link

The issue lies in the first line you commented out. m is a nil value, and you cannot use m.xxx to reference its member variables.

| username: Raymond | Original post link

Got it, thanks a lot!

| username: system | Original post link

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.