-
Notifications
You must be signed in to change notification settings - Fork 5.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
statistics: add basic analysis job queue (#50126)
ref #50132
- Loading branch information
1 parent
6bff4aa
commit b23b043
Showing
4 changed files
with
143 additions
and
0 deletions.
There are no files selected for viewing
20 changes: 20 additions & 0 deletions
20
pkg/statistics/handle/autoanalyze/priorityqueue/BUILD.bazel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") | ||
|
||
go_library( | ||
name = "priorityqueue", | ||
srcs = [ | ||
"job.go", | ||
"queue.go", | ||
], | ||
importpath = "github.com/pingcap/tidb/pkg/statistics/handle/autoanalyze/priorityqueue", | ||
visibility = ["//visibility:public"], | ||
) | ||
|
||
go_test( | ||
name = "priorityqueue_test", | ||
timeout = "short", | ||
srcs = ["queue_test.go"], | ||
embed = [":priorityqueue"], | ||
flaky = True, | ||
deps = ["@com_github_stretchr_testify//require"], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// Copyright 2024 PingCAP, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package priorityqueue | ||
|
||
// TableAnalysisJob defines the structure for table analysis job information. | ||
type TableAnalysisJob struct { | ||
// TODO: add more information about the job. | ||
Weight float64 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// Copyright 2024 PingCAP, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package priorityqueue | ||
|
||
// An AnalysisQueue implements heap.Interface and holds TableAnalysisJobs. | ||
type AnalysisQueue []*TableAnalysisJob | ||
|
||
// Implement the sort.Interface methods for the priority queue. | ||
|
||
func (aq AnalysisQueue) Len() int { return len(aq) } | ||
func (aq AnalysisQueue) Less(i, j int) bool { | ||
// We want Pop to give us the highest, not lowest, priority, so we use greater than here. | ||
return aq[i].Weight > aq[j].Weight | ||
} | ||
func (aq AnalysisQueue) Swap(i, j int) { | ||
aq[i], aq[j] = aq[j], aq[i] | ||
} | ||
|
||
// Push adds an item to the priority queue. | ||
func (aq *AnalysisQueue) Push(x any) { | ||
item := x.(*TableAnalysisJob) | ||
*aq = append(*aq, item) | ||
} | ||
|
||
// Pop removes the highest priority item from the queue. | ||
func (aq *AnalysisQueue) Pop() any { | ||
old := *aq | ||
n := len(old) | ||
item := old[n-1] | ||
*aq = old[0 : n-1] | ||
return item | ||
} |
58 changes: 58 additions & 0 deletions
58
pkg/statistics/handle/autoanalyze/priorityqueue/queue_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// Copyright 2024 PingCAP, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package priorityqueue | ||
|
||
import ( | ||
"container/heap" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestAnalysisQueue(t *testing.T) { | ||
// Test data | ||
job1 := &TableAnalysisJob{Weight: 10} | ||
job2 := &TableAnalysisJob{Weight: 5} | ||
job3 := &TableAnalysisJob{Weight: 15} | ||
|
||
// Create an empty priority queue | ||
queue := AnalysisQueue{} | ||
|
||
// Push items into the queue | ||
heap.Push(&queue, job1) | ||
heap.Push(&queue, job2) | ||
heap.Push(&queue, job3) | ||
|
||
// Test Len() | ||
require.Equal(t, 3, queue.Len(), "Length of the queue should be 3") | ||
|
||
// Test Less() | ||
require.True(t, queue.Less(0, 1), "Item at index 0 should have higher priority than item at index 1") | ||
|
||
// Test Swap() | ||
queue.Swap(0, 2) | ||
require.NotEqual(t, float64(15), queue[0].Weight, "Item at index 0 should not have weight 15 after swap") | ||
} | ||
|
||
func TestPushPop(t *testing.T) { | ||
// Test Push and Pop operations together | ||
queue := AnalysisQueue{} | ||
heap.Push(&queue, &TableAnalysisJob{Weight: 10}) | ||
heap.Push(&queue, &TableAnalysisJob{Weight: 5}) | ||
|
||
poppedItem := heap.Pop(&queue).(*TableAnalysisJob) | ||
require.Equal(t, float64(10), poppedItem.Weight, "Popped item should have weight 10") | ||
require.Equal(t, 1, queue.Len(), "After Pop, length of the queue should be 1") | ||
} |