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

ddl: remove mock.Context ref in ddl/session/session_pool.go #53378

Merged
merged 1 commit into from
May 20, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
10 changes: 9 additions & 1 deletion pkg/ddl/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"context"
"time"

"github.com/ngaut/pools"
"github.com/pingcap/tidb/pkg/ddl/copr"
"github.com/pingcap/tidb/pkg/ddl/internal/session"
"github.com/pingcap/tidb/pkg/kv"
Expand All @@ -26,6 +27,7 @@ import (
"github.com/pingcap/tidb/pkg/table"
"github.com/pingcap/tidb/pkg/types"
"github.com/pingcap/tidb/pkg/util/chunk"
"github.com/pingcap/tidb/pkg/util/mock"
)

type resultChanForTest struct {
Expand All @@ -47,14 +49,20 @@ func FetchChunk4Test(copCtx copr.CopContext, tbl table.PhysicalTable, startKey,
}
taskCh := make(chan *reorgBackfillTask, 5)
resultCh := make(chan IndexRecordChunk, 5)
sessPool := session.NewSessionPool(nil, store)
resPool := pools.NewResourcePool(func() (pools.Resource, error) {
ctx := mock.NewContext()
ctx.Store = store
return ctx, nil
}, 8, 8, 0)
sessPool := session.NewSessionPool(resPool, store)
pool := newCopReqSenderPool(context.Background(), copCtx, store, taskCh, sessPool, nil)
pool.chunkSender = &resultChanForTest{ch: resultCh}
pool.adjustSize(1)
pool.tasksCh <- task
rs := <-resultCh
close(taskCh)
pool.close(false)
sessPool.Close()
return rs.Chunk
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/ddl/internal/session/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ go_library(
"//pkg/sessionctx",
"//pkg/sessiontxn",
"//pkg/util/chunk",
"//pkg/util/mock",
"//pkg/util/intest",
"//pkg/util/sqlexec",
"@com_github_ngaut_pools//:pools",
"@com_github_pingcap_errors//:errors",
Expand Down
16 changes: 4 additions & 12 deletions pkg/ddl/internal/session/session_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
"github.com/pingcap/tidb/pkg/kv"
"github.com/pingcap/tidb/pkg/parser/mysql"
"github.com/pingcap/tidb/pkg/sessionctx"
"github.com/pingcap/tidb/pkg/util/mock"
"github.com/pingcap/tidb/pkg/util/intest"
)

// Pool is used to new Session.
Expand All @@ -40,18 +40,14 @@ type Pool struct {

// NewSessionPool creates a new Session pool.
func NewSessionPool(resPool *pools.ResourcePool, store kv.Storage) *Pool {
intest.AssertNotNil(resPool)
intest.AssertNotNil(store)
return &Pool{resPool: resPool, store: store}
}

// Get gets sessionCtx from context resource pool.
// Please remember to call Put after you finished using sessionCtx.
func (sg *Pool) Get() (sessionctx.Context, error) {
if sg.resPool == nil {
ctx := mock.NewContext()
ctx.Store = sg.store
return ctx, nil
}

sg.mu.Lock()
if sg.mu.closed {
sg.mu.Unlock()
Expand All @@ -78,10 +74,6 @@ func (sg *Pool) Get() (sessionctx.Context, error) {

// Put returns sessionCtx to context resource pool.
func (sg *Pool) Put(ctx sessionctx.Context) {
if sg.resPool == nil {
return
}

// no need to protect sg.resPool, even the sg.resPool is closed, the ctx still need to
// Put into resPool, because when resPool is closing, it will wait all the ctx returns, then resPool finish closing.
sg.resPool.Put(ctx.(pools.Resource))
Expand All @@ -93,7 +85,7 @@ func (sg *Pool) Close() {
sg.mu.Lock()
defer sg.mu.Unlock()
// prevent closing resPool twice.
if sg.mu.closed || sg.resPool == nil {
if sg.mu.closed {
return
}
logutil.DDLLogger().Info("closing session pool")
Expand Down