Skip to content

Commit

Permalink
Use etcdProvider (#76)
Browse files Browse the repository at this point in the history
Signed-off-by: Breezewish <me@breeswish.org>
  • Loading branch information
breezewish authored Feb 25, 2020
1 parent ff14cd9 commit ec4bbce
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 30 deletions.
19 changes: 10 additions & 9 deletions cmd/tidb-dashboard/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand All @@ -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()
Expand All @@ -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))
}

Expand Down Expand Up @@ -189,6 +190,6 @@ func main() {
}

func exit(code int) {
log.Sync() //nolint:errcheck
_ = log.Sync()
os.Exit(code)
}
2 changes: 1 addition & 1 deletion pkg/keyvisual/decorator/tidb_requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ type tableInfo struct {
}

func (s *tidbLabelStrategy) updateAddress() {
cli := s.Provider.GetEtcdClient()
cli := s.Provider.EtcdProvider.GetEtcdClient()
if cli == nil {
return
}
Expand Down
7 changes: 3 additions & 4 deletions pkg/keyvisual/region/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}
28 changes: 20 additions & 8 deletions pkg/pd/etcd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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
}
16 changes: 8 additions & 8 deletions pkg/tidb/forwarder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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(),
}
}

0 comments on commit ec4bbce

Please sign in to comment.