diff --git a/cmd/tidb-dashboard/main.go b/cmd/tidb-dashboard/main.go index e42f006e7e..870df8a06d 100644 --- a/cmd/tidb-dashboard/main.go +++ b/cmd/tidb-dashboard/main.go @@ -36,7 +36,6 @@ import ( "github.com/joho/godotenv" "github.com/pingcap/log" - "go.etcd.io/etcd/clientv3" "go.uber.org/zap" "github.com/pingcap-incubator/tidb-dashboard/pkg/apiserver" @@ -126,8 +125,12 @@ func main() { store := dbstore.MustOpenDBStore(cliConfig.CoreConfig) defer store.Close() //nolint:errcheck - etcdClient := pd.NewEtcdClient(cliConfig.CoreConfig) - tidbForwarder := tidb.NewForwarder(tidb.NewForwarderConfig(), etcdClient) + etcdProvider, err := pd.NewLocalEtcdClientProvider(cliConfig.CoreConfig) + if err != nil { + _ = store.Close() + log.Fatal("Cannot create etcd client", zap.Error(err)) + } + tidbForwarder := tidb.NewForwarder(tidb.NewForwarderConfig(), etcdProvider) // FIXME: Handle open error tidbForwarder.Open() //nolint:errcheck defer tidbForwarder.Close() //nolint:errcheck @@ -137,10 +140,8 @@ func main() { FileStartTime: cliConfig.KVFileStartTime, FileEndTime: cliConfig.KVFileEndTime, PeriodicGetter: keyvisualinput.NewAPIPeriodicGetter(cliConfig.CoreConfig.PDEndPoint), - GetEtcdClient: func() *clientv3.Client { - return etcdClient - }, - Store: store, + EtcdProvider: etcdProvider, + Store: store, } keyvisualService := keyvisual.NewService(ctx, wg, cliConfig.CoreConfig, remoteDataProvider) keyvisualService.Start() @@ -158,7 +159,7 @@ func main() { listenAddr := fmt.Sprintf("%s:%d", cliConfig.ListenHost, cliConfig.ListenPort) listener, err := net.Listen("tcp", listenAddr) if err != nil { - store.Close() //nolint:errcheck + _ = store.Close() log.Fatal("Dashboard server listen failed", zap.String("addr", listenAddr), zap.Error(err)) } @@ -189,6 +190,6 @@ func main() { } func exit(code int) { - log.Sync() //nolint:errcheck + _ = log.Sync() os.Exit(code) } diff --git a/pkg/keyvisual/decorator/tidb_requests.go b/pkg/keyvisual/decorator/tidb_requests.go index 0724f10c0b..636c213b2d 100644 --- a/pkg/keyvisual/decorator/tidb_requests.go +++ b/pkg/keyvisual/decorator/tidb_requests.go @@ -57,7 +57,7 @@ type tableInfo struct { } func (s *tidbLabelStrategy) updateAddress() { - cli := s.Provider.GetEtcdClient() + cli := s.Provider.EtcdProvider.GetEtcdClient() if cli == nil { return } diff --git a/pkg/keyvisual/region/interface.go b/pkg/keyvisual/region/interface.go index ea3db48134..38e6df768b 100644 --- a/pkg/keyvisual/region/interface.go +++ b/pkg/keyvisual/region/interface.go @@ -14,9 +14,8 @@ package region import ( - "go.etcd.io/etcd/clientv3" - "github.com/pingcap-incubator/tidb-dashboard/pkg/dbstore" + "github.com/pingcap-incubator/tidb-dashboard/pkg/pd" ) type RegionsInfo interface { @@ -35,6 +34,6 @@ type PDDataProvider struct { // This item takes effect only when both FileStartTime and FileEndTime are 0. PeriodicGetter RegionsInfoGenerator - GetEtcdClient func() *clientv3.Client - Store *dbstore.DB + EtcdProvider pd.EtcdProvider + Store *dbstore.DB } diff --git a/pkg/pd/etcd.go b/pkg/pd/etcd.go index dd113ab52c..f08aa9a239 100644 --- a/pkg/pd/etcd.go +++ b/pkg/pd/etcd.go @@ -16,9 +16,7 @@ package pd import ( "time" - "github.com/pingcap/log" "go.etcd.io/etcd/clientv3" - "go.uber.org/zap" "github.com/pingcap-incubator/tidb-dashboard/pkg/config" ) @@ -28,15 +26,29 @@ const ( TiDBServerInformationPath = "/tidb/server/info" ) -func NewEtcdClient(cfg *config.Config) *clientv3.Client { +var _ EtcdProvider = (*LocalEtcdProvider)(nil) + +type EtcdProvider interface { + GetEtcdClient() *clientv3.Client +} + +// FIXME: We should be able to provide etcd directly. However currently there are problems in PD. +type LocalEtcdProvider struct { + client *clientv3.Client +} + +func NewLocalEtcdClientProvider(config *config.Config) (*LocalEtcdProvider, error) { client, err := clientv3.New(clientv3.Config{ - Endpoints: []string{cfg.PDEndPoint}, + Endpoints: []string{config.PDEndPoint}, DialTimeout: EtcdTimeout, - TLS: cfg.TLSConfig, + TLS: config.TLSConfig, }) if err != nil { - log.Error("can not get etcd client", zap.Error(err)) - panic(err) + return nil, err } - return client + return &LocalEtcdProvider{client: client}, nil +} + +func (p *LocalEtcdProvider) GetEtcdClient() *clientv3.Client { + return p.client } diff --git a/pkg/tidb/forwarder.go b/pkg/tidb/forwarder.go index 9d38a9888b..36df26ade6 100644 --- a/pkg/tidb/forwarder.go +++ b/pkg/tidb/forwarder.go @@ -40,9 +40,9 @@ func NewForwarderConfig() *ForwarderConfig { } type Forwarder struct { - ctx context.Context - config *ForwarderConfig - etcdClient *clientv3.Client + ctx context.Context + config *ForwarderConfig + etcdProvider pd.EtcdProvider } func (f *Forwarder) Open() error { @@ -57,7 +57,7 @@ func (f *Forwarder) Close() error { func (f *Forwarder) GetDBConnProp() (host string, port int, err error) { ctx, cancel := context.WithTimeout(f.ctx, f.config.TiDBRetrieveTimeout) - resp, err := f.etcdClient.Get(ctx, pd.TiDBServerInformationPath, clientv3.WithPrefix()) + resp, err := f.etcdProvider.GetEtcdClient().Get(ctx, pd.TiDBServerInformationPath, clientv3.WithPrefix()) cancel() if err != nil { @@ -75,10 +75,10 @@ func (f *Forwarder) GetDBConnProp() (host string, port int, err error) { return "", 0, ErrNoAliveTiDB.New("no TiDB is alive") } -func NewForwarder(config *ForwarderConfig, etcdClient *clientv3.Client) *Forwarder { +func NewForwarder(config *ForwarderConfig, etcdProvider pd.EtcdProvider) *Forwarder { return &Forwarder{ - etcdClient: etcdClient, - config: config, - ctx: context.TODO(), + etcdProvider: etcdProvider, + config: config, + ctx: context.TODO(), } }