-
Notifications
You must be signed in to change notification settings - Fork 5.9k
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
*: remove limiting process id for auto analyze #54902
Changes from 10 commits
e81d9a2
49fcc42
fe68215
9e5b51e
ec513b4
be16f95
d47567a
2a68900
bf1c5bb
a6eb9e9
4409d44
bdede1f
2358073
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,27 +14,111 @@ | |
|
||
package util | ||
|
||
import ( | ||
"sync" | ||
|
||
"github.com/pingcap/tidb/pkg/sessionctx/sysproctrack" | ||
"golang.org/x/exp/maps" | ||
) | ||
|
||
// AutoAnalyzeProcIDGenerator is used to generate auto analyze proc ID. | ||
type AutoAnalyzeProcIDGenerator interface { | ||
// AutoAnalyzeProcID generates an analyze ID. | ||
AutoAnalyzeProcID() uint64 | ||
ReleaseAutoAnalyzeProcID(uint64) | ||
} | ||
|
||
var _ AutoAnalyzeProcIDGenerator = (*generator)(nil) | ||
|
||
type generator struct { | ||
hawkingrei marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// autoAnalyzeProcIDGetter is used to generate auto analyze ID. | ||
autoAnalyzeProcIDGetter func() uint64 | ||
autoAnalyzeProcIDGetter func() uint64 | ||
releaseAutoAnalyzeProcID func(uint64) | ||
} | ||
|
||
// NewGenerator creates a new Generator. | ||
func NewGenerator(autoAnalyzeProcIDGetter func() uint64) AutoAnalyzeProcIDGenerator { | ||
func NewGenerator(autoAnalyzeProcIDGetter func() uint64, releaseAutoAnalyzeProcID func(uint64)) AutoAnalyzeProcIDGenerator { | ||
return &generator{ | ||
autoAnalyzeProcIDGetter: autoAnalyzeProcIDGetter, | ||
autoAnalyzeProcIDGetter: autoAnalyzeProcIDGetter, | ||
releaseAutoAnalyzeProcID: releaseAutoAnalyzeProcID, | ||
} | ||
} | ||
|
||
// AutoAnalyzeProcID implements AutoAnalyzeProcIDGenerator. | ||
func (g *generator) AutoAnalyzeProcID() uint64 { | ||
return g.autoAnalyzeProcIDGetter() | ||
} | ||
|
||
// ReleaseAutoAnalyzeProcID implements AutoAnalyzeProcIDGenerator. | ||
func (g *generator) ReleaseAutoAnalyzeProcID(id uint64) { | ||
g.releaseAutoAnalyzeProcID(id) | ||
} | ||
|
||
// GlobalAutoAnalyzeProcessList is used to track the auto analyze process. | ||
var GlobalAutoAnalyzeProcessList = newGlobalAutoAnalyzeProcessList() | ||
|
||
type globalAutoAnalyzeProcessList struct { | ||
processes map[uint64]struct{} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We use global thread-safe map to record the running auto analyze task. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why not use a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The Map type is optimized for two common use cases: (1) when the entry for a given key is only ever written once but read many times, as in caches that only grow, or (2) when multiple goroutines read, write, and overwrite entries for disjoint sets of keys. In these two cases, use of a Map may significantly reduce lock contention compared to a Go map paired with a separate Mutex or RWMutex. so two common use cases are not the same as us. |
||
mu sync.RWMutex | ||
} | ||
|
||
func newGlobalAutoAnalyzeProcessList() *globalAutoAnalyzeProcessList { | ||
return &globalAutoAnalyzeProcessList{ | ||
processes: make(map[uint64]struct{}), | ||
} | ||
} | ||
|
||
// Tracker is used to track the auto analyze process. | ||
func (g *globalAutoAnalyzeProcessList) Tracker(id uint64) { | ||
g.mu.Lock() | ||
defer g.mu.Unlock() | ||
g.processes[id] = struct{}{} | ||
} | ||
|
||
// Untracker is used to untrack the auto analyze process. | ||
func (g *globalAutoAnalyzeProcessList) Untracker(id uint64) { | ||
g.mu.Lock() | ||
defer g.mu.Unlock() | ||
delete(g.processes, id) | ||
} | ||
|
||
// AutoAnalyzeTracker is used to track the auto analyze process. | ||
type AutoAnalyzeTracker struct { | ||
track func(id uint64, ctx sysproctrack.TrackProc) error | ||
untrack func(id uint64) | ||
} | ||
|
||
// All returns all the auto analyze process IDs. | ||
func (g *globalAutoAnalyzeProcessList) All() []uint64 { | ||
g.mu.RLock() | ||
defer g.mu.RUnlock() | ||
return maps.Keys(g.processes) | ||
} | ||
|
||
// Contains checks whether the auto analyze process ID is in the list. | ||
func (g *globalAutoAnalyzeProcessList) Contains(id uint64) bool { | ||
g.mu.RLock() | ||
defer g.mu.RUnlock() | ||
_, ok := g.processes[id] | ||
return ok | ||
} | ||
|
||
// NewAutoAnalyzeTracker creates a new AutoAnalyzeTracker. | ||
func NewAutoAnalyzeTracker(track func(id uint64, ctx sysproctrack.TrackProc) error, untrack func(id uint64)) *AutoAnalyzeTracker { | ||
return &AutoAnalyzeTracker{ | ||
track: track, | ||
untrack: untrack, | ||
} | ||
} | ||
|
||
// Track is used to track the auto analyze process. | ||
func (t *AutoAnalyzeTracker) Track(id uint64, ctx sysproctrack.TrackProc) error { | ||
GlobalAutoAnalyzeProcessList.Tracker(id) | ||
return t.track(id, ctx) | ||
} | ||
|
||
// UnTrack is used to untrack the auto analyze process. | ||
func (t *AutoAnalyzeTracker) UnTrack(id uint64) { | ||
GlobalAutoAnalyzeProcessList.Untracker(id) | ||
t.untrack(id) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here are the tests.