-
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 all 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
Some comments aren't visible on the classic Files Changed page.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,3 +21,6 @@ engine: | |
| "protocol": "SEMI2K", | ||
| "field": "FM64" | ||
| } | ||
| infoschema_cache: | ||
| enabled: true | ||
| ttl: 10m | ||
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
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,9 @@ import ( | |
| "reflect" | ||
| "sort" | ||
| "strings" | ||
| "sync" | ||
| "sync/atomic" | ||
| "time" | ||
|
|
||
| "github.com/sethvargo/go-password/password" | ||
| "gorm.io/gorm" | ||
|
|
@@ -53,6 +56,30 @@ var EnablePasswordCheck = false | |
| // keep creating order | ||
| var allTables = []interface{}{&User{}, &Database{}, &Table{}, &Column{}, &DatabasePriv{}, &TablePriv{}, &ColumnPriv{}} | ||
|
|
||
| // InfoSchema cache entry with TTL support | ||
| type infoCacheEntry struct { | ||
| schema infoschema.InfoSchema | ||
| createTime time.Time | ||
| } | ||
|
|
||
| // InfoSchema cache to avoid frequent database queries | ||
| var ( | ||
| infoSchemaCache sync.Map // map[string]*infoCacheEntry, key is database name | ||
| infoCacheHitCount int64 // accessed via atomic operations | ||
| infoCacheMissCount int64 // accessed via atomic operations | ||
| cacheEnabled bool // whether cache is enabled | ||
| cacheTTL time.Duration | ||
| ) | ||
|
|
||
| // InitInfoSchemaCache initializes the InfoSchema cache configuration | ||
| func InitInfoSchemaCache(enabled bool, ttl time.Duration) { | ||
| cacheEnabled = enabled | ||
| cacheTTL = ttl | ||
| if ttl <= 0 { | ||
| ttl = 10 * time.Minute // default to 10 minutes | ||
| } | ||
| } | ||
|
|
||
| // NeedBootstrap checks if the store is empty | ||
| func NeedBootstrap(store *gorm.DB) bool { | ||
| for _, tn := range allTables { | ||
|
|
@@ -173,6 +200,32 @@ func FindUserByParty(store *gorm.DB, partyCode string) (*User, error) { | |
| 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 by iterating and deleting each key to avoid race conditions | ||
| infoSchemaCache.Range(func(key, value interface{}) bool { | ||
| infoSchemaCache.Delete(key) | ||
| return true | ||
| }) | ||
| } else { | ||
| // Clear specific database cache | ||
| infoSchemaCache.Delete(dbName) | ||
| } | ||
| } | ||
|
|
||
| // ResetInfoSchemaCacheStats resets cache statistics (for testing) | ||
| func ResetInfoSchemaCacheStats() { | ||
| atomic.StoreInt64(&infoCacheHitCount, 0) | ||
| atomic.StoreInt64(&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里面 |
||
| return atomic.LoadInt64(&infoCacheHitCount), atomic.LoadInt64(&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 +297,39 @@ func queryInfoSchema(store *gorm.DB) (infoschema.InfoSchema, error) { | |
| } | ||
|
|
||
| func QueryDBInfoSchema(store *gorm.DB, dbName string) (result infoschema.InfoSchema, err error) { | ||
| // Try to get from cache first if enabled | ||
| if cacheEnabled { | ||
| if cached, ok := infoSchemaCache.Load(dbName); ok { | ||
| entry := cached.(*infoCacheEntry) | ||
| // Check if cache entry is still valid (not expired) | ||
| if time.Since(entry.createTime) < cacheTTL { | ||
| atomic.AddInt64(&infoCacheHitCount, 1) | ||
| return entry.schema, nil | ||
| } | ||
| // Cache expired, remove it | ||
| infoSchemaCache.Delete(dbName) | ||
| } | ||
| } | ||
|
|
||
| // Cache miss or disabled, query from database | ||
| atomic.AddInt64(&infoCacheMissCount, 1) | ||
|
|
||
| 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 enabled | ||
| if cacheEnabled && result != nil { | ||
| infoSchemaCache.Store(dbName, &infoCacheEntry{ | ||
| schema: result, | ||
| createTime: time.Now(), | ||
| }) | ||
| } | ||
|
|
||
| return result, nil | ||
| } | ||
|
|
||
|
|
||
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
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.
Uh oh!
There was an error while loading. Please reload this page.