-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
statistics: add AnalysisPriorityQueueV2
Signed-off-by: Rustin170506 <[email protected]>
- Loading branch information
1 parent
fef43c5
commit d7588c4
Showing
3 changed files
with
141 additions
and
35 deletions.
There are no files selected for viewing
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
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
102 changes: 102 additions & 0 deletions
102
pkg/statistics/handle/autoanalyze/priorityqueue/queue2.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,102 @@ | ||
// 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 ( | ||
"context" | ||
"sync/atomic" | ||
"time" | ||
|
||
"github.com/pingcap/tidb/pkg/statistics" | ||
"github.com/pingcap/tidb/pkg/statistics/handle/autoanalyze/internal/heap" | ||
"github.com/pingcap/tidb/pkg/statistics/handle/types" | ||
"github.com/pingcap/tidb/pkg/util" | ||
) | ||
|
||
// AnalysisPriorityQueueV2 is a priority queue for TableAnalysisJobs. | ||
type AnalysisPriorityQueueV2 struct { | ||
inner *heap.Heap[int64, AnalysisJob] | ||
|
||
ctx context.Context | ||
cancel context.CancelFunc | ||
wg util.WaitGroupWrapper | ||
statsCache types.StatsCache | ||
// lastDMLUpdateFetchTimestamp is the timestamp of the last DML update fetch. | ||
lastDMLUpdateFetchTimestamp atomic.Uint64 | ||
} | ||
|
||
// NewAnalysisPriorityQueue2 creates a new AnalysisPriorityQueue2. | ||
func NewAnalysisPriorityQueue2(statsCache types.StatsCache) *AnalysisPriorityQueueV2 { | ||
keyFunc := func(job AnalysisJob) (int64, error) { | ||
return job.GetTableID(), nil | ||
} | ||
lessFunc := func(a, b AnalysisJob) bool { | ||
return a.GetWeight() > b.GetWeight() | ||
} | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
pq := &AnalysisPriorityQueueV2{ | ||
ctx: ctx, | ||
cancel: cancel, | ||
statsCache: statsCache, | ||
inner: heap.NewHeap(keyFunc, lessFunc), | ||
} | ||
|
||
pq.wg.Run(pq.run) | ||
return pq | ||
} | ||
|
||
func (pq *AnalysisPriorityQueueV2) run() { | ||
fetchInterval := time.NewTicker(time.Minute * 5) | ||
defer fetchInterval.Stop() | ||
|
||
for { | ||
select { | ||
case <-pq.ctx.Done(): | ||
return | ||
case <-fetchInterval.C: | ||
pq.fetchDMLUpdate() | ||
} | ||
} | ||
} | ||
|
||
func (pq *AnalysisPriorityQueueV2) fetchDMLUpdate() { | ||
values := pq.statsCache.Values() | ||
lastFetchTimestamp := pq.lastDMLUpdateFetchTimestamp.Load() | ||
var newMaxVersion uint64 | ||
|
||
for _, value := range values { | ||
if value.Version > lastFetchTimestamp { | ||
// Handle the table stats | ||
pq.handleTableStats(value) | ||
} | ||
newMaxVersion = max(newMaxVersion, value.Version) | ||
} | ||
|
||
// Only update if we've seen a newer version | ||
if newMaxVersion > lastFetchTimestamp { | ||
pq.lastDMLUpdateFetchTimestamp.Store(newMaxVersion) | ||
} | ||
} | ||
|
||
func (pq *AnalysisPriorityQueueV2) handleTableStats(stats *statistics.Table) { | ||
// TODO: Implement the logic to handle table stats | ||
// This might include updating the priority queue based on the new stats | ||
} | ||
|
||
func (pq *AnalysisPriorityQueueV2) Close() { | ||
pq.cancel() | ||
pq.wg.Wait() | ||
pq.inner.Close() | ||
} |