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

domain,session: simplify the session pool of domain #8456

Merged
merged 6 commits into from
Nov 27, 2018
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
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
64 changes: 61 additions & 3 deletions domain/domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ type Domain struct {
info *InfoSyncer
m sync.Mutex
SchemaValidator SchemaValidator
sysSessionPool *pools.ResourcePool
sysSessionPool *sessionPool
exit chan struct{}
etcdClient *clientv3.Client
wg sync.WaitGroup
Expand Down Expand Up @@ -526,7 +526,7 @@ func NewDomain(store kv.Storage, ddlLease time.Duration, statsLease time.Duratio
store: store,
SchemaValidator: NewSchemaValidator(ddlLease),
exit: make(chan struct{}),
sysSessionPool: pools.NewResourcePool(factory, capacity, capacity, resourceIdleTimeout),
sysSessionPool: newSessionPool(capacity, factory),
lysu marked this conversation as resolved.
Show resolved Hide resolved
statsLease: statsLease,
infoHandle: infoschema.NewHandle(store),
slowQuery: newTopNSlowQueries(30, time.Hour*24*7, 500),
Expand Down Expand Up @@ -606,8 +606,66 @@ func (do *Domain) Init(ddlLease time.Duration, sysFactory func(*Domain) (pools.R
return nil
}

type sessionPool struct {
resources chan pools.Resource
factory pools.Factory
mu struct {
sync.RWMutex
closed bool
}
}

func newSessionPool(cap int, factory pools.Factory) *sessionPool {
return &sessionPool{
resources: make(chan pools.Resource, cap),
factory: factory,
}
}

func (p *sessionPool) Get() (resource pools.Resource, err error) {
Copy link
Member

Choose a reason for hiding this comment

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

should we check p.mu.closed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not necessary.
line 629 handles the sessionPool is closed scene.

var ok bool
select {
case resource, ok = <-p.resources:
if !ok {
err = errors.New("session pool closed")
}
default:
resource, err = p.factory()
}
return
}

func (p *sessionPool) Put(resource pools.Resource) {
p.mu.RLock()
defer p.mu.RUnlock()
lysu marked this conversation as resolved.
Show resolved Hide resolved
if p.mu.closed {
resource.Close()
return
}

select {
case p.resources <- resource:
default:
resource.Close()
}
}
func (p *sessionPool) Close() {
p.mu.Lock()
if p.mu.closed {
p.mu.Unlock()
return
}
p.mu.closed = true
close(p.resources)
p.mu.Unlock()

for r := range p.resources {
r.Close()
}
}

// SysSessionPool returns the system session pool.
func (do *Domain) SysSessionPool() *pools.ResourcePool {
func (do *Domain) SysSessionPool() *sessionPool {
return do.sysSessionPool
}

Expand Down
7 changes: 6 additions & 1 deletion session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -577,7 +577,12 @@ func sqlForLog(sql string) string {
return executor.QueryReplacer.Replace(sql)
}

func (s *session) sysSessionPool() *pools.ResourcePool {
type sessionPool interface {
Get() (pools.Resource, error)
Put(pools.Resource)
}

func (s *session) sysSessionPool() sessionPool {
return domain.GetDomain(s).SysSessionPool()
}

Expand Down
2 changes: 0 additions & 2 deletions session/tidb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,6 @@ func (s *testMainSuite) TestSysSessionPoolGoroutineLeak(c *C) {
}(se)
}
wg.Wait()
se.sysSessionPool().Close()
c.Assert(se.sysSessionPool().IsClosed(), Equals, true)
}

func newStore(c *C, dbPath string) kv.Storage {
Expand Down