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

session: add no register API for using TiDB as a library #17513

Merged
merged 20 commits into from
Jul 14, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
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
20 changes: 16 additions & 4 deletions domain/domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -655,6 +655,14 @@ func NewDomain(store kv.Storage, ddlLease time.Duration, statsLease time.Duratio

// Init initializes a domain.
func (do *Domain) Init(ddlLease time.Duration, sysFactory func(*Domain) (pools.Resource, error)) error {
if err := do.InitWithNoRegister(ddlLease, sysFactory); err != nil {
Copy link
Member

Choose a reason for hiding this comment

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

This also disables the DDL syncer, will it cause problems?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Seems InfoSyncer won't do things about DDL... Is there something I ignored...?

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't know how BR uses the DDL structure. If BR uses InitWithNoRegister to new a DDL, then the DDL also will campaign DDL owner, but it will make load schema slower then Init with InfoSyncer. Is it OK?

return err
}
return do.initInfoSyncer(context.Background())
}

// InitWithNoRegister initializes a domain, but not register itself to PD.
func (do *Domain) InitWithNoRegister(ddlLease time.Duration, sysFactory func(*Domain) (pools.Resource, error)) error {
perfschema.Init()
if ebd, ok := do.store.(tikv.EtcdBackend); ok {
if addrs := ebd.EtcdAddrs(); addrs != nil {
Expand Down Expand Up @@ -723,10 +731,6 @@ func (do *Domain) Init(ddlLease time.Duration, sysFactory func(*Domain) (pools.R
if err != nil {
return err
}
do.info, err = infosync.GlobalInfoSyncerInit(ctx, do.ddl.GetID(), do.etcdClient)
if err != nil {
return err
}
err = do.Reload()
if err != nil {
return err
Expand All @@ -741,6 +745,14 @@ func (do *Domain) Init(ddlLease time.Duration, sysFactory func(*Domain) (pools.R
}
do.wg.Add(1)
go do.topNSlowQueryLoop()
return nil
}

func (do *Domain) initInfoSyncer(ctx context.Context) (err error) {
do.info, err = infosync.GlobalInfoSyncerInit(ctx, do.ddl.GetID(), do.etcdClient)
if err != nil {
return
}

do.wg.Add(1)
go do.infoSyncerKeeper()
Expand Down
36 changes: 32 additions & 4 deletions session/tidb.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import (
"sync/atomic"
"time"

"github.com/ngaut/pools"

"github.com/opentracing/opentracing-go"
"github.com/pingcap/errors"
"github.com/pingcap/parser"
Expand All @@ -43,9 +45,30 @@ import (
"go.uber.org/zap"
)

type domainInitializer func(
YuJuncen marked this conversation as resolved.
Show resolved Hide resolved
dom *domain.Domain,
ddlLease time.Duration,
sysFactory func(*domain.Domain) (pools.Resource, error),
) error

type domainMap struct {
domains map[string]*domain.Domain
mu sync.Mutex
domInit domainInitializer
}

// NewDomainMap makes a new empty domain map.
func NewDomainMap() *domainMap {
return &domainMap{
domains: map[string]*domain.Domain{},
domInit: (*domain.Domain).Init,
}
}

// NoRegister tells this map when creating new Domain, don't register this domain to PD.
// this function is useful when you want to use TiDB as a library instead of server.
func (dm *domainMap) NoRegister() {
dm.domInit = (*domain.Domain).InitWithNoRegister
}

func (dm *domainMap) Get(store kv.Storage) (d *domain.Domain, err error) {
Expand Down Expand Up @@ -75,7 +98,7 @@ func (dm *domainMap) Get(store kv.Storage) (d *domain.Domain, err error) {
factory := createSessionFunc(store)
sysFactory := createSessionWithDomainFunc(store)
d = domain.NewDomain(store, ddlLease, statisticLease, factory)
err1 = d.Init(ddlLease, sysFactory)
err1 = dm.domInit(d, ddlLease, sysFactory)
if err1 != nil {
// If we don't clean it, there are some dirty data when retrying the function of Init.
d.Close()
Expand All @@ -99,9 +122,7 @@ func (dm *domainMap) Delete(store kv.Storage) {
}

var (
domap = &domainMap{
domains: map[string]*domain.Domain{},
}
domap = NewDomainMap()
// store.UUID()-> IfBootstrapped
storeBootstrapped = make(map[string]bool)
storeBootstrappedLock sync.Mutex
Expand All @@ -118,6 +139,13 @@ var (
statsLease = int64(3 * time.Second)
)

// NoRegister tells TiDB don't register itself to PD.
// Some of TiDB components will use TiDB as a library(instead of server),
// this function prevents those fake 'TiDB' present on metrics like dashboard.
func NoRegister() {
domap.NoRegister()
}

// ResetStoreForWithTiKVTest is only used in the test code.
// TODO: Remove domap and storeBootstrapped. Use store.SetOption() to do it.
func ResetStoreForWithTiKVTest(store kv.Storage) {
Expand Down