-
Notifications
You must be signed in to change notification settings - Fork 74
Feat: cache infoschema #772
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
Open
Song-Quan
wants to merge
10
commits into
main
Choose a base branch
from
feat/infoschema-cache
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
b7825b6
cache infoschema
Song-Quan 9269028
drop cache when schema changed
Song-Quan 3503a6e
add ut & remove unnecessary comments
Song-Quan 20b6b18
opt lock
Song-Quan 518a618
concentrate invalidate
Song-Quan 7e4d13f
infoschema cache configurable
Song-Quan a4d9d9e
enable infoschema cache
Song-Quan 0283252
add ut
Song-Quan c6f3087
Merge branch 'main' into feat/infoschema-cache
Song-Quan 4b90c31
fix go lint & update config ut
Song-Quan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -21,6 +21,7 @@ | |
| "reflect" | ||
| "sort" | ||
| "strings" | ||
| "sync" | ||
|
|
||
| "github.com/sethvargo/go-password/password" | ||
| "gorm.io/gorm" | ||
|
|
@@ -53,6 +54,14 @@ | |
| // keep creating order | ||
| var allTables = []interface{}{&User{}, &Database{}, &Table{}, &Column{}, &DatabasePriv{}, &TablePriv{}, &ColumnPriv{}} | ||
|
|
||
| // InfoSchema cache to avoid frequent database queries | ||
| var ( | ||
| infoSchemaCache sync.Map // map[string]infoschema.InfoSchema, key is database name | ||
| infoCacheHitCount int64 | ||
| infoCacheMissCount int64 | ||
| infoCacheMutex sync.RWMutex | ||
Song-Quan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ) | ||
|
|
||
| // NeedBootstrap checks if the store is empty | ||
| func NeedBootstrap(store *gorm.DB) bool { | ||
| for _, tn := range allTables { | ||
|
|
@@ -173,6 +182,33 @@ | |
| return &user, nil | ||
| } | ||
|
|
||
| // InvalidateInfoSchemaCache invalidates the InfoSchema cache for a specific database | ||
| // If dbName is empty, it invalidates all cached InfoSchemas | ||
| func InvalidateInfoSchemaCache(dbName string) { | ||
| if dbName == "" { | ||
| // Clear all cache | ||
| infoSchemaCache = sync.Map{} | ||
Song-Quan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } else { | ||
| // Clear specific database cache | ||
| infoSchemaCache.Delete(dbName) | ||
| } | ||
Song-Quan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| // ResetInfoSchemaCacheStats resets cache statistics (for testing) | ||
Song-Quan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| func ResetInfoSchemaCacheStats() { | ||
| infoCacheMutex.Lock() | ||
| defer infoCacheMutex.Unlock() | ||
| infoCacheHitCount = 0 | ||
| infoCacheMissCount = 0 | ||
| } | ||
|
|
||
| // GetInfoSchemaCacheStats returns cache hit and miss statistics | ||
| func GetInfoSchemaCacheStats() (hits, misses int64) { | ||
|
Collaborator
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. 这两个func只是为了测试用吗?可以看看是否适合加到metrics里面 |
||
| infoCacheMutex.RLock() | ||
| defer infoCacheMutex.RUnlock() | ||
| return infoCacheHitCount, infoCacheMissCount | ||
| } | ||
|
|
||
| func QueryInfoSchema(store *gorm.DB) (result infoschema.InfoSchema, err error) { | ||
Song-Quan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| callFc := func(tx *gorm.DB) error { | ||
| result, err = queryInfoSchema(tx) | ||
|
|
@@ -244,13 +280,32 @@ | |
| } | ||
|
|
||
| func QueryDBInfoSchema(store *gorm.DB, dbName string) (result infoschema.InfoSchema, err error) { | ||
| // Try to get from cache first | ||
| if cached, ok := infoSchemaCache.Load(dbName); ok { | ||
| infoCacheMutex.Lock() | ||
| infoCacheHitCount++ | ||
| infoCacheMutex.Unlock() | ||
Song-Quan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return cached.(infoschema.InfoSchema), nil | ||
| } | ||
Song-Quan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| // Cache miss, query from database | ||
| infoCacheMutex.Lock() | ||
| infoCacheMissCount++ | ||
| infoCacheMutex.Unlock() | ||
Song-Quan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
Song-Quan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| callFc := func(tx *gorm.DB) error { | ||
| result, err = queryDBInfoSchema(tx, dbName) | ||
| return err | ||
| } | ||
| if err := store.Transaction(callFc, &sql.TxOptions{ReadOnly: true}); err != nil { | ||
| return nil, fmt.Errorf("queryDBInfoSchema: %v", err) | ||
| } | ||
|
|
||
| // Store in cache | ||
| if result != nil { | ||
| infoSchemaCache.Store(dbName, result) | ||
| } | ||
Song-Quan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| return result, nil | ||
| } | ||
|
|
||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
invalid的操作直接在调用 executeCreateView 的层级上统一做应该会更简洁。
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.
done