Skip to content

Commit

Permalink
statistics: add basic analysis job queue (#50126)
Browse files Browse the repository at this point in the history
ref #50132
  • Loading branch information
Rustin170506 authored Jan 5, 2024
1 parent 6bff4aa commit b23b043
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 0 deletions.
20 changes: 20 additions & 0 deletions pkg/statistics/handle/autoanalyze/priorityqueue/BUILD.bazel
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"],
)
21 changes: 21 additions & 0 deletions pkg/statistics/handle/autoanalyze/priorityqueue/job.go
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
}
44 changes: 44 additions & 0 deletions pkg/statistics/handle/autoanalyze/priorityqueue/queue.go
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 pkg/statistics/handle/autoanalyze/priorityqueue/queue_test.go
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")
}

0 comments on commit b23b043

Please sign in to comment.