Skip to content

Commit 240ddf8

Browse files
authored
planner: encapsulate task dir and move test related code to test file (#52083)
close #51664
1 parent e7b4d31 commit 240ddf8

File tree

6 files changed

+82
-61
lines changed

6 files changed

+82
-61
lines changed

pkg/planner/memo/BUILD.bazel

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ go_library(
88
"group_expr.go",
99
"implementation.go",
1010
"pattern.go",
11-
"task.go",
12-
"task_scheduler.go",
1311
],
1412
importpath = "github.com/pingcap/tidb/pkg/planner/memo",
1513
visibility = ["//visibility:public"],
@@ -29,12 +27,10 @@ go_test(
2927
"group_test.go",
3028
"main_test.go",
3129
"pattern_test.go",
32-
"task_scheduler_test.go",
33-
"task_test.go",
3430
],
3531
embed = [":memo"],
3632
flaky = True,
37-
shard_count = 25,
33+
shard_count = 22,
3834
deps = [
3935
"//pkg/domain",
4036
"//pkg/expression",

pkg/planner/task/BUILD.bazel

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
2+
3+
go_library(
4+
name = "task",
5+
srcs = [
6+
"task.go",
7+
"task_scheduler.go",
8+
],
9+
importpath = "github.com/pingcap/tidb/pkg/planner/task",
10+
visibility = ["//visibility:public"],
11+
)
12+
13+
go_test(
14+
name = "task_test",
15+
timeout = "short",
16+
srcs = [
17+
"task_scheduler_test.go",
18+
"task_test.go",
19+
],
20+
embed = [":task"],
21+
flaky = True,
22+
shard_count = 3,
23+
deps = ["@com_github_stretchr_testify//require"],
24+
)

pkg/planner/memo/task.go renamed to pkg/planner/task/task.go

Lines changed: 6 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,15 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
package memo
15+
package task
1616

1717
import (
1818
"strings"
1919
"sync"
2020
)
2121

22-
// Task is an interface defined for all type of optimizing work: exploring, implementing, deriving-stats, join-reordering and so on.
22+
// Task is an interface defined for all type of optimizing work: exploring, implementing,
23+
// deriving-stats, join-reordering and so on.
2324
type Task interface {
2425
// task self executing logic
2526
execute() error
@@ -35,8 +36,8 @@ type Stack interface {
3536
Destroy()
3637
}
3738

38-
// TaskStackPool is initialized for memory saving by reusing taskStack.
39-
var TaskStackPool = sync.Pool{
39+
// StackTaskPool is initialized for memory saving by reusing taskStack.
40+
var StackTaskPool = sync.Pool{
4041
New: func() any {
4142
return newTaskStack()
4243
},
@@ -57,7 +58,7 @@ func newTaskStack() *taskStack {
5758
func (ts *taskStack) Destroy() {
5859
// when a taskStack itself is useless, we can destroy itself actively.
5960
clear(ts.tasks)
60-
TaskStackPool.Put(ts)
61+
StackTaskPool.Put(ts)
6162
}
6263

6364
// Desc is used to desc the detail info about current stack state.
@@ -102,39 +103,3 @@ func newTaskStackWithCap(c int) *taskStack {
102103
tasks: make([]Task, 0, c),
103104
}
104105
}
105-
106-
// TaskStack2 is used to store the optimizing tasks created before or during the optimizing process.
107-
type taskStack2 struct {
108-
tasks []*Task
109-
}
110-
111-
func newTaskStack2WithCap(c int) *taskStack2 {
112-
return &taskStack2{
113-
tasks: make([]*Task, 0, c),
114-
}
115-
}
116-
117-
// Push indicates to push one task into the stack.
118-
func (ts *taskStack2) Push(one Task) {
119-
ts.tasks = append(ts.tasks, &one)
120-
}
121-
122-
// Len indicates the length of current stack.
123-
func (ts *taskStack2) Len() int {
124-
return len(ts.tasks)
125-
}
126-
127-
// Empty indicates whether taskStack is empty.
128-
func (ts *taskStack2) Empty() bool {
129-
return ts.Len() == 0
130-
}
131-
132-
// Pop indicates to pop one task out of the stack.
133-
func (ts *taskStack2) Pop() Task {
134-
if !ts.Empty() {
135-
tmp := ts.tasks[len(ts.tasks)-1]
136-
ts.tasks = ts.tasks[:len(ts.tasks)-1]
137-
return *tmp
138-
}
139-
return nil
140-
}

pkg/planner/memo/task_scheduler.go renamed to pkg/planner/task/task_scheduler.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,19 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
package memo
15+
package task
1616

17-
var _ TaskScheduler = &SimpleTaskScheduler{}
17+
var _ Scheduler = &SimpleTaskScheduler{}
1818

19-
// TaskScheduler is a scheduling interface defined for serializing(single thread)/concurrent(multi thread) running.
20-
type TaskScheduler interface {
19+
// Scheduler is a scheduling interface defined for serializing(single thread)/concurrent(multi thread) running.
20+
type Scheduler interface {
2121
ExecuteTasks()
2222
}
2323

2424
// SimpleTaskScheduler is defined for serializing scheduling of memo tasks.
2525
type SimpleTaskScheduler struct {
2626
Err error
27-
SchedulerCtx TaskSchedulerContext
27+
SchedulerCtx SchedulerContext
2828
}
2929

3030
// ExecuteTasks implements the interface of TaskScheduler.
@@ -46,8 +46,8 @@ func (s *SimpleTaskScheduler) ExecuteTasks() {
4646
}
4747
}
4848

49-
// TaskSchedulerContext is defined for scheduling logic calling, also facilitate interface-oriented coding and testing.
50-
type TaskSchedulerContext interface {
49+
// SchedulerContext is defined for scheduling logic calling, also facilitate interface-oriented coding and testing.
50+
type SchedulerContext interface {
5151
// we exported the Stack interface here rather than the basic stack implementation.
5252
getStack() Stack
5353
// we exported the only one push action to user, Task is an interface definition.

pkg/planner/memo/task_scheduler_test.go renamed to pkg/planner/task/task_scheduler_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
package memo
15+
package task
1616

1717
import (
1818
"errors"
@@ -63,7 +63,7 @@ func TestSimpleTaskScheduler(t *testing.T) {
6363
testScheduler.SchedulerCtx.pushTask(&TestTaskImpl2{a: 2})
6464
testScheduler.SchedulerCtx.pushTask(&TestTaskImpl2{a: 3})
6565

66-
var testTaskScheduler TaskScheduler = testScheduler
66+
var testTaskScheduler Scheduler = testScheduler
6767
testTaskScheduler.ExecuteTasks()
6868
require.NotNil(t, testScheduler.Err)
6969
require.Equal(t, testScheduler.Err.Error(), "mock error at task id = 2")

pkg/planner/memo/task_test.go renamed to pkg/planner/task/task_test.go

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
package memo
15+
package task
1616

1717
import (
1818
"strconv"
@@ -50,7 +50,7 @@ func TestTaskStack(t *testing.T) {
5050
}
5151

5252
func TestTaskFunctionality(t *testing.T) {
53-
taskTaskPool := TaskStackPool.Get()
53+
taskTaskPool := StackTaskPool.Get()
5454
require.Equal(t, len(taskTaskPool.(*taskStack).tasks), 0)
5555
require.Equal(t, cap(taskTaskPool.(*taskStack).tasks), 4)
5656
ts := taskTaskPool.(*taskStack)
@@ -69,10 +69,10 @@ func TestTaskFunctionality(t *testing.T) {
6969
ts.Push(&TestTaskImpl{a: 5})
7070
ts.Push(&TestTaskImpl{a: 6})
7171
// no clean, put it back
72-
TaskStackPool.Put(taskTaskPool)
72+
StackTaskPool.Put(taskTaskPool)
7373

7474
// require again.
75-
ts = TaskStackPool.Get().(*taskStack)
75+
ts = StackTaskPool.Get().(*taskStack)
7676
require.Equal(t, len(ts.tasks), 4)
7777
require.Equal(t, cap(ts.tasks), 4)
7878
// clean the stack
@@ -89,11 +89,47 @@ func TestTaskFunctionality(t *testing.T) {
8989

9090
// self destroy.
9191
ts.Destroy()
92-
ts = TaskStackPool.Get().(*taskStack)
92+
ts = StackTaskPool.Get().(*taskStack)
9393
require.Equal(t, len(ts.tasks), 0)
9494
require.Equal(t, cap(ts.tasks), 4)
9595
}
9696

97+
// TaskStack2 is used to store the optimizing tasks created before or during the optimizing process.
98+
type taskStackForBench struct {
99+
tasks []*Task
100+
}
101+
102+
func newTaskStackForBenchWithCap(c int) *taskStackForBench {
103+
return &taskStackForBench{
104+
tasks: make([]*Task, 0, c),
105+
}
106+
}
107+
108+
// Push indicates to push one task into the stack.
109+
func (ts *taskStackForBench) Push(one Task) {
110+
ts.tasks = append(ts.tasks, &one)
111+
}
112+
113+
// Len indicates the length of current stack.
114+
func (ts *taskStackForBench) Len() int {
115+
return len(ts.tasks)
116+
}
117+
118+
// Empty indicates whether taskStack is empty.
119+
func (ts *taskStackForBench) Empty() bool {
120+
return ts.Len() == 0
121+
}
122+
123+
// Pop indicates to pop one task out of the stack.
124+
func (ts *taskStackForBench) Pop() Task {
125+
if !ts.Empty() {
126+
tmp := ts.tasks[len(ts.tasks)-1]
127+
ts.tasks = ts.tasks[:len(ts.tasks)-1]
128+
return *tmp
129+
}
130+
return nil
131+
}
132+
97133
// Benchmark result explanation:
98134
// On the right side of the function name, you have four values, 43803,27569 ns/op,24000 B/op and 2000 allocs/op
99135
// The former indicates the total number of times the loop was executed, while the latter is the average amount
@@ -104,7 +140,7 @@ func TestTaskFunctionality(t *testing.T) {
104140
// BenchmarkTestStack2Pointer-8 42889 27017 ns/op 24000 B/op 2000 allocs/op
105141
// BenchmarkTestStack2Pointer-8 43009 27524 ns/op 24000 B/op 2000 allocs/op
106142
func BenchmarkTestStack2Pointer(b *testing.B) {
107-
stack := newTaskStack2WithCap(1000)
143+
stack := newTaskStackForBenchWithCap(1000)
108144
fill := func() {
109145
for idx := int64(0); idx < 1000; idx++ {
110146
stack.Push(&TestTaskImpl{a: idx})

0 commit comments

Comments
 (0)