Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

store TiDB server info to PD and add http api handle #7082

Merged
merged 55 commits into from
Aug 15, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
55 commits
Select commit Hold shift + click to select a range
3918624
add get all server info api
crazycs520 Jul 17, 2018
2370bb0
rename and add comment
crazycs520 Jul 17, 2018
b574aff
add cluster info
crazycs520 Jul 18, 2018
aca6b97
remove isOwner from server static info
crazycs520 Jul 18, 2018
d5a1922
refine comment
crazycs520 Jul 18, 2018
3c8f418
Merge branch 'master' of https://github.com/pingcap/tidb into put_ser…
crazycs520 Jul 18, 2018
24eea21
add lease to server info and change tidb_http_api.md
crazycs520 Jul 18, 2018
f0007a4
refine log
crazycs520 Jul 18, 2018
159a7b4
refine http-api ddl/info
crazycs520 Jul 18, 2018
f143709
refine code
crazycs520 Jul 19, 2018
256798e
Merge branch 'master' of https://github.com/pingcap/tidb into put_ser…
crazycs520 Jul 19, 2018
8bd6c3d
rename variable
crazycs520 Jul 19, 2018
80cdd6a
refactor code
crazycs520 Jul 22, 2018
20e372f
Merge branch 'master' of https://github.com/pingcap/tidb into put_ser…
crazycs520 Jul 23, 2018
839f298
refactor and add comment
crazycs520 Jul 23, 2018
bb2f009
Merge branch 'master' of https://github.com/pingcap/tidb into put_ser…
crazycs520 Jul 23, 2018
ed0217e
Merge branch 'master' of https://github.com/pingcap/tidb into put_ser…
crazycs520 Jul 31, 2018
d0c1925
refine code
crazycs520 Aug 2, 2018
a925b92
Merge branch 'master' of https://github.com/pingcap/tidb into put_ser…
crazycs520 Aug 2, 2018
5fdba0b
refine code
crazycs520 Aug 2, 2018
73ea84f
add http test
crazycs520 Aug 2, 2018
440953f
fmt code
crazycs520 Aug 2, 2018
9815df2
add test and comment
crazycs520 Aug 5, 2018
321cf31
update test
crazycs520 Aug 5, 2018
027c983
refactor code
crazycs520 Aug 5, 2018
c2ff1e5
refactor code
crazycs520 Aug 6, 2018
8baa5be
refine code
crazycs520 Aug 6, 2018
be06280
refine code and add comment
crazycs520 Aug 7, 2018
8697127
Merge branch 'master' of https://github.com/pingcap/tidb into put_ser…
crazycs520 Aug 7, 2018
38e30f1
update test and comment
crazycs520 Aug 7, 2018
c814267
refine code
crazycs520 Aug 7, 2018
9d091c9
rename function
crazycs520 Aug 8, 2018
ea275c1
refine log
crazycs520 Aug 8, 2018
33d26a0
refine code
crazycs520 Aug 8, 2018
750407c
refine code and add comment
crazycs520 Aug 8, 2018
ec488e9
refine code and comment
crazycs520 Aug 8, 2018
b40cf74
refine code
crazycs520 Aug 8, 2018
c32d982
Merge branch 'master' of https://github.com/pingcap/tidb into put_ser…
crazycs520 Aug 9, 2018
7403bb1
only return self server info when do /info http request
crazycs520 Aug 9, 2018
3801efe
refine code and comment
crazycs520 Aug 9, 2018
b27b804
refine comment
crazycs520 Aug 9, 2018
11dfd86
store server info with lease
crazycs520 Aug 9, 2018
7fb9c1a
Merge branch 'master' of https://github.com/pingcap/tidb into put_ser…
crazycs520 Aug 9, 2018
c4586ea
use new seesion for server info syncer
crazycs520 Aug 10, 2018
b7b35e3
address comment
crazycs520 Aug 10, 2018
d54c1ce
Merge branch 'master' of https://github.com/pingcap/tidb into put_ser…
crazycs520 Aug 11, 2018
01eed94
Merge branch 'master' of https://github.com/pingcap/tidb into put_ser…
crazycs520 Aug 13, 2018
6060b57
address comment
crazycs520 Aug 13, 2018
2a97788
address comment
crazycs520 Aug 14, 2018
2da0623
address comment
crazycs520 Aug 14, 2018
26c8b25
address comment
crazycs520 Aug 14, 2018
93c76ce
Merge branch 'master' of https://github.com/pingcap/tidb into put_ser…
crazycs520 Aug 14, 2018
f8b7b98
Merge branch 'master' of https://github.com/pingcap/tidb into put_ser…
crazycs520 Aug 15, 2018
9831a1f
Merge branch 'master' of https://github.com/pingcap/tidb into put_ser…
crazycs520 Aug 15, 2018
34d12be
Merge branch 'master' into put_server_info_to_pd
crazycs520 Aug 15, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions ddl/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ func NewMockSchemaSyncer() SchemaSyncer {
return &mockSchemaSyncer{}
}

// GetSessionLeaseID implements SchemaSyncer.GetSessionLeaseID interface.
func (s *mockSchemaSyncer) GetSessionLeaseID() clientv3.LeaseID {
return 0
}

// Init implements SchemaSyncer.Init interface.
func (s *mockSchemaSyncer) Init(ctx context.Context) error {
s.globalVerCh = make(chan clientv3.WatchResponse, 1)
Expand Down
7 changes: 7 additions & 0 deletions ddl/syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ type SchemaSyncer interface {
// the latest schema version. If the result is false, wait for a while and check again util the processing time reach 2 * lease.
// It returns until all servers' versions are equal to the latest version or the ctx is done.
OwnerCheckAllVersions(ctx context.Context, latestVer int64) error
// GetSession return the session lease id of SchemaSyncer.
GetSessionLeaseID() clientv3.LeaseID
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/GetSessionLeaseID/GetLeaseID/

}

type schemaVersionSyncer struct {
Expand Down Expand Up @@ -127,6 +129,11 @@ func PutKVToEtcd(ctx context.Context, etcdCli *clientv3.Client, retryCnt int, ke
return errors.Trace(err)
}

// GetSessionLeaseID implements SchemaSyncer.GetSessionLeaseID interface.
func (s *schemaVersionSyncer) GetSessionLeaseID() clientv3.LeaseID {
return s.session.Lease()
}

// Init implements SchemaSyncer.Init interface.
func (s *schemaVersionSyncer) Init(ctx context.Context) error {
startTime := time.Now()
Expand Down
3 changes: 2 additions & 1 deletion domain/domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,7 @@ func (do *Domain) loadSchemaInLoop(lease time.Duration) {
break
}
do.SchemaValidator.Restart()
do.info.Restart(syncer.GetSessionLeaseID())
case <-do.exit:
return
}
Expand Down Expand Up @@ -509,7 +510,7 @@ func (do *Domain) Init(ddlLease time.Duration, sysFactory func(*Domain) (pools.R
if err != nil {
return errors.Trace(err)
}
do.info = NewInfoSyncer(do.ddl.GetID(), do.etcdClient)
do.info = NewInfoSyncer(do.ddl.GetID(), do.etcdClient, do.ddl.SchemaSyncer().GetSessionLeaseID())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

umm, could we create a new session in infoSyncer, like we do in schemaVersionSyncer.Init, then infoSyncer and DDL can have independent session.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

err = do.info.StoreServerInfo(ctx)
if err != nil {
return errors.Trace(err)
Expand Down
42 changes: 26 additions & 16 deletions domain/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ type InfoSyncer struct {
etcdCli *clientv3.Client
info *ServerInfo
serverInfoPath string
// InfoSyncer will reuse the leaseID of ddl.SchemaSyncer.
sessionLeaseID clientv3.LeaseID
}

// ServerInfo is server static information.
Expand All @@ -63,28 +65,15 @@ type ServerVersionInfo struct {
}

// NewInfoSyncer return new InfoSyncer. It is exported for testing.
func NewInfoSyncer(id string, etcdCli *clientv3.Client) *InfoSyncer {
func NewInfoSyncer(id string, etcdCli *clientv3.Client, leaseID clientv3.LeaseID) *InfoSyncer {
return &InfoSyncer{
etcdCli: etcdCli,
info: getServerInfo(id),
serverInfoPath: fmt.Sprintf("%s/%s", ServerInformationPath, id),
sessionLeaseID: leaseID,
}
}

func getServerInfo(id string) *ServerInfo {
cfg := config.GetGlobalConfig()
info := &ServerInfo{
ID: id,
IP: cfg.AdvertiseAddress,
Port: cfg.Port,
StatusPort: cfg.Status.StatusPort,
Lease: cfg.Lease,
}
info.Version = mysql.ServerVersion
info.GitHash = printer.TiDBGitHash
return info
}

//GetServerInfo gets self server static information.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s//Get// Get

func (is *InfoSyncer) GetServerInfo() *ServerInfo {
return is.info
Expand Down Expand Up @@ -130,7 +119,7 @@ func (is *InfoSyncer) StoreServerInfo(ctx context.Context) error {
if err != nil {
return errors.Trace(err)
}
err = ddl.PutKVToEtcd(ctx, is.etcdCli, keyOpDefaultRetryCnt, is.serverInfoPath, hack.String(infoBuf))
err = ddl.PutKVToEtcd(ctx, is.etcdCli, keyOpDefaultRetryCnt, is.serverInfoPath, hack.String(infoBuf), clientv3.WithLease(is.sessionLeaseID))
return errors.Trace(err)
}

Expand All @@ -145,6 +134,12 @@ func (is *InfoSyncer) RemoveServerInfo() {
}
}

// Restart restart the info syncer with new session leaseID and store server info to etcd again.
func (is *InfoSyncer) Restart(leaseID clientv3.LeaseID) {
is.sessionLeaseID = leaseID
is.StoreServerInfo(context.Background())
}

// getInfo gets server information from Etcd according to the key and opts.
func getInfo(ctx context.Context, etcdCli *clientv3.Client, key string, retryCnt int, timeout time.Duration, opts ...clientv3.OpOption) (map[string]*ServerInfo, error) {
var err error
Expand Down Expand Up @@ -178,3 +173,18 @@ func getInfo(ctx context.Context, etcdCli *clientv3.Client, key string, retryCnt
}
return nil, errors.Trace(err)
}

// getServerInfo gets self tidb server information.
func getServerInfo(id string) *ServerInfo {
cfg := config.GetGlobalConfig()
info := &ServerInfo{
ID: id,
IP: cfg.AdvertiseAddress,
Port: cfg.Port,
StatusPort: cfg.Status.StatusPort,
Lease: cfg.Lease,
}
info.Version = mysql.ServerVersion
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not move the two lines into the above block?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Version and GitHash belong to ServerVersionInfo. ServerInfo contain ServerVersionInfo with no attribute name.

info.GitHash = printer.TiDBGitHash
return info
}
2 changes: 2 additions & 0 deletions server/http_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -1269,6 +1269,7 @@ func (h serverInfoHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
do, err := session.GetDomain(h.store.(kv.Storage))
if err != nil {
writeError(w, errors.New("create session error"))
log.Error(err)
return
}
info := serverInfo{}
Expand All @@ -1291,6 +1292,7 @@ func (h allServerInfoHandler) ServeHTTP(w http.ResponseWriter, req *http.Request
do, err := session.GetDomain(h.store.(kv.Storage))
if err != nil {
writeError(w, errors.New("create session error"))
log.Error(err)
return
}
ctx := context.Background()
Expand Down